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");
}
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;
}
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;
}
}
}
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");
}
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");
}
Aggregations