RabbitMQ学习之spring配置文件rabbit标签的使用

下面我们通过一个实例看一下rabbit的使用。

1.实现一个消息监听器ReceiveMessageListener.java

package org.springframework.amqp.core;

/**
 * Listener interface to receive asynchronous delivery of Amqp Messages.
 *
 * @author Mark Pollack
 */
public interface MessageListener {

	void onMessage(Message message);

}

2.消费者配置Consumer.xml



 
   
    
         
   
   
   
   
    
    
   
    
        
            
        
    
    
    
     
    
    
        
    
3.生产者配置Producer.xml




	
	

	

	
	


	
	
		
			
		
	

	
	

	
	
4.消费者启动类ConsumerMain.java

package cn.slimsmart.rabbitmq.demo.spring.tag;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ConsumerMain {

	public static void main(String[] args) {
		new ClassPathXmlApplicationContext("Consumer.xml");  
	}
}
5.生产者启动类ProducerMain.java

package cn.slimsmart.rabbitmq.demo.spring.tag;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ProducerMain {
	
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("Producer.xml");  
        AmqpTemplate amqpTemplate = context.getBean(RabbitTemplate.class);   
        User user = new User();
        user.setName("niuniu");
        amqpTemplate.convertAndSend(user);
	}
}

先启动消费者,监听接收消息,再启动生产者发送消息。

输出: data :{"name":"niuniu"}

如下4中转发器类型标签

rabbit:fanout-exchange

rabbit:direct-exchange

rabbit:topic-exchange

rabbit:headers-exchange


参考:http://blog.csdn.net/michaelzhaozero/article/details/23741511

你可能感兴趣的:(RabbitMQ,RabbitMQ学习教程)