springmvc和activemq的整合使用


1、簡介:ActiveMQ 是Apache出品,最流行的,能力強勁的開源消息總線。ActiveMQ 是一個完全支持JMS1.1和J2EE 1.4規范的 JMS Provider實現,盡管JMS規范出台已經是很久的事情了,但是JMS在當今的J2EE應用中間仍然扮演着特殊的地位。

2、建議在使用以前了解一下jms的一些知識

3、第一步:我們部署相關的activemq(我這里是采用自己本地linux虛擬機來實現的,以模擬中間推送消息的原理)

  activemq下載地址:http://archive.apache.org/dist/activemq/我用的是目前最新的5.14.5版本

這里有這個zip的是windows用的,tar.gz的是Linux用的。我這里采用的Linux部署

將activemq的tar解壓啟動
啟動方式在:

/root/apache-activemq-5.14.5/bin/linux-x86-64

啟動:./activemq start 暫停: ./activemq stop 重啟:./activemq restart
啟動起來后,訪問地址為:http://192.168.5.10:8161/admin 賬號密碼:都是admin(默認)

開機自啟:vi /etc/rc.local 然后在末尾加入

su - root -c '/usr/local/activemq/bin/activemq start'

這樣整個activemq就部署好了,因為activemq是單獨的項目,啟動過后不用再理會
4、第二步:Java代碼實現,具體項目的配置

  1)導包:pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.troy</groupId>
  <artifactId>activemq</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
      <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>3.2.8.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-spring</artifactId>
      <version>5.14.5</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jms</artifactId>
      <version>3.2.8.RELEASE</version>
    </dependency>
  </dependencies>
</project>

  2)根據相關jar的屬性進行xml的配置
  (1)配置activemq的連接spring-config.properties

activemq_url=tcp://192.168.5.10:61616
activemq_username=admin
activemq_password=admin

  (2)配置相關的activeMQ

<?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:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    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-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
        http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.0.xsd
        http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.14.5.xsd
        ">
    
    <context:annotation-config/>
    <context:component-scan base-package="com.troy"/>
    
    <!-- 讀取配置文件 -->
    <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <array>
                <value>classpath:conf/spring-config.properties</value>
            </array>
        </property>
    </bean>
    
    <!-- 連接 activemq-->
    <amq:connectionFactory id="amqConnectionFactory" brokerURL="${activemq_url}" userName="${activemq_username}" password="${activemq_password}"/>        
    
    <!-- 這里可以采用連接池的方式連接PooledConnectionFactoryBean -->
    <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
        <!-- 配置連接 -->
        <property name="targetConnectionFactory" ref="amqConnectionFactory"/>
        <!-- 會話的最大連接數 -->
        <property name="sessionCacheSize" value="100"/>
    </bean>
    
    <!-- 定義消息隊列topic類型,queue的方式差不多 -->
    <bean id="topic" class="org.apache.activemq.command.ActiveMQTopic">
        <!-- 定義名稱 -->
        <constructor-arg index="0" value="topic"/>
    </bean>
        
    <!-- 配置JMS模板(topic),Spring提供的JMS工具類,它發送、接收消息。 -->
    <!-- 為了測試發送消息,保留jmsTemplate的配置,實際不存在發送,只需要配置監聽即可 -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="defaultDestination" ref="topic"/>
        <!-- 非pub/sub模型(發布/訂閱),true為topic,false為queue --> 
        <property name="pubSubDomain" value="true"/>
    </bean>
    
    <!-- 監聽方式,這種方式更實用,可以一直監聽消息 -->
    <bean id="topicMessageListen" class="com.troy.activemq.TopicMessageListen"/>    
    <bean id="defaultMessageListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory"/>
        <!-- 注冊activemq名稱 -->
        <property name="destination" ref="topic"/>
        <property name="messageListener" ref="topicMessageListen"/>
    </bean>

</beans>

  3)代碼層面我謝了兩個:方便測試
  (1)一個是監聽接口獲取數據(TopicMessageListen)

public class TopicMessageListen implements MessageListener{

    public void onMessage(Message message) {        
        System.out.println("監聽==================監聽");
        try {
            System.out.println(message);
            TextMessage tm = (TextMessage)(message);
            System.out.println(tm.getText());
        } catch (Exception e) {
            e.printStackTrace();
            
        }
        
    }

}

因為我們在配置文件里面加入了監聽,這里只需要實現MessageListener接口就可以了,然后在處理message信息
  (2)發送消息(TopicSendMessage)

public class TopicSendMessage {
    private  ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring/spring-mvc.xml");
    private  JmsTemplate jmsTemplate = (JmsTemplate) ac.getBean("jmsTemplate");
    public void send(){
        
        
        jmsTemplate.send(new MessageCreator() {
            
            public Message createMessage(Session session) throws JMSException {
                TextMessage msg = session.createTextMessage();
                msg.setText("發送數據++++++++++++發送數據");
                System.out.println("發送數據++++++++++++發送數據");
                return msg;
            }
        });
    }
    
    public void receive(){
        Message msg = jmsTemplate.receive();
        TextMessage tm = (TextMessage)msg;
        System.out.println("非監聽------------------非監聽");
        System.out.println(msg);
    }
    
    public static void main(String[] args) {
        new TopicSendMessage().send();
        
    }
}

說明:發送數據的方式基本上大同小異通過獲取jmsTemplate來實現發送的操作,因為我沒有直接啟動容器,所以采用獲取bean的方式
     接收上面沒有運行,因為接收的這個receive()方法是同步運行的,會卡線程這里不做演示,我通過啟動兩個方式測試可以成功的,但是不建議這種方式

  (3)展示結果:

  (4)activemq的簡單使用和配置方式就才不多這么多

5、我的項目結構

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM