Nav
You are viewing an older version of this section. Click here to navigate to the latest version.

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.


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
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. 


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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. 


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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.