IntegrationBroker

public final class IntegrationBroker implements ContextAware, AutoCloseable

Dispatches external messages from and to a Bounded Context with which this broker is associated.

In a multi-component environment messages may travel across components from one Bounded Context to another. Accordingly, an IntegrationBroker is available as a part of a Bounded Context.

An IntegrationBroker is always based upon transport that delivers messages from and to it. For several Bounded Contexts to communicate, their brokers have to share the transport. Typically, that would be a single message queue.

IntegrationBrokers communicate with each other in order to keep the whole list of requested messages (needs) and their potential sources (Bounded Contexts) up-to-date. They use two special messages for this:

  • ExternalMessagesSourceAvailable is sent when a broker is registered withing a Bounded Context;
  • RequestForExternalMessages is sent
    • in response to ExternalMessagesSourceAvailable sent by other brokers;
    • when internal needs for external messages are changed.

Receiving

The messages from external components received by an IntegrationBroker via the transport are propagated into the Bounded Context via the domestic EventBus.

Publishing

The messages requested by other parties are published from the domestic EventBus with the help of special dispatcher.

Sample Usage

Bounded Context "Projects" has a projection with an event handler that is subscribed to an external event as follows:


public class ProjectListView extends Projection<...>  {

    {@}Subscribe
     public void on(@External UserDeleted event) {
         // Remove the projects that belong to this user.
         // ...
     }
 }
 

Upon a registration of the corresponding repository for this projection in the context, the broker associated with that context is informed that one more external event is needed. It sends out an updated RequestForExternalMessages saying that UserDeleted events are needed too.

Let's say the second Context is "Users". Its broker will receive the RequestForExternalMessages sent by "Projects". To handle it, it will create a bridge between "Users"'s Event Bus (which may eventually be transmitting a UserDeleted event) and the transport.

Once UserDeleted is emitted locally in "Users" context, it will be received by this bridge (as well as other local dispatchers) and published to the external transport by its IntegrationBroker.

The integration broker on the "Projects" side will receive the UserDeleted external message. The event will be dispatched to the external event handler of the projection.

Limitations

An event type may be consumed by many Contexts but produced only by a single Context. This means that each given event type belongs only to one Context. The ownership of an event type may be changed between the versions of a system, but may not be changed while events are travelling between the Contexts.

Constructors

Link copied to clipboard
public void IntegrationBroker()

Functions

Link copied to clipboard
public void close()
Removes all subscriptions and closes all the underlying transport channels.
Link copied to clipboard
public boolean isRegistered()
Determines if this instance is already registered with a Bounded Context.
Link copied to clipboard
public void register(EventDispatcher dispatcher)
Registers a local dispatcher which is subscribed to external messages.
Link copied to clipboard
public void registerWith(BoundedContext context)
Registers this instance as a part of the given Bounded Context.
Link copied to clipboard
public String toString()
Link copied to clipboard
public void unregister(EventDispatcher dispatcher)
Unregisters a local dispatcher which should no longer be subscribed to external messages.

Inherited functions

Link copied to clipboard
public void checkNotRegistered()
Verifies that this instance is NOT registered yet.
Link copied to clipboard
public void checkRegistered()
Verifies that this instance is already registered.