The Spring Mediator exposes a spring bean as a mediator. The Spring Mediator creates an instance of a mediator, which is managed by Spring. This Spring bean must implement the
Syntax of Spring Mediator
For explaining clearly, I have created to Person and Student Beans.
Person Bean.
now, add the spring configuration (springConfig.xml) to the registry path /_system/config/repository/springConfig.xml
[1]. https://docs.wso2.com/display/ESB490/Spring+Mediator
Mediator
interface for it to act as a Mediator.Syntax of Spring Mediator
<spring:spring bean="exampleBean" key="string"/>First, we need to create a Mediator Project for Bean classes. (for more information visit the How to use WSO2 Class Mediator in WSO2 ESB )
For explaining clearly, I have created to Person and Student Beans.
Person Bean.
package lk.harshana; import org.apache.synapse.MessageContext; import org.apache.synapse.mediators.AbstractMediator; public class Person extends AbstractMediator { private String name; private String address; private String email; @Override public boolean mediate(MessageContext arg0) { System.out.println("starting person"); System.out.println("name : " + name); System.out.println("address : " + address); System.out.println("email : " + email); System.out.println("ending person bean"); return true; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }Student Bean
package lk.harshana; import org.apache.synapse.MessageContext; import org.apache.synapse.mediators.AbstractMediator; public class Student extends AbstractMediator { private String studentId; private String grade; private Person person; @Override public boolean mediate(MessageContext arg0) { System.out.println("===== student bean working ===="); System.out.println("student id : " + studentId); System.out.println("student grade : " + grade); System.out.println("student name : " + person.getName()); System.out.println("student address : " + person.getAddress()); System.out.println("student email : " + person.getEmail()); System.out.println("=====studnet bean ending ===="); return true; } public String getStudentId() { return studentId; } public void setStudentId(String studentId) { this.studentId = studentId; } public String getGrade() { return grade; } public void setGrade(String grade) { this.grade = grade; } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } }Build the project and copy the jar file into <ESB_HOME>/repository/components/lib folder.
now, add the spring configuration (springConfig.xml) to the registry path /_system/config/repository/springConfig.xml
<?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="Person1" class="lk.harshana.Person"> <property name="name" value="Harshana" /> <property name="address" value="Warakapola" /> <property name="email" value="harshanacslab@gmail.com" /> </bean> <bean id="Person2" class="lk.harshana.Person"> <property name="name" value="Madusanka" /> <property name="address" value="Kohombadeniya" /> <property name="email" value="abc@gmail.com" /> </bean> <bean id="Student" class="lk.harshana.Student"> <property name="studentId" value="ps-10-104" /> <property name="grade" value="university" /> <property name="person" ref="Person1"/> </bean> </beans>Okay, now we need to create the proxy service to implement the Spring mediator.
<?xml version="1.0" encoding="UTF-8"?> <proxy name="SpringSampleProxy" startOnLoad="true" transports="http https" xmlns="http://ws.apache.org/ns/synapse"> <target> <inSequence> <log level="full"> <property name="start_log" value="starting spring proxy"/> </log> <spring:spring bean="Person1" key="conf:/repository/springConfig.xml" xmlns:spring="http://ws.apache.org/ns/synapse"/> <spring:spring bean="Person2" key="conf:/repository/springConfig.xml" xmlns:spring="http://ws.apache.org/ns/synapse"/> <spring:spring bean="Student" key="conf:/repository/springConfig.xml" xmlns:spring="http://ws.apache.org/ns/synapse"/> <log level="full"> <property name="end_log" value="ending spring proxy"/> </log> </inSequence> <outSequence/> <faultSequence/> </target> </proxy>Invoke the proxy. you can see the following log in ESB
[2018-09-15 22:17:29,365] INFO - LogMediator To: /services/SpringSampleProxy.SpringSampleProxyHttpSoap12Endpoint, WSAction: urn:mediate, SOAPAction: urn:mediate, MessageID: urn:uuid:95fa9f49-f9b1-4af0-a896-afafc9a42a41, Direction: request, start_log = starting spring proxy, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"><soapenv:Body/></soapenv:Envelope> starting person name : Harshana address : Warakapola email : harshanacslab@gmail.com ending person bean starting person name : Madusanka address : Kohombadeniya email : abc@gmail.com ending person bean ===== student bean working ==== student id : ps-10-104 student grade : university student name : Harshana student address : Warakapola student email : harshanacslab@gmail.com =====studnet bean ending ==== [2018-09-15 22:17:29,394] INFO - LogMediator To: /services/SpringSampleProxy.SpringSampleProxyHttpSoap12Endpoint, WSAction: urn:mediate, SOAPAction: urn:mediate, MessageID: urn:uuid:95fa9f49-f9b1-4af0-a896-afafc9a42a41, Direction: request, end_log = ending spring proxy, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"><soapenv:Body/></soapenv:Envelope>References
[1]. https://docs.wso2.com/display/ESB490/Spring+Mediator
Comments
Post a Comment