簡體   English   中英

僅攔截來自使用者的Spring Cloud Stream消息

[英]Intercepting Spring Cloud Stream Messages from Consumer only

我目前正在使用帶有Kafka GlobalChannelInterceptorGlobalChannelInterceptor Spring Cloud Stream來為Spring Boot微服務執行消息記錄。

我有:

  1. 生產者將消息發布到SubscribableChannel
  2. 使用者從Stream偵聽(使用@StreamListener批注)

在從生產者向流發布消息並讓消費者監聽消息的整個過程中,可以看到preSend方法被觸發了兩次:

  1. 一旦在生產者端-消息發布到流時
  2. 一次在用戶端-從流中偵聽消息時

但是,出於我的日志記錄目的,我只需要在用戶端截獲並記錄該消息。

有沒有辦法僅在一側(例如,消費者側)攔截SCS消息?

我對此事有任何想法。 謝謝!

參考:

  1. GlobalChannelInterceptor文檔- https://docs.spring.io/spring-integration/api/org/springframework/integration/config/GlobalChannelInterceptor.html

編輯

制片人

public void sendToPushStream(PushStreamMessage message) {
        try {
            boolean results = streamChannel.pushStream().send(MessageBuilder.withPayload(new ObjectMapper().writeValueAsString(message)).build());
        log.info("Push stream message {} sent to {}.", results ? "successfully" : "not", StreamChannel.PUSH_STREAM);
        } catch (JsonProcessingException ex) {
            log.error("Unable to parse push stream message.", ex);
        }
    }

生產者的streamChannel

public interface StreamChannel {

    String PUSH_STREAM = "PushStream";

    @Output(StreamChannel.PUSH_STREAM)
    SubscribableChannel pushStream();

}

消費者

@StreamListener(StreamChannel.PUSH_STREAM)
public void handle(Message<PushStreamMessage> message) {
    log.info("Incoming stream message from {}, {}", streamChannel.pushStream(), message);

}

消費者的streamChannel

public interface StreamChannel {

    String PUSH_STREAM = "PushStream";

    @Input(StreamChannel.PUSH_STREAM)
    SubscribableChannel pushStream();

}

攔截器(公共庫)

public class GlobalStreamInterceptor extends ChannelInterceptorAdapter {

    @Override
    public Message<?> preSend(Message<?> msg, MessageChannel mc) {
       log.info("presend " + msg);
        return msg;
    }

    @Override
    public void postSend(Message<?> msg, MessageChannel mc, boolean sent) {
        log.info("postSend " + msg);
    }

}

是的,為什么不遵循GlobalChannelInterceptor選項並且不適用

通道名稱將與之匹配的一組簡單模式。

因此,您可能會遇到以下情況:

@GlobalChannelInterceptor(patterns = Processor.INPUT)

或使用SCSt應用程序的輸入通道的自定義名稱。

暫無
暫無

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

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