如何在springboot中使用消息中間件

如何在springboot中使用消息中間件?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

為明水等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及明水網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、明水網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

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使用消息中間件/h6>

@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.測(cè)試

啟動(dòng)springBoot

如下顯示表明:連接成功:

如何在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測(cè)試
  sender.send();
 }
}

如何在springboot中使用消息中間件

如何在springboot中使用消息中間件

關(guān)于如何在springboot中使用消息中間件問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

新聞標(biāo)題:如何在springboot中使用消息中間件
文章分享:http://www.muchs.cn/article26/gehsjg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、定制開發(fā)、網(wǎng)站導(dǎo)航微信公眾號(hào)網(wǎng)頁(yè)設(shè)計(jì)公司、響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)