use of org.jboss.netty.channel.ChannelFuture in project camel by apache.
the class SingleTCPNettyServerBootstrapFactory method doSuspend.
@Override
protected void doSuspend() throws Exception {
if (channel != null) {
LOG.debug("ServerBootstrap unbinding from {}:{}", configuration.getHost(), configuration.getPort());
ChannelFuture future = channel.unbind();
future.awaitUninterruptibly();
}
}
use of org.jboss.netty.channel.ChannelFuture in project cdap by caskdata.
the class HttpRequestHandler method messageReceived.
@Override
public void messageReceived(final ChannelHandlerContext ctx, MessageEvent event) throws Exception {
if (channelClosed) {
return;
}
final Channel inboundChannel = event.getChannel();
Object msg = event.getMessage();
if (msg instanceof HttpChunk) {
// This case below should never happen this would mean we get Chunks before HTTPMessage.
if (chunkSender == null) {
throw new HandlerException(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Chunk received and event sender is null");
}
chunkSender.send(msg);
} else if (msg instanceof HttpRequest) {
// Discover and forward event.
HttpRequest request = (HttpRequest) msg;
request = applyProxyRules(request);
// Suspend incoming traffic until connected to the outbound service.
inboundChannel.setReadable(false);
WrappedDiscoverable discoverable = getDiscoverable(request, (InetSocketAddress) inboundChannel.getLocalAddress());
// If no event sender, make new connection, otherwise reuse existing one.
MessageSender sender = discoveryLookup.get(discoverable);
if (sender == null || !sender.isConnected()) {
InetSocketAddress address = discoverable.getSocketAddress();
ChannelFuture future = clientBootstrap.connect(address);
final Channel outboundChannel = future.getChannel();
outboundChannel.getPipeline().addAfter("request-encoder", "outbound-handler", new OutboundHandler(inboundChannel));
if (Arrays.equals(Constants.Security.SSL_URI_SCHEME.getBytes(), discoverable.getPayload())) {
SSLContext clientContext;
try {
clientContext = SSLContext.getInstance("TLS");
clientContext.init(null, PermissiveTrustManagerFactory.getTrustManagers(), null);
} catch (NoSuchAlgorithmException | KeyManagementException e) {
throw new RuntimeException("SSL is enabled for app-fabric but failed to create SSLContext in the router " + "client.", e);
}
SSLEngine engine = clientContext.createSSLEngine();
engine.setUseClientMode(true);
engine.setEnabledProtocols(new String[] { "TLSv1.2", "TLSv1.1", "TLSv1" });
outboundChannel.getPipeline().addFirst("ssl", new SslHandler(engine));
LOG.trace("Adding ssl handler to the pipeline.");
}
sender = new MessageSender(inboundChannel, future);
discoveryLookup.put(discoverable, sender);
// Remember the in-flight outbound channel
inboundChannel.setAttachment(outboundChannel);
outboundChannel.getCloseFuture().addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
inboundChannel.getPipeline().execute(new Runnable() {
@Override
public void run() {
// close the inbound channel as well if it carries the in-flight request
if (outboundChannel.equals(inboundChannel.getAttachment())) {
closeOnFlush(inboundChannel);
}
}
});
}
});
} else {
Channel outboundChannel = (Channel) inboundChannel.getAttachment();
if (outboundChannel != null) {
// Set outbound channel to be readable in case previous request has set it as non-readable
outboundChannel.setReadable(true);
}
}
// Send the message.
sender.send(request);
inboundChannel.setReadable(true);
//Save the channelFuture for subsequent chunks
if (request.isChunked()) {
chunkSender = sender;
}
} else {
super.messageReceived(ctx, event);
}
}
use of org.jboss.netty.channel.ChannelFuture in project cdap by caskdata.
the class HttpStatusRequestHandler method messageReceived.
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent event) throws Exception {
Object msg = event.getMessage();
if (msg instanceof HttpRequest) {
HttpRequest request = (HttpRequest) msg;
// be served by the router itself without it talking to any downstream services
if (request.getUri().equals(Constants.EndPoints.STATUS)) {
String statusString = Constants.Monitor.STATUS_OK;
ChannelBuffer responseContent = ChannelBuffers.wrappedBuffer(Charsets.UTF_8.encode(statusString));
HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
httpResponse.setHeader(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
httpResponse.setHeader(HttpHeaders.Names.CONTENT_LENGTH, responseContent.readableBytes());
httpResponse.setContent(responseContent);
ChannelFuture writeFuture = Channels.future(event.getChannel());
Channels.write(ctx, writeFuture, httpResponse);
writeFuture.addListener(ChannelFutureListener.CLOSE);
return;
}
}
super.messageReceived(ctx, event);
}
use of org.jboss.netty.channel.ChannelFuture in project opennms by OpenNMS.
the class DetectFutureNettyImpl method addListener.
@Override
public DetectFuture addListener(final DetectFutureListener<DetectFuture> listener) {
final DetectFuture thisFuture = this;
m_future.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
listener.operationComplete(thisFuture);
}
});
return this;
}
use of org.jboss.netty.channel.ChannelFuture in project dubbo by alibaba.
the class NettyChannel method send.
public void send(Object message, boolean sent) throws RemotingException {
super.send(message, sent);
boolean success = true;
int timeout = 0;
try {
ChannelFuture future = channel.write(message);
if (sent) {
timeout = getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
success = future.await(timeout);
}
Throwable cause = future.getCause();
if (cause != null) {
throw cause;
}
} catch (Throwable e) {
throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
}
if (!success) {
throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + "in timeout(" + timeout + "ms) limit");
}
}
Aggregations