MQ接收和发送

//JMS:P2P模型
public void testQueueProducer() throws Exception {
	//1、创建一个连接工厂,需要指定服务的ip及端口。
	ConnectionFactory ConnectionFactory = new ActiveMQConnectionFacroty("tcp://192.168.25.155:61616");
	//2、使用工厂对象创建一个Connection对象
	Connection connection = ConnectionFactory.createConnection();
	//3、开启连接,调用Connection对象的start方法。
	connection.start();
	//4、创建一个Session对象。
	//第一个参数:是否开启事务。如果true开启事务,第二个参数无意义。一般不开启事务false。
	Session session = connection.createSession(false,Session AUTO_ACKNOWLEDGE);
    //5、使用Session对象创建一个Destination对象.两种形式queue、topic,现在应该使用queue
    Queue queue = session.createQueue("test-queue");
    //6、使用session创建一个Producer对象。
    Messageproducer produce = session.createProducer(queue);
    //7、创建一个Message对象,可以使用TextMessage。
    for(int i = 0; i < 50; i++){
    	TextMessage textMessage = session.createTextMessage("第"+i+"个ActiveMQ对列目的地的消息");
        //8、发送消息
        producer.send(textMessage);
    }
    //9、关闭资源
    producer.close();
    session.close();
    connection.close();

}

public void testQueueConsumer() throws Exception {
	//创建一个ConnectionFactory对象连接MQ服务器
	ConnectionFactory connetionFactory = new ActiveMQConnectionFacroty("tcp://192.168.25.155:61616");
	//创建一个连接
	Connection connection = connetionFactory.createConnection();
	//开启连接
	connection.start();
	//使用Connection连接创建一个Session对象
	Session session = connection.createSession(false,Session AUTO_ACKNOWLEDGE);
	//创建一个Destination对象。queue对象
	Queue queue = session.createQueue("test-queue");
    //创建一个消费者对象
    MessageConsumer consumer = session.createConsumer(queue);
    //接收消息
    consume.setMessageListener(new MessageListener(){
    	@Override
    	public void onMessage(Message message){
    		//打印结果
    		TextMessage textMessage = (TextMessage)message;
    		String text;
    		try{
    			text = textMessage.getText();
    			System.out.println("这是接收到的消息"+text);
    		}catch(JMSException e){
    			e.printStackTrace();
    		}
    	}
    });
    //等待接收消息
    System.in.read();
    consumer.close();
    session.close();
    connection.close();
}
//JMS:Pub/Sub模型

public void testTopicProducer() throws Exception{
	//创建一个连接工厂对象,需要指定服务ip及端口。
	ConnectionFactory connetionFactory = new ActiveMQConnectionFacroty("tcp://192.168.25.155:61616");
	//使用工厂对象创建一个Connection对象
	Connection connection = connetionFactory.createConnection();
	//开启连接
	connection.start();
	//创建一个Session对象
	//第一个参数;是否开启事务,如果true开启事务,第二个参数无意义,一般不开启事务false
	//第二个参数:应答模式。自动应答或手动应答。一般自动应答
	Session session = connection.createSession(false,Session AUTO_ACKNOWLEDGE);
	//使用Session对象创建一个Destination对象.两种形式queue、topic
	Topic topic = session.createTopic("test-topic");
	//创建一个Message对象,可以使用TextMessage.
	for(int i= 0; i<50;i++){
		TextMessage testMessage = session.createTextMessage("第"+i+"个ActiveMQ对列目的地的消息");
        //8、发送消息
		producer.send(textMessage);
	}
	//9、关闭资源
	producer.close();
	session.close();
	connection.close();
}
@Test
public void testTopicConsumer() throws Exception {
	//创建一个ConnectionFactory对象连接MQ服务器
	ConnectionFactory connetionFactory = new ActiveMQConnectionFacroty("tcp://192.168.25.155:61616");
	//创建一个Connection对象
	Connection connection = new connetionFactory.createConnection();
	//开启连接
	connection.start();
	//使用Connection创建一个Session对象
	Session session = connection.createSession(false,AUTO_ACKNOWLEDGE);
	//创建一个Desination对象,topic对象
	Topic topic = session.createTopic("test-topic");
	//使用Session创建一个消费者对象
	MessageConsumer consumer = session.createConsumer(topic);
	//接收消息
	consumer.setMessageListener(new MessageListener(){
		@Override
		public void onMessage(Message message){
			TextMessage textMessage = (TextMessage)message;
			String text;
			try{
				text = textMessage.getText();
				System.out.println("这是接收到的消息:"+text);
			}catch(JMSException e){
				e.printStackTrace();
			}
		}
	});
    System.out.println("topic消费者启动.........");
    System.in.read();
    //关闭资源
    consumer.close();
    session.close();
    connection.close();
}





@Test
    
public void testSpringActiveMq() throws Exception{
        
//初始化spring容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");
        
//从spring容器中获得JmsTemplate对象
        JmsTemplate jmsTemplate = applicationContext.getBean(JmsTemplate.class);
        
//从spring容器中取Destination对象
        Destination destination =(Destination)applicationContext.getBean("queueDestination");
        
//使用JmsTemplate对象发送消息。
        jmsTemplate.send(destination,new MessageCreator(){
            @Override
            public Messagem createMessage(Session session)throws JMSException{
                
//创建一个消息对象并返回
                TextMessage textMessage =session.createTextMessage("spring activemq queue message");
                return textMessage;
            }
        });
    }



 public class MyMessageListener implement MessagerListener{
 	@Override
 	public void onMessage(Message message){
 		try{
 			TextMessage textMessage=(TextMessage)message;
 			//获取消息内容
 			String text = textMessage.getText();
 			System.out.println(text);
 		}catch(JMSException e){
 			e.printStackTrace();
 		}
 	}
 }

@Test
public void testQueueConsumer()throws Exception{
	//初始化spring容器
	ApplicationContext applicationContext= new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");
	System.in.read();
}


<?xml version="1.0" encoding="UTF-8"?>
<beans
 
xmlns
=
"http://www.springframework.org/schema/beans"
    
xmlns:context
=
"http://www.springframework.org/schema/context"
 
xmlns:p
=
"http://www.springframework.org/schema/p"
    
xmlns:aop
=
"http://www.springframework.org/schema/aop"
 
xmlns:tx
=
"http://www.springframework.org/schema/tx"
    
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-4.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"
>


<!-- 真正可以产生Connection的ConnectionFactory,由对应的JMS服务厂商提供 -->

<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
	<property name="brokerURL" value="tcp://192.168.25.155:61616"/>
</bean>
<!-- Spring 用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
	<!-- 目标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"/>
</bean>
    
<!--这个是队列目的地,点对点的 -->
    
<bean  id="queueDestination"  class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg>
            <value>spring-queue</value>
        </constructor-arg>
</bean>
    
<!--这个是主题目的地,一对多的 -->
    
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
        <constructor-arg value="topic"/>
</bean>
</beans>



<?
xml version
=
"1.0"
 encoding
=
"UTF-8"
?>
<beans
 
xmlns
=
"http://www.springframework.org/schema/beans"
    
xmlns:context
=
"http://www.springframework.org/schema/context"
 
xmlns:p
=
"http://www.springframework.org/schema/p"
    
xmlns:aop
=
"http://www.springframework.org/schema/aop"
 
xmlns:tx
=
"http://www.springframework.org/schema/tx"
    
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-4.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"
>
    
<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
    
<bean
 
id
=
"targetConnectionFactory"
 
class
=
"org.apache.activemq.ActiveMQConnectionFactory"
>
        
<property
 
name
=
"brokerURL"
 
value
=
"tcp://192.168.25.168:61616"
 
/>
    
</bean>
    
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
    
<bean
 
id
=
"connectionFactory"
        
class
=
"org.springframework.jms.connection.SingleConnectionFactory"
>
        
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
        
<property
 
name
=
"targetConnectionFactory"
 
ref
=
"targetConnectionFactory"
 
/>
    
</bean>
    
<!--这个是队列目的地,点对点的 -->
    
<bean
 
id
=
"queueDestination"
 
class
=
"org.apache.activemq.command.ActiveMQQueue"
>
        
<constructor-arg>
            
<value>
spring-queue
</value>
        
</constructor-arg>
    
</bean>
    
<!--这个是主题目的地,一对多的 -->
    
<bean
 
id
=
"topicDestination"
 
class
=
"org.apache.activemq.command.ActiveMQTopic"
>
        
<constructor-arg
 
value
=
"topic"
 
/>
    
</bean>
    
<!-- 接收消息 -->
    
<!-- 配置监听器 -->
    
<bean
 
id
=
"myMessageListener"
 
class
=
"cn.e3mall.search.listener.MyMessageListener"
 
/>
    
<!-- 消息监听容器 -->
    
<bean
 
class
=
"org.springframework.jms.listener.DefaultMessageListenerContainer"
>
        
<property
 
name
=
"connectionFactory"
 
ref
=
"connectionFactory"
 
/>
        
<property
 
name
=
"destination"
 
ref
=
"queueDestination"
 
/>
        
<property
 
name
=
"messageListener"
 
ref
=
"myMessageListener"
 
/>
    
</bean>
</beans>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值