Search in sources :

Example 66 with ChannelFuture

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();
    }
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture)

Example 67 with ChannelFuture

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);
    }
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) ChannelFuture(org.jboss.netty.channel.ChannelFuture) HandlerException(co.cask.cdap.common.HandlerException) InetSocketAddress(java.net.InetSocketAddress) SSLEngine(javax.net.ssl.SSLEngine) Channel(org.jboss.netty.channel.Channel) SSLContext(javax.net.ssl.SSLContext) ChannelFutureListener(org.jboss.netty.channel.ChannelFutureListener) SslHandler(org.jboss.netty.handler.ssl.SslHandler) HttpChunk(org.jboss.netty.handler.codec.http.HttpChunk)

Example 68 with ChannelFuture

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);
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) ChannelFuture(org.jboss.netty.channel.ChannelFuture) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 69 with ChannelFuture

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;
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) DetectFuture(org.opennms.netmgt.provision.DetectFuture) ChannelFutureListener(org.jboss.netty.channel.ChannelFutureListener)

Example 70 with ChannelFuture

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");
    }
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) RemotingException(com.alibaba.dubbo.remoting.RemotingException)

Aggregations

ChannelFuture (org.jboss.netty.channel.ChannelFuture)122 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)36 Channel (org.jboss.netty.channel.Channel)33 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)29 ChannelFutureListener (org.jboss.netty.channel.ChannelFutureListener)26 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)25 InetSocketAddress (java.net.InetSocketAddress)22 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)22 DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)19 SucceededChannelFuture (org.jboss.netty.channel.SucceededChannelFuture)13 Test (org.junit.Test)13 ClientBootstrap (org.jboss.netty.bootstrap.ClientBootstrap)12 InvocationOnMock (org.mockito.invocation.InvocationOnMock)11 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10 NioClientSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory)8 Test (org.testng.annotations.Test)8 ConnectException (java.net.ConnectException)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6