简体   繁体   English

防止AMQP队列使用者中的无限循环

[英]Prevent Infinite loop in AMQP queue consumer

I want to implement web flux client inside AMQP queue listener. 我想在AMQP队列监听器中实现Web流量客户端。 I tried this: 我尝试了这个:

@Component
public class TransactionGenesisAuthorizeListener {

    public TransactionResponseFactory transactionGenesisAuthorizeProcess(AuthorizeRequestFactory tf) throws Exception {

        AuthorizeResponse response = null;              

        try {           
                RestClient client = RestClientBuilder.builder()
                        .gatewayUrl(URL)
                        .build();

                Mono<AuthorizeResponse> result = client.executeAndReceiveAuthorize(request);
                response = result.block();

        return parseRawSuccessResponse(response);
    }

    private TransactionResponseFactory parseRawSuccessResponse(AuthorizeResponse response) {
        ................

        return obj;
    }

}

// web client //网络客户端

public Mono<AuthorizeResponse> executeAndReceiveAuthorize(AuthorizeRequest transaction) {
        Mono<AuthorizeRequest> transactionMono = Mono.just(transaction);
        return client.post().uri(checkTrailingSlash(gatewayUrl) + token)
                .header(HttpHeaders.USER_AGENT, "Mozilla/5.0")
                .accept(MediaType.APPLICATION_XML)
                .contentType(MediaType.APPLICATION_XML)
                .body(transactionMono, AuthorizeRequest.class)
                .retrieve()
                .bodyToMono(AuthorizeResponse.class);
    }

Error log 错误日志

2019-08-27 19:42:09,280 INFO  [stdout] (processingTransactionGenesisAuthorizeContainer-1) 19:42:09.280 [processingTransactionGenesisAuthorizeContainer-1] WARN  o.s.a.r.l.SimpleMessageListenerContainer - Consumer raised exception, processing can restart if the connection factory supports it. Exception summary: org.springframework.amqp.rabbit.support.ConsumerCancelledException
2019-08-27 19:42:09,282 INFO  [stdout] (processingTransactionGenesisAuthorizeContainer-1) 19:42:09.282 [processingTransactionGenesisAuthorizeContainer-1] INFO  o.s.a.r.l.SimpleMessageListenerContainer - Restarting Consumer@6e537459: tags=[[]], channel=Cached Rabbit Channel: AMQChannel(amqp://guest@127.0.0.1:5672/,10), conn: Proxy@70156185 Shared Rabbit Connection: SimpleConnection@631ca718 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 49982], acknowledgeMode=AUTO local queue size=0

But unfortunately when I use wrong credentials for the connection of the web client I get infinite error message probably because AMQP client is resending the payload. 但是不幸的是,当我使用错误的凭据来连接Web客户端时,收到无限错误消息,可能是因为AMQP客户端正在重新发送有效负载。 Is there some way to trow exception and to stop re-sending the queue content and and again? 是否有某种方法引发异常并停止反复发送队列内容?

Consider to catch an exception and wrap it into the AmqpRejectAndDontRequeueException : 考虑捕获异常并将其包装到AmqpRejectAndDontRequeueException

/**
 * Exception for listener implementations used to indicate the
 * basic.reject will be sent with requeue=false in order to enable
 * features such as DLQ.
 * @author Gary Russell
 * @since 1.0.1
 *
 */
@SuppressWarnings("serial")
public class AmqpRejectAndDontRequeueException extends AmqpException {

Also see some use-case in Docs in regards for this exception, as well as retry configuration: https://docs.spring.io/spring-amqp/docs/current/reference/html/#async-listeners 另请参阅文档中有关此异常的一些用例以及重试配置: https : //docs.spring.io/spring-amqp/docs/current/reference/html/#async-listeners

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM