Java Websocket / MessageHandler return 到全局范围?
Java Websocket / MessageHandler return to global scope?
我遇到了以下问题,但我还没有找到可行的解决方案。
我有 3 个不同的应用程序应该相互通信:
- UI 部分 (1)
- 后台应用(二)
- 微服务"in the cloud" (3)
后端应用程序为 UI 提供 Web 服务 (REST),以获取和放置信息 from/to 微服务。
我想从微服务中获取的一切都可以正常工作,但是:
如果我想将数据放入微服务,规范需要一个 websocket 连接。这也可以正常工作,但是微服务 return 在(未)成功的命令之后发送一条消息,例如
{"statusCode":200,"messageId":"1234567890"}
现在的问题是:如何在我的应用程序中获取此消息并将其发送回 UI,以便用户知道命令是否成功?
目前我试过这个:
WebSocketClient.java
@OnMessage
public void onMessage(Session session, String msg) {
if (this.messageHandler != null) {
this.messageHandler.handleMessage(msg);
}
}
public void addMessageHandler(MessageHandler msgHandler) {
this.messageHandler = msgHandler;
}
public static interface MessageHandler {
public String handleMessage(String message);
}
MyTotalAwesomeController.java
public class MyTotalAwesomeController {
WebSocketClient wsc = new WebSocketClient();
...
@RequestMapping(value="/add", method={RequestMethod.POST, RequestMethod.OPTIONS})
public ResponseEntity<Object> putDataToMicroservice(@RequestBody Map<String, Object> payload, @RequestHeader(value = "authorization") String authorizationHeader) throws Exception {
...
wsc.addMessageHandler(new WebSocketClient.MessageHandler() {
public String handleMessage(String message) {
System.out.println("RETURN MSG FROM WSS : " + message);
return message;
}
});
return ResponseEntity.ok("worked");
}
我可以看到 MessageHandler return 的控制台输出,但我不知道如何将它传递给 return 的父方法而不是 returning ResponseEntity.ok()
.
我还不太习惯 Java 中的 WebSocket 连接,所以请不要评判我 ;-)
感谢您的帮助。
下面的代码将假设 @OnMessage
方法在 WebSocket 客户端运行时管理的线程中执行。请检查运行 @OnMessage
方法的线程。
如果上述前提为真,putDataToMicroservice()
方法,由全局范围内的线程执行,会一直等到WebSocket响应到达WS客户端线程,WS客户端线程会将消息重新传递给全局范围线程。然后在你的控制器中执行 class 将继续。
public class MyTotalAwesomeController {
WebSocketClient wsc = new WebSocketClient();
// Queue for communication between threads.
private BlockingQueue<String> queue;
@PostConstruct
void init() {
queue = new SynchronousQueue<>(true);
// This callback will be invoked by the WebSocket thread.
wsc.addMessageHandler(new WebSocketClient.MessageHandler() {
@Override
public String handleMessage(String message) {
System.out.println("RETURN MSG FROM WSS : " + message);
// Pass message to the controller thread.
queue.put(message);
// Note that the return value is not necessary.
// You can take it out of the interface as well.
return null;
}
});
}
@RequestMapping(value="/add", method={RequestMethod.POST, RequestMethod.OPTIONS})
public ResponseEntity<Object> putDataToMicroservice(@RequestBody Map<String, Object> payload, @RequestHeader(value = "authorization") String authorizationHeader) throws Exception {
// At this point you make a WebSocket request, is that right?
doWebSocketRequest();
// This poll call will block the current thread
// until the WebSocket server responds,
// or gives up waiting after the specified timeout.
//
// When the WebSocket server delivers a response,
// the WS client implementation will execute the
// @OnMessage annotated method in a thread
// managed by the WS client itself.
//
// The @OnMessage method will pass the message
// to this thread in the queue below.
String message = queue.poll(30, TimeUnit.SECONDS);
if (message == null) {
// WebSocket timeout.
}
return ResponseEntity.ok("worked");
}
}
我遇到了以下问题,但我还没有找到可行的解决方案。 我有 3 个不同的应用程序应该相互通信:
- UI 部分 (1)
- 后台应用(二)
- 微服务"in the cloud" (3)
后端应用程序为 UI 提供 Web 服务 (REST),以获取和放置信息 from/to 微服务。 我想从微服务中获取的一切都可以正常工作,但是: 如果我想将数据放入微服务,规范需要一个 websocket 连接。这也可以正常工作,但是微服务 return 在(未)成功的命令之后发送一条消息,例如
{"statusCode":200,"messageId":"1234567890"}
现在的问题是:如何在我的应用程序中获取此消息并将其发送回 UI,以便用户知道命令是否成功?
目前我试过这个:
WebSocketClient.java
@OnMessage
public void onMessage(Session session, String msg) {
if (this.messageHandler != null) {
this.messageHandler.handleMessage(msg);
}
}
public void addMessageHandler(MessageHandler msgHandler) {
this.messageHandler = msgHandler;
}
public static interface MessageHandler {
public String handleMessage(String message);
}
MyTotalAwesomeController.java
public class MyTotalAwesomeController {
WebSocketClient wsc = new WebSocketClient();
...
@RequestMapping(value="/add", method={RequestMethod.POST, RequestMethod.OPTIONS})
public ResponseEntity<Object> putDataToMicroservice(@RequestBody Map<String, Object> payload, @RequestHeader(value = "authorization") String authorizationHeader) throws Exception {
...
wsc.addMessageHandler(new WebSocketClient.MessageHandler() {
public String handleMessage(String message) {
System.out.println("RETURN MSG FROM WSS : " + message);
return message;
}
});
return ResponseEntity.ok("worked");
}
我可以看到 MessageHandler return 的控制台输出,但我不知道如何将它传递给 return 的父方法而不是 returning ResponseEntity.ok()
.
我还不太习惯 Java 中的 WebSocket 连接,所以请不要评判我 ;-)
感谢您的帮助。
下面的代码将假设 @OnMessage
方法在 WebSocket 客户端运行时管理的线程中执行。请检查运行 @OnMessage
方法的线程。
如果上述前提为真,putDataToMicroservice()
方法,由全局范围内的线程执行,会一直等到WebSocket响应到达WS客户端线程,WS客户端线程会将消息重新传递给全局范围线程。然后在你的控制器中执行 class 将继续。
public class MyTotalAwesomeController {
WebSocketClient wsc = new WebSocketClient();
// Queue for communication between threads.
private BlockingQueue<String> queue;
@PostConstruct
void init() {
queue = new SynchronousQueue<>(true);
// This callback will be invoked by the WebSocket thread.
wsc.addMessageHandler(new WebSocketClient.MessageHandler() {
@Override
public String handleMessage(String message) {
System.out.println("RETURN MSG FROM WSS : " + message);
// Pass message to the controller thread.
queue.put(message);
// Note that the return value is not necessary.
// You can take it out of the interface as well.
return null;
}
});
}
@RequestMapping(value="/add", method={RequestMethod.POST, RequestMethod.OPTIONS})
public ResponseEntity<Object> putDataToMicroservice(@RequestBody Map<String, Object> payload, @RequestHeader(value = "authorization") String authorizationHeader) throws Exception {
// At this point you make a WebSocket request, is that right?
doWebSocketRequest();
// This poll call will block the current thread
// until the WebSocket server responds,
// or gives up waiting after the specified timeout.
//
// When the WebSocket server delivers a response,
// the WS client implementation will execute the
// @OnMessage annotated method in a thread
// managed by the WS client itself.
//
// The @OnMessage method will pass the message
// to this thread in the queue below.
String message = queue.poll(30, TimeUnit.SECONDS);
if (message == null) {
// WebSocket timeout.
}
return ResponseEntity.ok("worked");
}
}