public void send(final String message) { try { executor.execute(new Runnable() { @Override public void run() { blockingSend(message); } }); } catch (Exception e) { handler.handle(e); } }
private void blockingSend(String message) { try { final byte[] sendData = message.getBytes(encoding); clientSocket.write(ByteBuffer.wrap(sendData)); } catch (Exception e) { handler.handle(e); } } }
/** * Cleanly shut down this StatsD client. This method may throw an exception if * the socket cannot be closed. */ @Override public void stop() { try { executor.shutdown(); executor.awaitTermination(30, TimeUnit.SECONDS); } catch (final Exception e) { handler.handle(e); } finally { if (clientChannel != null) { try { clientChannel.close(); } catch (final IOException e) { handler.handle(e); } } } }
/** * Cleanly shut down this StatsD client. This method may throw an exception if * the socket cannot be closed. */ @Override public void stop() { try { executor.shutdown(); executor.awaitTermination(30, TimeUnit.SECONDS); } catch (final Exception e) { handler.handle(e); } finally { if (clientChannel != null) { try { clientChannel.close(); } catch (final IOException e) { handler.handle(e); } } } }
private void blockingSend(final SocketAddress address) throws IOException { final int sizeOfBuffer = sendBuffer.position(); sendBuffer.flip(); final int sentBytes = clientChannel.send(sendBuffer, address); sendBuffer.limit(sendBuffer.capacity()); sendBuffer.rewind(); if (sizeOfBuffer != sentBytes) { handler.handle( new IOException( String.format( "Could not send entirely stat %s to %s. Only sent %d bytes out of %d bytes", sendBuffer.toString(), address.toString(), sentBytes, sizeOfBuffer))); } } }
private void blockingSend(final SocketAddress address) throws IOException { final int sizeOfBuffer = sendBuffer.position(); sendBuffer.flip(); final int sentBytes = clientChannel.send(sendBuffer, address); sendBuffer.limit(sendBuffer.capacity()); sendBuffer.rewind(); if (sizeOfBuffer != sentBytes) { handler.handle( new IOException( String.format( "Could not send entirely stat %s to %s. Only sent %d bytes out of %d bytes", sendBuffer.toString(), address.toString(), sentBytes, sizeOfBuffer))); } } }
@Override public void run() { while(!executor.isShutdown()) { try { final String message = queue.poll(1, TimeUnit.SECONDS); if(null != message) { final SocketAddress address = addressLookup.call(); final byte[] data = message.getBytes(MESSAGE_CHARSET); if(sendBuffer.remaining() < (data.length + 1)) { blockingSend(address); } if(sendBuffer.position() > 0) { sendBuffer.put( (byte) '\n'); } sendBuffer.put(data); if(null == queue.peek()) { blockingSend(address); } } } catch (final Exception e) { handler.handle(e); } } }
@Override public void run() { while(!executor.isShutdown()) { try { final String message = queue.poll(1, TimeUnit.SECONDS); if(null != message) { final SocketAddress address = addressLookup.call(); final byte[] data = message.getBytes(MESSAGE_CHARSET); if(sendBuffer.remaining() < (data.length + 1)) { blockingSend(address); } if(sendBuffer.position() > 0) { sendBuffer.put( (byte) '\n'); } sendBuffer.put(data); if(null == queue.peek()) { blockingSend(address); } } } catch (final Exception e) { handler.handle(e); } } }