CDI enabled Java Command-Bus
Command- Marker InterfaceCommandHandler- One Implementation perCommand. Provideshandle(CommandImplementation)Method.CommandBus- Finds and calls theCommandHandlerfor eachCommand.CommandBuscan be decorated, in order to implement cross-cutting concerns, such as logging, transaction handling, validation, autorization, metrics etc.
You can use JitPack to configure command-bus as a dependency in your project.
For example when using maven, define the JitPack repository:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>And the command-bus dependency:
<dependency>
<groupId>com.github.triologygmbh</groupId>
<artifactId>command-bus</artifactId>
<version>0.1.0</version>
</dependency>For further details and options refer to the JitPack website.
- Having the command-bus dependency on the classpath triggers the CDI extension that finds all
CommandHandlers and registeres them with the appropriateCommandin theRegistry. - Provide a Producer for the
CommandBusthat brings togetherRegistryandCDICommandBus. This producer is the central place where decorators can be instantiated. SeeCommandBusFactoryin tests, for example. - Implement your
Commands andCommandHandlers. SeeCDIITCase.
First example is the logging decorator (LoggingCommandBus) that logs entering and leaving (including time of execution) of CommandHandlers.
The Triology Command Bus provides two Prometheus metrics decorators. More information on Prometheus can be found on the
project's website.
In order to use them, make sure to provide the io.prometheus:simpleclient dependency on the classpath.
The 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.
The 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
constructor parameter.
Commands can specify return values. SeeHelloCommandandHelloCommandHandlerfor example.- If you don't want a return value, use
Void. SeeByeCommandandByeCommandHandlerfor example.