2016-11-21 24 views
0

我正面临以下问题,我发现还没有工作解决方案。 我有应该彼此通信3个不同的应用:Java Websocket/MessageHandler返回全局范围?

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

后端应用程序为UI提供了Web服务(REST),以便从微服务中获取信息并将信息放入微服务。 我想从微服务中抓取的所有东西都可以正常工作,但是: 如果我想将数据放到微服务中,则需要Websocket连接。这工作也没关系,但微服务的(未)成功命令后返回的消息,像

{"statusCode":200,"messageId":"1234567890"} 

现在的问题是:我怎么能在我的应用程序抓住这个消息,并发送回用户界面,所以用户知道命令是否成功?

对于我尝试这样的时刻:

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的回报控制台输出,但我不知道如何将其传递给ret的父级方法瓮只是返回ResponseEntity.ok()

我不是很习惯在Java的WebSocket连接还没有,所以请不要对我做出判断;-)

谢谢您的帮助。

+0

您的控制器是否有init方法? – nandsito

+0

它有'@ PostContruct'注释。 – sebastian

回答

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

你能解释一下吗?等待你的更新答案。 – sebastian

+0

@sebastian现在我更了解这个问题,它比我以前想象的要复杂一点。这将需要一些线程同步。您是否使用tyrus作为websocket实现? – nandsito

+0

不,我只使用javax.websocket。*。微服务不是由我开发的,所以我无法定义服务器端使用的是什么。 – sebastian