javax.websocket.SendResult Java Examples
The following examples show how to use
javax.websocket.SendResult.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MessageSender.java From everrest with Eclipse Public License 2.0 | 6 votes |
@Override public void onResult(SendResult result) { LOG.debug(" SendQueue size {} , maxNumberOfMessageInQueue {} result {}", sendQueue.size(), maxNumberOfMessageInQueue, result.isOK()); if (!result.isOK()) { try { session.close(); } catch (IOException ignored) { } finally { sendQueue.clear(); } } synchronized (lock) { if (sendQueue.isEmpty()) { sendingInProgress = false; } else { MessageWrapper message = sendQueue.remove(); doSend(message); } } }
Example #2
Source File: WsSession.java From Tomcat7.0.67 with Apache License 2.0 | 6 votes |
/** * Make the session aware of a {@link FutureToSendHandler} that will need to * be forcibly closed if the session closes before the * {@link FutureToSendHandler} completes. */ protected void registerFuture(FutureToSendHandler f2sh) { boolean fail = false; synchronized (stateLock) { // If the session has already been closed the any registered futures // will have been processed so the failure result for this future // needs to be set here. if (state == State.OPEN) { futures.put(f2sh, f2sh); } else { // Construct the exception outside of the sync block fail = true; } } if (fail) { IOException ioe = new IOException(sm.getString("wsSession.messageFailed")); SendResult sr = new SendResult(ioe); f2sh.onResult(sr); } }
Example #3
Source File: WebsocketStressTestCase.java From quarkus-http with Apache License 2.0 | 6 votes |
@Override public void run() { session.getAsyncRemote().sendText("t-" + thread + "-m-" + count.get(), new SendHandler() { @Override public void onResult(SendResult result) { if (!result.isOK()) { try { result.getException().printStackTrace(); session.close(); } catch (IOException e) { throw new RuntimeException(e); } } if (count.incrementAndGet() != NUM_REQUESTS) { executor.submit(SendRunnable.this); } else { executor.submit(new Runnable() { @Override public void run() { session.getAsyncRemote().sendText("close"); } }); } } }); }
Example #4
Source File: WebSocketSessionRemoteEndpoint.java From quarkus-http with Apache License 2.0 | 6 votes |
private void sendObjectImpl(final Object o, final SendHandler callback) { try { if (o instanceof String) { sendText((String) o, callback); } else if (o instanceof byte[]) { sendBinary(ByteBuffer.wrap((byte[]) o), callback); } else if (o instanceof ByteBuffer) { sendBinary((ByteBuffer) o, callback); } else if (encoding.canEncodeText(o.getClass())) { sendText(encoding.encodeText(o), callback); } else if (encoding.canEncodeBinary(o.getClass())) { sendBinary(encoding.encodeBinary(o), callback); } else { // TODO: Replace on bug is fixed // https://issues.jboss.org/browse/LOGTOOL-64 throw new EncodeException(o, "No suitable encoder found"); } } catch (Exception e) { callback.onResult(new SendResult(e)); } }
Example #5
Source File: TestWsSessionSuspendResume.java From Tomcat8-Source-Read with MIT License | 6 votes |
void addMessage(String message) { if (messages.size() == count) { ((WsSession) session).suspend(); session.getAsyncRemote().sendText(messages.toString(), new SendHandler() { @Override public void onResult(SendResult result) { ((WsSession) session).resume(); Assert.assertTrue(result.isOK()); } }); messages.clear(); } else { messages.add(message); } }
Example #6
Source File: WsSession.java From tomcatsrc with Apache License 2.0 | 6 votes |
/** * Make the session aware of a {@link FutureToSendHandler} that will need to * be forcibly closed if the session closes before the * {@link FutureToSendHandler} completes. */ protected void registerFuture(FutureToSendHandler f2sh) { boolean fail = false; synchronized (stateLock) { // If the session has already been closed the any registered futures // will have been processed so the failure result for this future // needs to be set here. if (state == State.OPEN || f2sh.isCloseMessage()) { // WebSocket session is open or this is the close message futures.put(f2sh, f2sh); } else if (f2sh.isDone()) { // NO-OP. The future completed before the session closed so no // need to register in case the session closes before it // completes. } else { // Construct the exception outside of the sync block fail = true; } } if (fail) { IOException ioe = new IOException(sm.getString("wsSession.messageFailed")); SendResult sr = new SendResult(ioe); f2sh.onResult(sr); } }
Example #7
Source File: SessionBasedAttachedClient.java From pnc with Apache License 2.0 | 6 votes |
@Override public void sendMessage(Object messageBody, MessageCallback callback) { String message; try { message = mapperProvider.getMapper().writeValueAsString(messageBody); } catch (JsonProcessingException e) { throw new IllegalArgumentException("Could not convert object to JSON", e); } session.getAsyncRemote().sendText(message, new SendHandler() { @Override public void onResult(SendResult sendResult) { if (!sendResult.isOK()) { callback.failed(SessionBasedAttachedClient.this, sendResult.getException()); } else { callback.successful(SessionBasedAttachedClient.this); } } }); }
Example #8
Source File: JmsProducerService.java From onetwo with Apache License 2.0 | 6 votes |
@Override public Object sendMessage(JmsMessageCreator jmsMessage, InterceptorPredicate interPredicate) { final InterceptorPredicate interceptorPredicate = interPredicate==null?SendMessageFlags.Default:interPredicate; Destination dest = jmsDestinationConverter.getDestination(jmsMessage); Assert.notNull(dest, "jms Destination can not be null"); return interceptableMessageSender.sendIntercetableMessage(interPredicate, messageInterceptors->{ SendMessageInterceptorChain chain = new SendMessageInterceptorChain(messageInterceptors, interceptorPredicate, ()->{ this.jmsMessagingTemplate.convertAndSend(dest, jmsMessage.getJmsMessage()); return null; }); SendMessageContext<Serializable> ctx = SendMessageContext.<Serializable>newBuilder() .message(jmsMessage) .chain(chain) .debug(false) .threadId(Thread.currentThread().getId()) .build(); chain.setSendMessageContext(ctx); chain.setDebug(ctx.isDebug()); return (SendResult)chain.invoke(); }); }
Example #9
Source File: RemoteP2PConnection.java From jReto with MIT License | 6 votes |
@Override public void writeData(ByteBuffer data) { if (!this.isConnected()) { System.err.println("attempted to write before connection is open."); return; } new Thread(() -> this.dataSession.getAsyncRemote().sendBinary(data, new SendHandler() { @Override public void onResult(SendResult arg0) { RemoteP2PConnection.this.executor.execute(new Runnable() { @Override public void run() { if (RemoteP2PConnection.this.handler != null) RemoteP2PConnection.this.handler.onDataSent(RemoteP2PConnection.this); } }); } })).start(); }
Example #10
Source File: Client.java From Tomcat8-Source-Read with MIT License | 5 votes |
@Override public void onResult(SendResult result) { if (!result.isOK()) { // Message could not be sent. In this case, we don't // set isSendingMessage to false because we must assume the connection // broke (and onClose will be called), so we don't try to send // other messages. // As a precaution, we close the session (e.g. if a send timeout occurred). // TODO: session.close() blocks, while this handler shouldn't block. // Ideally, there should be some abort() method that cancels the // connection immediately... try { session.close(); } catch (IOException ex) { // Ignore } } synchronized (messagesToSend) { if (!messagesToSend.isEmpty()) { AbstractWebsocketMessage msg = messagesToSend.remove(); messagesToSendLength -= calculateMessageLength(msg); internalSendMessageAsync(msg); } else { isSendingMessage = false; } } }
Example #11
Source File: Client.java From tomcatsrc with Apache License 2.0 | 5 votes |
@Override public void onResult(SendResult result) { if (!result.isOK()) { // Message could not be sent. In this case, we don't // set isSendingMessage to false because we must assume the connection // broke (and onClose will be called), so we don't try to send // other messages. // As a precaution, we close the session (e.g. if a send timeout occured). // TODO: session.close() blocks, while this handler shouldn't block. // Ideally, there should be some abort() method that cancels the // connection immediately... try { session.close(); } catch (IOException ex) { // Ignore } } synchronized (messagesToSend) { if (!messagesToSend.isEmpty()) { AbstractWebsocketMessage msg = messagesToSend.remove(); messagesToSendLength -= calculateMessageLength(msg); internalSendMessageAsync(msg); } else { isSendingMessage = false; } } }
Example #12
Source File: WsRemoteEndpointImplBase.java From Tomcat7.0.67 with Apache License 2.0 | 5 votes |
void endMessage(SendHandler handler, SendResult result) { boolean doWrite = false; MessagePart mpNext = null; synchronized (messagePartLock) { fragmented = nextFragmented; text = nextText; mpNext = messagePartQueue.poll(); if (mpNext == null) { messagePartInProgress = false; } else if (!closed){ // Session may have been closed unexpectedly in the middle of // sending a fragmented message closing the endpoint. If this // happens, clearly there is no point trying to send the rest of // the message. doWrite = true; } } if (doWrite) { // Actual write has to be outside sync block to avoid possible // deadlock between messagePartLock and writeLock in // o.a.coyote.http11.upgrade.AbstractServletOutputStream writeMessagePart(mpNext); } wsSession.updateLastActive(); // Some handlers, such as the IntermediateMessageHandler, do not have a // nested handler so handler may be null. if (handler != null) { handler.onResult(result); } }
Example #13
Source File: WsSession.java From Tomcat8-Source-Read with MIT License | 5 votes |
/** * WebSocket 1.0. Section 2.1.5. * Need internal close method as spec requires that the local endpoint * receives a 1006 on timeout. * * @param closeReasonMessage The close reason to pass to the remote endpoint * @param closeReasonLocal The close reason to pass to the local endpoint * @param closeSocket Should the socket be closed immediately rather than waiting * for the server to respond */ public void doClose(CloseReason closeReasonMessage, CloseReason closeReasonLocal, boolean closeSocket) { // Double-checked locking. OK because state is volatile if (state != State.OPEN) { return; } synchronized (stateLock) { if (state != State.OPEN) { return; } if (log.isDebugEnabled()) { log.debug(sm.getString("wsSession.doClose", id)); } try { wsRemoteEndpoint.setBatchingAllowed(false); } catch (IOException e) { log.warn(sm.getString("wsSession.flushFailOnClose"), e); fireEndpointOnError(e); } state = State.OUTPUT_CLOSED; sendCloseMessage(closeReasonMessage); if (closeSocket) { wsRemoteEndpoint.close(); } fireEndpointOnClose(closeReasonLocal); } IOException ioe = new IOException(sm.getString("wsSession.messageFailed")); SendResult sr = new SendResult(ioe); for (FutureToSendHandler f2sh : futures.keySet()) { f2sh.onResult(sr); } }
Example #14
Source File: WsRemoteEndpointImplBase.java From Tomcat7.0.67 with Apache License 2.0 | 5 votes |
@Override public void onResult(SendResult result) { if (isDone) { endpoint.stateMachine.complete(isLast); handler.onResult(result); } else if(!result.isOK()) { handler.onResult(result); } else if (closed){ SendResult sr = new SendResult(new IOException( sm.getString("wsRemoteEndpoint.closedDuringMessage"))); handler.onResult(sr); } else { write(); } }
Example #15
Source File: WsRemoteEndpointImplBase.java From Tomcat7.0.67 with Apache License 2.0 | 5 votes |
@Override public void onResult(SendResult result) { if (result.isOK()) { if (outputBuffer.hasRemaining()) { endpoint.doWrite(this, outputBuffer); } else { outputBuffer.clear(); write(); } } else { handler.onResult(result); } }
Example #16
Source File: BinaryFrameOutputStream.java From websocket-classloader with Apache License 2.0 | 5 votes |
@Override public void flush() { if (buffer.position() > 0) { buffer.flip(); remote.sendBinary(buffer, new SendHandler() { @Override public void onResult(SendResult sendResult) { if (!sendResult.isOK()) { logger.error("Failed to send messages.", sendResult.getException()); } } }); } buffer.clear(); }
Example #17
Source File: WsRemoteEndpointImplBase.java From Tomcat7.0.67 with Apache License 2.0 | 5 votes |
@Override public void onResult(SendResult result) { if (result.isOK()) { outputBuffer.clear(); } handler.onResult(result); }
Example #18
Source File: WsRemoteEndpointImplBase.java From Tomcat7.0.67 with Apache License 2.0 | 5 votes |
@Override public void onResult(SendResult result) { if (result.isOK()) { stateMachine.complete(true); } handler.onResult(result); }
Example #19
Source File: WsRemoteEndpointImplServer.java From Tomcat7.0.67 with Apache License 2.0 | 5 votes |
/** * * @param t The throwable associated with any error that * occurred * @param useDispatch Should {@link SendHandler#onResult(SendResult)} be * called from a new thread, keeping in mind the * requirements of * {@link javax.websocket.RemoteEndpoint.Async} */ private void clearHandler(Throwable t, boolean useDispatch) { // Setting the result marks this (partial) message as // complete which means the next one may be sent which // could update the value of the handler. Therefore, keep a // local copy before signalling the end of the (partial) // message. SendHandler sh = handler; handler = null; buffers = null; if (sh != null) { if (useDispatch) { OnResultRunnable r = onResultRunnables.poll(); if (r == null) { r = new OnResultRunnable(onResultRunnables); } r.init(sh, t); if (executorService == null || executorService.isShutdown()) { // Can't use the executor so call the runnable directly. // This may not be strictly specification compliant in all // cases but during shutdown only close messages are going // to be sent so there should not be the issue of nested // calls leading to stack overflow as described in bug // 55715. The issues with nested calls was the reason for // the separate thread requirement in the specification. r.run(); } else { executorService.execute(r); } } else { if (t == null) { sh.onResult(new SendResult()); } else { sh.onResult(new SendResult(t)); } } } }
Example #20
Source File: WsRemoteEndpointImplServer.java From Tomcat7.0.67 with Apache License 2.0 | 5 votes |
@Override public void run() { if (t == null) { sh.onResult(new SendResult()); } else { sh.onResult(new SendResult(t)); } t = null; sh = null; // Return the Runnable to the queue when it has been finished with // Note if this method takes an age to finish there shouldn't be any // thread safety issues as the fields are cleared above. queue.add(this); }
Example #21
Source File: Client.java From tomcatsrc with Apache License 2.0 | 5 votes |
@Override public void onResult(SendResult result) { if (!result.isOK()) { // Message could not be sent. In this case, we don't // set isSendingMessage to false because we must assume the connection // broke (and onClose will be called), so we don't try to send // other messages. // As a precaution, we close the session (e.g. if a send timeout occured). // TODO: session.close() blocks, while this handler shouldn't block. // Ideally, there should be some abort() method that cancels the // connection immediately... try { session.close(); } catch (IOException ex) { // Ignore } } synchronized (messagesToSend) { if (!messagesToSend.isEmpty()) { AbstractWebsocketMessage msg = messagesToSend.remove(); messagesToSendLength -= calculateMessageLength(msg); internalSendMessageAsync(msg); } else { isSendingMessage = false; } } }
Example #22
Source File: WsSession.java From tomcatsrc with Apache License 2.0 | 5 votes |
/** * WebSocket 1.0. Section 2.1.5. * Need internal close method as spec requires that the local endpoint * receives a 1006 on timeout. */ public void doClose(CloseReason closeReasonMessage, CloseReason closeReasonLocal) { // Double-checked locking. OK because state is volatile if (state != State.OPEN) { return; } synchronized (stateLock) { if (state != State.OPEN) { return; } if (log.isDebugEnabled()) { log.debug(sm.getString("wsSession.doClose", id)); } try { wsRemoteEndpoint.setBatchingAllowed(false); } catch (IOException e) { log.warn(sm.getString("wsSession.flushFailOnClose"), e); fireEndpointOnError(e); } state = State.OUTPUT_CLOSED; sendCloseMessage(closeReasonMessage); fireEndpointOnClose(closeReasonLocal); } IOException ioe = new IOException(sm.getString("wsSession.messageFailed")); SendResult sr = new SendResult(ioe); for (FutureToSendHandler f2sh : futures.keySet()) { f2sh.onResult(sr); } }
Example #23
Source File: WsRemoteEndpointImplBase.java From tomcatsrc with Apache License 2.0 | 5 votes |
void endMessage(SendHandler handler, SendResult result) { boolean doWrite = false; MessagePart mpNext = null; synchronized (messagePartLock) { fragmented = nextFragmented; text = nextText; mpNext = messagePartQueue.poll(); if (mpNext == null) { messagePartInProgress = false; } else if (!closed){ // Session may have been closed unexpectedly in the middle of // sending a fragmented message closing the endpoint. If this // happens, clearly there is no point trying to send the rest of // the message. doWrite = true; } } if (doWrite) { // Actual write has to be outside sync block to avoid possible // deadlock between messagePartLock and writeLock in // o.a.coyote.http11.upgrade.AbstractServletOutputStream writeMessagePart(mpNext); } wsSession.updateLastActive(); // Some handlers, such as the IntermediateMessageHandler, do not have a // nested handler so handler may be null. if (handler != null) { handler.onResult(result); } }
Example #24
Source File: WsRemoteEndpointImplBase.java From tomcatsrc with Apache License 2.0 | 5 votes |
@Override public void onResult(SendResult result) { if (isDone) { endpoint.stateMachine.complete(isLast); handler.onResult(result); } else if(!result.isOK()) { handler.onResult(result); } else if (closed){ SendResult sr = new SendResult(new IOException( sm.getString("wsRemoteEndpoint.closedDuringMessage"))); handler.onResult(sr); } else { write(); } }
Example #25
Source File: WsRemoteEndpointImplBase.java From tomcatsrc with Apache License 2.0 | 5 votes |
@Override public void onResult(SendResult result) { if (result.isOK()) { if (outputBuffer.hasRemaining()) { endpoint.doWrite(this, outputBuffer); } else { outputBuffer.clear(); write(); } } else { handler.onResult(result); } }
Example #26
Source File: WsRemoteEndpointImplBase.java From tomcatsrc with Apache License 2.0 | 5 votes |
@Override public void onResult(SendResult result) { if (result.isOK()) { outputBuffer.clear(); } handler.onResult(result); }
Example #27
Source File: WsRemoteEndpointImplBase.java From tomcatsrc with Apache License 2.0 | 5 votes |
@Override public void onResult(SendResult result) { if (result.isOK()) { stateMachine.complete(true); } handler.onResult(result); }
Example #28
Source File: WsRemoteEndpointImplServer.java From tomcatsrc with Apache License 2.0 | 5 votes |
/** * * @param t The throwable associated with any error that * occurred * @param useDispatch Should {@link SendHandler#onResult(SendResult)} be * called from a new thread, keeping in mind the * requirements of * {@link javax.websocket.RemoteEndpoint.Async} */ private void clearHandler(Throwable t, boolean useDispatch) { // Setting the result marks this (partial) message as // complete which means the next one may be sent which // could update the value of the handler. Therefore, keep a // local copy before signalling the end of the (partial) // message. SendHandler sh = handler; handler = null; buffers = null; if (sh != null) { if (useDispatch) { OnResultRunnable r = onResultRunnables.poll(); if (r == null) { r = new OnResultRunnable(onResultRunnables); } r.init(sh, t); if (executorService == null || executorService.isShutdown()) { // Can't use the executor so call the runnable directly. // This may not be strictly specification compliant in all // cases but during shutdown only close messages are going // to be sent so there should not be the issue of nested // calls leading to stack overflow as described in bug // 55715. The issues with nested calls was the reason for // the separate thread requirement in the specification. r.run(); } else { executorService.execute(r); } } else { if (t == null) { sh.onResult(new SendResult()); } else { sh.onResult(new SendResult(t)); } } } }
Example #29
Source File: WsRemoteEndpointImplServer.java From tomcatsrc with Apache License 2.0 | 5 votes |
@Override public void run() { if (t == null) { sh.onResult(new SendResult()); } else { sh.onResult(new SendResult(t)); } t = null; sh = null; // Return the Runnable to the queue when it has been finished with // Note if this method takes an age to finish there shouldn't be any // thread safety issues as the fields are cleared above. queue.add(this); }
Example #30
Source File: Client.java From tomcatsrc with Apache License 2.0 | 5 votes |
@Override public void onResult(SendResult result) { if (!result.isOK()) { // Message could not be sent. In this case, we don't // set isSendingMessage to false because we must assume the connection // broke (and onClose will be called), so we don't try to send // other messages. // As a precaution, we close the session (e.g. if a send timeout occured). // TODO: session.close() blocks, while this handler shouldn't block. // Ideally, there should be some abort() method that cancels the // connection immediately... try { session.close(); } catch (IOException ex) { // Ignore } } synchronized (messagesToSend) { if (!messagesToSend.isEmpty()) { AbstractWebsocketMessage msg = messagesToSend.remove(); messagesToSendLength -= calculateMessageLength(msg); internalSendMessageAsync(msg); } else { isSendingMessage = false; } } }