Search in sources :

Example 81 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture 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();
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) Checkpoint(com.linkedin.databus.core.Checkpoint) NioClientSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory) ClientBootstrap(org.jboss.netty.bootstrap.ClientBootstrap) InetSocketAddress(java.net.InetSocketAddress) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) Channel(org.jboss.netty.channel.Channel)

Example 82 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture in project databus by linkedin.

the class TestHttpResponseProcessor method testConnectFail.

@Test
public void testConnectFail() throws DatabusException {
    Logger log = Logger.getLogger("GenericHttpResponseHandler.testConnectFail");
    TestHttpResponseProcessor respProcessor = new TestHttpResponseProcessor(log);
    TestConnectListener connectListener = new TestConnectListener(log);
    TestSendRequestListener requestListener = new TestSendRequestListener(log);
    TestCloseListener closeListener = new TestCloseListener(log);
    //Need this call to set respProcessor without triggering erroneous check
    final GenericHttpResponseHandler responseHandler = new GenericHttpResponseHandler(respProcessor, KeepAliveType.KEEP_ALIVE);
    responseHandler.setRequestListener(requestListener);
    responseHandler.setConnectionListener(connectListener);
    responseHandler.setCloseListener(closeListener);
    //use port 0 to generate connect fail
    ChannelFuture channelFuture = createChannelFuture(responseHandler, 0);
    Channel channel = channelFuture.getChannel();
    try {
        channel.write(new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/test"));
        final List<String> respCallbacks = respProcessor.getCallbacks();
        final List<String> connectCallbacks = connectListener.getCallbacks();
        final List<String> requestCallbacks = requestListener.getCallbacks();
        final List<String> closeChannelCallbacks = closeListener.getCallbacks();
        TestUtil.assertWithBackoff(new ConditionCheck() {

            @Override
            public boolean check() {
                return 1 == closeChannelCallbacks.size();
            }
        }, "waiting for close channel callback", 1000, null);
        //make sure that no new callbacks have showed up
        stateSanityCheck(connectCallbacks, requestCallbacks, respCallbacks, closeChannelCallbacks);
        Assert.assertEquals(connectCallbacks.size(), 1);
        Assert.assertEquals(connectCallbacks.get(0), "onConnectFailure");
        Assert.assertEquals(closeChannelCallbacks.size(), 1);
        Assert.assertEquals(closeChannelCallbacks.get(0), "onChannelClose");
    } finally {
        channel.close();
    }
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) ConditionCheck(com.linkedin.databus2.test.ConditionCheck) Channel(org.jboss.netty.channel.Channel) Logger(org.apache.log4j.Logger) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) Test(org.testng.annotations.Test)

Example 83 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture in project databus by linkedin.

the class SimpleTestClientConnection method start.

public void start(final int serverAddr) {
    _shutdownRequested = false;
    _shutdown = false;
    _connected = false;
    _thread = new Thread(new Runnable() {

        @Override
        public void run() {
            //System.err.println("Client running on thread: " + Thread.currentThread());
            ChannelFuture connectFuture = _clientBootstrap.connect(new LocalAddress(serverAddr));
            connectFuture.awaitUninterruptibly();
            _channel = connectFuture.getChannel();
            _lock.lock();
            try {
                _connected = connectFuture.isSuccess();
                _connectedCondition.signalAll();
                while (!_shutdownRequested) {
                    try {
                        _shutdownReqCondition.await();
                    } catch (InterruptedException ie) {
                    }
                }
                _shutdown = true;
                _shutdownCondition.signalAll();
            } finally {
                _lock.unlock();
            }
        }
    });
    _thread.setDaemon(true);
    _thread.start();
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) LocalAddress(org.jboss.netty.channel.local.LocalAddress)

Example 84 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture in project databus by linkedin.

the class SimpleTestClientConnection method stop.

public void stop() {
    _lock.lock();
    try {
        _shutdownRequested = true;
        _shutdownReqCondition.signalAll();
        while (!_shutdown) {
            try {
                _shutdownCondition.await();
            } catch (InterruptedException ie) {
            }
        }
    } finally {
        _lock.unlock();
    }
    ChannelFuture closeFuture = _channel.close();
    closeFuture.awaitUninterruptibly();
    _clientBootstrap.releaseExternalResources();
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture)

Example 85 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture in project databus by linkedin.

the class SimpleDatabusResponse method writeToChannelAsBinary.

@Override
public void writeToChannelAsBinary(ChannelHandlerContext ctx, ChannelFuture future) {
    ChannelFuture realFuture = (null != future) ? future : Channels.future(ctx.getChannel());
    ChannelBuffer serializedResponse = serializeToBinary();
    DownstreamMessageEvent e = new DownstreamMessageEvent(ctx.getChannel(), realFuture, serializedResponse, ctx.getChannel().getRemoteAddress());
    ctx.sendDownstream(e);
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) DownstreamMessageEvent(org.jboss.netty.channel.DownstreamMessageEvent) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

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