Lambda 在 Websocket 会话中不起作用
Lambda Does Not Work In Websocket Session
我刚遇到一个奇怪的小问题:
javax.websocket.Session session = //...
// this works
newSession.addMessageHandler(new MessageHandler.Whole<String>() {
@Override
public void onMessage(String message) {
MyWebSocket.this.onMessage(message);
}
});
// these don't work
newSession.addMessageHandler((MessageHandler.Whole<String>) MyWebSocket.this::onMessage);
newSession.addMessageHandler((MessageHandler.Whole<String>) message -> MyWebSocket.this.onMessage(message));
void onMessage(String message) {
System.out.println(message);
}
有人知道为什么 lambda 表达式在这种情况下不起作用吗?没有编译错误,没有异常,什么都没有。只是没有调用方法“'onMessage'”。
我使用 Java 1.8.0_65 和 Tyrus 参考实现 1.9.
根据我的理解,MessageHandler
需要是 @FunctionalInterface
才能在此处允许使用 lambda 表达式,但事实并非如此。
请参阅https://blogs.oracle.com/PavelBucek/entry/websocket_api_1_1_released
tldr;你必须使用 Session#addMessageHandler(Class<T> clazz, MessageHandler.Whole<T> handler)
/**
* Register to handle to incoming messages in this conversation. A maximum of one message handler per
* native websocket message type (text, binary, pong) may be added to each Session. I.e. a maximum
* of one message handler to handle incoming text messages a maximum of one message handler for
* handling incoming binary messages, and a maximum of one for handling incoming pong
* messages. For further details of which message handlers handle which of the native websocket
* message types please see {@link MessageHandler.Whole} and {@link MessageHandler.Partial}.
* Adding more than one of any one type will result in a runtime exception.
*
* @param clazz type of the message processed by message handler to be registered.
* @param handler whole message handler to be added.
* @throws IllegalStateException if there is already a MessageHandler registered for the same native
* websocket message type as this handler.
*/
public void addMessageHandler(Class<T> clazz, MessageHandler.Whole<T> handler);
为了使用 lambda 作为消息处理程序。
我刚遇到一个奇怪的小问题:
javax.websocket.Session session = //...
// this works
newSession.addMessageHandler(new MessageHandler.Whole<String>() {
@Override
public void onMessage(String message) {
MyWebSocket.this.onMessage(message);
}
});
// these don't work
newSession.addMessageHandler((MessageHandler.Whole<String>) MyWebSocket.this::onMessage);
newSession.addMessageHandler((MessageHandler.Whole<String>) message -> MyWebSocket.this.onMessage(message));
void onMessage(String message) {
System.out.println(message);
}
有人知道为什么 lambda 表达式在这种情况下不起作用吗?没有编译错误,没有异常,什么都没有。只是没有调用方法“'onMessage'”。
我使用 Java 1.8.0_65 和 Tyrus 参考实现 1.9.
根据我的理解,MessageHandler
需要是 @FunctionalInterface
才能在此处允许使用 lambda 表达式,但事实并非如此。
请参阅https://blogs.oracle.com/PavelBucek/entry/websocket_api_1_1_released
tldr;你必须使用 Session#addMessageHandler(Class<T> clazz, MessageHandler.Whole<T> handler)
/**
* Register to handle to incoming messages in this conversation. A maximum of one message handler per
* native websocket message type (text, binary, pong) may be added to each Session. I.e. a maximum
* of one message handler to handle incoming text messages a maximum of one message handler for
* handling incoming binary messages, and a maximum of one for handling incoming pong
* messages. For further details of which message handlers handle which of the native websocket
* message types please see {@link MessageHandler.Whole} and {@link MessageHandler.Partial}.
* Adding more than one of any one type will result in a runtime exception.
*
* @param clazz type of the message processed by message handler to be registered.
* @param handler whole message handler to be added.
* @throws IllegalStateException if there is already a MessageHandler registered for the same native
* websocket message type as this handler.
*/
public void addMessageHandler(Class<T> clazz, MessageHandler.Whole<T> handler);
为了使用 lambda 作为消息处理程序。