spring整合ActiveMQ
配置ConnectionFactory
<!--JMS厂商提供的ConnectionFactory -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<constructor-arg name="brokerURL" value="tcp://ip:61616"></constructor-arg>
</bean>
<!--Spring对ConnectionFactory的封装 -->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory" ref="targetConnectionFactory"></property>
</bean>
配置生产者
<!-- 配置JMS template -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"></property>
</bean>
<!-- 配置destination对象 -->
<bean id="test-queue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg name="name" value="test-queue"></constructor-arg>
</bean>
<bean id="test-topic" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg name="name" value="test-topic"></constructor-arg>
</bean>
发送消息,写测试类
@Test
public void testJMSTemlate() {
// 初始化spring容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"classpath:spring/applicationContext-activemq.xml");
// 从容器中获得JMS对象
JmsTemplate jmsTemplate = applicationContext.getBean(JmsTemplate.class);
// 从容器中获取Destination对象
Destination destination = (Destination) applicationContext.getBean("test-queue");
// 发送消息
jmsTemplate.send(destination, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage("spring activeMQ");
return textMessage;
}
});
}
2.配置消费者
一个MessageListenerContainer的时候需要指定三个属性:
1、一个是表示从哪里监听的ConnectionFactory
2、一个是表示监听什么的Destination;
3、一个是接收到消息以后进行消息处理的MessageListener。
常用的MessageListenerContainer实现类是DefaultMessageListenerContainer。
MessageListener.java
public class MyMessageListener implements MessageListener {
@Override
public void onMessage(Message message) {
// 接收到消息
try {
TextMessage textMessage = (TextMessage) message;
String text = textMessage.getText();
// 打印消息
System.out.println(text);
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
spring配置文件
<!--JMS厂商提供的ConnectionFactory -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<constructor-arg name="brokerURL" value="tcp://ip:61616"></constructor-arg>
</bean>
<!--Spring对ConnectionFactory的封装 -->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory" ref="targetConnectionFactory"></property>
</bean>
<!-- 配置destination对象 -->
<bean id="test-queue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg name="name" value="test-queue"></constructor-arg>
</bean>
<bean id="test-topic" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg name="name" value="test-topic"></constructor-arg>
</bean>
<!-- MessageListener对象 -->
<bean id="myMessageListener" class="com.taotao.listener.MyMessageListener"></bean>
<!-- 配置接收消息者 -->
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="test-queue"/>
<property name="messageListener" ref="myMessageListener" />
</bean>
接收消息,编写测试类
@Test
public void testSpringActiveMq() throws IOException {
// 初始化spring容器
ApplicationContext context = new ClassPathXmlApplicationContext(
"classpath:spring/applicationContext-activemq.xml");
// 等待
System.in.read();
}