31
Chanaka Fernando Senior Technical Lead WSO2 Inc. WSO2 ESB internals and advanced concepts

Advaced training-wso2-esb

Embed Size (px)

Citation preview

Chanaka FernandoSenior Technical Lead

WSO2 Inc.

WSO2 ESB internals and advanced

concepts

Agenda•ESB Internal Architecture

•Transports and axis2 engine

•Synapse Runtime

•Solving common integration problems

•Extending the ESB with custom code

•ESB Connectors

Source : http://bonfirehealth.com/week-13-insights-spark-integration/

ESB Internal Architecture High level architecture

ESB Internal Architecture Component architecture (synapse)

Transports and axis2 engine Axis2 Transport

Transports and axis2 engineTransportListenerImplementations of this interface should specify how incoming messages are received and processed before handing them over to the Axis2 engine for further Processing.

TransportSenderImplementations of this interface should specify how a message can be sent out from the Axis2 engine.

TransportReceivers and TransportSenders are registered to the server using axis2.xml file

Same axis2.xml file can be used to define transport configuration parameters

Editing axis2.xml need to be followed by a server restart

Value of the name attribute can be used to uniquely identifythe transport with ESB

<transportReceiver name="http"class="org.apache.synapse.transport.nhttp.HttpCoreNIOListener“/>

<transportSender name="http"class="org.apache.synapse.transport.nhttp.HttpCoreNIOSender“/>

Transports and axis2 engineMessage BuilderIdentify the message using the content type and convert it to common XML. There is a message builder associated with each content type. WSO2 ESB includes message builders for text-based and binary content.

Message FormatterThe opposite partners of the message builders. The formatter converts the message back to its original format by referencing the content type just before the message handover to the transports.

<messageBuilder contentType="application/xml" class="org.apache.axis2.builder.ApplicationXMLBuilder"/>

<messageFormatter contentType="application/xml" class="org.apache.axis2.transport.http.ApplicationXMLFormatter"/>

Synapse RuntimeNHTTP transport

Synapse RuntimeNHTTP transport

The key advantage of this architecture is that it enables the ESB (mediators) to intercept all the messages and manipulate them in any way necessary.

The main downside is every message happens to go through the Axiom layer, which is not really necessary in cases like HTTP load balancing and HTTP header-based routing.

Also the overhead of moving data from one buffer to another was not always justifiable in this model.

The default HTTP/HTTPS transport prior to ESB 4.6.0

Synapse RuntimeBinary Relay

Synapse RuntimeBinary Relay

A Message Builder, that takes the input stream and hides it inside a fake SOAP message without reading it, and a Message Formatter that takes the input stream and writes it directly to a output stream.

• Builder : org.wso2.carbon.relay.BinaryRelayBuilder• Formatter :org.wso2.carbon.relay.ExpandingMessageFormatter

The Builder Mediator can be used to build the actual SOAP message from a message coming in to ESB through the Message Relay.

Synapse RuntimePassthrough Transport

Synapse RuntimePassthrough Transport

Based on a single buffer model and completely bypassed the Axiom layer.

On-demand message parsing in the mediation engine.

The default HTTP/HTTPS transport since ESB 4.6.0.

<transportSender name="http"class="org.apache.synapse.transport.passthru.PassThroughHttpSender“/>

<transportReceiver name="http"class="org.apache.synapse.transport.passthru.PassThroughHttpListener“/>

Synapse RuntimePassthrough Transport high level view

Synapse RuntimePassthrough Transport state machine

Solving common integration problemsMessage Transformations• Possible to add , remove , edit elements in thepayload.• Possible to transform into entirely differentmessage or different content type.

XML = > XMLXML => JSON

Mediators

XSLTXQueryPaylodFactorySmooksEnrich

Use mediators like XSLT and XQuery incomplex transformation.• Payload Factory and Enrich mediator performbetter in simple transformation scenarios

Solving common integration problemsXML->JSON

<proxy name="JSONTempProxy"><target><outSequence><property name="messageType" value="application/json" scope="axis2"/><send/></outSequence><endpoint><address uri=“...."/></endpoint></target></proxy>

Solving common integration problemsMessage Validation

Solving common integration problemsMessage Validation

<validate><schema key="temp-msg.xsd"/><on-fail><makefault version="soap11"><code xmlns:tns="http://www.w3.org/2003/05/soap-envelope" value="tns:Receiver"/><reason value="Invalid Request!!!"/></makefault><property name="RESPONSE" value="true"/><header name="To" action="remove"/><send/></on-fail></validate>

Solving common integration problemsService Chaining

Extending ESB with custom codeScheduler Task

MessageInjector

Default Task implementationInject messages into “Main” flowSupport simple scheduling and cron statementsSupport for JavaBean like property settings

import org.apache.synapse.ManagedLifecycle;import org.apache.synapse.task.Task;public class HelloWorldTask implements Task , ManagedLifecycle{private String message;public String getMessage () {}public void setMessage(String message) { }}

Extending ESB with custom codeExtending the ESB with Class mediator

Messages coming in to ESB can be processed from custom java code

Class mediator gets the same privilege as an internal ESB mediator

Needs to implement AbstractMediator interface

Parameters can be passed through setter methods

Message can be altered inside the mediate method through MessageContext object

Can be built as a jar file or as an osgi bundle

Extending ESB with custom codeExtending the ESB with Class mediator

import javax.xml.namespace.QName; import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.synapse.MessageContext;import org.apache.synapse.mediators.AbstractMediator; public class DiscountQuoteMediator extends AbstractMediator { public boolean mediate(MessageContext mc) { String price = mc.getEnvelope().getBody().getFirstElement().getFirstElement(). getFirstChildWithName(new QName("http://services.samples/xsd", "last")).getText(); System.out.println("Original price: " + price); System.out.println("Discounted price: " + discountedPrice); return true; } public void setTraceState(int traceState) { traceState = 0; } public int getTraceState() { return 0; } }

ESB ConnectorsA connector is a ready made and convenient tool to reach publicly available web API’s. Simplified configuration to access external api

‘Cloud to Cloud’ and ‘Cloud to Enterprise’ Integration

On premise integration

Connecting with systems located in your enterprise perimeter Ex: SAP, Database, SOAP/REST web serviceCan be proprietary/ legacy systems Security is a less concern when connecting with these systemsPerformance is a critical factor for business success

Cloud integration

Connecting with a publicly available cloud APIs Ex: Twitter, Salesforce, Google Spreadsheet Security needs to be handled carefully when connecting to public APIsPerformance is not critical when connecting to cloud APIsConfiguration driven approach to perform different operations on the cloud platform

ESB ConnectorsUse Case I – Connect to cloud API and extract data

ESB ConnectorsUse Case II – Cloud to Cloud integration

ESB ConnectorsUse Case III – On premise to cloud integration

soap

ESB ConnectorsWriting a custom connector to public API

A connector is a collection of templates that define operations users can call from their ESB configurations to easily access specific logic for processing messages.

Typically, connectors are used to wrap the API of an external service. For example, there are several default connectors provided with the ESB that call the APIs of services like Twitter and JIRA.

Creating a connector involves the following high-level tasks:

Research the APIs provided by the service for which you want to create a connector.

Decide which API you are going to use to write the connector. For example, JIRA provides a REST API and Java API. If you choose the REST API, you can create your connector and operations entirely from XML configuration files. If you choose a Java API, you create XML configuration files that define your connector and point to your Java classes that define the operations.

Use the connector core libraries to write your connector.

After you create the files, you package them in a ZIP file, which you can then add to an ESB instance.

ESB ConnectorsWriting a custom connector to public API

Here are some useful resources for step by step guides to write a connector for a public API

http://chanakaindrajith.blogspot.com/2014/04/getting-started-with-wso2-esb-connectors.html

https://docs.wso2.com/display/ESB481/Creating+a+Connector

http://wso2.com/library/articles/2014/02/how-to-write-a-esb-connector/

Questions

Thank You