Spring 整合RabbitMQ,笔记整理

1.创建生产者工程  

spring-rabbitmq-producer

2.pom.xml添加依赖


    
        org.springframework
        spring-context
        5.1.7.RELEASE
    
    
        org.springframework.amqp
        spring-rabbit
        2.1.8.RELEASE
    
    
        junitjunit 4.12
    
    
        org.springframework
        spring-test
        5.1.7.RELEASE
    

3.创建rabbitmq.properties

rabbitmq.host=192.168.64.139
rabbitmq.port=5672
rabbitmq.username=javacto
rabbitmq.password=javacto
rabbitmq.virtual-host=/javacto

4.创建spring-rabbitmq-producer.xml 整合配置文件



    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
        
            
            
        
    
    
    
    
    
    
    
    
    
    
        
            
            
            
        
    
    
    

5.创建测试文件发送消息\src\test\java\com\javacto\rabbitmq\ProducerTest.java

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations ="classpath:spring-rabbitmq-producer.xml")
public class ProductTest {
    //1.注入RabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;
    //2.发送消息
    @Test
    public  void testHelloWorld(){
        rabbitTemplate.convertAndSend("spring_queue","hello world spring");
    }
    //2.发送消息
    @Test
    public  void testFanout(){
        rabbitTemplate.convertAndSend("spring_fanout_exchange","","spring fanout...");
    }
    //2.发送消息
    @Test
    public  void testTopics(){
        rabbitTemplate.convertAndSend("spring_topic_exchange","javacto.add","spring topic...");
    }
}

1.创建消费者工程  

spring-rabbitmq-consumer

2.pom.xml添加依赖


    
        org.springframework
        spring-context
        5.1.7.RELEASE
    
    
        org.springframework.amqp
        spring-rabbit
        2.1.8.RELEASE
    
    
        junitjunit 4.12
    
    
        org.springframework
        spring-test
        5.1.7.RELEASE
    

3.创建rabbitmq.properties

rabbitmq.host=192.168.64.139
rabbitmq.port=5672
rabbitmq.username=javacto
rabbitmq.password=javacto
rabbitmq.virtual-host=/javacto

4.创建spring-rabbitmq-producer.xml 整合配置文件



    
    
    
    
    
    
    
        
        
    

 5.队列监听器  创建 com.javacto.rabbitmq.listener.SpringQueueListener

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
public class SpringQueueListener  implements MessageListener {
    public void onMessage(Message message) {
        //打印消息
        System.out.println(new String(message.getBody()));
    }
}

6.创建ConsumerTest 文件

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-rabbitmp-consumer.xml")
public class ConsumerTest {
    @Test
    public  void test01(){
        while (true){

        }
    }
}
  1. 广播监听器spring配置文件代码   
    FanoutListener1 类同  SpringQueueListener类代码一模一样


    
    
    
    
    
    
    
    
    
        
        
        
        
    

你可能感兴趣的:(RabbitMQ,java-rabbitmq,spring,rabbitmq)