簡體   English   中英

spring amqp死信隊列不適用於事務

[英]spring amqp dead letter queue does not work with transaction

當出現問題而不是重新排隊時,如何在channelTransacted(true)將消息放入死信隊列時設置偵聽器容器? 當我不使用channelTransacted時,一切正常,我可以在死信隊列中看到該消息。

@Bean(name = "amqpInboundEsignRequest")
public IntegrationFlow amqpInbound(ConnectionFactory connectionFactory, PlatformTransactionManager transactionManager) {
    return IntegrationFlows.from(
            Amqp.inboundAdapter(connectionFactory, esignIAutoRequestQueue())
                    .acknowledgeMode(AcknowledgeMode.AUTO)
                    .messageConverter(new Jackson2JsonMessageConverter())
                    .autoStartup(false)
                    .defaultRequeueRejected(false)
                    //.channelTransacted(true) // dead letter does not work
                    //.transactionManager(transactionManager) // dead letter does not work
    )
            .log("amqpInbound.start-process")
            .handle(p -> {
                throw new RuntimeException("Something wrong!");
            })
            .get();
}

編輯

這些是依賴項的版本。

[INFO] +- org.springframework.boot:spring-boot-starter-amqp:jar:1.5.9.RELEASE:compile
[INFO] |  +- org.springframework:spring-messaging:jar:4.3.13.RELEASE:compile
[INFO] |  \- org.springframework.amqp:spring-rabbit:jar:1.7.4.RELEASE:compile
[INFO] |     +- com.rabbitmq:http-client:jar:1.1.1.RELEASE:compile
[INFO] |     +- com.rabbitmq:amqp-client:jar:4.0.3:compile
[INFO] |     +- org.springframework.retry:spring-retry:jar:1.2.1.RELEASE:compile
[INFO] |     \- org.springframework.amqp:spring-amqp:jar:1.7.4.RELEASE:compile

[INFO] +- org.springframework.boot:spring-boot-starter-integration:jar:1.5.9.RELEASE:compile
[INFO] |  +- org.springframework.integration:spring-integration-core:jar:4.3.12.RELEASE:compile
[INFO] |  \- org.springframework.integration:spring-integration-java-dsl:jar:1.2.3.RELEASE:compile
[INFO] |     \- org.reactivestreams:reactive-streams:jar:1.0.0:compile

我想使事務與外部事務數據庫(PlatformTransactionManager)同步。 當我在偵聽器容器上設置transactionManager(transactionManager)時,它總是會重新排隊。

您使用什么版本? 我剛剛使用Spring Integration 5.0.2和Spring AMQP 2.0.2以及Spring Integration 4.3.14和Spring AMQP 1.7.6測試了channelTransacted=true ,並且一切正常,失敗的消息發送到了DLQ。

@SpringBootApplication
public class So48914749Application {

    public static void main(String[] args) {
        SpringApplication.run(So48914749Application.class, args);
    }

    @Bean
    public IntegrationFlow amqpInbound(ConnectionFactory connectionFactory) {
        return IntegrationFlows.from(
                Amqp.inboundAdapter(connectionFactory, "foo")
                        .acknowledgeMode(AcknowledgeMode.AUTO)
                        .messageConverter(new Jackson2JsonMessageConverter())
                        .autoStartup(true)
                        .defaultRequeueRejected(false)
                        .channelTransacted(true) // dead letter does not work
        // .transactionManager(transactionManager) // dead letter does not work
        )
                .log("amqpInbound.start-process")
                .handle(p -> {
                    throw new RuntimeException("Something wrong!");
                })
                .get();
    }

    @Bean
    public Queue queue() {
        return QueueBuilder.durable("foo")
                .withArgument("x-dead-letter-exchange", "")
                .withArgument("x-dead-letter-routing-key", "dlq")
                .build();
    }

    @Bean
    public Queue dlq() {
        return new Queue("dlq");
    }

    @RabbitListener(queues = "dlq")
    public void dlq(Message in) {
        System.out.println(in);
    }

}

結果:

(正文:“ [B @ 17793549(byte [8])”)MessageProperties [headers = {x-first-death-exchange =,x-death = [{原因=已拒絕,count = 1,交換=,時間= 2月周三21:43:48 EST 2018,路由鍵= [foo],隊列= foo}],x-first-death-reason =已拒絕,x-first-death-queue = foo},時間戳= null,messageId = null,userId = null,receivedUserId = null,appId = null,clusterId = null,type = null,correlationId = null,correlationIdString = null,replyTo = null,contentType = null,contentEncoding = null,contentLength = 0,deliveryMode = null, receiveedDeliveryMode = NON_PERSISTENT,到期時間為null,優先級為null,重新交付的結果為false,receivedExchange =,receivedRoutingKey = dlq,receivedDelay = null,deliveryTag = 4,messageCount = 0,consumerTag = amq.ctag-dOypXDkkQ5Hvw2W9cUIpzg,consumerQueue = dlq)

編輯

將來請更精確; 您的問題暗示無論是否提供了事務管理器,都會發生相同的問題。

請參閱回滾收到的郵件的注釋”下的第二個注釋 這在2.0中已修復。

在1.7.x中,為了向后兼容,我們必須默認保留舊的行為。 1.7.x文檔說明您必須將alwaysRequeueWithTxManagerRollback設置為false才能獲得新的(一致的)行為。

這是一個偵聽器容器屬性,因此您需要連接一個容器並將其注入入站通道適配器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM