Free MuleSoft CONNECT Keynote & Expo Pass Available!

Register now+
Nav

Lifecycle of a Message Source

Unlike operations, where the lifecycle is tied to the owning Flow, sources can be started or stopped separately through Runtime Manager.

To support that, the Source class forces you to implement the onStart() and onStop() methods.

On Start

This example from the VM connector shows the onStart() method:


         
      
1
2
3
4
@Override
public void onStart(SourceCallback<Serializable, VMMessageAttributes> sourceCallback) throws MuleException {

}

In this method, you have to initialize all the resources (such as threads, schedulers, connections, and so on) that you need so that you can produce messages. If an exception or error condition is found trying to do that, then you have to:

  • Release any resources you did got to allocate.

  • Throw an exception with a meaningful message.

If the source fails to start, the app that contains it will fail to start.

All of the source parameters values that defined through the @Parameter annotation are resolved and available at this point.

SourceCallback

The method receives a SourceCallback object. The SourceCallback is arguably the most important piece of the Source, since it is involved in pretty much every aspect of the source’s functionality. We will elaborate on the uses and responsibilities of the SourceCallback as this documentation progresses, but in a nutshell, the SourceCallback is what the source uses to communicate with the runtime. It is used for:

  • Pushing messages into the Flow

  • Communicating connection issues and triggering reconnection

  • Passing state between the push phase of the Source into the response handling part

Because the SourceCallback plays such an important role, it is very important that the provided instance is always available. Depending on how your source’s complexity and code organization, you may accomplish this by simply holding it as a method argument. More complex sources might need to keep a reference to it through an instance field. If you decide to do this, then you need to make sure that the reference is cleared when the onStop() method is executed

On Stop

This method will be invoked by the runtime to make the source stop producing messages. This method should not fail. Any exceptions found during the stop process should be logged and correctly handled by the source, but after invoking this method the source must:

  • Stop producing messages

  • Free any allocated resources

  • Be capable of being restarted

In this topic: