Failed to publish TcpConnectionOpenEvent 或 TcpConnectionOpenEvent, Dispatcher failed to deliver Message

Failed to publish TcpConnectionOpenEvent Or TcpConnectionOpenEvent, Dispatcher failed to deliver Message

在 spring 集成中,我使用事件侦听器来处理 TcpConnectionOpenEvent 或 TcpConnectionCloseEvent。

当我使用下面的代码博客时,我可以处理连接打开关闭应用程序事件,但我收到类似

的警告日志

2020-08-11 11:15:46.857 WARN 7104 --- [ XNIO-1 task-1] o.s.i.i.tcp.connection.TcpNetConnection : Failed to publish TcpConnectionOpenEvent [SOME DETAIL] OPENED:Dispatcher failed to deliver Message; nested exception is java.lang.NullPointerException

@Bean
public MessageChannel connectionStatusChannel() {
    DirectChannel directChannel = new DirectChannel();
    directChannel.subscribe(new ServerConnectionStatusHandler());
    return directChannel;
}

@EventListener
public void listen(TcpConnectionOpenEvent event) {
    String eventName = "TcpConnectionOpenEvent";
    String destination = event.getConnectionFactoryName();
    connectionStatusChannel().send(new GenericMessage<>(eventName + "-" + destination));
}

@Component
public class ServerConnectionStatusHandler implements MessageHandler {

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

    @Autowired
    private SimpMessageSendingOperations messagingTemplate;

    public ServerConnectionStatusHandler() {
    }

    @Override
    public void handleMessage(Message<?> message) throws MessagingException {
        log.debug("ServerConnectionStatusHandler # handleMessage message : {} ", message.getPayload());
        messagingTemplate.convertAndSend("/topic/agent/connection/tracker", message.getPayload());
    }
}

这不会中断我的流程,但我想了解我为什么要接受此警告以及如何清除它。没有@EventListener 它就会消失...

感谢您的帮助

在我看来 listen(...) event.getConnectionFactoryName() 为 null,或者 connectionStatusHandler() 可能返回 null

如果您想访问应用程序上下文中的某些 bean,则不能说 new

我的意思是:

directChannel.subscribe(new ServerConnectionStatusHandler());

因此,如果您的 class,您将创建一个 non-managed 实例。同时,您需要自动连接现有的 bean:

@Bean
public MessageChannel connectionStatusChannel(ServerConnectionStatusHandler handler) {
    DirectChannel directChannel = new DirectChannel();
    directChannel.subscribe(handler);
    return directChannel;
}