标签:
概要:
package com.ll.compositeDestination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class JmsMessageListener implements MessageListener {
public void onMessage(Message message) {
System.out.println("消息全部内容:" + message.toString());
try {
System.out.println("消息主题:" + message.getJMSDestination().toString());
} catch (JMSException e1) {
e1.printStackTrace();
}
TextMessage tm = (TextMessage) message;
try {
System.out.println("消息体:" + tm.getText());
} catch (JMSException e) {
e.printStackTrace();
}
System.out.println("------------------------------------");
}
}
package com.ll.compositeDestination;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.Session;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
public class Sender {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"applicationContext-compositeDestination.xml");
JmsTemplate template = (JmsTemplate) applicationContext
.getBean("jmsTemplate");
Destination destination =(Destination) applicationContext
.getBean("destinationProducer");
// Queue queue = new ActiveMQQueue("FOO.A,FOO.B,FOO.C");
//发送消息
template.send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session
.createTextMessage("同时向三个Queue中发送相同的消息");
}
});
System.out.println("同时向三个Queue中发送相同的消息-发送完成...");
}
}
<?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-2.0.xsd">
<!--创建连接工厂 -->
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616"></property>
</bean>
<!-- 通配符 供消费者使用 -->
<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="FOO.*"></constructor-arg>
</bean>
<!-- composite destination 供生产者使用 -->
<bean id="destinationProducer" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="FOO.A,FOO.B,FOO.C,FOO.D"></constructor-arg>
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="defaultDestination" ref="destinationProducer"></property>
<property name="receiveTimeout" value="600"></property>
</bean>
<!-- 消息监听接口 -->
<bean id="jmsMessageListener" class="com.ll.compositeDestination.JmsMessageListener">
</bean>
<!-- 消费者,通过消息侦听器实现 -->
<bean id="consumer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="destination" />
<property name="messageListener" ref="jmsMessageListener" />
</bean>
</beans>
package com.ll.compositeDestination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class JmsMessageListener implements MessageListener {
public void onMessage(Message message) {
System.out.println("消息全部内容:" + message.toString());
try {
System.out.println("消息主题:" + message.getJMSDestination().toString());
} catch (JMSException e1) {
e1.printStackTrace();
}
TextMessage tm = (TextMessage) message;
try {
System.out.println("消息体:" + tm.getText());
} catch (JMSException e) {
e.printStackTrace();
}
System.out.println("------------------------------------");
}
}
package com.ll.compositeDestination;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.Session;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
public class Sender {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"applicationContext-compositeDestination.xml");
JmsTemplate template = (JmsTemplate) applicationContext
.getBean("jmsTemplate");
Destination destination = (Destination) applicationContext
.getBean("destinationProducer");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 发送消息
template.send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("同时向多个Queue、Topic中发送相同的消息");
}
});
}
}
<?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-2.0.xsd">
<!--创建连接工厂 -->
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616"></property>
</bean>
<!-- 通配符FOO.* 供消费者使用 -->
<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="FOO.*"></constructor-arg>
</bean>
<!-- 通配符NOTIFY.FOO.* 供消费者使用 -->
<bean id="destination2" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg index="0" value="NOTIFY.FOO.*"></constructor-arg>
</bean>
<!-- composite destination 供生产者使用 ,多个Queue和多个Topic -->
<bean id="destinationProducer" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0"
value="FOO.1,FOO.2,FOO.3,FOO.4,topic://NOTIFY.FOO.D,topic://NOTIFY.FOO.E,topic://NOTIFY.FOO.F"></constructor-arg>
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="defaultDestination" ref="destinationProducer"></property>
<property name="receiveTimeout" value="600"></property>
</bean>
<!-- 消息监听接口 -->
<bean id="jmsMessageListener" class="com.ll.compositeDestination.JmsMessageListener">
</bean>
<!-- 消息侦听器容器,监听destination -->
<bean id="consumer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="destination" />
<property name="messageListener" ref="jmsMessageListener" />
</bean>
<!-- 消息侦听器容器,监听destination2 -->
<bean id="consumer2"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="destination2" />
<property name="messageListener" ref="jmsMessageListener" />
</bean>
</beans>
【ActiveMQ入门-11】ActiveMQ学习-compositeDestination
标签:
原文地址:http://www.cnblogs.com/ssslinppp/p/4469337.html