gpt4 book ai didi

Java Websocket/MessageHandler 返回全局范围?

转载 作者:行者123 更新时间:2023-12-01 09:13:59 25 4
gpt4 key购买 nike

我面临以下问题,但尚未找到有效的解决方案。我有 3 个不同的应用程序,它们应该相互通信:

  • 用户界面部分(1)
  • 后端应用程序(2)
  • “云端”微服务(3)

后端应用程序为 UI 提供 Web 服务 (REST),以从微服务获取信息或将信息放入微服务。我想从微服务中获取的所有内容都工作正常,但是:如果我想将数据放入微服务,规范需要 websocket 连接。这也可以正常工作,但是微服务在(不)成功的命令之后返回一条消息,例如

{"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 返回的控制台输出,但我不知道如何将其传递给父方法以进行返回,而不是仅返回 ResponseEntity.ok()

我还不太习惯 Java 中的 WebSocket 连接,所以请不要评判我;-)

感谢您的帮助。

最佳答案

下面的代码将在以下假设下工作:@OnMessage 方法在 WebSocket 客户端运行时管理的线程中执行。请检查运行 @OnMessage 方法的线程。

如果上述前提成立,则由全局范围内的线程执行的 putDataToMicroservice() 方法将等待,直到 WebSocket 响应到达 WS 客户端线程,该线程将重新传递消息到全局范围线程。然后 Controller 类中的执行将继续。

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

关于Java Websocket/MessageHandler 返回全局范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40714942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com