spring 整合 RabbitMQ 3.9.11

spring 整合 RabbitMQ 3.9.11,第1张

spring 整合 RabbitMQ 3.9.11 spring 整合 RabbitMQ 3.9.11

rabbitmq 3.9.11

创建工程

不管用什么办法创建出一个空的工程出来,或者在已有工程里面创建一个新的module,下面只说创建新module的方法。

  在菜单里选择Project Structure

先点加号,再点New Module

然后直接下一步

然后把Module名字,所在位置都填好之后直接finish

在idea中新建Module都是这么 *** 作。

maven配置

直接把下面的配置信息根据需要粘贴到自己的pom.xml里面。



    4.0.0

    com.itheima
    spring-rabbitmq
    1.0-SNAPSHOT


    
        
        
            org.springframework
            spring-context
            5.2.10.RELEASE
        
        
        
            org.springframework.amqp
            spring-rabbit
            2.1.8.RELEASE
        

        
        
            junit
            junit
            4.12
        

        
            org.springframework
            spring-test
            5.2.10.RELEASE
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.8.0
                
                    1.8
                    1.8
                
            
        
    


提醒一下:pom.xml的文件在下面这个位置

resource 配置文件

以下三个文件直接复制下面的代码就可以,把rabbitmq配置改成自己的就好了,bean什么的写成自己的

rabbitmq.properties

rabbitmq.host=127.5.19.118  (服务器ip)
rabbitmq.port=5672  (rabbitmq端口)
rabbitmq.username=admin   (rabbitmq用户名)
rabbitmq.password=11111  (rabbitmq密码)
rabbitmq.virtual-host=/itcast  (rabbitmq虚拟机)

spring-rabbitmq-consumer.xml



    
    

    
    

    






    
        





    

spring-rabbitmq-producer.xml



    
    

    
    
    
    

    
    

    
    
    

    
    

    
    
        
            
            
        
    

    
    
    
    
    
    
    

    
        
             
             
            
        
    

    
    

Producer

为了能测试执行代码,就写在测试类里面了。

rabbitTemplate.convertAndSend("spring_fanout_exchange","","fanout消息已经发送了"); 当中的 spring_fanout_exchange 是绑定的上面 spring-rabbitmq-producer.xml 当中的对应的bean (),有关exchange绑定 queue的配置都在spring-rabbitmq-producer.xml当中。

package com.itheima.springRabbitmq;

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 ProducerTest {

    // 注入rabbitmqTemple
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void testProducer() throws InterruptedException {
        Integer i = 0;
        while (true) {
            Thread.sleep(5 * 1000);

            i++;
            rabbitTemplate.convertAndSend("spring_queue","消息发送了"+i);
        }

    }

    @Test
    public void testFanout() {
        rabbitTemplate.convertAndSend("spring_fanout_exchange","","fanout消息已经发送了");
    }
    @Test
    public void testTopic() {
        rabbitTemplate.convertAndSend("spring_topic_exchange","heima.heeh.haha","topic消息已经发送了");
    }
}
Consumer

这个代码是跑起来自动监听的 ,实现一下MessageListener这个类,重写onMessage方法,message.getBody()就是消息内容

package com.itheima.springRabbitmq.consumer;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;


public class SpringQueueListener implements MessageListener {
    @Override
    public void onMessage(Message message) {
        System.out.println("收到的消息:"+new String(message.getBody()));
//        System.out.println(message.toString());
    }
}

测试代码

package com.itheima.springRabbitmq.consumer;

import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
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-consumer.xml")
public class SpringQueueListenerTest{

    @Test
    public void testOnMessage() throws InterruptedException {
        boolean flag = true;
        while (true) {
            Thread.sleep(5 * 1000);
            System.out.println("正在监听");
        }

    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5706506.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存