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

 

posted @ 2018-03-20 16:26  裸奔的太阳  阅读(2484)  评论(0编辑  收藏  举报