use of org.jboss.netty.channel.ChannelPipelineFactory in project yyl_example by Relucent.
the class NettyClient method main.
public static void main(String[] args) {
ClientBootstrap bootstrap = new ClientBootstrap(new //
NioClientSocketChannelFactory(//
Executors.newCachedThreadPool(), //
Executors.newCachedThreadPool()));
// Set up the default event pipeline.
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
@Override
public ChannelPipeline getPipeline() throws Exception {
return Channels.pipeline(new StringDecoder(), new StringEncoder(), new ClientHandler());
}
});
// Start the connection attempt.
ChannelFuture future = bootstrap.connect(new InetSocketAddress("localhost", 8000));
// Wait until the connection is closed or the connection attempt fails.
future.getChannel().getCloseFuture().awaitUninterruptibly();
// Shut down thread pools to exit.
bootstrap.releaseExternalResources();
}
use of org.jboss.netty.channel.ChannelPipelineFactory in project cdap by caskdata.
the class NettyRouter method bootstrapServer.
private void bootstrapServer(final ChannelUpstreamHandler connectionTracker) throws ServiceBindException {
ExecutorService serverBossExecutor = createExecutorService(serverBossThreadPoolSize, "router-server-boss-thread-%d");
ExecutorService serverWorkerExecutor = createExecutorService(serverWorkerThreadPoolSize, "router-server-worker-thread-%d");
serverBootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(serverBossExecutor, serverWorkerExecutor));
serverBootstrap.setOption("backlog", serverConnectionBacklog);
serverBootstrap.setOption("child.bufferFactory", new DirectChannelBufferFactory());
// Setup the pipeline factory
serverBootstrap.setPipelineFactory(new ChannelPipelineFactory() {
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
if (isSSLEnabled()) {
// Add SSLHandler is SSL is enabled
pipeline.addLast("ssl", sslHandlerFactory.create());
}
pipeline.addLast("tracker", connectionTracker);
pipeline.addLast("http-response-encoder", new HttpResponseEncoder());
pipeline.addLast("http-decoder", new HttpRequestDecoder());
pipeline.addLast("http-status-request-handler", new HttpStatusRequestHandler());
if (securityEnabled) {
pipeline.addLast("access-token-authenticator", new SecurityAuthenticationHttpHandler(realm, tokenValidator, configuration, accessTokenTransformer, discoveryServiceClient));
}
// for now there's only one hardcoded rule, but if there will be more, we may want it generic and configurable
pipeline.addLast("http-request-handler", new HttpRequestHandler(clientBootstrap, serviceLookup, ImmutableList.<ProxyRule>of()));
return pipeline;
}
});
// Start listening on ports.
ImmutableMap.Builder<Integer, String> serviceMapBuilder = ImmutableMap.builder();
for (Map.Entry<String, Integer> forward : serviceToPortMap.entrySet()) {
int port = forward.getValue();
String service = forward.getKey();
String boundService = serviceLookup.getService(port);
if (boundService != null) {
LOG.warn("Port {} is already configured to service {}, ignoring forward for service {}", port, boundService, service);
continue;
}
InetSocketAddress bindAddress = new InetSocketAddress(hostname, port);
LOG.info("Starting Netty Router for service {} on address {}...", service, bindAddress);
try {
Channel channel = serverBootstrap.bind(bindAddress);
InetSocketAddress boundAddress = (InetSocketAddress) channel.getLocalAddress();
serviceMapBuilder.put(boundAddress.getPort(), service);
channelGroup.add(channel);
// Update service map
serviceLookup.updateServiceMap(serviceMapBuilder.build());
LOG.info("Started Netty Router for service {} on address {}.", service, boundAddress);
} catch (ChannelException e) {
if ((Throwables.getRootCause(e) instanceof BindException)) {
throw new ServiceBindException("Router", hostname.getCanonicalHostName(), port, e);
}
throw e;
}
}
}
use of org.jboss.netty.channel.ChannelPipelineFactory in project cdap by caskdata.
the class NettyRouter method bootstrapClient.
private void bootstrapClient(final ChannelUpstreamHandler connectionTracker) {
ExecutorService clientBossExecutor = createExecutorService(clientBossThreadPoolSize, "router-client-boss-thread-%d");
ExecutorService clientWorkerExecutor = createExecutorService(clientWorkerThreadPoolSize, "router-client-worker-thread-%d");
clientBootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(new NioClientBossPool(clientBossExecutor, clientBossThreadPoolSize), new NioWorkerPool(clientWorkerExecutor, clientWorkerThreadPoolSize)));
ChannelPipelineFactory pipelineFactory = new ClientChannelPipelineFactory(connectionTracker, connectionTimeout, timer);
clientBootstrap.setPipelineFactory(pipelineFactory);
clientBootstrap.setOption("bufferFactory", new DirectChannelBufferFactory());
}
use of org.jboss.netty.channel.ChannelPipelineFactory in project storm by apache.
the class ThriftNettyClientCodec method pipelineFactory.
public ChannelPipelineFactory pipelineFactory() {
return new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("encoder", new ThriftEncoder());
pipeline.addLast("decoder", new ThriftDecoder());
if (authMethod == AuthMethod.KERBEROS) {
try {
LOG.debug("Adding KerberosSaslClientHandler to pacemaker client pipeline.");
pipeline.addLast(KERBEROS_HANDLER, new KerberosSaslClientHandler(client, storm_conf, AuthUtils.LOGIN_CONTEXT_PACEMAKER_CLIENT, host));
} catch (IOException e) {
throw new RuntimeException(e);
}
} else if (authMethod == AuthMethod.DIGEST) {
try {
LOG.debug("Adding SaslStormClientHandler to pacemaker client pipeline.");
pipeline.addLast(SASL_HANDLER, new SaslStormClientHandler(client));
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
client.channelReady();
}
pipeline.addLast("PacemakerClientHandler", new PacemakerClientHandler(client));
return pipeline;
}
};
}
use of org.jboss.netty.channel.ChannelPipelineFactory in project storm by apache.
the class ThriftNettyServerCodec method pipelineFactory.
public ChannelPipelineFactory pipelineFactory() {
return new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("encoder", new ThriftEncoder());
pipeline.addLast("decoder", new ThriftDecoder());
if (authMethod == AuthMethod.DIGEST) {
try {
LOG.debug("Adding SaslStormServerHandler to pacemaker server pipeline.");
pipeline.addLast(SASL_HANDLER, new SaslStormServerHandler((ISaslServer) server));
} catch (IOException e) {
throw new RuntimeException(e);
}
} else if (authMethod == AuthMethod.KERBEROS) {
try {
LOG.debug("Adding KerberosSaslServerHandler to pacemaker server pipeline.");
ArrayList<String> authorizedUsers = new ArrayList(1);
authorizedUsers.add((String) storm_conf.get(Config.NIMBUS_DAEMON_USER));
pipeline.addLast(KERBEROS_HANDLER, new KerberosSaslServerHandler((ISaslServer) server, storm_conf, AuthUtils.LOGIN_CONTEXT_PACEMAKER_SERVER, authorizedUsers));
} catch (IOException e) {
throw new RuntimeException(e);
}
} else if (authMethod == AuthMethod.NONE) {
LOG.debug("Not authenticating any clients. AuthMethod is NONE");
}
pipeline.addLast("handler", new StormServerHandler(server));
return pipeline;
}
};
}
Aggregations