一. 下載安裝部署
(1) 下載地址: http://activemq.apache.org/activemq-5144-release.html
(2) 解壓下載的 apache-activemq-5.14.4-bin.zip 包到任意目錄;
(3) 啓動ActiveMQ ; 我是64位的系統所以啓動的就是(apache-activemq-5.14.4\bin\win64\activemq.bat)
(4) 登錄ActiveMQ;在瀏覽器中輸入地址: http://127.0.0.1:8161/admin/ 賬戶/密碼 : admin/admin
二.搭建工程
(1)spring-jmsTemplate.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"
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:property-placeholder location="classpath:datasource/activemq.properties" ignore-unresolvable="true"/>
<!-- 配置JMS連接工廠 -->
<bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="${jms.broker.url}" />
<property name="userName" value="${jms.username}" />
<property name="password" value="${jms.password}" />
</bean>
<!-- 配置JMS模板(Queue),Spring提供的JMS工具類,它發送、接收消息。 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="defaultDestination" ref="demoQueueDestination" />
<property name="receiveTimeout" value="${jms.receive.timeout}" />
<property name="pubSubDomain" value="false" /><!-- true是topic,false是queue,默認是false,此處顯示寫出false -->
</bean>
<!-- 配置JMS連接時長 -->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<constructor-arg ref="amqConnectionFactory" />
<property name="sessionCacheSize" value="${jms.connect.timeout}" />
</bean>
<!-- 定義消息隊列(Queue) -->
<bean id="demoQueueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<!-- 設置消息隊列的名字 -->
<constructor-arg>
<value>${jms.queue.name}</value>
</constructor-arg>
</bean>
<!-- 配置消息隊列監聽者(Queue) -->
<bean id="queueMessageListener" class="com.manager.jms.impl.QueueMessageListener"/>
<!-- 顯示注入消息監聽容器(Queue),配置連接工廠,監聽的目標是demoQueueDestination,監聽器是上面定義的監聽器 -->
<bean id="queueListenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="demoQueueDestination" />
<property name="messageListener" ref="queueMessageListener" />
</bean>
</beans>
(2)ProducerServiceImpl
package com.manager.jms.impl;
import com.manager.jms.ProducerService;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.Message;
/**
* Created by static on 2017/4/7.
*
*/
@Service
public class ProducerServiceImpl implements ProducerService {
@Resource(name="jmsTemplate")
public JmsTemplate jmsTemplate;
/**
* 像指定隊列發送信息
* @param destination 隊列名
* @param msg 消息
*/
@Override
public void sendMessage(Destination destination,final String msg) {
jmsTemplate.send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(msg);
}
});
}
}
(3)ConsumerServiceImpl
package com.manager.jms.impl;
import com.manager.jms.ConsumerService;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.TextMessage;
/**
* Created by static on 2017/4/10.
*
*/
@Service
public class ConsumerServiceImpl implements ConsumerService {
@Resource(name="jmsTemplate")
private JmsTemplate jmsTemplate;
@Override
public TextMessage receive(Destination destination) {
return (TextMessage) jmsTemplate.receive(destination);
}
}
(4)QueueMessageListener
package com.manager.jms.impl;
import javax.jms.Message;
import javax.jms.MessageListener;
/**
* Created by static on 2017/4/10.
* ActiveMQ 消息監聽器
*/
public class QueueMessageListener implements MessageListener {
@Override
public void onMessage(Message message) {
System.out.println(message);
}
}