spring-boot集成RabbitMQ
程序员文章站
2022-07-13 08:57:40
...
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); } }
上一篇: 使用Spring框架实现RESTful
推荐阅读
-
springcloud学习之路: (五) springcloud集成SpringCloudConfig分布式配置中心
-
asp.net core集成kindeditor实现图片上传功能
-
集成显卡3D加速怎么开启?
-
spring集成mybatis原理(spring和mybatis整合步骤)
-
Spring Boot集成Mybatis的实例代码(简洁版)
-
springboot2.x集成swagger的方法示例
-
Python_RabbitMQ实现简单的进程间通信
-
Spring Boot中使用RabbitMQ的示例代码
-
Spring Security OAuth2集成短信验证码登录以及第三方登录
-
spring集成okhttp3的步骤详解