## Apache Synapse

## Sample 460: Introduction to the Spring Mediator

<definitions xmlns="http://ws.apache.org/ns/synapse"> <registry provider="org.apache.synapse.registry.url.SimpleURLRegistry"> <parameter name="root">file:repository/conf/sample/resources/</parameter> <parameter name="cachableDuration">15000</parameter> </registry> <sequence name="main"> <!--Setting the Spring Mediator and its Spring Beans xml file location --> <!--Note that springtest is the bean id used in springCustomLogger.xml --> <spring bean="springtest" key="spring/springCustomLogger.xml"/> <send/> </sequence> </definitions>

This sample configuration loads an external SpringBean from a file named springCustomLogger.xml. Contents of this file are as follows.

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="springtest" class="samples.mediators.extentions.SpringCustomLogger" singleton="false"> <property name="userName"><value>"Synapse User"</value></property> <property name="email"><value>"usr@synapse.org"</value></property> </bean> </beans>

### Objective

Demonstrate how to initialize and use a SpringBean as a mediator

### Pre-requisites

- Deploy the SimpleStockQuoteService in the sample Axis2 server and start Axis2
- Start Synapse using the configuration numbered 460 (repository/conf/sample/synapse_sample_460.xml)
  Unix/Linux: sh synapse.sh -sample 460
  Windows: synapse.bat -sample 460

### Executing the Client

In this sample, the Spring Bean named 'SpringCustomLogger' gets loaded from the springCustomLogger.xml file and then it is used to log the message ID of each message being mediated. To see it in action, invoke the sample client as follows.

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/

If you have enabled logging for the samples.mediators package in the log4j.properties file, you will see an output similar to the following, on the console.

2010-09-26 20:46:57,946 [-] [HttpServerWorker-1] INFO SpringCustomLogger Starting Spring Meditor 2010-09-26 20:46:57,946 [-] [HttpServerWorker-1] INFO SpringCustomLogger Bean in Initialized with User:["Synapse User"] 2010-09-26 20:46:57,946 [-] [HttpServerWorker-1] INFO SpringCustomLogger E-MAIL:["usr@synapse.org"] 2010-09-26 20:46:57,946 [-] [HttpServerWorker-1] INFO SpringCustomLogger Massage Id: urn:uuid:383FA8B27D7CC549D91285514217720 2010-09-26 20:46:57,946 [-] [HttpServerWorker-1] INFO SpringCustomLogger Logged....

Similarly you can import any SpringBean into the Synapse runtime using the spring mediator, and use Spring to execute mediation rules.

Back to Catalog