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

Managing Connections

The DevKit can generate automatic connection management around a POJO. By adding special annotations the DevKit will generate code that will automatically manage connections. It is necessary to mark methods that will handle connecting, disconnecting, validating connections, getting a session identifier and the DevKit’s Annotation Processing Tool will take care of the rest. This is what truly differentiates a @Connector annotated extension from a @Module one.

As an example if a third party API fails because the session expired, the DevKit will automatically drop the connection, establish a new one and retry the operation.

Example:


         
      
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 marks a method inside a @Connector as the responsible for creating a connection. It will be called by the connector’s connection manager. There must be exactly one method annotated with @Connect otherwise compilation will fail.

A method annotated with @Connect has to follow certain rules:

  • it must be public

  • it has to declare it throws org.mule.api.ConnectionException, no other exceptions are allowed

  • it cannot have a return type different than void

The @Disconnect Annotation

This annotation marks a method inside a @Connector as the responsible for disposing a connection. It will be called by the connector’s connection manager. There must be exactly one method annotated with @Disconnect otherwise compilation will fail.

A method annotated with @Disconnect has to follow certain rules:

  • it must be public

  • it cannot receive any parameters

  • it cannot have a return type different than void

The @ValidateConnection Annotation

This annotation marks a method inside a @Connector as the responsible for validating if the connector is actually connected or not. There must be exactly one method annotated with @ValidateConnection otherwise compilation will fail.

A method annotated with @ValidateConnection has to follow certain rules:

  • it must be public

  • it cannot receive parameters

  • it cannot have a return type different than boolean or java.lang.Boolean

The @ConnectionIdentifier Annotation

This annotation marks a method inside a @Connector as the responsible for identifying a connection. It will be called by the connector’s connection manager for debugging purposes. There must be exactly one method annotated with @ConnectionIdentifier otherwise compilation will fail.

A method annotated with @ConnectionIdentifier has to follow certain rules:

  • it must be public

  • it must not be static

  • it cannot receive parameters

  • it must return java.lang.String

The @InvalidateConnectionOn Annotation

This annotation can be attached to any method also annotated with @Processor or @Source. It receives a single argument which is the class of the exception to be caught. If the exception is thrown by the Processor/Source it will automatically invalidate the connection.