private <P, R> Response<R> sendRequestWebSocket(Request<P> request, Class<R> resultClass) { log.info("Req-> {}", request.toString()); Future<Response<JsonElement>> responseFuture = null; if (request.getId() != null) { responseFuture = pendingRequests.prepareResponse(request.getId()); } try { synchronized (wsSession) { wsSession.sendMessage(new TextMessage(JsonUtils.toJson(request))); } } catch (Exception e) { throw new KurentoException( "Exception while sending message '" + JsonUtils.toJson(request) + "' to websocket with native sessionId '" + wsSession.getId() + "'", e); } if (responseFuture == null) { return null; } Response<JsonElement> responseJsonObject; try { responseJsonObject = responseFuture.get(TIMEOUT, TimeUnit.MILLISECONDS); log.info("<-Res {}", responseJsonObject.toString()); } catch (InterruptedException e) { // TODO What to do in this case? throw new JsonRpcException("Interrupted while waiting for a response", e); } catch (ExecutionException e) { // TODO Is there a better way to handle this? throw new JsonRpcException("This exception shouldn't be thrown", e); } catch (TimeoutException e) { throw new TransportException( "Timeout of " + TIMEOUT + " milliseconds waiting from response to request with id:" + request.getId(), e); } return MessageUtils.convertResponse(responseJsonObject, resultClass); }
@Override public void handleRequest(Transaction transaction, Request<JsonObject> request) throws Exception { if (demoBean == null) { throw new RuntimeException("Not autowired dependencies"); } log.debug("Request id:" + request.getId()); log.debug("Request method:" + request.getMethod()); log.debug("Request params:" + request.getParams()); transaction.sendResponse(request.getParams()); }