下载ActiveMQ
官方网站:http://activemq.apache.org/
ActiveMQ默认启动时,启动了内置的jetty服务器,提供一个用于监控ActiveMQ的admin应用。
admin:http://127.0.0.1:8161/admin/
用户名和密码都是admin
参考地址:http://blog.csdn.net/jiuqiyuliang/article/details/47160259
一、jms简介:
jms即Java消息服务。它主要用于在生产者和消费者之间进行消息传递,生产者负责产生消息,而消费者负责接收消息。把它应用到实际的业务需求中的话我们可以在特定的时候利用生产者生成一消息,并进行发送,对应的消费者在接收到对应的消息后去完成对应的业务逻辑。对于消息的传递有两种类型,一种是点对点的,即一个生产者和一个消费者一一对应;另一种是发布/订阅模式,即一个生产者产生消息并进行发送后,可以由多个消费者进行接收。
二、jms的简单demo
此例为queue类型,点对点。
生产者代码(直接运行,产生消息):
/**
* 消息生产者(发送者)
* @author swh
*
*/
public class JMSProducer {
//默认连接用户名
private static final String username = ActiveMQConnection.DEFAULT_USER;
//默认连接密码
private static final String password = ActiveMQConnection.DEFAULT_PASSWORD;
//默认链接地址
private static final String brokeurl = ActiveMQConnection.DEFAULT_BROKER_URL;
//发送的消息数量
private static final int sendnum = 10;
public static void main(String[] args) {
//连接工厂
ConnectionFactory connectionFactory;
//连接
Connection connection = null;
//会话 接受或者发送消息的线程
Session session = null;
//消息的目的地
Destination destination;
//消息生产者
MessageProducer messageProducer;
//实例化连接工厂
connectionFactory = new ActiveMQConnectionFactory(JMSProducer.username, JMSProducer.password, JMSProducer.brokeurl);
try {
//通过连接工厂获取连接
connection = connectionFactory.createConnection();
//启动连接
connection.start();
//创建session
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);//true为设置事务,此时第二个参数被忽略
//创建一个名称为helloworld的消息队列
destination = session.createQueue("helloworld");
//创建消息生产者
messageProducer = session.createProducer(destination);
//发送消息
sendMessage(session,messageProducer);
session.commit();
} catch (JMSException e) {
e.printStackTrace();
}finally{
try {
if(session != null){
session.close();
}
if(connection != null){
connection.close();
}
} catch (JMSException e) {
e.printStackTrace();
}
}
}
private static void sendMessage(Session session, MessageProducer messageProducer) throws JMSException {
for(int i=0;i<JMSProducer.sendnum;i++){
//创建一条文本消息
TextMessage message = session.createTextMessage("activemq发送消息"+i);
System.out.println("发送消息:activemq发送消息"+i);
//通过消息生产者发出消息
messageProducer.send(message);
}
}
}
消费者代码:
public class JMSConsumer {
//默认连接用户名
private static final String username = ActiveMQConnection.DEFAULT_USER;
//默认连接密码
private static final String password = ActiveMQConnection.DEFAULT_PASSWORD;
//默认链接地址
private static final String brokeurl = ActiveMQConnection.DEFAULT_BROKER_URL;
public static void main(String[] args) {
//连接工厂
ConnectionFactory connectionFactory;
//连接
Connection connection = null;
//会话 接受或者发送消息的线程
Session session = null;
//消息的目的地
Destination destination;
//消息生产者
MessageConsumer messageConsumer;
//实例化连接工厂
connectionFactory = new ActiveMQConnectionFactory(JMSConsumer.username, JMSConsumer.password, JMSConsumer.brokeurl);
try {
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);//false为不设事务,参数2是自动确认,客户端发送和接收消息不需要做额外的工作
destination = session.createQueue("helloworld");
messageConsumer = session.createConsumer(destination);
while(true){
TextMessage textMessage = (TextMessage)messageConsumer.receive(10000);
if(textMessage != null){
System.out.println("收到的消息:"+textMessage.getText());
}else{
System.out.println("---null--");
break;
}
}
} catch (JMSException e) {
e.printStackTrace();
}finally{
try {
if(session != null){
session.close();
}
if(connection != null){
connection.close();
}
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
三、spring整合jms
(1)maven:
<!-- activemq -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.13.3</version>
</dependency>
(2)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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616"/>
</bean>
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<!-- 目标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"/>
<!-- 开启订阅模式 default is false,queue可不设置,默认即为false-->
<property name="pubSubDomain" value="false" />
<!-- deliveryMode, priority, timeToLive 的开关,要生效,必须配置为true,默认false-->
<property name="explicitQosEnabled" value="true" />
<!-- 发送模式 DeliveryMode.NON_PERSISTENT=1:非持久 ; DeliveryMode.PERSISTENT=2:持久-->
<property name="deliveryMode" value="2" />
<!-- 消息应答方式:1自动签收;2客户端调用acknowledge方法手动签收;3不必必须签收,消息可能会重发 -->
<property name="sessionAcknowledgeMode" value="1" />
</bean>
<!--这个是队列目的地,点对点的-->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>queue</value>
</constructor-arg>
</bean>
<!-- 消息监听(queue) -->
<bean id="consumerMessageListener" class="com.activemq.spring.consumer.QueueConsumerListener"></bean>
<!-- 消息监听容器 此处监听队列queueDestination-->
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="destination" ref="queueDestination"></property>
<property name="messageListener" ref="consumerMessageListener"></property>
</bean>
<!-- topic订阅模式 -->
<bean id="topicTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
<property name="connectionFactory" ref="connectionFactory"/>
<!-- 开启订阅模式 default is false,queue可不设置,默认即为false-->
<property name="pubSubDomain" value="true" />
<!-- deliveryMode, priority, timeToLive 的开关,要生效,必须配置为true,默认false-->
<property name="explicitQosEnabled" value="true" />
<!-- 发送模式 DeliveryMode.NON_PERSISTENT=1:非持久 ; DeliveryMode.PERSISTENT=2:持久-->
<property name="deliveryMode" value="2" />
<!-- 消息应答方式:1自动签收;2客户端调用acknowledge方法手动签收;3不必必须签收,消息可能会重发 -->
<property name="sessionAcknowledgeMode" value="1" />
</bean>
<!--这个是主题目的地,一对多的-->
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="topic"/>
</bean>
<!-- 消息监听(topic) -->
<bean id="topicConsumerMessageListener" class="com.activemq.spring.consumer.TopicConsumerListener"></bean>
<!-- 消息监听容器 此处监听队列topic-->
<bean id="topicContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="destination" ref="topicDestination"></property>
<property name="messageListener" ref="topicConsumerMessageListener"></property>
</bean>
</beans>
(3)junit测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:/ActiveMQ.xml","classpath*:/spring-mvc.xml"})
public class MyTest {
@Resource
private QueueProducer queueProducer;
@Resource
private TopicProducer topicProducer;
@Resource
private QueueConsumerListener queueConsumerListener;
@Resource
private Destination queueDestination;
@Resource
private Destination topicDestination;
@Test
public void testQueueSend(){
for(int i=0;i<2;i++){
queueProducer.sendMessage(queueDestination, "hello , i'm a producer.it's a message num:"+i);
}
}
@Test
public void testTopicSend(){
topicProducer.sendMessage(topicDestination, "hi, this is topic test");
}
}
总结:1.接收信息有同步、异步接收,此处是用的异步接收(即配置并实现监听接口)。
2.因此处有两个目的地(destination),所以此处应通过byName的方法注入。
法一:@Resource
private Destination queueDestination;//queueDestination为配置文件中的id名
法二:
@Autowired
@Qualifier("topicDestination")
private Destination destinationTopic;
参考地址:http://haohaoxuexi.iteye.com/blog/1893038