如何在Spring Boot中为通道内的每个接收者分别修改每个代理消息?

时间:2019-07-01 11:06:43

标签: java spring-boot rabbitmq spring-websocket messagebroker

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

有经验的人吗?

2 个答案:

答案 0 :(得分:0)

您可以创建一个@MessageMapping控制器,以自定义途中的发送消息。

@MessageMapping("/yourDestinationName")
public String customizeMessage(Message<String> message, Principal principal) {
    return "Principal: " + principal.getName() + ", Message: " + message.getPayload();
}

答案 1 :(得分:0)

你去了。

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;
    }

}

就这样。