【Rabbitmq】JAVA的Rabbitmq初步应用随记(安装完成后)

发布于:2025-02-10 ⋅ 阅读:(111) ⋅ 点赞:(0)


前提摘要:

首先要装好rabbitmq.这里的步骤可以参考外面的文章
Rabbitmq安装
安装效果:localhost:15672有反应
在这里插入图片描述

加一个虚拟主机:

在这里插入图片描述
在这里插入图片描述
用guest账号添加的,那么guest账号自然就拥有zyz虚拟主机。

加一个队列

:zyz
(这里可以查看当前待处理的消息)
在这里插入图片描述添加的Name是队列名字
在这里插入图片描述
依赖与配置文件:
依赖

<!-- mq依赖 -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

配置文件:

#RabbitMQ
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=15672
spring.rabbitmq.virtual-host=zyz
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
#设置此属性配置可以确保消息成功发送到交换器
spring.rabbitmq.publisher-confirms=true
#可以确保消息在未被队列接收时返回
spring.rabbitmq.publisher-returns=true
#手动签收
spring.rabbitmq.listener.simple.acknowledge-mode=manual
spring.rabbitmq.listener.direct.acknowledge-mode=manual

配置机与交换机:

RabbitMqConfig

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;



@Configuration
public class RabbitMqConfig {

   @Bean
   public Queue queue(){
       return new Queue("bootqueue");
   }

   @Bean
   public DirectExchange directExchange(){
       return new DirectExchange("DirectExchange");
   }

   @Bean
   public Binding bindExchange(){
       return BindingBuilder.bind(queue()).to(directExchange()).with("bootqueue.key");
   }


}

发送者:

Producer

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class Producer implements RabbitTemplate.ReturnCallback{
   @Autowired
   RabbitTemplate rabbitTemplate;

   // 队列:boot队列   发送字符串
   public void send(String msg){
       //回调
       rabbitTemplate.setReturnCallback(this);
       rabbitTemplate.convertAndSend("DirectExchange","bootqueue.key",msg);
   }
}

接收者

Consumer

import com.rabbitmq.client.Channel;


import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;

import org.springframework.stereotype.Component;


@Component
public class Consumer {

   // 队列:boot队列   接受字符串
   @RabbitListener(queues = "bootqueue")
   public void process(String msg, Channel channel, Message message){
       System.out.println("AckReciever : 收到消息"+msg);
       try{
           //Thread.sleep(5000);
           channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
           System.out.println("消息接收成功:TRUE");
       }catch (Exception e){
           System.out.println("消息接收失败:FlASE");
       }
   }
}

调用测试代码:

@Resource
private Producer producer;

//异步字符串测试
@GetMapping("/produceTest")
public void produceTest(String msg) throws IOException {
   producer.send(msg);
   System.out.println("消息已经成功发送");
   //进行其他业务处理
   
}