Search in sources :

Example 96 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture in project storm by nathanmarz.

the class StormClientHandler method sendRequests.

/**
     * Retrieve a request from message queue, and send to server
     * @param channel
     */
private void sendRequests(Channel channel, final MessageBatch requests) {
    if (requests == null || requests.size() == 0 || being_closed.get())
        return;
    //if task==CLOSE_MESSAGE for our last request, the channel is to be closed
    Object last_msg = requests.get(requests.size() - 1);
    if (last_msg == ControlMessage.CLOSE_MESSAGE) {
        being_closed.set(true);
        requests.remove(last_msg);
    }
    //we may don't need do anything if no requests found
    if (requests.isEmpty()) {
        if (being_closed.get())
            client.close_n_release();
        return;
    }
    //write request into socket channel
    ChannelFuture future = channel.write(requests);
    future.addListener(new ChannelFutureListener() {

        public void operationComplete(ChannelFuture future) throws Exception {
            if (!future.isSuccess()) {
                LOG.info("failed to send requests:", future.getCause());
                future.getChannel().close();
            } else {
                LOG.debug("{} request(s) sent", requests.size());
            }
            if (being_closed.get())
                client.close_n_release();
        }
    });
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) ChannelFutureListener(org.jboss.netty.channel.ChannelFutureListener) ConnectException(java.net.ConnectException)

Example 97 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture in project pinpoint by naver.

the class DefaultPinpointClientFactory method connect.

public PinpointClient connect(InetSocketAddress connectAddress) throws PinpointSocketException {
    ChannelFuture connectFuture = bootstrap.connect(connectAddress);
    PinpointClientHandler pinpointClientHandler = getSocketHandler(connectFuture, connectAddress);
    PinpointClient pinpointClient = new DefaultPinpointClient(pinpointClientHandler);
    traceSocket(pinpointClient);
    return pinpointClient;
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture)

Example 98 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture in project pinpoint by naver.

the class PinpointClientHandshaker method handshake.

private void handshake(HandshakeJob handshakeJob) {
    handshakeCount.incrementAndGet();
    Channel channel = handshakeJob.getChannel();
    ControlHandshakePacket packet = handshakeJob.getHandshakePacket();
    logger.info("{} do handshake({}/{}). channel:{}.", simpleClassNameAndHashCodeString(), handshakeCount.get(), maxHandshakeCount, channel);
    final ChannelFuture future = channel.write(packet);
    future.addListener(handShakeFailFutureListener);
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) Channel(org.jboss.netty.channel.Channel) ControlHandshakePacket(com.navercorp.pinpoint.rpc.packet.ControlHandshakePacket)

Example 99 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture in project pinpoint by naver.

the class PinpointClientStateTest method connect.

private DefaultPinpointClientHandler connect(PinpointClientFactory factory) {
    ChannelFuture future = factory.reconnect(new InetSocketAddress("127.0.0.1", bindPort));
    PinpointClientHandler handler = getSocketHandler(future, new InetSocketAddress("127.0.0.1", bindPort));
    return (DefaultPinpointClientHandler) handler;
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress)

Example 100 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture in project bagheera by mozilla-metrics.

the class SubmissionHandler method exceptionCaught.

@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
    Throwable cause = e.getCause();
    HttpResponse response = null;
    if (cause instanceof ClosedChannelException) {
    // NOOP
    } else if (cause instanceof TooLongFrameException) {
        response = new DefaultHttpResponse(HTTP_1_1, REQUEST_ENTITY_TOO_LARGE);
    } else if (cause instanceof InvalidPathException) {
        response = new DefaultHttpResponse(HTTP_1_1, NOT_FOUND);
    } else if (cause instanceof HttpSecurityException) {
        LOG.error(cause.getMessage());
        response = new DefaultHttpResponse(HTTP_1_1, FORBIDDEN);
    } else {
        LOG.error(cause.getMessage());
        response = new DefaultHttpResponse(HTTP_1_1, INTERNAL_SERVER_ERROR);
    }
    if (response != null) {
        ChannelFuture future = e.getChannel().write(response);
        future.addListener(ChannelFutureListener.CLOSE);
        updateResponseMetrics(null, response.getStatus().getCode());
    }
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) ClosedChannelException(java.nio.channels.ClosedChannelException) TooLongFrameException(org.jboss.netty.handler.codec.frame.TooLongFrameException) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse)

Aggregations

ChannelFuture (org.jboss.netty.channel.ChannelFuture)105 Channel (org.jboss.netty.channel.Channel)31 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)25 ChannelFutureListener (org.jboss.netty.channel.ChannelFutureListener)24 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)20 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)19 InetSocketAddress (java.net.InetSocketAddress)18 DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)18 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)14 SucceededChannelFuture (org.jboss.netty.channel.SucceededChannelFuture)13 Test (org.junit.Test)13 InvocationOnMock (org.mockito.invocation.InvocationOnMock)11 ClientBootstrap (org.jboss.netty.bootstrap.ClientBootstrap)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10 Test (org.testng.annotations.Test)8 ConnectException (java.net.ConnectException)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 NioClientSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory)6 ChunkedBodyWritableByteChannel (com.linkedin.databus2.core.container.netty.ChunkedBodyWritableByteChannel)5