RabbitMQ和Spring的整合

spring-RabbitMQ

MAVEN


    4.0.0

    com.ghghcn
    rmq
    1.0-SNAPSHOT
    jar

    rmq
    http://maven.apache.org

    
        UTF-8
        4.3.12.RELEASE
        1.7.4.RELEASE
        1.7.25
        1.2.3
        1.8.11
        1.1.0.Final
        5.4.1.Final
        1.8
    
    
        
            org.springframework
            spring-core
            ${spring.version}
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            org.springframework
            spring-context-support
            ${spring.version}
        
        
            org.springframework
            spring-aop
            ${spring.version}
        
        
            org.springframework
            spring-aspects
            ${spring.version}
        
        
            org.springframework
            spring-tx
            ${spring.version}
        
        
            org.springframework
            spring-jdbc
            ${spring.version}
        
        
            org.springframework
            spring-web
            ${spring.version}
        
        
        
            org.springframework.amqp
            spring-rabbit
            1.7.4.RELEASE
        
        
        
            org.slf4j
            slf4j-api
            ${slf4j.version}
        
        
        
            org.slf4j
            jcl-over-slf4j
            ${slf4j.version}
        
        
        
            org.slf4j
            log4j-over-slf4j
            ${slf4j.version}
        
        
        
            org.slf4j
            jul-to-slf4j
            ${slf4j.version}
        
        
        
            ch.qos.logback
            logback-classic
            ${logback.verison}
        
        
            ch.qos.logback
            logback-access
            ${logback.verison}
        
        
            junit
            junit
            4.12
            test
        
    



spring.xml




    


    
  
    

    

    
    
    
    


service 发送者

package com.ghghcn.rmq.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;

@Service
public class MessageProducer {
    private Logger logger = LoggerFactory.getLogger(MessageProducer.class);

    @Resource(name="amqpTemplate")
    private AmqpTemplate amqpTemplate;

    @Resource(name="amqpTemplate2")
    private AmqpTemplate amqpTemplate2;

    public void sendMessage(Object message) throws IOException {
        logger.info("to send message:{}", message);
        amqpTemplate.convertAndSend("queueTestKey", message);
        amqpTemplate.convertAndSend("queueTestChris", message);
        amqpTemplate2.convertAndSend("shijj.xxxx.wsdwd", message);
    }
}

接收者

package com.ghghcn.rmq.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

public class ChrisConsumer implements MessageListener {
    private Logger logger = LoggerFactory.getLogger(ChrisConsumer.class);


    public void onMessage(Message message) {
        logger.info("chris receive message------->:{}", message);
    }

}

package com.ghghcn.rmq.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

public class MessageConsumer implements MessageListener {
    private Logger logger = LoggerFactory.getLogger(MessageConsumer.class);


    public void onMessage(Message message) {
        logger.error("consumer receive message------->:{}", message);

    }

}

package com.ghghcn.rmq.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

public class ShijjConsumer implements MessageListener {
    private Logger logger = LoggerFactory.getLogger(ShijjConsumer.class);

    public void onMessage(Message message) {
        logger.info("shijj receive message------->:{}", message);
    }
}

spring-RabbitMQ.xml



    
    


    
    

    
    

    
    
        
            
        
    

    
    

    
    

    
    
        
    

    
    

    
    
        
            
        
    

    
    

    
    
        
    

    
    
    

    
    

    
    

    
    
        
            
        
    

    
    

    
    

    
    
        
    

测试

package com.ghghcn;

import com.ghghcn.rmq.service.MessageProducer;

import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestClass {
    private Logger logger = LoggerFactory.getLogger(TestClass.class);

    private ApplicationContext context = null;

    @Before
    public void setUp() throws Exception {
        context = new ClassPathXmlApplicationContext("spring/spring.xml");
    }


    @Test
    public void should_send_a_amq_message() throws Exception {
        MessageProducer messageProducer = (MessageProducer) context.getBean("messageProducer");

        int a = 100;
        while (a > 0) {
            //amqpTemplate.convertAndSend("Hello, I am amq sender num :" + a--);
            messageProducer.sendMessage("Hello, I am amq sender num :" + a--);

            try {
                //暂停一下,好让消息消费者去取消息打印出来
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}

system.properties

rabbit.address=127.0.0.1
rabbit.port=5672
rabbit.username=admin
rabbit.password=admin

你可能感兴趣的:(RabbitMQ和Spring的整合)