SpringBoot停止启动时测试检查rabbitmq操作
目录
- SpringBoot停止启动时测试检查rabbitmq
- 问题
- 解决
- RabbitMQ的简单使用的Demo
- 1.声明
- 2.创建一个测试账户
- 3.pom依赖
- 5.创建入口类
- 6.测试
- 7.总结
SpringBoot停止启动时测试检查rabbitmq
问题
在Springboot项目中配置rabbitmq后,总是在每次启动时自动测试MQ的连接,如果测试不通过,就一直重连,导致项目无法正常启动。自己在开发与MQ无关的功能时,无法正常进行,十分耽误时间。如下所示:
org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection timed out: connect at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:62) ~[spring-rabbit-2.0.6.RELEASE.jar:2.0.6.RELEASE] at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:484) ~[spring-rabbit-2.0.6.RELEASE.jar:2.0.6.RELEASE] at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:628) ~[spring-rabbit-2.0.6.RELEASE.jar:2.0.6.RELEASE] at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.createConnection(ConnectionFactoryUtils.java:240) ~[spring-rabbit-2.0.6.RELEASE.jar:2.0.6.RELEASE] at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:1816) ~[spring-rabbit-2.0.6.RELEASE.jar:2.0.6.RELEASE] at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:1790) ~[spring-rabbit-2.0.6.RELEASE.jar:2.0.6.RELEASE] at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:1771) ~[spring-rabbit-2.0.6.RELEASE.jar:2.0.6.RELEASE] at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueProperties(RabbitAdmin.java:345) ~[spring-rabbit-2.0.6.RELEASE.jar:2.0.6.RELEASE] at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.redeclareElementsIfNecessary(AbstractMessageListenerContainer.java:1604) ~[spring-rabbit-2.0.6.RELEASE.jar:2.0.6.RELEASE] at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:995) [spring-rabbit-2.0.6.RELEASE.jar:2.0.6.RELEASE] at java.lang.Thread.run(Thread.java:748) [na:1.8.0_231] Caused by: java.net.ConnectException: Connection timed out: connect at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_231] at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[na:1.8.0_231] at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_231] at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_231] at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_231] at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_231] at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_231] at java.net.Socket.connect(Socket.java:606) ~[na:1.8.0_231] at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:60) ~[amqp-client-5.4.1.jar:5.4.1] at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1102) ~[amqp-client-5.4.1.jar:5.4.1] at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1054) ~[amqp-client-5.4.1.jar:5.4.1] at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:994) ~[amqp-client-5.4.1.jar:5.4.1] at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:457) ~[spring-rabbit-2.0.6.RELEASE.jar:2.0.6.RELEASE] ... 9 common frames omitted
查找了很多地方,这里记录一下,如何在Springboot启动项目时,如何关闭RabbitMq的连接测试。
解决
在配置文件里增加
spring.rabbitmq.listener.direct.auto-startup= false spring.rabbitmq.listener.simple.auto-startup= false
这样启动时就不会每次都检查RabbitMq是否可以连接了
RabbitMQ的简单使用的Demo
当前使用的版本:rabbitmq-server-windows-3.8.3
1.声明
当前内容用于本人学习和使用RabbitMQ这个消息队列,当前内容为SpringBoot操作RabbitMQ的基本操作
本人使用windows10方式使用RabbitMQ
2.创建一个测试账户
注意这里的Can access virtual hosts必须修改为/,否则可能出现连接失败,这里使用账户为root
3.pom依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath /> </parent> <groupId>SpringBoot-RabbitMQ</groupId> <artifactId>SpringBoot-RabbitMQ</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SpringBoot-RabbitMQ</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version><!-- 更新SpringCloud的版本 --> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
4.配置文件application.properties
spring.application.name=rabbitmq-hello server.port=8080 spring.rabbitmq.host spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=root spring.rabbitmq.password=root
5.创建入口类
@RestController @SpringBootApplication public class RabbitMQApplication { @Autowired AmqpTemplate amqpTemplate; @RequestMapping("/send") public String send(String msg) { amqpTemplate.convertAndSend("test", msg); return "发送消息成功:===>" + msg; } public static void main(String[] args) { SpringApplication.run(RabbitMQApplication.class, args); } @Bean Queue queue() { return new Queue("test"); } @Component @RabbitListener(queues = "test") class MsgCustomer { @RabbitHandler public void handlerMsg(String msg) { System.out.println("消息消费者消费===>" + msg); } } }
6.测试
7.总结
1.通过使用SpringBoot操作RabbitMQ的时候非常简单,直接使用AmqpTemplate即可
2.通过@RabbitListener指定需要监听的队列,通过@RabbitHandler方式处理监听后的数据
以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。
赞 (0)