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

Managing Connections

Mule DevKit can generate automatic connection management around a POJO. Special annotations trigger the DevKit to generate code that automatically manages connections. You annotate methods that handle connecting, disconnecting, and validating connections, and getting a session identifier; Mule DevKit’s Annotation Processor manages the rest.

For example, if a 3rd-party API fails because the session expires, Mule DevKit automatically drops the connection, establishes a new one, then retries the operation (see code below).


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Connector(name = "saas")
public class SaaSConnector
{
    @Connect
    public void connect(@ConnectionKey String username, String password) { ... }
 
    @Disconnect
    public void disconnect() { ... }
 
    @ValidateConnection
    public boolean isConnected() { ... }
 
    @ConnectionIdentifier
    public boolean getSessionId() { ... }
}

The @Connect Annotation

This annotation indicates the method inside a @Connector that is responsible for creating a connection. The connector’s connection manager calls this @Connect annotation. There must be exactly one method annotated with @Connect or the compilation fails.

A method annotated with @Connect must:

  • be public

  • throw org.mule.api.ConnectionException exceptions (DevKit permits no other exceptions)

  • have a void return type

The @Disconnect Annotation

This annotation indicates the method inside a @Connector that is responsible for disposing of a connection. The connector’s connection manager calls this @Disconnect annotation. There must be exactly one method annotated with @Disconnect or the compilation fails.

A method annotated with @Disconnect must:

  • be public

  • decline to receive parameters

  • have a void return type

The @ValidateConnection Annotation

This annotation indicates the method inside a @Connector that checks whether the connector is actually connected or not. There must be exactly one method annotated with @ValidateConnection or the compilation fails.

A method annotated with @ValidateConnection must:

  • be public

  • decline to receive parameters

  • return boolean or java.lang.Boolean

The @ConnectionIdentifier Annotation

This annotation indicates the method inside a @Connector that is responsible for identifying a connection. The connector’s connection manager calls this @Disconnect annotation for debugging purposes. There must be exactly one method annotated with @ConnectionIdentifier or the compilation fails.

A method annotated with @ConnectionIdentifier must:

  • be public

  • not be static

  • not take arguments

  • return java.lang.String

The @InvalidateConnectionOn Annotation

Attach this annotation to any method which contains an @Processor or @Source annotation. @InvalidataeConnectionOn receives a single argument which is the class of the exception to be caught. If the Processor/Source throws an exception, @InvalidateConnectionOn automatically invalidates the connection. Reference the Reconnection Strategy documentation for more detail.