简体   繁体   English

如何拦截Spring Cloud Stream消息?

[英]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.

相关问题 拦截Spring Cloud Stream SubscribableChannel的传入消息 - Intercept incoming message of Spring Cloud Stream SubscribableChannel 使用Spring Cloud Stream阅读主题中的所有消息 - Read all messages in topic with spring cloud stream Spring 云 Stream 处理消息后发送 - Spring Cloud Stream handling messages after sent Spring Cloud Stream Kafka生产者消息 - Spring Cloud Stream Kafka Producer messages Spring Cloud Stream RabbitMQ动态路由消息 - Spring cloud stream RabbitMQ routing messages dynamically Spring 云 stream 支持消息动态路由 - Spring cloud stream to support routing messages dynamically 如何从spring cloud stream app starter源生成的kafka消息中删除内容类型标头 - How to remove the content type header from kafka messages produced by the spring cloud stream app starter sources 如何在不弃用 class Checkpointer 的情况下使用 azure-spring-cloud-stream-binder-servicebus-queue 接受消息 - How to use azure-spring-cloud-stream-binder-servicebus-queue to accept messages without deprecated class Checkpointer 如何配置 DeadLetterPublisherRecoverer 以在 Spring Cloud Stream 批处理模式下将错误消息发送到 DLQ - How to configure DeadLetterPublisherRecoverer to send error messages to a DLQ in Spring Cloud Stream batch mode RabbitMQ(或Spring云流)可以独占消息吗? - Can RabbitMQ (or spring cloud stream) consume messages exclusively?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM