[英]How to intercept Spring Cloud Stream messages?
Spring allows interception of messages for many of their products, like RestTemplate and SpringMVC. Spring允许拦截许多产品的消息,比如RestTemplate和SpringMVC。 Is it possible to intercept Spring Cloud Stream messages?
是否可以拦截Spring Cloud Stream消息? For both incoming and outgoing messages.
对于传入和传出消息。
Was able to intercept inbound and outbound Spring Cloud Stream messages using the GlobalChannelInterceptor
annotation and ChannelInterceptor
interface. 能够使用
GlobalChannelInterceptor
注释和ChannelInterceptor
接口拦截入站和出站Spring Cloud Stream消息。 See sample below. 见下面的示例。
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");
}
}
Not sure what you mean by interception here - both examples you give are not message-based :). 不确定你在这里截取的意思 - 你给出的两个例子都不是基于消息的:)。
But you want to get access to the full message, you can use that as argument to a @StreamListener
or @ServiceActivator
-annotated method. 但是您希望能够访问完整的消息,您可以将其用作
@StreamListener
或@ServiceActivator
@StreamListener
方法的参数。 Also, Spring Cloud Stream allows you to set up a full Spring Integration pipeline, so you can add advices and everything you need - see here: https://github.com/spring-projects/spring-integration-java-dsl/wiki/Spring-Integration-Java-DSL-Reference . 此外,Spring Cloud Stream允许您设置完整的Spring Integration管道,因此您可以添加建议和所需的一切 - 请参阅此处: https : //github.com/spring-projects/spring-integration-java-dsl/wiki / Spring-Integration-Java-DSL-Reference 。
I would encourage you to take a look at the Spring Integration reference as well http://docs.spring.io/autorepo/docs/spring-integration/4.2.6.RELEASE/reference/html/ . 我建议您查看Spring Integration参考以及http://docs.spring.io/autorepo/docs/spring-integration/4.2.6.RELEASE/reference/html/ 。 Spring Cloud Stream injects the channels automatically, and from there you have full freedom on how you construct your pipeline.
Spring Cloud Stream会自动注入通道,从那里您可以完全自由地构建管道。
Hope this helps, Marius 希望这有帮助,马吕斯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.