1. 程式人生 > >消息中間件JMS(三)

消息中間件JMS(三)

生產者 fault sca 訂閱 消息發送 work 一個 system ESS

1. Spring整合JMS

1.1消息生產者

創建工程springJMS_producer,並在pom文件中引入SpringJms 、activeMQ以及單元測試相關依賴

<properties>
      <spring.version>4.2.4.RELEASE</spring.version>
  </properties>
  
  <dependencies>
    <dependency>
        <groupId>org.springframework</
groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <
version>${spring.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> </dependency> <dependency> <groupId>
org.apache.activemq</groupId> <artifactId>activemq-client</artifactId> <version>5.13.4</version> </dependency> </dependencies>

在src/main/resources下創建spring配置文件applicationContext-jms-producer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:amq="http://activemq.apache.org/schema/core"
    xmlns:jms="http://www.springframework.org/schema/jms"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context.xsd">
        
        
    <context:component-scan base-package="com.java.demo"></context:component-scan>     
    
       
    <!-- 真正可以產生Connection的ConnectionFactory,由對應的 JMS服務廠商提供 -->  
    <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">  
        <property name="brokerURL" value="tcp://192.168.25.129:61616"/>  
    </bean>
       
    <!-- Spring用於管理真正的ConnectionFactory的ConnectionFactory -->  
    <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">  
    <!-- 目標ConnectionFactory對應真實的可以產生JMS Connection的ConnectionFactory -->  
        <property name="targetConnectionFactory" ref="targetConnectionFactory"/>  
    </bean>  
           
    <!-- Spring提供的JMS工具類,它可以進行消息發送、接收等 -->  
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">  
        <!-- 這個connectionFactory對應的是我們定義的Spring提供的那個ConnectionFactory對象 -->  
        <property name="connectionFactory" ref="connectionFactory"/>  
    </bean>      
    <!--這個是隊列目的地,點對點的  文本信息-->  
    <bean id="queueTextDestination" class="org.apache.activemq.command.ActiveMQQueue"> 
        <!-- 隊列名稱 --> 
        <constructor-arg value="queue_text"/>  
    </bean>    
    
    <!--這個是訂閱模式  文本信息-->  
    <bean id="topicTextDestination" class="org.apache.activemq.command.ActiveMQTopic">  
        <constructor-arg value="topic_text"/>  
    </bean>  
    
</beans>

在com.java.demo包下創建消息生產者類

@Component
public class QueueProducer {

    @Autowired
    private JmsTemplate jmsTemplate;
    @Autowired
    private Destination queueTextDestination;
    
    /**
     * 發送文本信息
     * @param text
     */
    public void sendTextMessage(final String text) {
        jmsTemplate.send(queueTextDestination, new MessageCreator() {
            
            public Message createMessage(Session session) throws JMSException {
                //在匿名內部類使用外部類的參數,需要用final來修飾
                return session.createTextMessage(text);
            }
        });
    }
}
@Component
public class TopicProducer {
    
    @Autowired
    private JmsTemplate jmsTemplate;
    @Autowired
    private Destination topicTextDestination;
    
    public void sendTextMessage(final String text) {
        jmsTemplate.send(topicTextDestination, new MessageCreator() {
            
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(text);
            }
        });
    }

}

單元測試

在src/test/java創建測試類

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-jms-producer.xml")
public class TestQueue {

    @Autowired
    private QueueProducer queueProducer;
    @Autowired
    private TopicProducer topicProducer;
    
    @Test
    public void sendText() {
        queueProducer.sendTextMessage("Spring JMS point to point");
    }
    
    @Test
    public void sendText2() {
        topicProducer.sendTextMessage("Spring JMS publish/topic");
    }
    
}

1.2 消息消費者

創建工程springjms_consumer,在POM文件中引入依賴 (同上一個工程)

創建配置文件 applicationContext-jms-consumer-queue.xml(點對點)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:amq="http://activemq.apache.org/schema/core"
    xmlns:jms="http://www.springframework.org/schema/jms"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!-- 真正可以產生Connection的ConnectionFactory,由對應的 JMS服務廠商提供-->  
    <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">  
        <property name="brokerURL" value="tcp://192.168.25.129:61616"/>  
    </bean>
       
    <!-- Spring用於管理真正的ConnectionFactory的ConnectionFactory -->  
    <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">  
    <!-- 目標ConnectionFactory對應真實的可以產生JMS Connection的ConnectionFactory -->  
        <property name="targetConnectionFactory" ref="targetConnectionFactory"/>  
    </bean>  
    
    <!--這個是隊列目的地,點對點的  文本信息-->  
    <bean id="queueTextDestination" class="org.apache.activemq.command.ActiveMQQueue">  
        <constructor-arg value="queue_text"/>  
    </bean>    
    
    <!-- 我的監聽類 -->
    <bean id="myMessageListener" class="com.java.demo.MyMessageListener"></bean>
    <!-- 消息監聽容器 -->
    <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="destination" ref="queueTextDestination" />
        <property name="messageListener" ref="myMessageListener" />
    </bean>
    
</beans>

創建配置文件 applicationContext-jms-consumer-topic.xml(發布/訂閱),與上面的配置沒什麽區別,就是引入的隊列不一樣

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:amq="http://activemq.apache.org/schema/core"
    xmlns:jms="http://www.springframework.org/schema/jms"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!-- 真正可以產生Connection的ConnectionFactory,由對應的 JMS服務廠商提供-->  
    <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">  
        <property name="brokerURL" value="tcp://192.168.25.129:61616"/>  
    </bean>
       
    <!-- Spring用於管理真正的ConnectionFactory的ConnectionFactory -->  
    <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">  
    <!-- 目標ConnectionFactory對應真實的可以產生JMS Connection的ConnectionFactory -->  
        <property name="targetConnectionFactory" ref="targetConnectionFactory"/>  
    </bean>  
    
    <!--這個是隊列目的地,點對點的  文本信息-->  
    <bean id="topicTextDestination" class="org.apache.activemq.command.ActiveMQTopic">  
        <constructor-arg value="topic_text"/>  
    </bean>    
    
    <!-- 我的監聽類 -->
    <bean id="myMessageListener" class="com.java.demo.MyMessageListener"></bean>
    <!-- 消息監聽容器 -->
    <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="destination" ref="topicTextDestination" />
        <property name="messageListener" ref="myMessageListener" />
    </bean>
    
</beans>

編寫監聽類

public class MyMessageListener implements MessageListener {

    public void onMessage(Message message) {
        TextMessage textMessage = (TextMessage) message;
        try {
            System.out.println("接收消息:" + textMessage.getText());
        } catch (JMSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

單元測試

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-jms-consumer-queue.xml")
public class TestQueue {

    @Test
    public void testQueue() {
        try {
            System.in.read();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-jms-consumer-topic.xml")
public class TestTopic {

    @Test
    public void testQueue() {
        try {
            System.in.read();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

結果:

(1) 點對點

技術分享圖片

技術分享圖片

(2) 發布訂閱

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

消息中間件JMS(三)