CDI/Spring enabled Java Command-Bus
Command- Marker Interface
CommandHandler- One Implementation per
CommandBus- Finds and calls the
CommandBuscan be decorated, in order to implement cross-cutting concerns, such as logging, transaction handling, validation, autorization, metrics etc.
Add the latest stable version of command-bus to the dependency management tool of your choice.
You can also get snapshot versions from our snapshot repository (for the most recent commit on develop branch).
To do so, add the following repo to your
<repository> <id>snapshots-repo</id> <url>https://oss.sonatype.org/content/repositories/snapshots</url> <releases><enabled>false</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </repository>
There are different versions of command-bus for either CDI or spring.
Dependency for CDI
<dependency> <groupId>com.cloudogu.cb</groupId> <artifactId>command-bus-cdi</artifactId> <version>1.0.1</version> </dependency>
Dependency for Spring
<dependency> <groupId>com.cloudogu.cb</groupId> <artifactId>command-bus-spring</artifactId> <version>1.0.1</version> </dependency>
- CDI: Having the
command-bus-cdidependency on the classpath triggers the CDI extension
- Spring: All CommandHandlers must be within the application context (e.g.
@Componentin spring boot)
- CDI: Having the
- Implement your
Commands and the logic in appropriate
- You can now just inject the
CommandBusand pass your
execute()method. It will automatically pass it to the appropriate handler.
- If you want to decorate your command bus (for logging, metrics, etc.), a factory/producer for the
CommandBusis the central place where decorators can be instantiated. It brings together your
SpringCommandBus) with decorators (see bellow). Example
- CDI: The
CommandHandlers and puts them on the
- Spring: The
Registryitself gets all
CommandHandlers from the application context.
Command Bus Decorators
First example is the logging decorator (
LoggingCommandBus) that logs entering and leaving (including time of execution) of
Prometheus metric decorators
The Command Bus provides two Prometheus metrics decorators. More information on Prometheus can be found on the
In order to use them, make sure to provide the
io.prometheus:simpleclient dependency on the classpath.
PrometheusMetricsCountingCommandBus counts every executed command, using a Prometheus Counter.
The counter to be used must be provided as a constructor parameter. For each type of command (i.e. it's class name) a
label is created automatically.
PrometheusMetricsTimingCommandBus captures the time a command's execution takes and provides the metric as a
Prometheus Histogram. Similarly to the
PrometheusMetricsCountingCommandBus, the Histogram needs to be provided as a
Commands can specify return values. See
- If you don't want a return value, use