一、簡介:ActiveMQ 是Apache出品,最流行的,能力強勁的開源消息總線。ActiveMQ 是一個徹底支持JMS1.1和J2EE 1.4規範的 JMS Provider實現,儘管JMS規範出臺已是好久的事情了,可是JMS在當今的J2EE應用中間仍然扮演着特殊的地位。linux
二、建議在使用之前瞭解一下jms的一些知識web
三、第一步:咱們部署相關的activemq(我這裏是採用本身本地linux虛擬機來實現的,以模擬中間推送消息的原理)spring
activemq下載地址:http://archive.apache.org/dist/activemq/我用的是目前最新的5.14.5版本apache
這裏有這個zip的是windows用的,tar.gz的是Linux用的。我這裏採用的Linux部署windows
將activemq的tar解壓啓動
啓動方式在:spring-mvc
/root/apache-activemq-5.14.5/bin/linux-x86-64
啓動:./activemq start 暫停: ./activemq stop 重啓:./activemq restart
啓動起來後,訪問地址爲:http://192.168.5.10:8161/admin 帳號密碼:都是admin(默認)session
開機自啓:vi /etc/rc.local 而後在末尾加入mvc
su - root -c '/usr/local/activemq/bin/activemq start'maven
這樣整個activemq就部署好了,由於activemq是單獨的項目,啓動事後不用再理會
四、第二步:Java代碼實現,具體項目的配置tcp
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的簡單使用和配置方式就才很少這麼多
五、個人項目結構