In This Section

Overview

Oftentimes applications will want to perform somr initial prep work before beginning to process live messages. Some usecases include:

Because applications may only send outbound messages and operate on state from a message handler, the AepEngine emits an AepMessagingPrestartEvent that applications can handle and tag with messages that will be dispatched prior to any live messages from the message buses that it is about to start. Application event handlers for these messages are then able to perform normal logic that any other message handler would perform. 

Adding a First Message

To schedule a First Message that will be injected before any live (or initial messages) and application may register an EventHandler for the AepMessagingPrestartEvent.

@EventHandler
public void onMessagingPrestart(AepMessagingPrestartEvent event) {
  event.setFirstMessage(new MyFirstMessage.create()); 
}

Prior to the messaging starting, the AepEngine will then schedule MyFirstMessage to any event handlers for MyFirstMessage. 

Adding Additional Initial Messages

For large applications that are made of many loosely coupled event handlers it is sometime for convenient for several event handlers to register independent first message types. For this case the AepMessagingPrestartEvent also provides the ability to add additional Initial Messages. These messages are scheduled before any live messages but after the first message. 

public class SomeComponent {
  @EventHandler
  public void onMessagingPrestart(AepMessagingPrestartEvent event) {
    event.addInitialMessage(new Component1InitialMessage.create()); 
  }
 
  public void onFirstMessage(Component1InitialMessage message) {
    /*do something*/
  }
}

public class SomeOtherComponent {
  @EventHandler

  public void onMessagingPrestart(AepMessagingPrestartEvent event) {
    event.addInitialMessage(new Component2InitialMessage.create()); 
  }


  public void onFirstMessage(Component2InitialMessage message) {
    /*do something else*/
  }
}

HA Implications for Initial Messages

The handling of first and initial messages depends on the HA Model in use for the application, though the net result is the same. For both HA Models, messaging is only started for the primary application when it transitions to the Primary role, so only that instance will receive the AepMessagingPrestartEvent that triggers scheduling of initial messages. It is also worth noting that if a primary application fails and a backup instance takes over, its messaging will be started and first and initial messages will be scheduled on that instance as well. Applications are at liberty not to add first or initial messages if their initialization logic has already been done by a previous messaging startup. 

Event Sourcing Considerations

With EventSourcing the the first and initial messages are replicated and logged just like a live message would be. This means that a backup or recovering event handlers of the initial messages will be invoked, though isReplayedMessage() will returned true in this case.

An EventSourcing application may record that it has processed initial messages f it wishes to suppress scheduling initial messages on subsequent messaging startups that result from failover or restart. 

State Replication Considerations

In State Replication, the first and initial messages are not replicated to the backup, but state changes and outbound messages made in their event handlers will be replicated.

A State Replication application may as the result of processing an initial message type, update state indicating that the application has been initialized if it wishes to suppress scheduling initial messages on subsequent messaging startups.