Example usage for java.net StandardSocketOptions SO_SNDBUF

List of usage examples for java.net StandardSocketOptions SO_SNDBUF

Introduction

In this page you can find the example usage for java.net StandardSocketOptions SO_SNDBUF.

Prototype

SocketOption SO_SNDBUF

To view the source code for java.net StandardSocketOptions SO_SNDBUF.

Click Source Link

Document

The size of the socket send buffer.

Usage

From source file:org.apache.nifi.processor.util.put.sender.DatagramChannelSender.java

@Override
public void open() throws IOException {
    if (channel == null) {
        channel = DatagramChannel.open();

        if (maxSendBufferSize > 0) {
            channel.setOption(StandardSocketOptions.SO_SNDBUF, maxSendBufferSize);
            final int actualSendBufSize = channel.getOption(StandardSocketOptions.SO_SNDBUF);
            if (actualSendBufSize < maxSendBufferSize) {
                logger.warn("Attempted to set Socket Send Buffer Size to " + maxSendBufferSize
                        + " bytes but could only set to " + actualSendBufSize + "bytes. You may want to "
                        + "consider changing the Operating System's maximum receive buffer");
            }//from  w  w w .j av a2s .  c  o  m
        }
    }

    if (!channel.isConnected()) {
        channel.connect(new InetSocketAddress(InetAddress.getByName(host), port));
    }
}

From source file:org.apache.nifi.processor.util.put.sender.SocketChannelSender.java

@Override
public void open() throws IOException {
    if (channel == null) {
        channel = SocketChannel.open();
        channel.configureBlocking(false);

        if (maxSendBufferSize > 0) {
            channel.setOption(StandardSocketOptions.SO_SNDBUF, maxSendBufferSize);
            final int actualSendBufSize = channel.getOption(StandardSocketOptions.SO_SNDBUF);
            if (actualSendBufSize < maxSendBufferSize) {
                logger.warn("Attempted to set Socket Send Buffer Size to " + maxSendBufferSize
                        + " bytes but could only set to " + actualSendBufSize + "bytes. You may want to "
                        + "consider changing the Operating System's maximum receive buffer");
            }/*from   w  w w.ja v  a 2 s  .  c o  m*/
        }
    }

    if (!channel.isConnected()) {
        final long startTime = System.currentTimeMillis();
        final InetSocketAddress socketAddress = new InetSocketAddress(InetAddress.getByName(host), port);

        if (!channel.connect(socketAddress)) {
            while (!channel.finishConnect()) {
                if (System.currentTimeMillis() > startTime + timeout) {
                    throw new SocketTimeoutException("Timed out connecting to " + host + ":" + port);
                }

                try {
                    Thread.sleep(50L);
                } catch (final InterruptedException e) {
                }
            }
        }

        socketChannelOutput = new SocketChannelOutputStream(channel);
        socketChannelOutput.setTimeout(timeout);
    }
}