springboot使用消息中間件
前言
使用SpringBoot集成rabbitmq實現(xiàn)一個發(fā)送和接收
內(nèi)容
1.引入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>
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.發(fā)送類< 大專欄 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 JavaMailSender javaMailSender;*/
@Autowired
private Sender sender;
@Test
public void contextLoads() {
//mq測試
sender.send();
}
}


以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot微信消息接口配置詳解
- Spring boot項目redisTemplate實現(xiàn)輕量級消息隊列的方法
- Spring Boot RabbitMQ 延遲消息實現(xiàn)完整版示例
- 詳解Spring Boot 定制HTTP消息轉(zhuǎn)換器
- SpringBoot利用redis集成消息隊列的方法
- spring boot整合spring-kafka實現(xiàn)發(fā)送接收消息實例代碼
- SpringBoot webSocket實現(xiàn)發(fā)送廣播、點對點消息和Android接收
- Spring Boot實戰(zhàn)之netty-socketio實現(xiàn)簡單聊天室(給指定用戶推送消息)
相關(guān)文章
Springboot項目中定時任務(wù)的四種實現(xiàn)方式詳解
Spring的@Scheduled注解是一種非常簡單和便捷的實現(xiàn)定時任務(wù)的方式,通過在方法上添加@Scheduled注解,我們可以指定方法在特定的時間間隔或固定的時間點執(zhí)行,本文給大家介紹Springboot項目中定時任務(wù)的四種實現(xiàn)方式,感興趣的的朋友一起看看b2024-02-02
shiro與spring?security用自定義異常處理401錯誤
這篇文章主要介紹了shiro與spring?security用自定義異常處理401錯誤,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
Java多態(tài)和實現(xiàn)接口的類的對象賦值給接口引用的方法(推薦)
下面小編就為大家?guī)硪黄狫ava多態(tài)和實現(xiàn)接口的類的對象賦值給接口引用的方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
Springboot Vue可配置調(diào)度任務(wù)實現(xiàn)示例詳解
這篇文章主要為大家介紹了Springboot Vue可配置調(diào)度任務(wù)實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
spring5 SAXParseException:cvc-elt.1: 找不到元素“beans 的聲明詳解
這篇文章主要給大家介紹了關(guān)于spring5 SAXParseException:cvc-elt.1: 找不到元素“beans 聲明的相關(guān)資料,需要的朋友可以參考下2020-08-08
SpringBoot應(yīng)用監(jiān)控帶郵件警報的實現(xiàn)示例
本文主要介紹了SpringBoot應(yīng)用監(jiān)控帶郵件警報的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02

