欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring-boot集成RabbitMQ

程序员文章站 2022-06-22 15:53:50
...

 

RabbitMQ的安装不在此赘述,想了解的可以参考:Spring Boot中使用RabbitMQ

 

1、新建一个spring-boot工程,本示例命名为:spring-boot-rabbitmq-demo

 

2、在pom.xml中引入amqp

 

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

 

 

3、在application.properties中配置rabbitmq

 

spring.application.name=spring-boot-rabbitmq-demo

spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.listener.simple.concurrency=10

 

 

4、配置RabbitMQ的队列和消息转换器

 

package com.huatech.config;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {

    @Bean
    public Queue helloQueue() {
        return new Queue("user");
    }

    @Bean
    public MessageConverter messageConverter() {
    	return new Jackson2JsonMessageConverter();
    }
    
    @Bean
    public SimpleRabbitListenerContainerFactory containerFactory(SimpleRabbitListenerContainerFactory factory) {
    	factory.setMessageConverter(messageConverter());
    	return factory;
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
    	RabbitTemplate template = new RabbitTemplate(connectionFactory);
    	template.setMessageConverter(messageConverter());
    	return template;
    }
    
    

}

 

 

5、消息生产者UserSender

package com.huatech.rabbit;

import java.util.Date;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.huatech.domain.User;

@Component
public class UserSender {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send() {
    	for (int i = 0; i < 100; i++) {
    		User user = new User();
    		user.setCreateTime(new Date());
    		user.setEmail("lihua_java@163.com");
    		user.setId(11L);
    		user.setRemark("remark");
    		user.setUsername("lihua");        
    		System.out.println("Sender : " + user.toString());
    		this.rabbitTemplate.convertAndSend("user", user);			
		}
    }

}

 

 

6、消息消费者UserReceiver

package com.huatech.rabbit;

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

import com.huatech.domain.User;

@Component
@RabbitListener(queues = "user", containerFactory = "containerFactory")
public class UserReceiver {

    @RabbitHandler
    public void process(User user) {
        System.out.println("Receiver : " + user.toString());
    }
	

}

 

7、测试

package com.huatech;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.huatech.rabbit.UserSender;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {

	@Autowired
	private UserSender sender;

	@Test
	public void testUser() throws Exception {
		sender.send();
		Thread.sleep(5000);
	}

}