Apache Camel - 在事务回滚之后的X分钟后重新使用JMS消息

时间:2017-11-15 11:49:07

标签: java transactions apache-camel ibm-mq hystrix

我在我的应用程序中使用Apache Camel,SpringBoot和WMQ连接到外部服务。当与此服务的连接失败时,例如当服务停止时,我使用JMS事务将消息回滚到输入队列。但是,重新传递后消息会立即重新消耗,这不是我想要的。我希望这些消息能够再次被消费,但是每30分钟就说一次,外部服务有足够的时间重新上线。

有没有办法用JMS事务配置它并在Apache Camel中添加Hystrix CircuitBreaker(或任何其他库)?

由于

JMS配置:

@Bean
public UserCredentialsConnectionFactoryAdapter connectionFactoryAdapter(final MQQueueConnectionFactory connectionFactory) {
    UserCredentialsConnectionFactoryAdapter connectionFactoryAdapter = new UserCredentialsConnectionFactoryAdapter();
    connectionFactoryAdapter.setTargetConnectionFactory(connectionFactory);
    return connectionFactoryAdapter;

}

@Bean
public CachingConnectionFactory cachingConnectionFactory(
    final UserCredentialsConnectionFactoryAdapter connectionFactoryAdapter) {
    CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
    cachingConnectionFactory.setCacheConsumers(true);
    cachingConnectionFactory.setCacheProducers(true);
    cachingConnectionFactory.setReconnectOnException(true);
    cachingConnectionFactory.setSessionCacheSize(1);
    cachingConnectionFactory.setTargetConnectionFactory(connectionFactoryAdapter);
    return cachingConnectionFactory;
}

@Bean
public JmsTransactionManager jmsTransactionManager(final CachingConnectionFactory cachingConnectionFactory) {
    JmsTransactionManager jmsTransactionManager = new JmsTransactionManager();
    jmsTransactionManager.setConnectionFactory(cachingConnectionFactory);
    return jmsTransactionManager;
}

@Bean
public SpringTransactionPolicy springTransactionPolicy(final PlatformTransactionManager jmsTransactionManager) {
    SpringTransactionPolicy policy = new SpringTransactionPolicy();
    policy.setTransactionManager(jmsTransactionManager);
    policy.setPropagationBehaviorName("PROPAGATION_REQUIRED");
    return policy;
}

@Bean
public JmsConfiguration jmsConfiguration(final CachingConnectionFactory cachingConnectionFactory,
    final JmsTransactionManager jmsTransactionManager) {
    JmsConfiguration config = new JmsConfiguration();
    config.setConnectionFactory(cachingConnectionFactory);
    config.setTransactionManager(jmsTransactionManager);
    config.setTransacted(true);
    config.setConcurrentConsumers(4);
    config.setMaxConcurrentConsumers(8);
    return config;
}

@Bean
public JmsComponent jmsComponent(final JmsConfiguration jmsConfiguration) {
    JmsComponent component = new JmsComponent();
    component.setConfiguration(jmsConfiguration);
    component.setPreserveMessageQos(true);
    component.setDeliveryPersistent(false);
    return component;
}

@Bean
public RedeliveryPolicy redeliveryPolicy() {
    RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy();
    redeliveryPolicy.setMaximumRedeliveries(2);
    redeliveryPolicy.setMaximumRedeliveryDelay(10000);
    redeliveryPolicy.setRedeliveryDelay(10000);
    return redeliveryPolicy;
}

Camel Routes(使用SpringRouteBuilder):

errorHandler(
        transactionErrorHandler()
            .maximumRedeliveries(redeliveryPolicy.getMaximumRedeliveries())
            .redeliveryDelay(redeliveryPolicy.getRedeliveryDelay())
            .maximumRedeliveryDelay(redeliveryPolicy.getMaximumRedeliveryDelay()));

onException(MyException.class)
        .markRollbackOnly()
        .redeliveryPolicy(redeliveryPolicy)
        .useExponentialBackOff()
        .handled(true)

from("jms:queue:myQueue")
        .id("myRoute")
        .autoStartup(true)
        .tracing()
        .transacted()
        .process(bean)
        .stop();

0 个答案:

没有答案