成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

SpringBoot非官方教程 | 第十五篇:Springboot整合RabbitMQ

HollisChuang / 2744人閱讀

摘要:創(chuàng)建消息監(jiān)聽,并發(fā)送一條消息在程序中,提供了發(fā)送消息和接收消息的所有方法。

這篇文章帶你了解怎么整合RabbitMQ服務(wù)器,并且通過它怎么去發(fā)送和接收消息。我將構(gòu)建一個springboot工程,通過RabbitTemplate去通過MessageListenerAdapter去訂閱一個POJO類型的消息。

準(zhǔn)備工作
15min
IDEA
maven 3.0

在開始構(gòu)建項目之前,機(jī)器需要安裝rabbitmq,你可以去官網(wǎng)下載,http://www.rabbitmq.com/downl... ,如果你是用的Mac(程序員都應(yīng)該用mac吧),你可以這樣下載:

brew install rabbitmq

安裝完成后開啟服務(wù)器:

rabbitmq-server

開啟服務(wù)器成功,你可以看到以下信息:

    

    RabbitMQ 3.1.3. Copyright (C) 2007-2013 VMware, Inc.
##  ##      Licensed under the MPL.  See http://www.rabbitmq.com/
##  ##
##########  Logs: /usr/local/var/log/rabbitmq/rabbit@localhost.log
######  ##        /usr/local/var/log/rabbitmq/rabbit@localhost-sasl.log
##########
            Starting broker... completed with 6 plugins.
構(gòu)建工程

構(gòu)架一個SpringBoot工程,其pom文件依賴加上spring-boot-starter-amqp的起步依賴:


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

創(chuàng)建消息接收者

在任何的消息隊列程序中,你需要創(chuàng)建一個消息接收者,用于響應(yīng)發(fā)送的消息。

@Component
public class Receiver {

    private CountDownLatch latch = new CountDownLatch(1);

    public void receiveMessage(String message) {
        System.out.println("Received <" + message + ">");
        latch.countDown();
    }

    public CountDownLatch getLatch() {
        return latch;
    }

}

消息接收者是一個簡單的POJO類,它定義了一個方法去接收消息,當(dāng)你注冊它去接收消息,你可以給它取任何的名字。其中,它有CountDownLatch這樣的一個類,它是用于告訴發(fā)送者消息已經(jīng)收到了,你不需要在應(yīng)用程序中具體實現(xiàn)它,只需要latch.countDown()就行了。
創(chuàng)建消息監(jiān)聽,并發(fā)送一條消息

在spring程序中,RabbitTemplate提供了發(fā)送消息和接收消息的所有方法。你只需簡單的配置下就行了:

需要一個消息監(jiān)聽容器
聲明一個quene,一個exchange,并且綁定它們
一個組件去發(fā)送消息

代碼清單如下:

package com.forezp;

import com.forezp.message.Receiver;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;


@SpringBootApplication
public class SpringbootRabbitmqApplication {

     final static String queueName = "spring-boot";

    @Bean
    Queue queue() {
        return new Queue(queueName, false);
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange("spring-boot-exchange");
    }

    @Bean
    Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(queueName);
    }

    @Bean
    SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
                                             MessageListenerAdapter listenerAdapter) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(queueName);
        container.setMessageListener(listenerAdapter);
        return container;
    }

    @Bean
    MessageListenerAdapter listenerAdapter(Receiver receiver) {
        return new MessageListenerAdapter(receiver, "receiveMessage");
    }


    public static void main(String[] args) {
        SpringApplication.run(SpringbootRabbitmqApplication.class, args);
    }
}

創(chuàng)建一個測試方法:

@Component
public class Runner implements CommandLineRunner {

    private final RabbitTemplate rabbitTemplate;
    private final Receiver receiver;
    private final ConfigurableApplicationContext context;

    public Runner(Receiver receiver, RabbitTemplate rabbitTemplate,
            ConfigurableApplicationContext context) {
        this.receiver = receiver;
        this.rabbitTemplate = rabbitTemplate;
        this.context = context;
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Sending message...");
        rabbitTemplate.convertAndSend(Application.queueName, "Hello from RabbitMQ!");
        receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);
        context.close();
    }

}

啟動程序,你會發(fā)現(xiàn)控制臺打印:

Sending message...
Received 
總結(jié)

恭喜!你剛才已經(jīng)學(xué)會了如何通過spring raabitmq去構(gòu)建一個消息發(fā)送和訂閱的程序。 這僅僅是一個好的開始,你可以通過spring-rabbitmq做更多的事,點擊這里。

源碼下載:https://github.com/forezp/Spr...
參考資料

https://spring.io/guides/gs/m...

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/68035.html

相關(guān)文章

  • SpringBoot官方教程 | 第二十五篇:2小時學(xué)會springboot

    摘要:一什么是摘自官網(wǎng)翻譯采納了建立生產(chǎn)就緒應(yīng)用程序的觀點。優(yōu)先于配置的慣例,旨在讓您盡快啟動和運(yùn)行。致力于簡潔,讓開發(fā)者寫更少的配置,程序能夠更快的運(yùn)行和啟動。二搭建第一個程序可以在上建項目,也可以用構(gòu)建。已經(jīng)凌晨了,我要睡了源碼 一.什么是spring boot Takes an opinionated view of building production-ready Spring a...

    baukh789 評論0 收藏0
  • SpringBoot官方教程 | 第五篇SpringBoot整合 beatlsql

    摘要:整合階段由于沒有對的快速啟動裝配,所以需要我自己導(dǎo)入相關(guān)的,包括數(shù)據(jù)源,包掃描,事物管理器等。另外它的中文文檔比較友好。源碼下載參考資料中文文檔 BeetSql是一個全功能DAO工具, 同時具有Hibernate 優(yōu)點 & Mybatis優(yōu)點功能,適用于承認(rèn)以SQL為中心,同時又需求工具能自動能生成大量常用的SQL的應(yīng)用。 beatlsql 優(yōu)點 開發(fā)效率 無需注解,自動使用大...

    microelec 評論0 收藏0
  • 一起來學(xué)SpringBoot | 第十二篇:初探RabbitMQ消息隊列

    摘要:用于控制活動人數(shù),將超過此一定閥值的訂單直接丟棄。緩解短時間的高流量壓垮應(yīng)用。目前比較推薦的就是我們手動然后將消費錯誤的消息轉(zhuǎn)移到其它的消息隊列中,做補(bǔ)償處理消費者該方案是默認(rèn)的方式不太推薦。 SpringBoot 是為了簡化 Spring 應(yīng)用的創(chuàng)建、運(yùn)行、調(diào)試、部署等一系列問題而誕生的產(chǎn)物,自動裝配的特性讓我們可以更好的關(guān)注業(yè)務(wù)本身而不是外部的XML配置,我們只需遵循規(guī)范,引入相...

    Baoyuan 評論0 收藏0
  • SpringBoot RabbitMQ 整合使用

    摘要:可以在地址看到如何使用講解下上面命令行表示控制臺端口號,可以在瀏覽器中通過控制臺來執(zhí)行的相關(guān)操作。同時從控制臺可以看到發(fā)送的速率多線程測試性能開了個線程,每個線程發(fā)送條消息。 showImg(http://ww2.sinaimg.cn/large/006tNc79ly1g5jjb62t88j30u00gwdi2.jpg); 前提 上次寫了篇文章,《SpringBoot Kafka 整合...

    yuanxin 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<