栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot使用消息中间件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot使用消息中间件

前言

使用SpringBoot集成rabbitmq实现一个发送和接收

内容

1.引入依赖


 org.springframework.boot
 spring-boot-starter-amqp

2.application.properties

#rabbitmq配置
spring.application.name=springboot-mq
spring.rabbitmq.host=192.168.17.129
spring.rabbitmq.port=5672
spring.rabbitmq.username=mytest
spring.rabbitmq.password=mytest

3.rabbitmap配置类

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 mqQueue(){
  return new Queue("mqboot");
 }
}

4.发送类< 大专栏 zyzx(53)-springboot使用消息中间件/h5>

@Component
public class Sender {

 @Autowired
 private AmqpTemplate rabbitTemplate;

 public void send(){
  String content = "send: hello"+new Date();
  System.out.println("Sender:"+content)
  this.rabbitTemplate.convertAndSend("mqboot",content);
 }
}

收类

@Component
@RabbitListener(queues = "mqboot")
public class Receiver {

 @RabbitHandler
 public void process(String data){
  System.out.println("Receiver:"+data);
 }
}

6.测试

启动springBoot

如下显示表明:连接成功:

@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
 @Autowired
 private TeacherRepository teacherRepository;

 

 @Autowired
 private Sender sender;

 @Test
 public void contextLoads() {
  //mq测试
  sender.send();
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/136637.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号