如何在 Spring Boot 中单独修改通道内每个接收器的每个代理消息?

How to modify each broker message for each receiver inside a channel individually in Spring Boot?

我正在向频道发送消息,但必须为每个客户端修改它。

谁有这方面的经验?

给你。

Spring 为传入和传出通道提供拦截器。 因此,只需添加一个拦截器,您就可以捕获每条传入和传出消息并执行任何您想做的事情。

首先你的配置:

...

@Autowired
private InboundMessagesChannelInterceptor inboundMessagesChannelInterceptor;

@Autowired
private OutboundMessagesChannelInterceptor outboundMessagesChannelInterceptor;

@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
    registration.interceptors(inboundMessagesChannelInterceptor);
}

@Override
public void configureClientOutboundChannel(ChannelRegistration registration) {
    registration.interceptors(outboundMessagesChannelInterceptor);
}

...

和你的拦截器:

@Component
public class OutboundMessagesChannelInterceptor implements ChannelInterceptor {

    @Override
    public Message<?> preSend(Message<?> message, MessageChannel channel) {
        // modify your message as needed
        return message;
    }

}

就是这样。