## Apache Synapse

## Sample 381: Class Mediator for CBR of Binary Messages

<definitions xmlns="http://ws.apache.org/ns/synapse"> <proxy name="JMSBinaryProxy" transports="jms"> <target inSequence="BINARY_CBR_SEQ"/> </proxy> <sequence name="BINARY_CBR_SEQ"> <in> <log level="full"/> <property action="set" name="OUT_ONLY" value="true"/> <class name="samples.mediators.BinaryExtractMediator"> <property name="offset" value="11"/> <property name="length" value="4"/> <property name="variableName" value="symbol"/> <property name="binaryEncoding" value="utf-8"/> </class> <log level="custom"> <property name="symbol" expression="get-property('symbol')"/> </log> <switch source="get-property('symbol')"> <case regex="GOOG"> <send> <endpoint> <address uri="jms:/dynamicTopics/mdd.GOOG?transport.jms.ConnectionFactoryJNDIName=TopicConnectionFactory&java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory&java.naming.provider.url=tcp://localhost:61616&transport.jms.DestinationType=topic"/> </endpoint> </send> </case> <case regex="MSFT"> <send> <endpoint> <address uri="jms:/dynamicTopics/mdd.MSFT?transport.jms.ConnectionFactoryJNDIName=TopicConnectionFactory&java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory&java.naming.provider.url=tcp://localhost:61616&transport.jms.DestinationType=topic"/> </endpoint> </send> </case> <default/> </switch> </in> </sequence> </definitions>

### Objective

Demonstrate an advanced content based routing (CBR) scenario using a custom mediator

### Pre-requisites

- Setup and start a JMS broker (Apache ActiveMQ can be used as the JMS broker for this scenario.Refer JMS setup guide for information on how to run ActiveMQ.)
- Enable the JMS transport receiver and sender of Synapse (Refer JMS setup guide for more details)
- Start Synapse using the configuration numbered 381 (repository/conf/sample/synapse_sample_381.xml)
  Unix/Linux: sh synapse.sh -sample 381
  Windows: synapse.bat -sample 381

### Executing the Client

In this configuration, a proxy service has been defined to accept incoming JMS messages. JMS messages contain binary payloads. User configures the offset, length, and binary encoding of the text literal that Synapse should use for CBR. Configuration simply routes the messages based on this text to different endpoints.

A JMS producer and two instances of a consumer used to demonstrate the CBR functionality.

Now run the first consumer using the following command.

ant mddconsumer -Djms_topic=mdd.MSFT

Now run the second consumer using the following command.

ant mddconsumer -Djms_topic=mdd.GOOG

Now run the market data producer to genenrate market data for symbol 'MSFT' using the following command.

ant mddproducer -Dsymbol=MSFT

Now run the market data producer to genenrate market data for symbol 'GOOG' using the following command.

ant mddproducer -Dsymbol=GOOG

You will see the below output in the client console(s) based on the symbol.

mddconsumer: [java] Market data recived for symbol : topic://mdd.MSFT [java] Market data recived for symbol : topic://mdd.MSFT

Back to Catalog