connection management

26
Connection Management Srilatha Kante

Upload: srilatha-kante

Post on 03-Mar-2017

60 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: Connection management

Connection Management

Srilatha Kante

Page 2: Connection management

Objective• Overview of Devkit• Custom Connector Creation• Connection Management Framework introduction• Authentication Protocols• Connection Management Annotation• Connection Management Devkit Annotations

Page 3: Connection management

Overview of Devkit• What is Devkit?

• Devkit has the tooling and interfaces for the custom connector creation

• Where to get it?• Devkit can be found as a plugin to the Anypoint Studio in update sites.

• What it does?• Devkit provides all the supporting tools and pulls all the dependency

interfaces in order to create our own custom connector.

Page 4: Connection management

Custom Connector Creation• Anypoint Studio supports to create our own connector project• Steps :

• New – Select Anypoint Connector Project• This should be maven based project• Should automatically pull all the dependencies for the connector project• Look for the FunctionalTest java file which is implemented with the sample

processor.• By default there should be Greet operation implemented for this connector

Page 5: Connection management

Connection Management • The Connection Management framework provides multi-tenancy

capabilities for a connector (managing multiple simultaneous connections with different credentials for each connector within your application), as well as connection pooling and instance pooling.

• These benefits are available for most authentication schemes other than OAuth, and for APIs that do not require authentication. Basic Authentication is designated in a connector with the @ConnectionManagement attribute.

Page 6: Connection management

About Connection Management• Transparent multi-tenancy – A Mule application can open many

connections to a single target using different credentials to connect on behalf of many users simultaneously.

• Connection pooling – Automated management of a pool of instances of the connector class to manage connection resources effectively.

• Exceptions - The ability to automatically invalidate connections on exceptions and to reconnect as needed.

Page 7: Connection management

Connection Management Framework and Authentication Protocols• Connection Management framework in conjunction with most

authentication protocols, such as • username and password authentication (most common), • SAML,• Kerberos,• LDAP,• NTLM

• OAuth related annotations can be used for more OAuth based authentication

Page 8: Connection management

Connection Management Annotation• @ConnectionManagement uses following method level annotations

• @Connect method - Creates a connection• @Disconnect method - Explicitly closes a connection• @ValidateConnection method - Returns true if a connection is still valid, false

otherwise• @ConnectionIdentifier method - Returns a prefix used in generating unique

identifiers for connector instances in the connection pool

Page 9: Connection management

ConnectionManagementStrategy class• @ConnectionManagement(friendlyName="Connector Connection")

public class ConnectionManagementStrategy{ @Connect @TestConnectivity public void

connect(@ConnectionKey String username, @Password String password){ ... } @Disconnect public void disconnect() { client = null; } @ValidateConnection public boolean isConnected() { return client != null; } @ConnectionIdentifier public String connectionId() { return client.toString(); } }

Page 10: Connection management

Note:• All the annotated methods are called automatically as needed by the

DevKit framework; you never call them directly from your code

Page 11: Connection management

@Connector class• @Connector(name = "connector") public class MyConnectorWithConnectionManagement { @ConnectionStrategy

@Config ConnectorConfig config;private ConnectionManagementStrategy strategy; /** * Processors */ }

Page 12: Connection management

In Connector Class

Page 13: Connection management

@Connect• This method is responsible for creating a connection to the target. • The @Connect method is called automatically by Mule when the

connector starts up, or if the connection to the API has been lost and must be reestablished.

• When this method finishes, if authentication is successful, the connector instance is ready to make operations

Page 14: Connection management

More on @Connect

• A method annotated with @Connect must:• Be public• Throw org.mule.api.ConnectionException (and no other exceptions)• Have a void return type• Annotate only one method with @Connect• Annotate @Connect method with @TestConnectivity• Annotate at least one parameter with @ConnectionKey

Page 15: Connection management

Example of @Connect

Page 16: Connection management

@TestConnectivity• Annotation displays a button in Anypoint Studio when configuring the

Connector, this button allows the user to test if the connection is successful with his configuration.

• @TestConnectivity runs @Connect method and expects an org.mule.api.ConnectionExcetion, if this exception occurs then the test fails, if not connection is assumed successful.

• This can be set as inactive by specifying,• @TestConnectivity(active = false)

Page 17: Connection management

@ConnectionKey• For username and password authentication, the username is the

obvious choice for @ConnectionKey• For other protocols, identify the value that is most obviously

associated with different users and access privileges that connect to your service, and use this value as your @ConnectionKey

Page 18: Connection management

@Disconnect• This annotation indicates the method inside a

@ConnectionManagement class that is responsible for disposal of a connection. This method is called when the connector is shut down or the connection is explicitly terminated.

• A method annotated with @Disconnect must:Be publicTake no input parametersHave a void return typeThe class must have exactly one annotated @Disconnect method

Page 19: Connection management

@Disconnect Example

Page 20: Connection management

@ValidateConnection• This method is called by Mule to check whether the connection is

actually open or not.• A method annotated with @ValidateConnection

• Be public• Take no input parameters• Return boolean or java.lang.Boolean• Only one method on the class can be annotated with @ValidateConnection

Page 21: Connection management

Example of ValidateConnection• @ValidateConnection

public boolean isConnected() { return connection != null;

}

Page 22: Connection management

@ConnectionIdentifier• This annotation identifies a method inside the

@ConnectionManagement class that returns a unique identifier for the connection, used for logging and debugging.

• A method annotated with @ConnectionIdentifier must,• Be public• Not be static• Not take arguments• Return java.lang.String• Annotate only one method with @ConnectionIdentifier

Page 23: Connection management

@ConnectionIdentifier• This code returns the connection

SessionId as an identifier (if available).

• The SessionHeader object in this case contains header information about the current connection to the API, including the session ID.

Page 24: Connection management

@ReconnectOn• Receives list of exceptions instead of just a single exception, and can

be used at both the class and processor levels.• This annotation is used for exception handling related to connections.

It can be used at class level or at a method level. If the Connector or Processor throws an exception of this class @ReconnectOn then automatically invalidates the connection

• @ReconnectOn receives a list containing the classes of the exceptions to be caught.

When an exception occurs, the @ReconnectOn behavior is based on the configured reconnection strategy

Page 25: Connection management

Example @ReconnectOn• @Processor @ReconnectOn(exceptions = {InvalidSessionFault.class,

PasswordChangedException.class}) public void myOperation(@Optional String source, @Optional Object destination) throws InvalidSessionFault, PasswordChangedException, InvalidParameterException {

/** * CODE FOR MY OPERATION */

}

Page 26: Connection management

Enjoy Your Connector!!Thank You