Synapse handler is new feature which come with the ESB 4.9.0. It provide abstract handler implementation to the users. User can create their own concrete handlers which is executing in the synapse layer. Main intention of this blog post is to explain how to write synapse handler and explain basic theoretical background.
1. What is the handler?
Handlers are basically talking with the chain of responsibility pattern. Chain of responsibility allows a number of classes to attempt to handle a request independently of any other object along the chain. Once the request is handled, it completes it's journey through the chain.
The Handler defines the interface which required to handle the request and concreteHandlers handle request in a specific manner that they are responsible for.
2. What is Synapse handler?
Synapse handler is providing abstract handle implementation which executes in the following four scenarios.
1. Request in flow
This is executing when request is hitting to the synapse engine.
public boolean handleRequestInFlow(MessageContext synCtx);
2. request out flow
This is executing when request goes out from the synapse engine.
public boolean handleRequestOutFlow(MessageContext synCtx);
3. Response in flow
This is executing when response is hitting to the synapse engine.
public boolean handleResponseInFlow(MessageContext synCtx);
4. Response out flow
This is executing when response goes out from the synapse engine.
public boolean handleResponseOutFlow(MessageContext synCtx);
Following diagram shows the basic component structure of the ESB and how above mentioned scenarios are executing in the request and response flow.
3. How to write concrete Synapse handler?
You can implement concrete handler by implementing SynapseHandler(org.apache.synapse.SynapseHandler) interface or can extends AbstractSynapseHandler(org.apache.synapse.AbstractSynapseHandler) class.
public class TestHandler extends AbstractSynapseHandler {
private static final Log log = LogFactory.getLog(TestHandler.class);
@Override
public boolean handleRequestInFlow(MessageContext synCtx) {
log.info("Request In Flow");
return true;
}
@Override
public boolean handleRequestOutFlow(MessageContext synCtx) {
log.info("Request Out Flow");
return true;
}
@Override
public boolean handleResponseInFlow(MessageContext synCtx) {
log.info("Response In Flow");
return true;
}
@Override
public boolean handleResponseOutFlow(MessageContext synCtx) {
log.info("Response Out Flow");
return true;
}
}
4. Configuration
You need to add following configuration item to the synapse-handler.xml(repository/conf) file to enable deployed handler.
<handlers>
<handler name="TestHandler" class="package.TestHandler"/>
</handlers>
5. Deployment
Handler can be deployed as a OSGI bundle or jar file to the ESB.
Comments