SpringBoot停止启动时测试检查rabbitmq操作

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.创建一个测试账户

SpringBoot停止启动时测试检查rabbitmq操作_第1张图片

注意这里的Can access virtual hosts必须修改为/,否则可能出现连接失败,这里使用账户为root

3.pom依赖


		org.springframework.boot
		spring-boot-starter-parent
		1.5.2.RELEASE
		
	
	SpringBoot-RabbitMQ
	SpringBoot-RabbitMQ
	0.0.1-SNAPSHOT
	jar
	SpringBoot-RabbitMQ
	http://maven.apache.org
	
		UTF-8
		UTF-8
		1.8
	
	
		
			org.springframework.boot
			spring-boot-starter-amqp
		
		
			org.springframework.boot
			spring-boot-starter-actuator
		
		
			org.springframework.boot
			spring-boot-starter-web
		
	
	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

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.测试

SpringBoot停止启动时测试检查rabbitmq操作_第2张图片

SpringBoot停止启动时测试检查rabbitmq操作_第3张图片

7.总结

1.通过使用SpringBoot操作RabbitMQ的时候非常简单,直接使用AmqpTemplate即可

2.通过@RabbitListener指定需要监听的队列,通过@RabbitHandler方式处理监听后的数据

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

你可能感兴趣的:(SpringBoot停止启动时测试检查rabbitmq操作)