Mule学习笔记(1)—— 使用JMS发送和接收消息

mule-config.xml配置如下:(src/main/app)

<? xml version= "1.0" encoding= "UTF-8" ?>

< mule xmlns:tracking = "http://www.mulesoft.org/schema/mule/ee/tracking"
       xmlns:jms= "http://www.mulesoft.org/schema/mule/jms"
       xmlns:mulexml= "http://www.mulesoft.org/schema/mule/xml"
       xmlns= "http://www.mulesoft.org/schema/mule/core"
       xmlns:context= "http://www.springframework.org/schema/context"
       xmlns:doc= "http://www.mulesoft.org/schema/mule/documentation"
       xmlns:beans= "http://www.springframework.org/schema/beans" version= "EE-3.5.1"
       xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation=
       "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
      http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd       
      http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-current.xsd
      http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
      http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd">

    <context:property-placeholder location= "classpath:mule-app.properties" />

    <jms:activemq-connector name = "Active_MQ" specification= "1.1"
    brokerURL= "${jms.url} "
    validateConnections= "true" doc:name= "Active_MQ" />
   
    <custom-transformer class = "com.adcc.esb.transformers.JmsToXmlTrans" name= "JmsToXmlTrans" />

   
    <flow name = "esbFlow1" doc:name= "esbFlow1">
   
       <!-- 使用jms主题接入 -->
        <jms:inbound-endpoint topic = "${message.in} " connector-ref ="Active_MQ"doc:name = "JMS.in"/>
        <!-- 记录接收消息的时间 -->
        <logger level = "INFO"
                      message= "Message received:#[org . mule. util . DateUtils. getTimeStamp ( 'yyyyMMddHHmmss.SSS ' ) ]"/>
             <!-- 使用自定义转换器生成DOM树 -->
             <transformer ref = "JmsToXmlTrans" />
             <!-- mulexml缺省转换器转换DOM树到xml -->
             <mulexml:dom-to-xml-transformer />
            
             <!-- 使用jms主题输出消息 -->
        <jms:outbound-endpoint topic = "${message.out} " connector-ref ="Active_MQ"doc:name = "JMS.out"/>
       
    </flow >
</ mule>


自定义转换器JmsToXmlTrans,使用dom4j生成dom树:(src/main/java)

package com.adcc.esb.transformers;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractTransformer;
import com.adcc.esb.pojo.Person;

public class JmsToXmlTrans extends AbstractTransformer {
       @Override
       protected Object doTransform(Object payload, String enc)
                   throws TransformerException {

            Person person = (Person) payload;
            
            Document document = DocumentHelper. createDocument();
            Element root = document.addElement( "people");
            Element student = root.addElement( "student");
            Element name = student.addElement( "name");
            Element age = student.addElement( "age");
            
            name.setText(person.getName());
            age.setText(person.getAge() + "");
      
            return document;
      }

}


传输的对象Person类:(src/main/java)

package com.adcc.esb.pojo;
import java.io.Serializable;
public class Person implements Serializable {
       private static final long serialVersionUID = 1L;
       private String name;
       private Integer age;
       public String getName() {
             return name;
      }
       public void setName(String name) {
             this. name = name;
      }
       public Integer getAge() {
             return age;
      }
       public void setAge(Integer age) {
             this. age = age;
      }
       @Override
       public String toString() {
             return "Person [name=" + name + ", age=" + age + "]";
      }

}


属性配置文件mule-app.properties:(src/main/app)

jms.url=tcp://localhost:61616
message.in= messages.v1
message.out= messages.v2


测试类:(src/test/java)

package com.adcc.esb.test;
import static org.junit.Assert.assertNotNull ;
import static org.junit.Assert.assertNull ;
import org.junit.Test;
import org.mule.api.MuleMessage;
import org.mule.api.client.MuleClient;
import org.mule.tck.junit4.FunctionalTestCase;
import com.adcc.esb.pojo.Person;

public class TestJms extends FunctionalTestCase {
       @Override
       protected String getConfigResources() {
             return "mule-config.xml" ;
      }
       @Test
       public void testJms() throws Exception {
            
            Person p2 = new Person();
            p2.setName( "Wang");
            p2.setAge(23);
            
            MuleClient client = muleContext.getClient();
            
            client.dispatch( "jms://topic:messages.v1" , p2, null );
            MuleMessage result = client.request("jms://topic:messages.v2" ,RECEIVE_TIMEOUT);
            System. out.println( "\n\n###################" + result.getPayload() + "####################\n\n" );
             assertNotNull(result);
             assertNull(result.getExceptionPayload());
      }
      
}


你可能感兴趣的:(java,javaee,jms,mule,ESB)