A common question Mule ESB users have is: How do I perform some actions on Mule startup? Well, the fact is one can do it many ways, including:
- Programmatically get a hold of MuleContext in your component or agent (e.g., by implementing MuleContextAware) and register your listener and interest in a specific event. Typically the MuleContextNotification.CONTEXT_STARTED is the one you’d want.
- A combination of a listener and declarative registration. For an example, see this forum post for a solution.
Today, however, I want to talk about yet another option, which often goes unnoticed. In fact it is one of my favorites, as it provides for conventions-over-configuration approach: bootstrapping the registry. The documentation has more details, but here’s how one would do it:
- Create your class (it needs not even be any Mule agent or component, can just be your own POJO) and package it as a JAR.
- Add the file registry-bootstrap.properties in the JAR at META-INF/services/org/mule/config/ with the following entry:
my-startup-object-name=com.example.MyStartupClass
- Drop the JAR in $MULE_HOME/lib/user and enjoy!
Your class will then be added to Mule’s internal registry and put through a complete lifecycle if needed (see the documentation link above). As long as the JAR is available to Mule, the class will be started automatically, without any extra configuration steps.