Search in sources :

Example 6 with ListenerEndpoint

use of org.apache.http.nio.reactor.ListenerEndpoint in project metrics by dropwizard.

the class HttpClientTestBase method startServerWithGlobalRequestHandler.

/**
 * Start a local server that uses the {@code handler} to handle requests.
 * <p>
 * The server will be (if started) terminated in the {@link #tearDown()} {@link After} method.
 *
 * @param handler The request handler that will be used to respond to every request.
 * @return The {@link HttpHost} of the server
 * @throws IOException          in case it's not possible to start the server
 * @throws InterruptedException in case the server's main thread was interrupted
 */
public HttpHost startServerWithGlobalRequestHandler(HttpRequestHandler handler) throws IOException, InterruptedException {
    // If there is an existing instance, terminate it
    tearDown();
    ServerBootstrap serverBootstrap = ServerBootstrap.bootstrap();
    serverBootstrap.registerHandler("/*", new BasicAsyncRequestHandler(handler));
    server = serverBootstrap.create();
    server.start();
    ListenerEndpoint endpoint = server.getEndpoint();
    endpoint.waitFor();
    InetSocketAddress address = (InetSocketAddress) endpoint.getAddress();
    return new HttpHost("localhost", address.getPort(), "http");
}
Also used : ListenerEndpoint(org.apache.http.nio.reactor.ListenerEndpoint) InetSocketAddress(java.net.InetSocketAddress) HttpHost(org.apache.http.HttpHost) BasicAsyncRequestHandler(org.apache.http.nio.protocol.BasicAsyncRequestHandler) ServerBootstrap(org.apache.http.impl.nio.bootstrap.ServerBootstrap)

Example 7 with ListenerEndpoint

use of org.apache.http.nio.reactor.ListenerEndpoint in project wso2-synapse by wso2.

the class PassThroughListeningIOReactorManager method closeDynamicPTTEndpoint.

/**
 * Close external endpoints listen in shared IO Reactor
 *
 * @param port Port of the endpoint need to close
 * @return Is endpoint closed
 */
public boolean closeDynamicPTTEndpoint(int port) {
    int portCloseVerifyTimeout = System.getProperty(PassThroughConstants.SYSTEMPROP_PORT_CLOSE_VERIFY_TIMEOUT) == null ? DEFAULT_PORT_CLOSE_VERIFY_TIMEOUT : Integer.parseInt(System.getProperty(PassThroughConstants.SYSTEMPROP_PORT_CLOSE_VERIFY_TIMEOUT));
    try {
        log.info("Closing Endpoint Listener for port " + port);
        dynamicPTTListeningEndpointMapper.get(port).close();
    } catch (Exception e) {
        log.error("Cannot close  Endpoint relevant to port " + port, e);
        return false;
    } finally {
        if (serverConnectionFactoryMapper.containsKey(port)) {
            serverConnectionFactoryMapper.remove(port);
        }
        /*
             * validate whether port is closed successfully. Here we wait till port is successfully get closed
             * iteratively trying to bind a ServerSocket to the relevant port. As default time check for 10s and log
             * warning and move on.
             *
             * We need to do this since even though we close the
             * org.apache.http.nio.reactor.ListenerEndpoint above, on some operating systems it takes time to release
             * the port. And at redeployment of a inbound endpoint redeployment happens immediately and in some environments
             * redeployment fails due to this issue.
             *
             * If 10s timeout is not enough (depends on the environment) the timeout can be tuned using
             * "synapse.transport.portCloseVerifyTimeout" system property
             */
        if (isPortCloseSuccess(port, portCloseVerifyTimeout)) {
            log.info("Successfully closed Endpoint Listener for port " + port);
        } else {
            log.warn("Port close verify timeout " + portCloseVerifyTimeout + "s exceeded. Endpoint Listener for port " + port + " still bound to the ListenerEndpoint.");
        }
        dynamicPTTListeningEndpointMapper.remove(port);
    }
    return true;
}
Also used : ListenerEndpoint(org.apache.http.nio.reactor.ListenerEndpoint) IOException(java.io.IOException) IOReactorException(org.apache.http.nio.reactor.IOReactorException)

Example 8 with ListenerEndpoint

use of org.apache.http.nio.reactor.ListenerEndpoint in project wso2-synapse by wso2.

the class HttpCoreNIOListener method startEndpoints.

private void startEndpoints() throws AxisFault {
    Queue<ListenerEndpoint> endpoints = new LinkedList<ListenerEndpoint>();
    Set<InetSocketAddress> addressSet = new HashSet<InetSocketAddress>();
    addressSet.addAll(connFactory.getBindAddresses());
    if (NHttpConfiguration.getInstance().getMaxActiveConnections() != -1) {
        addMaxConnectionCountController(NHttpConfiguration.getInstance().getMaxActiveConnections());
    }
    if (listenerContext.getBindAddress() != null) {
        addressSet.add(new InetSocketAddress(listenerContext.getBindAddress(), listenerContext.getPort()));
    }
    if (addressSet.isEmpty()) {
        addressSet.add(new InetSocketAddress(listenerContext.getPort()));
    }
    // Ensure simple but stable order
    List<InetSocketAddress> addressList = new ArrayList<InetSocketAddress>(addressSet);
    Collections.sort(addressList, new Comparator<InetSocketAddress>() {

        public int compare(InetSocketAddress a1, InetSocketAddress a2) {
            String s1 = a1.toString();
            String s2 = a2.toString();
            return s1.compareTo(s2);
        }
    });
    for (InetSocketAddress address : addressList) {
        endpoints.add(ioReactor.listen(address));
    }
    // requests.
    while (!endpoints.isEmpty()) {
        ListenerEndpoint endpoint = endpoints.remove();
        try {
            endpoint.waitFor();
            if (log.isInfoEnabled()) {
                InetSocketAddress address = (InetSocketAddress) endpoint.getAddress();
                if (!address.isUnresolved()) {
                    log.info(name + " started on " + address.getHostName() + ":" + address.getPort());
                } else {
                    log.info(name + " started on " + address);
                }
            }
        } catch (InterruptedException e) {
            log.warn("Listener startup was interrupted");
            break;
        }
    }
}
Also used : ListenerEndpoint(org.apache.http.nio.reactor.ListenerEndpoint) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Example 9 with ListenerEndpoint

use of org.apache.http.nio.reactor.ListenerEndpoint in project wso2-synapse by wso2.

the class HttpCoreNIOListener method reload.

public void reload(final TransportInDescription transportIn) throws AxisFault {
    if (state != BaseConstants.STARTED)
        return;
    // Close all listener endpoints and stop accepting new connections
    Set<ListenerEndpoint> endpoints = ioReactor.getEndpoints();
    for (ListenerEndpoint endpoint : endpoints) {
        endpoint.close();
    }
    // Rebuild connection factory
    HttpHost host = new HttpHost(listenerContext.getHostname(), listenerContext.getPort(), scheme.getName());
    ServerConnFactoryBuilder connFactoryBuilder = initConnFactoryBuilder(transportIn, host);
    connFactory = connFactoryBuilder.build(params);
    iodispatch.update(connFactory);
    startEndpoints();
    log.info(name + " Reloaded");
}
Also used : ListenerEndpoint(org.apache.http.nio.reactor.ListenerEndpoint) HttpHost(org.apache.http.HttpHost) ServerConnFactoryBuilder(org.apache.synapse.transport.nhttp.config.ServerConnFactoryBuilder)

Example 10 with ListenerEndpoint

use of org.apache.http.nio.reactor.ListenerEndpoint in project wso2-synapse by wso2.

the class HttpCoreNIOListener method reloadSpecificEndpoints.

/**
 * Restart specific endpoints which was updated by new configurations
 *
 * @param transportIn TransportInDescription of new configuration
 * @throws AxisFault
 */
public void reloadSpecificEndpoints(final TransportInDescription transportIn) throws AxisFault {
    if (state != BaseConstants.STARTED) {
        return;
    }
    HttpHost host = new HttpHost(listenerContext.getHostname(), listenerContext.getPort(), scheme.getName());
    // Rebuild connection factory
    ServerConnFactoryBuilder connFactoryBuilder = initConnFactoryBuilder(transportIn, host);
    connFactory = connFactoryBuilder.build(params);
    iodispatch.update(connFactory);
    List<InetSocketAddress> endPointsClosed = new ArrayList<InetSocketAddress>();
    // Close endpoints related to new profile's bind addresses
    Set<InetSocketAddress> endPointsToReload = connFactory.getBindAddresses();
    for (InetSocketAddress inetSocketAddress : endPointsToReload) {
        for (ListenerEndpoint listenerEndpoint : ioReactor.getEndpoints()) {
            if (inetSocketAddress.getHostName().equalsIgnoreCase(((InetSocketAddress) listenerEndpoint.getAddress()).getHostName())) {
                listenerEndpoint.close();
                endPointsClosed.add((InetSocketAddress) listenerEndpoint.getAddress());
            }
        }
    }
    // Start closed inpoints again with new configurations
    startSpecificEndpoints(endPointsClosed);
    log.info(name + " Reloaded");
}
Also used : ListenerEndpoint(org.apache.http.nio.reactor.ListenerEndpoint) HttpHost(org.apache.http.HttpHost) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) ServerConnFactoryBuilder(org.apache.synapse.transport.nhttp.config.ServerConnFactoryBuilder)

Aggregations

ListenerEndpoint (org.apache.http.nio.reactor.ListenerEndpoint)10 InetSocketAddress (java.net.InetSocketAddress)6 IOException (java.io.IOException)5 IOReactorException (org.apache.http.nio.reactor.IOReactorException)5 HttpHost (org.apache.http.HttpHost)3 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 ServerConnFactoryBuilder (org.apache.synapse.transport.nhttp.config.ServerConnFactoryBuilder)2 HashSet (java.util.HashSet)1 ServerBootstrap (org.apache.http.impl.nio.bootstrap.ServerBootstrap)1 DefaultListeningIOReactor (org.apache.http.impl.nio.reactor.DefaultListeningIOReactor)1 BasicAsyncRequestHandler (org.apache.http.nio.protocol.BasicAsyncRequestHandler)1 ListeningIOReactor (org.apache.http.nio.reactor.ListeningIOReactor)1