spring-web 集成 rabbitmq

 

引入java包:

本项目中仅引入了四个java包:amqp-client-5.7.3.jar,spring-rabbit-2.2.2.RELEASE.jar,spring-retry-1.2.4.RELEASE.jar,spring-amqp-2.2.2.RELEASE.jar

 

spring-rabbitmq.xml



       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">

    
    
                               username="guest" password="guest" host="localhost" port="5672" />

    
    
                     exchange="exchangeTest" />

    
    

    
    

    
    
        
            
        
    

    
    class="club.codeapes.web.core.rabbitmq.RabbitMqMessageConsumer">

    
    
        
    



 spring中需要引入这个xml,  主要在总spring.xml。或者web.xml中需要引入下。

 

RabbitMqMessageConsumer

package club.codeapes.web.core.rabbitmq;

import club.codeapes.common.date.DateUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

public class RabbitMqMessageConsumer implements MessageListener {

    @Override
    public void onMessage(Message message) {
        System.out.println("消费信息," + DateUtil.getNow("yyyy-MM-dd HH:mm:ss") + "---->" + message);
    }

}

 

RabbitMqMessageProducer:

package club.codeapes.web.core.rabbitmq;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


@Repository
public class RabbitMqMessageProducer{

    @Autowired
    private AmqpTemplate amqpTemplate;

    public void sendMessage(Object message) {
        System.out.println("to send message:" + message);
        amqpTemplate.convertAndSend("queueTestKey", message);
    }

}

 

 



 

 

 

 

你可能感兴趣的:(spring-web 集成 rabbitmq)