Contact Us 1-800-596-4880

Integrating Connectors with the Mule Lifecycle

This practice is not recommended. Refer to Implementing Connection Management for a more robust approach.

It is possible to make connectors fully aware of Mule’s lifecycle and trigger specific methods during the Mule Initialize, Start, Stop and Dispose phases with the available annotations.

If a method is annotated with one of these annotations it will be invoked during the associated Mule lifecycle phase, as described below.

Restrictions

  • The lifecycle annotations cannot be applied to:

  • static methods

  • non-public methods

    • methods that take arguments

    • methods that do not return void

  • Each of the lifecycle annotations can be applied at most once in a @Connector class.

Initialize Phase: @PostConstruct

To invoke a method during the Initialize phase, apply the @PostConstruct annotation.

import javax.annotation.PostConstruct;

@Connector(name = "myconnector")
public class MyConnector {

    ...

    @PostConstruct
    public void anyMethodName() {
        // your code here
    }

    ...

Start Phase: @Start

To invoke a method during the Start phase, apply the @Start annotation.

import org.mule.api.annotations.Module;
import org.mule.api.annotations.lifecycle.Start;

@Module(name = "myconnector")
public class MyConnector {

    ...

    @Start
    public void anyMethodName() {
        // your code here
    }

    ...

Stop Phase: @Stop

To invoke a method during the Stop phase, apply the @Stop annotation.

import org.mule.api.annotations.Module;
import org.mule.api.annotations.lifecycle.Stop;

@Module(name = "myconnector")
public class MyConnector {

    ...

    @Stop
    public void anyMethodName() {
        // your code here
    }

    ...

Dispose Phase: @PreDestroy

To invoke a method during the Dispose phase, apply the @PreDestroy annotation.

import org.mule.api.annotations.Module;
import org.mule.api.annotations.lifecycle.Dispose;

@Module(name = "myconnector")
public class MyConnector {

    ...

    @PreDestroy
    public void anyMethodName() {
        // your code here
    }

    ...

Example

The following code fragment is taken from the Twilio Connector. During the Start phase, an instance of the @TwilioClient class is created.

package org.mule.module.twilio;

import org.mule.api.annotations.Configurable;
import org.mule.api.annotations.Module;
import org.mule.api.annotations.Processor;
import org.mule.api.annotations.lifecycle.Start;
import org.mule.api.annotations.param.Optional;
import org.mule.api.callback.HttpCallback;

@Module(name = "twilio")
public class TwilioConnector {

    /**
     * The account sid to be used to connect to Twilio.
     */
    @Configurable
    private String accountSid;
    /**
     * The authentication token to be used to connect to Twilio
     */
    @Configurable
    private String authToken;

    private TwilioClient twilioClient;

    @Start
    public void createTwilioClient() {
        twilioClient = new TwilioClient(accountSid, authToken);
    }

Integration with the Mule lifecycle is also used in the Rest Jersey Client sample.