1. 程式人生 > 程式設計 >JavaScript之clipboard用法詳解

JavaScript之clipboard用法詳解

生產者整合

  引入依賴:

<!--父工程-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.8.RELEASE</version>
</parent>
<!--依賴-->
<dependencies>
    <dependency>
        <
groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </
dependency> </dependencies>

  然後我們可以寫一個config類,來定義交換機和佇列,以及他們的繫結關係

package com.layton.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig { public static final String Exchange_NAME = "boot_topic_exchange"; public static final String Queue_NAME = "boot_topic_queue"; //1.交換機 @Bean("bootExchange") public Exchange bootExchange(){ return ExchangeBuilder.topicExchange(Exchange_NAME).durable(true).build(); } //2.佇列 @Bean("bootQueue") public Queue bootQueue(){ return QueueBuilder.durable(Queue_NAME).build(); } //3.佇列和交換機繫結的關係Binding @Bean public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue,@Qualifier("bootExchange") Exchange exchange){ return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs(); } }

  這樣繫結完了之後,我們可以在生產端想發訊息的地方注入RabbitTemplate工具類,然後完成方法的傳送

package com.layton.test;

import com.layton.rabbitmq.config.RabbitMQConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
public class ProducerTest {
    //1注入RabbitTemplate類
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void testSend(){
        rabbitTemplate.convertAndSend(RabbitMQConfig.Exchange_NAME,"boot.haha","boot mq hello~");
    }
}

消費端整合

  同樣的引入依賴:

<!--父工程-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.8.RELEASE</version>
</parent>
<!--依賴-->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
</dependencies>

  然後定義消費端的監聽器,用於監聽佇列的資訊,需要注意的是RabbitListener註解中的queues屬性就是指定佇列名,同時需要注意這個類要加Component註解,讓spring進行管理

package com.layton.mq;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class RabbitMQListener {
    @RabbitListener(queues = "boot_topic_queue")
    public void ListenerQueue(Message message){
        System.out.println(message);
    }
}
一點一點積累,一點一點蛻變!