spring整合jms系列之----點對點(一)

JMS做爲一個支持點對點(PTP)和訂閱式(pub/sub)式的消息中間件,爲不少項目開發者所使用。Spring對JMS提供了很好的支持,能夠經過JmsTemplate來方便地實現消息服務,因爲JMS對Spring的支持性很好,本文將着重於詳細說明如何spring如何整合jms作簡單的測試用例:java

1.所需jar包:

spring.jar , activemq-all-5.4.3.jar , commons-logging-api-1.1.jar , commons-io-1.3.2.jar,這些jar包能夠去本文最後面的連接裏下載。web

2.applicationContext.xml配置

 2.1 applicationContext.xml以下

 <?xml version="1.0" encoding="UTF-8"?> 
<beans          
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:jee="http://www.springframework.org/schema/jee"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  
 <!-- jms 鏈接工廠 -->
    <bean id="jmsFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <!-- 配置代理的地址,即配置activeMQ的鏈接URI,
         讓jms工廠可以鏈接到activeMQ服務器(將activeMQ暴露給客戶端使用,
         負責客戶端與activeMQ之間的鏈接通訊) -->
        <property name="brokerURL">
            <value>tcp://localhost:61616</value><!-- 一種標準URI地址,意思是說標識一個本地的端口號位61616的TCP鏈接(其中,"61616"是activeMQ默認的鏈接端口號) -->
        </property>
    </bean>
    <!-- ActiveMQ鏈接器將這種簡單等級結構的URI模式稱爲低等級的鏈接器(low-levelconnectors),
     併爲這些鏈接器實現了基本的網絡通訊協議。低等級鏈接器URIs使用主題(scheme)標識底層使用的網絡協議,
     使用路徑元素定位網絡資源服務(通常爲主機名加上端口號),使用查詢元素用來肯定鏈接器附加信息。 -->
 
    <!-- jms 鏈接池 -->
     
    <!--  
    <bean id="pooledJmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
        <property name="connectionFactory">
            <ref local="jmsFactory" />
        </property>
    </bean>
    -->
 <!-- jms 模板 -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory">
            <ref local="jmsFactory" />
        </property>
    </bean>
     
    <!-- jms Topic -->
    <bean id="myTopic" class="org.apache.activemq.command.ActiveMQTopic"
        autowire="constructor">
        <constructor-arg value="STOCKS.JAVA" />
    </bean>
 
    <!-- jms Consumer -->
    <bean id="javaConsumer"
        class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="jmsFactory" />
        <property name="destination" ref="myTopic" />
        <property name="messageListener" ref="myTextListener" />
    </bean>
 
 <!-- 消息監聽器 -->
    <bean id="myTextListener" class="demo.TextListener">
    </bean>
     
    <!-- 消息發佈器 -->
    <bean id="springPublisher" class="demo.SpringPublisher">
        <property name="template">
            <ref local="jmsTemplate" />
        </property>
        <property name="topic">
            <ref local="myTopic" />
        </property>
    </bean>
</beans>

2.2 消息生成器

package demo;
import java.util.Date;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.springframework.jms.core.MessageCreator;
public class MyMessageCreator implements MessageCreator {
 /**
     * 消息序號
     */
    private int msgNo;
  
    public MyMessageCreator(int no) {
        this.msgNo = no;
    }
     
 @Override
 public Message createMessage(Session session) throws JMSException {
  TextMessage textMsg = session.createTextMessage();
        textMsg.setText(new Date() + "第" + this.msgNo + "條消息發出");
  
        return textMsg;
 }
 
}

2.3 消息監聽器

package demo;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class TextListener implements MessageListener {
 @Override
 public void onMessage(Message message) {
  TextMessage msg = null;
    
        try {
            if (message instanceof TextMessage) {
                msg = (TextMessage) message;
                System.out.println("Reading message: " + msg.getText());
            } else {
                System.out.println("Message of wrong type: "
                        + message.getClass().getName());
            }
        } catch (JMSException e) {
            System.out.println("JMSException in onMessage(): " + e.toString());
        } catch (Throwable t) {
            System.out.println("Exception in onMessage():" + t.getMessage());
        }
 }
}

2.4 消息發佈器

package demo;
import javax.jms.Destination;
import org.springframework.jms.core.JmsTemplate;
public class SpringPublisher {
 /**
     * Jms模板
     */
    private JmsTemplate template;
  
    /**
     * Topic
     */
    private Destination topic;
  
    public JmsTemplate getTemplate() {
        return template;
    }
  
    public void setTemplate(JmsTemplate template) {
        this.template = template;
    }
  
    public Destination getTopic() {
        return topic;
    }
  
    public void setTopic(Destination topic) {
        this.topic = topic;
    }
  
    /**
     * Start
     * 
     * @throws InterruptedException
     */
    public void start() throws InterruptedException {
  
        int messageCount = 10;
  
        while ((--messageCount) > 0) {
            sendMessage(messageCount);
            Thread.sleep(1000);
        }
    }
  
    /**
     * 消息發送
     */
    protected void sendMessage(int msgNO) {
  
        this.template.send(this.topic, new MyMessageCreator(msgNO));
    }
}

2.5 測試類(注意,運行測試類前請啓動activeMQ)

 package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import demo.SpringPublisher;
public class SpringJmsTestMain {
 /**
  * @param args
  */
 public static void main(String[] args) throws InterruptedException {
    
        ApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "applicationContext.xml" });
  
        SpringPublisher publisher = (SpringPublisher) context
                .getBean("springPublisher");
        publisher.start();
    }
}

 好了,本文主要就是測試基本的spring整合jms。另:下面是我在淘寶上看到的JMS教學視頻,若是朋友們想要細緻的學習JMS,能夠移步到這裏下載JMS視頻:JMS教學視頻-淘寶spring

下篇文章將着重講解如何開發訂閱模式jms,並將闡述如何將ActiveMQ嵌入Spring中。apache

相關文章
相關標籤/搜索