Spring Boot入门教程:十三、spring boot 2.0 集成 rabbitmq

rabbitmq官方下载地址:http://www.rabbitmq.com/download.html

1、在pom.xml中添加rabbitmq的maven依赖


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

2、在application.properties文件中添加rabbitmq的配置信息

#rabbitmq
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

3、配置消息队列:RabbitMqConfig.java,跟启动类放在同一层级目录下

package com.ldy.bootv2.demo;


import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class RabbitMqConfig {
     
	//定义消息队列名称
    public static final String QUEUE_NAME = "ldy-task-queue";
     
    @Bean(name="taskQueue")
    public Queue queue() {
        return new Queue(QUEUE_NAME);
    }
}

4、生产者代码:RabbitMqSender.java

package com.ldy.bootv2.demo.service;

import java.util.Date;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
 
/**
 * @类名: RabbitMqSender
* @描述: 生产者
*/ @Service public class RabbitMqSender { private static Logger logger = LoggerFactory.getLogger(RabbitMqSender.class); @Autowired private AmqpTemplate rabbitTemplate; //注入name="taskQueue"的Queue @Resource(name="taskQueue") private Queue queue; /** * @方法名: execute
* @描述: 测试,5秒发送一条消息
*/ @Scheduled(fixedRate = 1000*5) public void execute() { String msg = "ldy的测试消息 "+ new Date().getTime(); this.send(msg); } /** * @方法名: send
* @描述: 发送消息
* @param msg */ public void send(String msg) { logger.info("send:" + msg); rabbitTemplate.convertAndSend(queue.getName(), msg); } }

注意,测试方法上的@Scheduled注解需要开启任务调度才可以生效,你也可以通过其他方法测试。

在入口类上添加@EnableScheduling开启任务调度,也可以直接在当前类上加该注解

package com.ldy.bootv2.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication
public class BootV2App {

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

5、消费者代码:RabbitMqReceiver.java

package com.ldy.bootv2.demo.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import com.ldy.bootv2.demo.RabbitMqConfig;

/**
 * @类名: RabbitMqReceiver
* @描述: 消费者
*/ @Service public class RabbitMqReceiver { private static Logger logger = LoggerFactory.getLogger(RabbitMqReceiver.class); @RabbitListener(queues = RabbitMqConfig.QUEUE_NAME) // //监听器监听指定的Queue public void receive(String msg) { logger.info("receiver: " + msg); } }

6、启动项目查看运行效果如图

Spring Boot入门教程:十三、spring boot 2.0 集成 rabbitmq_第1张图片

源码下载地址:https://pan.baidu.com/s/1Z771VDiuabDBJJV445xLeA#list/path=%2Fspring%20boot%202.x%20%E4%BB%A3%E7%A0%81

你可能感兴趣的:(spring,boot,rabbitmq,spring,boot,集成rabbitmq,spring,boot,spring,boot,2.0,入门实战教程)