版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fu123123fu/article/details/84424449
核心类介绍
ConnectionFactory(用于管理连接的工厂)
一个spring为我们提供的连接池
JmsTemplate每次发送消息都会重新创建连接、会话和producer
spring中提供了SingleConnectionFactory和CachingConnectionFactory
JmsTemplate(用于发送和接收消息的模板类)
是spring提供的,只需向spring容器类注册这个类就可以使用JmsTemplate方便的操作jms
JmsTemplate是线程安全的,可以在整个应用范围使用
MessageListener(消息监听器)
实现一个onMessage方法,该方法只接收一个Message参数
实例代码(生产者)
创建一个生产者接口:ProducerService.java
public interface ProducerService {
void sendMessage(String message);
}
具体实现类:ProducerServiceImpl.java
public class ProducerServiceImpl implements ProducerService {
@Autowired
private JmsTemplate jmsTemplate;
@Resource(name = "topicDestination")//资源方式注入,name作区分。因为可能会存在多个destination
private Destination destination;
@Override
public void sendMessage(final String message) {
//使用jmsTemplate发送消息
jmsTemplate.send(destination, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage=session.createTextMessage(message);
return textMessage;
}
});
System.out.println("发送消息:"+message);
}
}
启动类:AppProducer.java
public class AppProducer {
public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("producer.xml");
ProducerService producerService=(ProducerService) context.getBean(ProducerService.class);
for(int i=0;i<50;i++){
producerService.sendMessage("fut"+i);
}
context.close();
}
}
实例代码(消费者)
创建一个监听器类:ConsumerMessageListener.java
public class ConsumerMessageListener implements MessageListener{
@Override
public void onMessage(Message message) {
TextMessage textMessage=(TextMessage)message;
try {
System.out.println("接收消息:"+textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
启动类:AppConsumer.java
public class AppConsumer {
public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("consumer.xml");
}
}
资源文件配置
spring资源文件配置(生产者和消费者公共资源):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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<!--ActiveMQ为我们提供的ConnectionFactory-->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.63.137: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>
<!--主题模式目的地-->
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="topic"/>
</bean>
</beans>
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<import resource="common.xml"/>
<!--配置JmsTemplate,用于发送消息-->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
</bean>
<bean class="com.ft.jms.activemq.producer.ProducerServiceImpl"/>
</beans>
spring资源文件配置(消费者):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-3.0.xsd">
<!--导入公共配置-->
<import resource="common.xml"/>
<!--配置消息监听器-->
<bean id="consumerMessageListener" class="com.ft.jms.activemq.consumer.ConsumerMessageListener"/>
<!--配置消息监听容器-->
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="destination" ref="topicDestination"/>
<property name="messageListener" ref="consumerMessageListener"/>
</bean>
</beans>
总结
以上代码是主题模式的消息发布/订阅,如果要切换到队列模式,只需将topic那块改为queue即可。