我正在使用:
基本上,我有一个带有Apache Camel路由的SpringBoot应用程序,该路由使用ActiveMQ的消息进行事务处理。我需要在ActiveMQ上设置RedeliveryPolicy,以便在处理中发生错误时,重试该消息多次。
我使用ActiveMQ的bean创建了一个配置类,事务按预期工作,但是RedeliveryPolicy不起作用。有人可以帮我了解这是怎么回事吗?
以下是产生错误的消息的日志输出:
2018-10-23 10:35:28.005调试10524 --- [mer [entryQueue]] oacsspi.TransactionErrorHandler:为(MessageId:ID:EPIC-LAP-25-)重新开始事务(0x35d60381)(false) 50304-1540306817804-4:3:1:1:2在ExchangeId:ID-EPIC-LAP-25-1540312510586-0-1)) 2018-10-23 10:35:28.020调试10524 --- [mer [entryQueue]] o.apache.camel.processor.SendProcessor:>>>> direct://中间交换[ID-EPIC-LAP-25- 1540312510586-0-1] 2018-10-23 10:35:28.375调试10524 --- [mer [entryQueue]] oacamel.processor.DefaultErrorHandler:((MessageId:ID:EPIC-LAP-25-50304-1540306817804-4:3:在ExchangeId上为1:1:2:ID-EPIC-LAP-25-1540312510586-0-1)。交付尝试:0捕获:java.lang.RuntimeException:ExceptionTest:订单失败 2018-10-23 10:35:28.390错误10524 --- [mer [entryQueue]] oacamel.processor.DefaultErrorHandler:(MessageId:ID:EPIC-LAP-25-50304-1540306817804-4:3的交付失败:在ExchangeId上为1:1:2:ID-EPIC-LAP-25-1540312510586-0-1)。尝试交付后用尽:1个被捕获:java.lang.RuntimeException:ExceptionTest:订单失败
这是我的ActiveMQ配置类:
import org.apache.activemq.ActiveMQConnectionFactory
import org.apache.activemq.RedeliveryPolicy
import org.apache.activemq.camel.component.ActiveMQComponent
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.jms.connection.JmsTransactionManager
import javax.jms.DeliveryMode
@Configuration
class ActiveMQConfiguration {
@Bean
ActiveMQConnectionFactory activeMQConnectionFactory() {
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory()
activeMQConnectionFactory.brokerURL = 'tcp://localhost:61616'
activeMQConnectionFactory.userName = 'admin'
activeMQConnectionFactory.password = 'admin'
RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy()
redeliveryPolicy.maximumRedeliveries = 3
redeliveryPolicy.redeliveryDelay = 150L
redeliveryPolicy.useExponentialBackOff = true
redeliveryPolicy.backOffMultiplier = 1.5
activeMQConnectionFactory.setRedeliveryPolicy(redeliveryPolicy)
activeMQConnectionFactory
}
@Bean
ActiveMQComponent activeMQComponent(@Qualifier('activeMQConnectionFactory')ActiveMQConnectionFactory activeMQConnectionFactory) {
ActiveMQComponent activeMQComponent = new ActiveMQComponent()
activeMQComponent.connectionFactory = activeMQConnectionFactory
activeMQComponent.transacted = true
activeMQComponent.transactionManager = txManager()
activeMQComponent.cacheLevelName = 'CACHE_CONSUMER'
activeMQComponent.lazyCreateTransactionManager = false
activeMQComponent.deliveryMode = DeliveryMode.PERSISTENT
activeMQComponent
}
@Bean
JmsTransactionManager txManager(@Qualifier('activeMQConnectionFactory') ActiveMQConnectionFactory activeMQConnectionFactory) {
JmsTransactionManager txManager = new JmsTransactionManager()
txManager.connectionFactory = activeMQConnectionFactory
txManager.rollbackOnCommitFailure = true
txManager
}
}
答案 0 :(得分:1)
不久前,我遇到了dlq队列的问题-并非代码中设置的所有参数都能正常工作。我必须将设置添加到acitvemq配置。是的,划分配置不是一个好的决定,但我没有找到其他配置。 以下是我的jms配置类和通过activemq.xml进行示例队列配置:
@Configuration
@EnableJms
public class JmsConfig {
private Environment env;
@Autowired
public void setEnv(Environment env) {
this.env = env;
}
@Bean(name = "activemq")
public ActiveMQComponent activemq(@Qualifier("activemqTransactionManager") JmsTransactionManager jmsTransactionManager,
@Qualifier("activemqConnectionFactory") ConnectionFactory connectionFactory) {
ActiveMQComponent activeMQComponent = new ActiveMQComponent();
activeMQComponent.setTransactionManager(jmsTransactionManager);
activeMQComponent.setConnectionFactory(connectionFactory);
return activeMQComponent;
}
@Bean(name = "activemqJmsTemplate")
public JmsTemplate jmsTemplate(@Qualifier("activemqConnectionFactory") ConnectionFactory connectionFactory) {
JmsTemplate template = new JmsTemplate();
template.setConnectionFactory(connectionFactory);
return template;
}
@Bean(name = "activemqTransactionPolicy")
public SpringTransactionPolicy activemqTransactionPolicy(
@Qualifier("activemqTransactionManager") JmsTransactionManager jmsTransactionManager) {
SpringTransactionPolicy springTransactionPolicy = new SpringTransactionPolicy(jmsTransactionManager);
springTransactionPolicy.setPropagationBehaviorName("PROPAGATION_REQUIRED");
return springTransactionPolicy;
}
@Bean(name = "activemqTransactionManager")
public JmsTransactionManager activemqTransactionManager(
@Qualifier("activemqConnectionFactory") ConnectionFactory connectionFactory) {
return new JmsTransactionManager(connectionFactory);
}
@Bean(name = "activemqConnectionFactory")
public ConnectionFactory connectionFactory(@Qualifier("activemqRedeliveryPolicy") RedeliveryPolicy redeliveryPolicy) {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
connectionFactory.setBrokerURL("tcp://" + env.getProperty("queue.url"));
connectionFactory.setTrustAllPackages(true);
RedeliveryPolicyMap map = connectionFactory.getRedeliveryPolicyMap();
map.put(new ActiveMQQueue("queueName.DLQ"), redeliveryPolicy);
return connectionFactory;
}
@Bean(name = "activemqRedeliveryPolicy")
public RedeliveryPolicy redeliveryPolicy() {
RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy();
redeliveryPolicy.setMaximumRedeliveries(0);
return redeliveryPolicy;
}
}
activevq.xml中的更改:
<destinationPolicy>
<policyMap>
<policyEntries>
<!--set dead letter queue for our queue. It name will be "myQueueName.DLQ"-->
<policyEntry queue="myQueueName">
<deadLetterStrategy>
<individualDeadLetterStrategy queuePrefix="" queueSuffix=".DLQ"/>
</deadLetterStrategy>
</policyEntry>
<policyEntry topic=">">
<pendingMessageLimitStrategy>
<constantPendingMessageLimitStrategy limit="1000"/>
</pendingMessageLimitStrategy>
</policyEntry>
</policyEntries>
</policyMap>
</destinationPolicy>
<plugins>
<redeliveryPlugin fallbackToDeadLetter="true" sendToDlqIfMaxRetriesExceeded="true">
<redeliveryPolicyMap>
<redeliveryPolicyMap>
<redeliveryPolicyEntries>
<!--Set the redelivery delay to one hour-->
<redeliveryPolicy queue="myQueueName.DLQ" maximumRedeliveries="-1" redeliveryDelay="3600000"/>
</redeliveryPolicyEntries>
</redeliveryPolicyMap>
</redeliveryPolicyMap>
</redeliveryPlugin>
</plugins>
答案 1 :(得分:1)
这里有两个问题
1。您有两个交易经理
由于在配置骆驼ActiveMQ组件的以下两行中,您配置了两个事务管理器。这是问题的根源。
activeMQComponent.transacted = true // activates local JMS transactions
activeMQComponent.transactionManager = txManager() // additional tx manager
如果您只想使用ActiveMQ的事务性,则您不需要配置Spring事务管理器。
配置的这两行足以与ActiveMQ代理进行本地事务。
activeMQComponent.transacted = true
activeMQComponent.lazyCreateTransactionManager = false
因此,您应该删除此行以及整个txManager
bean
activeMQComponent.transactionManager = txManager()
如果您当前在骆驼路线中设置了交易标记,则也必须将其删除。正如我所写,即使删除了所有这些,从ActiveMQ消耗的路由仍会被处理。
2。重新交付不起作用
您尚未发布您的Camel路线,但是根据错误输出,我认为经纪人不会重新投放,因为错误是由Camel处理的。
当发生错误时,将启动Camel错误处理程序o.a.camel.processor.DefaultErrorHandler
,并且由于它处理错误,因此消息是针对代理提交的,因此不会重新交付。
尝试禁用Camel错误处理,以查看代理是否根据错误重新发送消息。
errorHandler(noErrorHandler());