app开发者平台在数字化时代的重要性与发展趋势解析
1442
2022-12-09
SpringBoot停止启动时测试检查rabbitmq操作
目录SpringBoot停止启动时测试检查rabbitmq问题解决RabbitMQ的简单使用的Demo1.声明2.创建一个测试账户3.pom依赖5.创建入口类6.测试7.总结
SpringBoot停止启动时测试检查rabbitmq
问题
在Springboot项目中配置rabbitmq后,总是在每次启动时自动测试MQ的连接,如果测试不通过,就一直重连,导致项目无法正常启动。自己在开发与MQ无关的功能时,无法正常进行,十分耽误时间。如下所示:
org.springframework.amqp.AmqpConnectException: java-.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-.ConnectException: Connection timed out: connect
at java-.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_231]
at java-.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[na:1.8.0_231]
at java-.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_231]
at java-.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_231]
at java-.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_231]
at java-.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_231]
at java-.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_231]
at java-.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依赖
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方式处理监听后的数据
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~