Search in sources :

Example 11 with InetSocketAddress

use of java.net.InetSocketAddress in project camel by apache.

the class MllpTcpClientProducer method checkConnection.

/**
     * Validate the TCP Connection
     *
     * @return null if the connection is valid, otherwise the Exception encountered checking the connection
     */
void checkConnection() throws IOException {
    if (null == socket || socket.isClosed() || !socket.isConnected()) {
        socket = new Socket();
        socket.setKeepAlive(endpoint.keepAlive);
        socket.setTcpNoDelay(endpoint.tcpNoDelay);
        if (null != endpoint.receiveBufferSize) {
            socket.setReceiveBufferSize(endpoint.receiveBufferSize);
        } else {
            endpoint.receiveBufferSize = socket.getReceiveBufferSize();
        }
        if (null != endpoint.sendBufferSize) {
            socket.setSendBufferSize(endpoint.sendBufferSize);
        } else {
            endpoint.sendBufferSize = socket.getSendBufferSize();
        }
        socket.setReuseAddress(endpoint.reuseAddress);
        socket.setSoLinger(false, -1);
        InetSocketAddress socketAddress;
        if (null == endpoint.getHostname()) {
            socketAddress = new InetSocketAddress(endpoint.getPort());
        } else {
            socketAddress = new InetSocketAddress(endpoint.getHostname(), endpoint.getPort());
        }
        log.debug("Connecting to socket on {}", socketAddress);
        socket.connect(socketAddress, endpoint.connectTimeout);
        log.debug("Creating MllpSocketReader and MllpSocketWriter");
        mllpSocketReader = new MllpSocketReader(socket, endpoint.receiveTimeout, endpoint.readTimeout, true);
        if (endpoint.bufferWrites) {
            mllpSocketWriter = new MllpBufferedSocketWriter(socket, false);
        } else {
            mllpSocketWriter = new MllpSocketWriter(socket, false);
        }
    }
}
Also used : MllpSocketReader(org.apache.camel.component.mllp.impl.MllpSocketReader) MllpBufferedSocketWriter(org.apache.camel.component.mllp.impl.MllpBufferedSocketWriter) InetSocketAddress(java.net.InetSocketAddress) MllpSocketWriter(org.apache.camel.component.mllp.impl.MllpSocketWriter) Socket(java.net.Socket)

Example 12 with InetSocketAddress

use of java.net.InetSocketAddress in project camel by apache.

the class MllpTcpServerConsumer method doStart.

@Override
protected void doStart() throws Exception {
    log.debug("doStart() - creating acceptor thread");
    ServerSocket serverSocket = new ServerSocket();
    if (null != endpoint.receiveBufferSize) {
        serverSocket.setReceiveBufferSize(endpoint.receiveBufferSize);
    }
    serverSocket.setReuseAddress(endpoint.reuseAddress);
    // Accept Timeout
    serverSocket.setSoTimeout(endpoint.acceptTimeout);
    InetSocketAddress socketAddress;
    if (null == endpoint.getHostname()) {
        socketAddress = new InetSocketAddress(endpoint.getPort());
    } else {
        socketAddress = new InetSocketAddress(endpoint.getHostname(), endpoint.getPort());
    }
    long startTicks = System.currentTimeMillis();
    do {
        try {
            serverSocket.bind(socketAddress, endpoint.backlog);
        } catch (BindException bindException) {
            if (System.currentTimeMillis() > startTicks + endpoint.getBindTimeout()) {
                log.error("Failed to bind to address {} within timeout {}", socketAddress, endpoint.getBindTimeout());
                throw bindException;
            } else {
                log.warn("Failed to bind to address {} - retrying in {} milliseconds", socketAddress, endpoint.getBindRetryInterval());
                Thread.sleep(endpoint.getBindRetryInterval());
            }
        }
    } while (!serverSocket.isBound());
    serverSocketThread = new ServerSocketThread(serverSocket);
    serverSocketThread.start();
    super.doStart();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) BindException(java.net.BindException) ServerSocket(java.net.ServerSocket)

Example 13 with InetSocketAddress

use of java.net.InetSocketAddress in project camel by apache.

the class MllpClientResource method connect.

public void connect(int connectTimeout) {
    try {
        clientSocket = new Socket();
        clientSocket.connect(new InetSocketAddress(mllpHost, mllpPort), connectTimeout);
        clientSocket.setSoTimeout(soTimeout);
        clientSocket.setSoLinger(false, -1);
        clientSocket.setReuseAddress(reuseAddress);
        clientSocket.setTcpNoDelay(tcpNoDelay);
        inputStream = clientSocket.getInputStream();
        outputStream = new BufferedOutputStream(clientSocket.getOutputStream(), 2048);
    } catch (IOException e) {
        String errorMessage = String.format("Unable to establish connection to %s:%s", mllpHost, mllpPort);
        log.error(errorMessage, e);
        throw new MllpJUnitResourceException(errorMessage, e);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) Socket(java.net.Socket)

Example 14 with InetSocketAddress

use of java.net.InetSocketAddress in project flink by apache.

the class RuntimeMonitorHandler method respondAsLeader.

@Override
protected void respondAsLeader(ChannelHandlerContext ctx, Routed routed, ActorGateway jobManager) {
    FullHttpResponse response;
    try {
        // we only pass the first element in the list to the handlers.
        Map<String, String> queryParams = new HashMap<>();
        for (String key : routed.queryParams().keySet()) {
            queryParams.put(key, routed.queryParam(key));
        }
        Map<String, String> pathParams = new HashMap<>(routed.pathParams().size());
        for (String key : routed.pathParams().keySet()) {
            pathParams.put(key, URLDecoder.decode(routed.pathParams().get(key), ENCODING.toString()));
        }
        InetSocketAddress address = (InetSocketAddress) ctx.channel().localAddress();
        queryParams.put(WEB_MONITOR_ADDRESS_KEY, (httpsEnabled ? "https://" : "http://") + address.getHostName() + ":" + address.getPort());
        response = handler.handleRequest(pathParams, queryParams, jobManager);
    } catch (NotFoundException e) {
        // this should result in a 404 error code (not found)
        ByteBuf message = e.getMessage() == null ? Unpooled.buffer(0) : Unpooled.wrappedBuffer(e.getMessage().getBytes(ENCODING));
        response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND, message);
        response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
        response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
        LOG.debug("Error while handling request", e);
    } catch (Exception e) {
        byte[] bytes = ExceptionUtils.stringifyException(e).getBytes(ENCODING);
        response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR, Unpooled.wrappedBuffer(bytes));
        response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
        response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
        LOG.debug("Error while handling request", e);
    }
    response.headers().set(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
    // Content-Encoding:utf-8
    response.headers().set(HttpHeaders.Names.CONTENT_ENCODING, ENCODING.name());
    KeepAliveWrite.flush(ctx, routed.request(), response);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) ByteBuf(io.netty.buffer.ByteBuf)

Example 15 with InetSocketAddress

use of java.net.InetSocketAddress in project flink by apache.

the class TaskManagerServicesConfiguration method parseNetworkEnvironmentConfiguration.

// --------------------------------------------------------------------------
//  Parsing and checking the TaskManager Configuration
// --------------------------------------------------------------------------
/**
	 * Creates the {@link NetworkEnvironmentConfiguration} from the given {@link Configuration}.
	 *
	 * @param configuration to create the network environment configuration from
	 * @param localTaskManagerCommunication true if task manager communication is local
	 * @param taskManagerAddress address of the task manager
	 * @param slots to start the task manager with
	 * @return Network environment configuration
	 */
private static NetworkEnvironmentConfiguration parseNetworkEnvironmentConfiguration(Configuration configuration, boolean localTaskManagerCommunication, InetAddress taskManagerAddress, int slots) throws Exception {
    // ----> hosts / ports for communication and data exchange
    int dataport = configuration.getInteger(ConfigConstants.TASK_MANAGER_DATA_PORT_KEY, ConfigConstants.DEFAULT_TASK_MANAGER_DATA_PORT);
    checkConfigParameter(dataport >= 0, dataport, ConfigConstants.TASK_MANAGER_DATA_PORT_KEY, "Leave config parameter empty or use 0 to let the system choose a port automatically.");
    checkConfigParameter(slots >= 1, slots, ConfigConstants.TASK_MANAGER_NUM_TASK_SLOTS, "Number of task slots must be at least one.");
    final int numNetworkBuffers = configuration.getInteger(ConfigConstants.TASK_MANAGER_NETWORK_NUM_BUFFERS_KEY, ConfigConstants.DEFAULT_TASK_MANAGER_NETWORK_NUM_BUFFERS);
    checkConfigParameter(numNetworkBuffers > 0, numNetworkBuffers, ConfigConstants.TASK_MANAGER_NETWORK_NUM_BUFFERS_KEY, "");
    final int pageSize = configuration.getInteger(ConfigConstants.TASK_MANAGER_MEMORY_SEGMENT_SIZE_KEY, ConfigConstants.DEFAULT_TASK_MANAGER_MEMORY_SEGMENT_SIZE);
    // check page size of for minimum size
    checkConfigParameter(pageSize >= MemoryManager.MIN_PAGE_SIZE, pageSize, ConfigConstants.TASK_MANAGER_MEMORY_SEGMENT_SIZE_KEY, "Minimum memory segment size is " + MemoryManager.MIN_PAGE_SIZE);
    // check page size for power of two
    checkConfigParameter(MathUtils.isPowerOf2(pageSize), pageSize, ConfigConstants.TASK_MANAGER_MEMORY_SEGMENT_SIZE_KEY, "Memory segment size must be a power of 2.");
    // check whether we use heap or off-heap memory
    final MemoryType memType;
    if (configuration.getBoolean(ConfigConstants.TASK_MANAGER_MEMORY_OFF_HEAP_KEY, false)) {
        memType = MemoryType.OFF_HEAP;
    } else {
        memType = MemoryType.HEAP;
    }
    // TODO - this should be in the TaskManager, not the configuration
    if (memType == MemoryType.HEAP) {
        if (!MemorySegmentFactory.initializeIfNotInitialized(HeapMemorySegment.FACTORY)) {
            throw new Exception("Memory type is set to heap memory, but memory segment " + "factory has been initialized for off-heap memory segments");
        }
    } else {
        if (!MemorySegmentFactory.initializeIfNotInitialized(HybridMemorySegment.FACTORY)) {
            throw new Exception("Memory type is set to off-heap memory, but memory segment " + "factory has been initialized for heap memory segments");
        }
    }
    final NettyConfig nettyConfig;
    if (!localTaskManagerCommunication) {
        final InetSocketAddress taskManagerInetSocketAddress = new InetSocketAddress(taskManagerAddress, dataport);
        nettyConfig = new NettyConfig(taskManagerInetSocketAddress.getAddress(), taskManagerInetSocketAddress.getPort(), pageSize, slots, configuration);
    } else {
        nettyConfig = null;
    }
    // Default spill I/O mode for intermediate results
    final String syncOrAsync = configuration.getString(ConfigConstants.TASK_MANAGER_NETWORK_DEFAULT_IO_MODE, ConfigConstants.DEFAULT_TASK_MANAGER_NETWORK_DEFAULT_IO_MODE);
    final IOManager.IOMode ioMode;
    if (syncOrAsync.equals("async")) {
        ioMode = IOManager.IOMode.ASYNC;
    } else {
        ioMode = IOManager.IOMode.SYNC;
    }
    int initialRequestBackoff = configuration.getInteger(TaskManagerOptions.NETWORK_REQUEST_BACKOFF_INITIAL);
    int maxRequestBackoff = configuration.getInteger(TaskManagerOptions.NETWORK_REQUEST_BACKOFF_MAX);
    int buffersPerChannel = configuration.getInteger(TaskManagerOptions.NETWORK_BUFFERS_PER_CHANNEL);
    int extraBuffersPerGate = configuration.getInteger(TaskManagerOptions.NETWORK_EXTRA_BUFFERS_PER_GATE);
    return new NetworkEnvironmentConfiguration(numNetworkBuffers, pageSize, memType, ioMode, initialRequestBackoff, maxRequestBackoff, buffersPerChannel, extraBuffersPerGate, nettyConfig);
}
Also used : IOManager(org.apache.flink.runtime.io.disk.iomanager.IOManager) InetSocketAddress(java.net.InetSocketAddress) NettyConfig(org.apache.flink.runtime.io.network.netty.NettyConfig) IllegalConfigurationException(org.apache.flink.configuration.IllegalConfigurationException) MemoryType(org.apache.flink.core.memory.MemoryType) NetworkEnvironmentConfiguration(org.apache.flink.runtime.taskmanager.NetworkEnvironmentConfiguration)

Aggregations

InetSocketAddress (java.net.InetSocketAddress)2586 Test (org.junit.Test)595 IOException (java.io.IOException)592 Socket (java.net.Socket)345 InetAddress (java.net.InetAddress)242 SocketAddress (java.net.SocketAddress)176 ServerSocket (java.net.ServerSocket)170 ArrayList (java.util.ArrayList)168 Configuration (org.apache.hadoop.conf.Configuration)140 ByteBuffer (java.nio.ByteBuffer)129 UnknownHostException (java.net.UnknownHostException)122 InputStream (java.io.InputStream)102 OutputStream (java.io.OutputStream)101 SocketChannel (java.nio.channels.SocketChannel)101 SocketException (java.net.SocketException)89 File (java.io.File)88 HashMap (java.util.HashMap)78 URI (java.net.URI)72 Proxy (java.net.Proxy)65 SocketTimeoutException (java.net.SocketTimeoutException)65