gpt4 book ai didi

java - 如何拦截Spring Cloud Stream消息?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:53:49 42 4
gpt4 key购买 nike

Spring 允许拦截其许多产品的消息,例如 RestTemplate 和 SpringMVC。是否可以拦截 Spring Cloud Stream 消息?对于传入和传出消息。

最佳答案

能够使用 GlobalChannelInterceptor 注释和 ChannelInterceptor 接口(interface)拦截入站和出站 Spring Cloud Stream 消息。请参见下面的示例。

import org.springframework.integration.config.GlobalChannelInterceptor;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.stereotype.Component;

@Component
@GlobalChannelInterceptor
public class Interceptor implements ChannelInterceptor {

private final Logger log = LoggerFactory.getLogger(Interceptor.class);

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

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

@Override
public void afterSendCompletion(Message<?> msg, MessageChannel mc, boolean bln, Exception excptn) {
log.info("In afterSendCompletion");
}

@Override
public boolean preReceive(MessageChannel mc) {
log.info("In preReceive");
return true;
}

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

@Override
public void afterReceiveCompletion(Message<?> msg, MessageChannel mc, Exception excptn) {
log.info("In afterReceiveCompletion");
}

}

关于java - 如何拦截Spring Cloud Stream消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37450293/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com