天天看點

SpringBoot整合rabbitmq

上一篇文章有介紹rabbitmq的簡單實用,是通過一個簡單的java類來實作的。如果用的是springboot項目,那麼我們使用rabbitmq就更簡單了,因為springboot有提供對rabbitmq的接口操作。

那麼這篇文章介紹通過springboot實作rabbitmq的HelloWorld示例

springboot內建RabbitMQ簡單使用:

1.建立一個springboot項目,名為task-mq

2.在pom.xml中引入依賴:

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		
	<!-- rabbitmq -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
      

  3.在

application.properties

中配置關于RabbitMQ的連接配接和使用者資訊

spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
      

  注意:配置檔案的格式是key=value,這些key是預設的,如果寫錯了,就會報錯說連接配接失敗,我也就是将key寫錯,折騰了很長時間。  

spring-boot:約定大于配置。
隻要classpath下有某功能對應的jar包, 同時在配置檔案中又有該功能的配置,那麼就可以使用該功能(該功能就被激活)
      

  4.配置類:

/**
 * 建立配置類:
 * 	用來配置隊列、交換器、路由等進階資訊
 * 2018-3-19 10:17:29
 */
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Config {
	
//	public static final String QUEUE_NAME = "task-queue mytest 2018-3-21";
	public static final String QUEUE_NAME = "task-queue mytest 2018-3-22";
	
	@Bean(name="taskQueue")
	public Queue queue() {
		return new Queue(Config.QUEUE_NAME);
	}
}
      

 5.建立生産者:

  通過注入

AmqpTemplate

接口的執行個體來實作消息的發送,

AmqpTemplate

接口定義了一套針對AMQP協定的基礎操作

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 生産者發送消息
 *
 */
@Component
public class Sender {
	
	@Autowired
	private AmqpTemplate rabbitTemplate;
	
	@Resource(name="taskQueue") //注入name="taskQueue"的Queue
	private Queue queue; 
	
	
	@PostConstruct
	public void test1() {
		this.send();
	}
	
	public void send () {
		
		// 發送對象類型的消息
		Event event = new Event(); //實作Serializable接口
		event.setId(1101);
		event.setName("printscreen event");
		event.setCreateTimestamp(System.currentTimeMillis());
		event.setUpdateTimestamp(System.currentTimeMillis());
		
		System.out.println(queue.getName());
		rabbitTemplate.convertAndSend(queue.getName(), event); // 隊列名稱,消息
	}
	
}
      

  

 6.建立消費者:

  通過

@RabbitListener

注解定義該類對Config.QUEUE_NAME 隊列的監聽,并用

@RabbitHandler

注解來指定對消息的處理方法

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

/**
 * 接收者
 * 2018-3-17 23:18:04
 *
 */
@Component
public class Receiver {
	
	@RabbitListener(queues = Config.QUEUE_NAME) // //監聽器監聽指定的Queue
	public void receive(String msg) {
		System.out.println("receiver: " + msg);
	}
}
      

7.啟動springboot主類,進行測試

參考連結:

http://blog.didispace.com/spring-boot-rabbitmq/

http://www.ityouknow.com/springboot/2016/11/30/springboot(%E5%85%AB)-RabbitMQ%E8%AF%A6%E8%A7%A3.html