RabbitMQ @RabbitListener注解和使用



	
	


	
	

	
	

	
	
		
		
	
    
    
	
    
    
    
    
	
	    
	    
	    
	    
	



消费者
//java代码 不需要和配置文件有任何关联
@Component //必须要加,不然注解@RabbitListener 无效
public class Mqlisten {  

    @RabbitListener(queues = "1688gzs.email1")
    public void email1(Message message) throws UnsupportedEncodingException{
        System.out.println(new String(message.getBody(),"utf-8"));
    }
    
    @RabbitListener(queues = "1688gzs.email2")
    public void email2(Message message) throws UnsupportedEncodingException{
        System.out.println(new String(message.getBody(),"utf-8"));
    }
    
    
}





@RequestMapping(value = "phpmessage")
@Controller
public class PhpmessageController {
	//利用spring整合的rabbitmq工具
	@Resource(name = "template")
	private AmqpTemplate template;

@RequestMapping(value = "demo")
	public String demo() throws IOException {
 						//参数:交换机
    //参数:通配符
    //参数:内容 
        template.convertAndSend("1688gzs.email", "1688gzs.test", "你好");
		return messageStr;
	}

}

你可能感兴趣的:(RabbitMQ)