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

目录


前提摘要:

首先要装好rabbitmq.这里的步骤可以参考外面的文章
Rabbitmq安装

安装效果:localhost:15672有反应

加一个虚拟主机:


用guest账号添加的,那么guest账号自然就拥有zyz虚拟主机。

加一个队列

:zyz

(这里可以查看当前待处理的消息)
添加的Name是队列名字

依赖与配置文件:

依赖

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

配置文件:

java 复制代码
#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

java 复制代码
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

java 复制代码
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

java 复制代码
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");
       }
   }
}

调用测试代码:

java 复制代码
@Resource
private Producer producer;

//异步字符串测试
@GetMapping("/produceTest")
public void produceTest(String msg) throws IOException {
   producer.send(msg);
   System.out.println("消息已经成功发送");
   //进行其他业务处理
   
}
相关推荐
Lansonli1 小时前
大数据Spark(八十八):Spark任务资源之Standalone集群测试
java·服务器·spark
budingxiaomoli2 小时前
Spring AOP
java·spring·java-ee
特立独行的猫A2 小时前
Ionic + 仓颉语言的 cj-tauri 开发框架实战:开发精美移动端 APP
java·后端·python
nice_lcj5202 小时前
LRU 缓存入门:它是什么、怎么用、面试怎么手写
java·数据结构·缓存·面试·lru
文丑颜不良啊3 小时前
LeetCode2741~2760题目解析
java·leetcode
金銀銅鐵12 小时前
带您看懂字节码指令(二):空循环里有什么
java·后端·ai编程
吴名氏.14 小时前
Java转AI Agent教程(第十三周)混合检索与重排
java·数据库·人工智能·ai·ai agent
.痞老板.15 小时前
java ee--多线程(进阶)
java·java-ee
深入云栈16 小时前
Netty 4.2.x 源码深度解析 (二十三):ByteBuf 核心设计 —— 索引、类型与零拷贝
java·后端·架构
wang_shu_mo_ran17 小时前
MyBatis 从入门到进阶(一)
java·spring·mybatis