springboot使用消息中間件
更新時間:2019年10月21日 14:17:38 作者:三鮮豆皮
這篇文章主要介紹了springboot使用消息中間件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
前言
使用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)文章
Java?properties?和?yml?的區(qū)別解析
properties和yml都是Spring?Boot支持的兩種配置文件,它們可以看做Spring?Boot在不同時期的兩種“產(chǎn)品”,這篇文章主要介紹了Java?properties?和?yml?的區(qū)別,需要的朋友可以參考下2023-02-02
SpringCloud+Redis實現(xiàn)Api接口限流防止惡意刷接口
接口限流是為了保護(hù)系統(tǒng)和服務(wù),防止因為過多的請求而崩潰,本文主要介紹了SpringCloud+Redis實現(xiàn)Api接口限流防止惡意刷接口,具有一定的參考價值,感興趣的可以了解一下2024-03-03
Springboot 1.5.7整合Kafka-client代碼示例
這篇文章主要介紹了Springboot 1.5.7整合Kafka-client代碼示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10

