0

我面临以下问题,但我还没有找到可行的解决方案。我有 3 个不同的应用程序应该相互通信:

  • UI部分(一)
  • 后端应用程序 (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 返回的控制台输出,但我不知道如何将其传递给父方法以返回 insted,而不是仅返回ResponseEntity.ok().

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

谢谢您的帮助。

4

1 回答 1

1

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

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

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");
    }
}
于 2016-11-21T09:05:51.797 回答