spring boot-分布式定时任务中,异步执行时JMS无法重新投递消息

这篇博客是紧接着上篇博客来说的http://blog.csdn.net/liuchuanhong1/article/details/64126346

在上篇博客中,我们基本上实现了分布式的定时任务架构的搭建,但是,当我们异步消费消息中间件中的消息的时候,如果执行的过程中抛了异常,那么,我们无法做到消息重试,示例如下:

@Component // 必须加此注解,该类才会被作为Message Driven POJO使用
public class Consumer {
	@JmsListener(destination = "mytest.queue")
	@Async // 异步执行此方法
	public void receiveQueue(TextMessage text) throws Exception {
		throw new Exception();
	}
}
测试结果如下:

console结果

2017-03-20 18:38:10.883 ERROR 6276 --- [TaskExecutor-91] .a.i.SimpleAsyncUncaughtExceptionHandler : Unexpected error occurred invoking async method 'public void com.chhliu.springboot.jms.Consumer.receiveQueue(javax.jms.TextMessage) throws java.lang.Exception'.

java.lang.Exception: null
	at com.chhliu.springboot.jms.Consumer.receiveQueue(Consumer.java:15) ~[classes/:na]
	at com.chhliu.springboot.jms.Consumer$$FastClassBySpringCGLIB$$68e885f7.invoke(<generated>) ~[classes/:na]
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.3.5.RELEASE.jar:4.3.5.RELEASE]
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:721) ~[spring-aop-4.3.5.RELEASE.jar:4.3.5.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.3.5.RELEASE.jar:4.3.5.RELEASE]
	at org.springframework.aop.interceptor.AsyncExecutionInterceptor$1.call(AsyncExecutionInterceptor.java:115) ~[spring-aop-4.3.5.RELEASE.jar:4.3.5.RELEASE]
	at java.util.concurrent.FutureTask.run(Unknown Source) [na:1.8.0_121]
	at java.lang.Thread.run(Unknown Source) [na:1.8.0_121]

ActiveMq的管理页面

我们发现入队列100条消息,出队列也是100条消息

从上面的测试中,可以看出,当我们使用异步来执行的时候,即使程序中抛出了异常,ActiveMq的消息重试机制也不会生效,那如果我们从队列中接收到消息,但是在执行程序的过程中,程序抛了异常,我们又想重试,怎么处理了?

1、不使用异步

2、使用jms自带的并发

3、使用客户端消息确认机制

        JMS在默认的情况下,使用的是自动的消息确认机制,确认总是隐式的在

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值