强烈推荐
分享一个大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到人工智能的队伍中来!http://www.captainbed.net
通过上一节的消息中间件学习笔记六 – Spring JMS理论的学习,知道spring是如何集成了jms的,本节中我们将学习如何在spring中使用jms。
使用idea创建一个maven项目
引入依赖
<?xml version="1.0" encoding="UTF-8"?>
<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.winter.jms</groupId>
<artifactId>spring-jms</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<spring.version>4.2.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
<exclusions>
<exclusion><!-- 因为我们自己已经引用了这个包,并且这个包中也附带了这个包,所以需要排除掉 -->
<artifactId>spring-context</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
项目结构
创建配置spring配置: producer.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:annotation-config/>
<!-- ActiveMQ为我们提供的ConnectionFactory -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://127.0.0.1:61616" />
</bean>
<!-- spring jms 为我们提供的连接池 -->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory" ref="targetConnectionFactory"/>
</bean>
<!-- 一个队列的目的地,点对点 -->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="queue"/>
</bean>
<!-- 配置JMSTemplate,用于发送消息 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
</bean>
<bean class="com.winter.jms.producer.impl.ProducerServiceImpl"/>
</beans>
队列模式
创建生产者服务
接口
package com.winter.jms.producer;
/**
*
* Created by Administrator on 2017/11/2.
*/
public interface ProducerService {
void sendMessage(String message);
}
实现
package com.winter.jms.producer.impl;
import com.winter.jms.producer.ProducerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import javax.annotation.Resource;
import javax.jms.*;
/**
*
* Created by Administrator on 2017/11/2.
*/
public class ProducerServiceImpl implements ProducerService {
@Autowired
JmsTemplate jmsTemplate;
@Resource(name = "queueDestination")
Destination destination;
public void sendMessage(final String message) {
jmsTemplate.send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage(message);
System.out.println("发送消息 = [" + textMessage.getText() + "]");
return textMessage;
}
});
}
}
启动类
package com.winter.jms.producer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 生产者服务的启动类
* Created by Administrator on 2017/11/2.
*/
public class AppProducer {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("producer.xml");
ProducerService service = context.getBean(ProducerService.class);
for (int i = 0; i < 100; i++) {
service.sendMessage("test" + i);
}
context.close();
}
}
测试
- 打开ActiveMQ的后台:http://localhost:8161/
- 启动刚才创建的生产者启动类中的"main"方法
即可看到正常的输出了。
创建消费者服务
创建新的包:consumer
现在看一下项目的结构:
创建新的spring配置文件:consumer.xml
由于一些配置两个配置文件有公用的部分,所以我们添加了一个新的配置文件:common.xml
用于存放两个配置文件中相同的配置,看一下更改之后的三个配置文件的内容吧
common.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:annotation-config/>
<!-- ActiveMQ为我们提供的ConnectionFactory -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://127.0.0.1:61616" />
</bean>
<!-- spring jms 为我们提供的连接池 -->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory" ref="targetConnectionFactory"/>
</bean>
<!-- 一个队列的目的地,点对点 -->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="queue"/>
</bean>
</beans>
producer.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">
<!-- 导入公共配置 -->
<import resource="common.xml"/>
<!-- 配置JMSTemplate,用于发送消息 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
</bean>
<bean class="com.winter.jms.producer.impl.ProducerServiceImpl"/>
</beans>
consumer.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">
<!-- 导入公共配置 -->
<import resource="common.xml"/>
<!-- 配置消息监听器 -->
<bean id="consumerMessageListener" class="com.winter.jms.consumer.ConsumerMessageListener"/>
<!-- 配置消息监听容器 -->
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<!-- 消息监听地址 -->
<property name="destination" ref="queueDestination"/>
<!-- 消息监听器 -->
<property name="messageListener" ref="consumerMessageListener"/>
</bean>
</beans>
消费者
消费服务监听器
ConsumerMessageListener
package com.winter.jms.consumer;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
/**
* 消息监听器
* Created by Administrator on 2017/11/2.
*/
public class ConsumerMessageListener implements MessageListener {
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("接收message: " + textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
消费监听器启动类
AppConsumer
package com.winter.jms.consumer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 消费者启动类
* Created by Administrator on 2017/11/2.
*/
public class AppConsumer {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
}
}
测试
启动"main"方法即可看到消息被消费了,查看ActiveMQ后台可看到。由于之前我生产者启动了两次,导致生产了200条消息,现在已经被全部消费掉了
如果我们同时启动两个消费者,会出现什么样的现象呢?
然后我们再启动一个生产者。
可以看到控制台打印出的信息:
消费者A(AppConsumer):
接收message: test0
接收message: test2
接收message: test4
接收message: test6
接收message: test8
接收message: test10
接收message: test12
接收message: test14
接收message: test16
接收message: test18
接收message: test20
接收message: test22
接收message: test24
接收message: test26
.
.
.
消费者B(AppConsumer):
接收message: test1
接收message: test3
接收message: test5
接收message: test7
接收message: test9
接收message: test11
接收message: test13
接收message: test15
接收message: test17
接收message: test19
接收message: test21
接收message: test23
接收message: test25
接收message: test27
.
.
.
可以看出队列模式是平均消费的,被消费者A消费的消息不能再被消费者B消费
主题模式
配置
- 在common.xml中加入:
<!-- 一个主题目的地,发布订阅模式 -->
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="topic"/>
</bean>
- 将
ProducerServiceImpl
中的:
@Resource(name = "queueDestination")
Destination destination;
改成
@Resource(name = "topicDestination")
Destination destination;
- 修改consumer.xml:
修改
<!-- 消息监听地址 -->
<property name="destination" ref="queueDestination"/>
修改成:
<!-- 消息监听地址 -->
<property name="destination" ref="topicDestination"/>
只需要三步就可以将 队列模式 改成 主题模式 了。
测试:
启动两个消费者 AppConsumer
, 然后启动一个生产者 Producer
可以看到输出了:
消费者A:
接收message: test0
接收message: test1
接收message: test2
接收message: test3
接收message: test4
接收message: test5
接收message: test6
接收message: test7
接收message: test8
接收message: test9
接收message: test10
接收message: test11
接收message: test12
接收message: test13
接收message: test14
接收message: test15
接收message: test16
接收message: test17
接收message: test18
.
.
.
消费者B:
接收message: test0
接收message: test1
接收message: test2
接收message: test3
接收message: test4
接收message: test5
接收message: test6
接收message: test7
接收message: test8
接收message: test9
接收message: test10
接收message: test11
接收message: test12
接收message: test13
接收message: test14
接收message: test15
接收message: test16
接收message: test17
接收message: test18
.
.
.
可以看出:主题模式下,生产者发布的消息,只要消费者提前订阅,就可以拿到所有的消息。