Spring mvc spring中rabbitmq侦听器的异常处理
与spring合作,我是rabbitmq的新手,我想知道我错在哪里 我编写了一个rabbitmq连接工厂和一个包含侦听器的侦听器容器。我还为侦听器容器提供了一个错误处理程序,但它似乎不起作用 我的春豆:Spring mvc spring中rabbitmq侦听器的异常处理,spring-mvc,rabbitmq,spring-rabbit,Spring Mvc,Rabbitmq,Spring Rabbit,与spring合作,我是rabbitmq的新手,我想知道我错在哪里 我编写了一个rabbitmq连接工厂和一个包含侦听器的侦听器容器。我还为侦听器容器提供了一个错误处理程序,但它似乎不起作用 我的春豆: <rabbit:connection-factory id="RabbitMQConnectionFactory" virtual-host="${rabbitmq.vhost}" host="${rabbitmq.host}" port="${rabbitmq.port}" userna
<rabbit:connection-factory id="RabbitMQConnectionFactory" virtual-host="${rabbitmq.vhost}" host="${rabbitmq.host}" port="${rabbitmq.port}" username="${rabbitmq.username}" password="${rabbitmq.password}"/>
<rabbit:listener-container missing-queues-fatal="false" declaration-retries="0" error-handler="errorHandlinginRabbitMQ" recovery-interval="10000" auto-startup="${rabbitmq.apc.autostartup}" max-concurrency="1" prefetch="1" concurrency="1" connection-factory="RabbitMQConnectionFactory" acknowledge="manual">
<rabbit:listener ref="apcRabbitMQListener" queue-names="${queue.tpg.rabbitmq.destination.apc}" exclusive="true" />
</rabbit:listener-container>
<bean id="errorHandlinginRabbitMQ" class="RabbitMQErrorHandler"/>
我的假设是,如果我向连接工厂提供无效凭据,则应执行RabbitMQErrorHandler类的handleError方法,并且服务器应正确启动,但是,当我尝试运行服务器时,该方法不执行(在控制台中引发异常),服务器无法启动。我在哪里遗漏了什么?可能是什么?错误处理程序用于在消息传递过程中处理错误;由于您尚未连接,因此没有要处理错误的消息 要获取连接异常,应该实现
ApplicationListener
,如果将失败作为bean添加到应用程序上下文中,则会将其作为事件接收
如果实现ApplicationListener
,您将获得其他事件(使用者启动、使用者停止等)
编辑
<rabbit:listener-container auto-startup="false">
<rabbit:listener id="fooContainer" ref="foo" method="handleMessage"
queue-names="si.test.queue" />
</rabbit:listener-container>
<bean id="foo" class="com.example.Foo" />
应用程序:
谢谢,成功了。然而,我想要执行的方法仍然不允许服务器正常启动。我想要的是,即使由于监听器中的一些异常,监听器没有被激活,我服务器上的其他服务也会继续工作。请注意,身份验证错误被认为是致命的;您可以设置
auto startup=“false”
并手动启动容器,以便捕获异常。我们如何手动启动容器?这是我最近几天以来一直在寻找的东西。此外,侦听器容器中没有自动启动标记请参见编辑我的答案;这个属性确实存在,明白了!非常感谢@Gary:)
<rabbit:listener-container auto-startup="false">
<rabbit:listener id="fooContainer" ref="foo" method="handleMessage"
queue-names="si.test.queue" />
</rabbit:listener-container>
<bean id="foo" class="com.example.Foo" />
public class Foo {
public final CountDownLatch latch = new CountDownLatch(1);
public void handleMessage(String foo) {
System.out.println(foo);
this.latch.countDown();
}
}
@SpringBootApplication
@ImportResource("context.xml")
public class So43208940Application implements CommandLineRunner {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(So43208940Application.class, args);
context.close();
}
@Autowired
private SimpleMessageListenerContainer fooContainer;
@Autowired
private CachingConnectionFactory connectionFactory;
@Autowired
private RabbitTemplate template;
@Autowired
private Foo foo;
@Override
public void run(String... args) throws Exception {
this.connectionFactory.setUsername("junk");
try {
this.fooContainer.start();
}
catch (Exception e) {
e.printStackTrace();
}
Thread.sleep(5000);
this.connectionFactory.setUsername("guest");
this.fooContainer.start();
System.out.println("Container started");
this.template.convertAndSend("si.test.queue", "foo");
foo.latch.await(10, TimeUnit.SECONDS);
}
}