@Dependent
public abstract class EventBinder
extends java.lang.Object
Binds incoming CDI events to queues and outgoing CDI events to exchanges of a broker.
Inherit from this class and override its bindEvents() method to create bindings.
Queue example:
protected void bindEvents() {
bind(MyEventOne.class).toQueue("myQueueOne");
bind(MyEventTwo.class).toQueue("myQueueTwo").autoAck();
}
Exchange example:
protected void bindEvents() {
bind(MyEvent.class).toExchange("myExchange").withRoutingKey("myRoutingKey")
.withPublisherTransactions();
bind(MyEvent.class).toExchange("myExchange").withRoutingKey("myRoutingKey")
.withEncoder(new MyCustomEncoder()).withPublisherTransactions();
}
To initialize the event bindings, inject the instance of this class and call initialize().
In a web application, you would normally do this in a context listener on application startup
after your CDI framework was initialized.
| Modifier and Type | Class and Description |
|---|---|
static class |
EventBinder.BinderConfiguration |
static class |
EventBinder.EventBindingBuilder<T> |
static class |
EventBinder.ExchangeBinding<T>
Configures and stores the binding between an event class and an exchange.
|
static class |
EventBinder.QueueBinding<T>
Configures and stores the binding between and event class and a queue.
|
| Constructor and Description |
|---|
EventBinder() |
| Modifier and Type | Method and Description |
|---|---|
<M> EventBinder.EventBindingBuilder<M> |
bind(java.lang.Class<M> event)
Starting point for binding an event.
|
protected abstract void |
bindEvents()
Extend
EventBinder and implement this method to create the event bindings for your
application. |
EventBinder.BinderConfiguration |
configuration()
Returns the configuration object for the event binder, in order to configure the connection
specific part.
|
void |
initialize()
Initializes the event binder and effectively enables all bindings created in
bindEvents(). |
protected abstract void bindEvents()
EventBinder and implement this method to create the event bindings for your
application.
Binder example:
public class MyEventBinder extends EventBinder {
@Override
protected void bindEvents() {
bind(MyEvent.class).toExchange("my.exchange").withRoutingKey("my.routing.Key")
.withDecoder(new MyDecoder());
}
}
public EventBinder.BinderConfiguration configuration()
Configuration example:
binder.configuration().setHost("somehost.somedomain").setUsername("user")
.setPassword("password");
public void initialize()
throws java.io.IOException
bindEvents().
Inject your event binder implementation at the beginning of your application's life cycle and call this method. In web applications, a good place for this is a ServletContextListener.
After this method was successfully called, consumers are registered at the target broker for every queue binding. Also, for every exchange binding messages are going to be published to the target broker.
java.io.IOException - if the initialization failed due to a broker related issuepublic <M> EventBinder.EventBindingBuilder<M> bind(java.lang.Class<M> event)
Binding fired events to be published to an exchange:
bind(MyEvent.class).toExchange("my.exchange");
Binding consuming from a queue to fire an event:
bind(MyEvent.class).toQueue("my.queue");
M - the type of the eventevent - The event