use of org.jboss.netty.bootstrap.ClientBootstrap in project sockjs-netty by cgbystrom.
the class SockJsClient method newClient.
public static SockJsClient newClient(URI url, Protocol protocol, final SessionCallback callback) {
ClientBootstrap bootstrap = new ClientBootstrap(socketChannelFactory);
switch(protocol) {
case WEBSOCKET:
final WebSocketClient clientHandler = new WebSocketClient(bootstrap, url, callback);
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("decoder", new HttpResponseDecoder());
pipeline.addLast("encoder", new HttpRequestEncoder());
pipeline.addLast("ws-handler", clientHandler);
return pipeline;
}
});
return clientHandler;
}
throw new IllegalArgumentException("Invalid protocol specified");
}
use of org.jboss.netty.bootstrap.ClientBootstrap in project sockjs-netty by cgbystrom.
the class SockJsClient method newLocalClient.
public static SockJsClient newLocalClient(URI url, Protocol protocol, final SessionCallback callback) {
ClientBootstrap bootstrap = new ClientBootstrap(localSocketChannelFactory);
switch(protocol) {
case WEBSOCKET:
final WebSocketClient clientHandler = new WebSocketClient(bootstrap, url, callback);
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("decoder", new HttpResponseDecoder());
pipeline.addLast("encoder", new HttpRequestEncoder());
pipeline.addLast("ws-handler", clientHandler);
return pipeline;
}
});
return clientHandler;
}
throw new IllegalArgumentException("Invalid protocol specified");
}
use of org.jboss.netty.bootstrap.ClientBootstrap in project databus by linkedin.
the class TestDatabusRelayMain method testClient.
private void testClient(int relayPort, int fetchSize, long scn, HttpResponseHandler handler) throws Exception {
Checkpoint ckpt = Checkpoint.createOnlineConsumptionCheckpoint(scn);
//TODO why is this needed
//ckpt.setCatchupSource("foo");
String uristr = "/stream?sources=105&output=json&size=" + fetchSize + "&streamFromLatestScn=false&checkPoint=" + ckpt.toString();
ClientBootstrap bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
bootstrap.setPipelineFactory(new HttpClientPipelineFactory(handler));
ChannelFuture future = bootstrap.connect(new InetSocketAddress("localhost", relayPort));
Channel channel = future.awaitUninterruptibly().getChannel();
Assert.assertTrue(future.isSuccess(), "Cannot connect to relay at localhost:" + relayPort);
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uristr);
request.setHeader(HttpHeaders.Names.HOST, "localhost");
channel.write(request);
channel.getCloseFuture().awaitUninterruptibly();
}
use of org.jboss.netty.bootstrap.ClientBootstrap in project adbcj by mheath.
the class Encoder method initBootstrap.
private ClientBootstrap initBootstrap(ChannelFactory channelFactory, String host, int port) {
ClientBootstrap bootstrap = new ClientBootstrap(channelFactory);
bootstrap.setPipelineFactory(Channels.pipelineFactory(Channels.pipeline()));
bootstrap.setOption("tcpNoDelay", true);
bootstrap.setOption("keepAlive", true);
bootstrap.setOption("remoteAddress", new InetSocketAddress(host, port));
return bootstrap;
}
use of org.jboss.netty.bootstrap.ClientBootstrap in project neo4j by neo4j.
the class Client method start.
@Override
public void start() {
bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(newCachedThreadPool(daemon(getClass().getSimpleName() + "-boss@" + destination)), newCachedThreadPool(daemon(getClass().getSimpleName() + "-worker@" + destination))));
bootstrap.setPipelineFactory(this);
channelPool = new ResourcePool<ChannelContext>(maxUnusedChannels, new ResourcePool.CheckStrategy.TimeoutCheckStrategy(DEFAULT_CHECK_INTERVAL, Clocks.systemClock()), new LoggingResourcePoolMonitor(msgLog)) {
@Override
protected ChannelContext create() {
msgLog.info(threadInfo() + "Trying to open a new channel from " + origin + " to " + destination, true);
// We must specify the origin address in case the server has multiple IPs per interface
ChannelFuture channelFuture = bootstrap.connect(destination, origin);
channelFuture.awaitUninterruptibly(5, TimeUnit.SECONDS);
if (channelFuture.isSuccess()) {
msgLog.info(threadInfo() + "Opened a new channel from " + channelFuture.getChannel().getLocalAddress() + " to " + channelFuture.getChannel().getRemoteAddress());
return new ChannelContext(channelFuture.getChannel(), ChannelBuffers.dynamicBuffer(), ByteBuffer.allocate(1024 * 1024));
}
Throwable cause = channelFuture.getCause();
String msg = Client.this.getClass().getSimpleName() + " could not connect from " + origin + " to " + destination;
msgLog.debug(msg, true);
throw traceComException(new ComException(msg, cause), "Client.start");
}
@Override
protected boolean isAlive(ChannelContext context) {
return context.channel().isConnected();
}
@Override
protected void dispose(ChannelContext context) {
Channel channel = context.channel();
if (channel.isConnected()) {
msgLog.info(threadInfo() + "Closing: " + context + ". " + "Channel pool size is now " + currentSize());
channel.close();
}
}
private String threadInfo() {
return "Thread[" + Thread.currentThread().getId() + ", " + Thread.currentThread().getName() + "] ";
}
};
/*
* This is here to couple the channel releasing to Response.close() itself and not
* to TransactionStream.close() as it is implemented here. The reason is that a Response
* that is returned without a TransactionStream will still hold the channel and should
* release it eventually. Also, logically, closing the channel is not dependent on the
* TransactionStream.
*/
resourcePoolReleaser = () -> {
if (channelPool != null) {
channelPool.release();
}
};
}
Aggregations