This folder contains the Java modules for MyServiceBus. The Gradle multi-project build resides at the repository root.
- JDK 17 (Temurin/OpenJDK recommended)
- Gradle
- Docker (optional) to run RabbitMQ locally
- From the repository root, build all modules and run tests with the system Gradle installation:
gradle test
For a new Java project, prefer the MyServiceBus service collection and decorator structure:
ServiceCollection services = ServiceCollection.create();
services.from(MessageBusServices.class)
.addServiceBus(cfg -> {
cfg.addConsumer(SubmitOrderConsumer.class);
cfg.using(RabbitMqFactoryConfigurator.class,
(context, rabbit) -> rabbit.configureEndpoints(context));
});
ServiceProvider provider = services.buildServiceProvider();
MessageBus bus = provider.getRequiredService(MessageBus.class);
bus.start();The decorator structure is the Java equivalent of the extension-method composition style used by the C# client.
Use the bus factory when adding MyServiceBus to a Java application that already owns its construction or dependency-injection model. Application code does not need to build or resolve through a MyServiceBus service provider:
MessageBus bus = MessageBus.factory.create(RabbitMqFactoryConfigurator.class, cfg -> {
cfg.host("localhost");
cfg.receiveEndpoint("submit-order", endpoint ->
endpoint.handler(SubmitOrder.class, context ->
new SubmitOrderConsumer().consume(context)));
});
bus.start();The handler may resolve a consumer from an existing Spring, CDI, Dagger, Guice, or application-owned container. That container is not installed into the default Guice provider:
endpoint.handler(SubmitOrder.class, context ->
applicationContext.getBean(SubmitOrderConsumer.class).consume(context));Java applications can register consumers explicitly or use the optional, framework-neutral JSR 269 processor:
dependencies {
implementation 'io.github.marinasundstrom.myservicebus:myservicebus:0.1.0-preview.12'
annotationProcessor 'io.github.marinasundstrom.myservicebus:myservicebus-processor:0.1.0-preview.12'
}Annotate consumer methods with @MessageConsumer, then register the generated catalog with GeneratedConsumerCatalog.INSTANCE::register. This avoids runtime method discovery and reflective invocation. The GraalVM Native Image path is an MVP proof of concept and remains work in progress; run its end-to-end smoke test from the repository root with:
./eng/verify-java-aot.shThe MyServiceBus ServiceCollection and decorator structure form a container-neutral programming model. ServiceCollection.create() materializes it with the included Guice-backed implementation, while a framework adapter can materialize the same registrations with another container. ServiceCollection.createAot() selects a factory-only implementation with no reflective constructor activation. An existing application may instead use an explicit bus factory that closes over Spring, CDI, Dagger, Guice, or an application-owned resolver without adopting the MyServiceBus provider model. See Java dependency-injection boundary.
Zero-argument service registrations accept the JDK-standard Supplier<? extends T>. Providers from javax.inject, jakarta.inject, Spring, Dagger, or another framework adapt through method references such as provider::get or provider::getObject; MyServiceBus does not select one DI namespace for its core API.
From the repository root, start RabbitMQ using Docker Compose:
docker compose up -d rabbitmqRabbitMQ defaults: host localhost, port 5672, mgmt UI http://localhost:15672 (guest/guest).
- From the repository root:
RABBITMQ_HOST=localhost HTTP_PORT=5301 gradle :testapp:run
Helper script (equivalent):
cd src/Java/testapp
RABBITMQ_HOST=localhost HTTP_PORT=5301 ./run.shThe app starts an HTTP server (default port 5301) with routes:
GET /publish– publishes SubmitOrderGET /send– sends SubmitOrder to a queueGET /request– request/response demoGET /request_multi– request/response with fault handling
Run multiple instances by changing HTTP_PORT (e.g., 5301 and 5302).
RABBITMQ_HOST: RabbitMQ host (defaultlocalhost)HTTP_PORT: HTTP port for testapp (default5301)
To run the Java test app under Aspire, two files must be present:
- The OpenTelemetry Java agent JAR must exist at
src/AspireApp/agents/opentelemetry-javaagent.jar. - A trusted Aspire OTLP certificate PEM must exist at
src/AspireApp/agents/aspire-localhost-cert.pem.
The AppHost supervises the Java application's Gradle run task, so no separate JAR build is required.
Set up the Java agent:
mkdir -p src/AspireApp/agents
curl -fL \
https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/latest/download/opentelemetry-javaagent.jar \
-o src/AspireApp/agents/opentelemetry-javaagent.jarThe AppHost also configures the Java agent to trust a local Aspire OTLP certificate PEM at:
src/AspireApp/agents/aspire-localhost-cert.pem
Then start Aspire:
dotnet run --project src/AspireApp/AspireApp.csprojAdditional details are documented in docs/development/aspire-java-telemetry.md.
See also: docs/two-service-sample.md for running .NET and Java together.
- Lombok is configured as an annotation processor via the root
build.gradleand does not need to be added per-module. - Modules and external dependency versions are centralized in the root
build.gradle.