use of org.jboss.netty.channel.ChannelHandler in project camel by apache.
the class DefaultClientPipelineFactory method getPipeline.
public ChannelPipeline getPipeline() throws Exception {
// create a new pipeline
ChannelPipeline channelPipeline = Channels.pipeline();
SslHandler sslHandler = configureClientSSLOnDemand();
if (sslHandler != null) {
// must close on SSL exception
sslHandler.setCloseOnSSLException(true);
LOG.debug("Client SSL handler configured and added to the ChannelPipeline: {}", sslHandler);
addToPipeline("ssl", channelPipeline, sslHandler);
}
List<ChannelHandler> decoders = producer.getConfiguration().getDecoders();
for (int x = 0; x < decoders.size(); x++) {
ChannelHandler decoder = decoders.get(x);
if (decoder instanceof ChannelHandlerFactory) {
// use the factory to create a new instance of the channel as it may not be shareable
decoder = ((ChannelHandlerFactory) decoder).newChannelHandler();
}
addToPipeline("decoder-" + x, channelPipeline, decoder);
}
List<ChannelHandler> encoders = producer.getConfiguration().getEncoders();
for (int x = 0; x < encoders.size(); x++) {
ChannelHandler encoder = encoders.get(x);
if (encoder instanceof ChannelHandlerFactory) {
// use the factory to create a new instance of the channel as it may not be shareable
encoder = ((ChannelHandlerFactory) encoder).newChannelHandler();
}
addToPipeline("encoder-" + x, channelPipeline, encoder);
}
// do we use request timeout?
if (producer.getConfiguration().getRequestTimeout() > 0) {
if (LOG.isTraceEnabled()) {
LOG.trace("Using request timeout {} millis", producer.getConfiguration().getRequestTimeout());
}
ChannelHandler timeout = new ReadTimeoutHandler(producer.getEndpoint().getTimer(), producer.getConfiguration().getRequestTimeout(), TimeUnit.MILLISECONDS);
addToPipeline("timeout", channelPipeline, timeout);
}
// our handler must be added last
addToPipeline("handler", channelPipeline, new ClientChannelHandler(producer));
LOG.trace("Created ChannelPipeline: {}", channelPipeline);
return channelPipeline;
}
use of org.jboss.netty.channel.ChannelHandler in project camel by apache.
the class NettyHttpCompressTest method createRegistry.
// setup the decompress decoder here
@Override
protected JndiRegistry createRegistry() throws Exception {
JndiRegistry registry = super.createRegistry();
List<ChannelHandler> decoders = new ArrayList<ChannelHandler>();
decoders.add(new HttpContentDecompressor());
registry.bind("myDecoders", decoders);
return registry;
}
use of org.jboss.netty.channel.ChannelHandler in project databus by linkedin.
the class TestHttpResponseProcessor method getResponseHandler.
static GenericHttpResponseHandler getResponseHandler(Channel clientChannel) {
ChannelPipeline pipe = clientChannel.getPipeline();
ChannelHandler handler = pipe.get("3");
Assert.assertNotNull(handler, "unable to find handler. Did client pipeline factory change?");
Assert.assertTrue(handler instanceof GenericHttpResponseHandler, "expected GenericHttpResponseHandler; found: " + handler.getClass() + ". Did client pipeline factory change?");
return (GenericHttpResponseHandler) handler;
}
use of org.jboss.netty.channel.ChannelHandler in project databus by linkedin.
the class DummySuccessfulErrorCountingConsumer method testInStreamTimeOut2.
/**
* Tests the logic of the client to handle Timeout that comes while processing stream request.
* the script:
* setup client and connect to one of the servers
* wait for /sources and register call and replay
* save the 'future' of the write operation for the /stream call. Replace this future down the stream with the fake one,
* so the notification of write completion will never come
* make server send only headers info first
* make server send data, but intercept the message before it reaches the client. At this moment fire WriteTimeout
* exception from a separate thread.
* Make sure PullerThread doesn't get two error messages (and as a result tries to setup up two new connections)
*/
@Test
public void testInStreamTimeOut2() throws Exception {
final Logger log = Logger.getLogger("TestDatabusHttpClient.testInStreamTimeout2");
MockServerChannelHandler.LOG.setLevel(Level.DEBUG);
// log.setLevel(Level.);
final int eventsNum = 20;
DbusEventInfo[] eventInfos = createSampleSchema1Events(eventsNum);
// simulate relay buffers
DbusEventBuffer relayBuffer = new DbusEventBuffer(_bufCfg);
relayBuffer.start(0);
writeEventsToBuffer(relayBuffer, eventInfos, 4);
// prepare stream response
Checkpoint cp = Checkpoint.createFlexibleCheckpoint();
final DbusEventsStatisticsCollector stats = new DbusEventsStatisticsCollector(1, "test1", true, false, null);
// create ChunnelBuffer and fill it with events from relayBuffer
ChannelBuffer streamResPrefix = NettyTestUtils.streamToChannelBuffer(relayBuffer, cp, 20000, stats);
// create client
_stdClientCfgBuilder.getContainer().setReadTimeoutMs(DEFAULT_READ_TIMEOUT_MS);
final DatabusHttpClientImpl client = new DatabusHttpClientImpl(_stdClientCfgBuilder.build());
final TestConsumer consumer = new TestConsumer();
client.registerDatabusStreamListener(consumer, null, SOURCE1_NAME);
// connect to a relay created in SetupClass (one out of three)
client.start();
// wait until a connection made
try {
TestUtil.assertWithBackoff(new ConditionCheck() {
@Override
public boolean check() {
return client._relayConnections.size() == 1;
}
}, "sources connection present", 100, log);
// get the connection
final DatabusSourcesConnection clientConn = client._relayConnections.get(0);
TestUtil.assertWithBackoff(new ConditionCheck() {
@Override
public boolean check() {
return null != clientConn.getRelayPullThread().getLastOpenConnection();
}
}, "relay connection present", 100, log);
// figure out connection details
final NettyHttpDatabusRelayConnection relayConn = (NettyHttpDatabusRelayConnection) clientConn.getRelayPullThread().getLastOpenConnection();
final NettyHttpDatabusRelayConnectionInspector relayConnInsp = new NettyHttpDatabusRelayConnectionInspector(relayConn);
// wait until client is connected
TestUtil.assertWithBackoff(new ConditionCheck() {
@Override
public boolean check() {
return null != relayConnInsp.getChannel() && relayConnInsp.getChannel().isConnected();
}
}, "client connected", 200, log);
// figure out which port we got connected to on the server side
Channel clientChannel = relayConnInsp.getChannel();
InetSocketAddress relayAddr = (InetSocketAddress) clientChannel.getRemoteAddress();
int relayPort = relayAddr.getPort();
log.info("relay selected: " + relayPort);
// add our handler to the client's pipeline which will generate the timeout
MockServerChannelHandler mock = new MockServerChannelHandler();
clientChannel.getPipeline().addBefore("inflater", "mockServer", mock);
Map<String, ChannelHandler> map = clientChannel.getPipeline().toMap();
boolean handlerFound = false;
for (Map.Entry<String, ChannelHandler> m : map.entrySet()) {
if (LOG.isDebugEnabled())
LOG.debug(m.getKey() + "=>" + m.getValue());
if (m.getKey().equals("mockServer"))
handlerFound = true;
}
Assert.assertTrue(handlerFound, "handler added");
SimpleTestServerConnection relay = null;
// Find the relay's object
for (int i = 0; i < RELAY_PORT.length; ++i) {
if (relayPort == RELAY_PORT[i])
relay = _dummyServer[i];
}
assertTrue(null != relay);
SocketAddress clientAddr = clientChannel.getLocalAddress();
final SocketAddress testClientAddr = clientAddr;
final SimpleTestServerConnection testRelay = relay;
TestUtil.assertWithBackoff(new ConditionCheck() {
@Override
public boolean check() {
return null != testRelay.getChildChannel(testClientAddr);
}
}, "relay detects new connection", 1000, log);
Channel serverChannel = relay.getChildChannel(clientAddr);
assertTrue(null != serverChannel);
ChannelPipeline serverPipeline = serverChannel.getPipeline();
SimpleObjectCaptureHandler objCapture = (SimpleObjectCaptureHandler) serverPipeline.get("3");
// process the /sources request
NettyTestUtils.waitForHttpRequest(objCapture, SOURCES_REQUEST_REGEX, 1000);
objCapture.clear();
// send back the /sources response
HttpResponse sourcesResp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
sourcesResp.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
sourcesResp.setHeader(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED);
HttpChunk body = new DefaultHttpChunk(ChannelBuffers.wrappedBuffer(("[{\"id\":1,\"name\":\"" + SOURCE1_NAME + "\"}]").getBytes(Charset.defaultCharset())));
NettyTestUtils.sendServerResponses(relay, clientAddr, sourcesResp, body);
// make sure the client processes the response correctly
TestUtil.assertWithBackoff(new ConditionCheck() {
@Override
public boolean check() {
String idListString = clientConn.getRelayPullThread()._currentState.getSourcesIdListString();
return "1".equals(idListString);
}
}, "client processes /sources response", 100, log);
log.info("process the /register request");
NettyTestUtils.waitForHttpRequest(objCapture, "/register.*", 1000);
objCapture.clear();
String msgHistory = clientConn.getRelayPullThread().getMessageHistoryLog();
log.info("MSG HISTORY before: " + msgHistory);
// make sure our handler will save the 'future' of the next write operation - 'stream'
mock.enableSaveTheFuture(true);
log.info("send back the /register response");
RegisterResponseEntry entry = new RegisterResponseEntry(1L, (short) 1, SOURCE1_SCHEMA_STR);
String responseStr = NettyTestUtils.generateRegisterResponse(entry);
body = new DefaultHttpChunk(ChannelBuffers.wrappedBuffer(responseStr.getBytes(Charset.defaultCharset())));
NettyTestUtils.sendServerResponses(relay, clientAddr, sourcesResp, body);
log.info("make sure the client processes the response /register correctly");
TestUtil.assertWithBackoff(new ConditionCheck() {
@Override
public boolean check() {
DispatcherState dispState = clientConn.getRelayDispatcher().getDispatcherState();
return null != dispState.getSchemaMap() && 1 == dispState.getSchemaMap().size();
}
}, "client processes /register response", 100, log);
mock.disableWriteComplete(true);
log.info("process /stream call and return a response");
NettyTestUtils.waitForHttpRequest(objCapture, "/stream.*", 1000);
objCapture.clear();
log.info("***1");
// disable save future as it should be saved by now
mock.enableSaveTheFuture(false);
log.info("***2");
final HttpResponse streamResp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
streamResp.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
streamResp.setHeader(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED);
log.info("***3");
// timeout for local netty calls (in test only)
int timeout = 1000;
// send header info
relay.sendServerResponse(clientAddr, sourcesResp, timeout);
TestUtil.sleep(1000);
log.info("***4");
// when write data arrives from the server - we want to simulate/throw WriteTimeoutException
mock.enableThrowWTOException(true);
// send data
relay.sendServerResponse(clientAddr, new DefaultHttpChunk(streamResPrefix), timeout);
relay.sendServerResponse(clientAddr, HttpChunk.LAST_CHUNK, timeout);
log.info("***5");
// make sure close channel event and future failure are propagated
TestUtil.sleep(3000);
// get the history and validate it
String expectedHistory = "[START, PICK_SERVER, REQUEST_SOURCES, SOURCES_RESPONSE_SUCCESS, REQUEST_REGISTER, REGISTER_RESPONSE_SUCCESS, REQUEST_STREAM, STREAM_REQUEST_SUCCESS, STREAM_RESPONSE_DONE, REQUEST_STREAM, STREAM_REQUEST_ERROR, PICK_SERVER, REQUEST_SOURCES]".trim();
msgHistory = clientConn.getRelayPullThread().getMessageHistoryLog().trim();
log.info("***6");
LOG.info("MSG HISTORY: " + msgHistory);
Assert.assertEquals(msgHistory, expectedHistory, "Puller thread message history doesn't match");
log.info("***7");
} catch (Exception e2) {
log.info("Got exception" + e2);
} finally {
client.shutdown();
}
}
use of org.jboss.netty.channel.ChannelHandler in project databus by linkedin.
the class DummySuccessfulErrorCountingConsumer method testInStreamTimeOut3.
/**
* same as above, but server doesn't send any data, and WriteComplete comes between WriteTimeout
* and channel close
* @throws Exception
*/
@Test
public void testInStreamTimeOut3() throws Exception {
final Logger log = Logger.getLogger("TestDatabusHttpClient.testInStreamTimeout3");
Level debugLevel = Level.DEBUG;
log.setLevel(debugLevel);
// Logger.getRootLogger().setLevel(Level.DEBUG);
MockServerChannelHandler.LOG.setLevel(debugLevel);
final int eventsNum = 20;
DbusEventInfo[] eventInfos = createSampleSchema1Events(eventsNum);
// simulate relay buffers
DbusEventBuffer relayBuffer = new DbusEventBuffer(_bufCfg);
relayBuffer.start(0);
writeEventsToBuffer(relayBuffer, eventInfos, 4);
// prepare stream response ??????????????//
Checkpoint cp = Checkpoint.createFlexibleCheckpoint();
final DbusEventsStatisticsCollector stats = new DbusEventsStatisticsCollector(1, "test1", true, false, null);
// create ChunnelBuffer and fill it with events from relayBuffer
ChannelBuffer streamResPrefix = NettyTestUtils.streamToChannelBuffer(relayBuffer, cp, 20000, stats);
// create client
_stdClientCfgBuilder.getContainer().setReadTimeoutMs(DEFAULT_READ_TIMEOUT_MS);
final DatabusHttpClientImpl client = new DatabusHttpClientImpl(_stdClientCfgBuilder.build());
final TestConsumer consumer = new TestConsumer();
client.registerDatabusStreamListener(consumer, null, SOURCE1_NAME);
// connect to a relay created in SetupClass (one out of three)
client.start();
// wait until a connection made
try {
TestUtil.assertWithBackoff(new ConditionCheck() {
@Override
public boolean check() {
return client._relayConnections.size() == 1;
}
}, "sources connection present", 100, log);
// get the connection
final DatabusSourcesConnection clientConn = client._relayConnections.get(0);
TestUtil.assertWithBackoff(new ConditionCheck() {
@Override
public boolean check() {
return null != clientConn.getRelayPullThread().getLastOpenConnection();
}
}, "relay connection present", 100, log);
// figure out connection details
final NettyHttpDatabusRelayConnection relayConn = (NettyHttpDatabusRelayConnection) clientConn.getRelayPullThread().getLastOpenConnection();
final NettyHttpDatabusRelayConnectionInspector relayConnInsp = new NettyHttpDatabusRelayConnectionInspector(relayConn);
relayConnInsp.getHandler().getLog().setLevel(debugLevel);
// wait until client is connected
TestUtil.assertWithBackoff(new ConditionCheck() {
@Override
public boolean check() {
return null != relayConnInsp.getChannel() && relayConnInsp.getChannel().isConnected();
}
}, "client connected", 200, log);
// figure out which port we got connected to on the server side
Channel clientChannel = relayConnInsp.getChannel();
InetSocketAddress relayAddr = (InetSocketAddress) clientChannel.getRemoteAddress();
int relayPort = relayAddr.getPort();
log.info("relay selected: " + relayPort);
// add our handler to the client's pipeline which will generate the timeout
MockServerChannelHandler mock = new MockServerChannelHandler();
clientChannel.getPipeline().addBefore("inflater", "mockServer", mock);
// verify it is there
Map<String, ChannelHandler> map = clientChannel.getPipeline().toMap();
boolean handlerFound = false;
for (Map.Entry<String, ChannelHandler> m : map.entrySet()) {
if (LOG.isDebugEnabled())
LOG.debug(m.getKey() + "=>" + m.getValue());
if (m.getKey().equals("mockServer"))
handlerFound = true;
}
Assert.assertTrue(handlerFound, "handler added");
SimpleTestServerConnection relay = null;
// Find the relay's object
for (int i = 0; i < RELAY_PORT.length; ++i) {
if (relayPort == RELAY_PORT[i])
relay = _dummyServer[i];
}
assertTrue(null != relay);
SocketAddress clientAddr = clientChannel.getLocalAddress();
final SocketAddress testClientAddr = clientAddr;
final SimpleTestServerConnection testRelay = relay;
TestUtil.assertWithBackoff(new ConditionCheck() {
@Override
public boolean check() {
return null != testRelay.getChildChannel(testClientAddr);
}
}, "relay detects new connection", 1000, log);
Channel serverChannel = relay.getChildChannel(clientAddr);
assertTrue(null != serverChannel);
ChannelPipeline serverPipeline = serverChannel.getPipeline();
SimpleObjectCaptureHandler objCapture = (SimpleObjectCaptureHandler) serverPipeline.get("3");
// process the /sources request
NettyTestUtils.waitForHttpRequest(objCapture, SOURCES_REQUEST_REGEX, 1000);
objCapture.clear();
// send back the /sources response
HttpResponse httpResp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
httpResp.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
httpResp.setHeader(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED);
HttpChunk body = new DefaultHttpChunk(ChannelBuffers.wrappedBuffer(("[{\"id\":1,\"name\":\"" + SOURCE1_NAME + "\"}]").getBytes(Charset.defaultCharset())));
NettyTestUtils.sendServerResponses(relay, clientAddr, httpResp, body);
// make sure the client processes the response correctly
TestUtil.assertWithBackoff(new ConditionCheck() {
@Override
public boolean check() {
String idListString = clientConn.getRelayPullThread()._currentState.getSourcesIdListString();
return "1".equals(idListString);
}
}, "client processes /sources response", 100, log);
log.debug("process the /register request");
NettyTestUtils.waitForHttpRequest(objCapture, "/register.*", 1000);
objCapture.clear();
String msgHistory = clientConn.getRelayPullThread().getMessageHistoryLog();
log.debug("MSG HISTORY before: " + msgHistory);
// make sure our handler will save the 'future' of the next write operation - 'stream'
mock.enableSaveTheFuture(true);
// delay write complete. insert Timeout exception before that
mock.delayWriteComplete(true);
log.debug("send back the /register response");
RegisterResponseEntry entry = new RegisterResponseEntry(1L, (short) 1, SOURCE1_SCHEMA_STR);
String responseStr = NettyTestUtils.generateRegisterResponse(entry);
body = new DefaultHttpChunk(ChannelBuffers.wrappedBuffer(responseStr.getBytes(Charset.defaultCharset())));
NettyTestUtils.sendServerResponses(relay, clientAddr, httpResp, body);
log.debug("make sure the client processes the response /register correctly");
TestUtil.assertWithBackoff(new ConditionCheck() {
@Override
public boolean check() {
DispatcherState dispState = clientConn.getRelayDispatcher().getDispatcherState();
return null != dispState.getSchemaMap() && 1 == dispState.getSchemaMap().size();
}
}, "client processes /register response", 100, log);
LOG.info("*************>Message state after write complete is " + relayConnInsp.getResponseHandlerMessageState().toString());
msgHistory = clientConn.getRelayPullThread().getMessageHistoryLog();
log.debug("MSG HISTORY after: " + msgHistory);
// should be one error only
Assert.assertEquals(countOccurencesOfWord(msgHistory, "_ERROR"), 1);
// ////////////////////////////////////////////////////////////////////////////////////////////////
/*
TestUtil.assertWithBackoff(new ConditionCheck()
{
@Override
public boolean check()
{
return client._relayConnections.size() == 1;
}
}, "sources connection present", 100, log);
//get the connection
final DatabusSourcesConnection clientConn1 = client._relayConnections.get(0);
TestUtil.assertWithBackoff(new ConditionCheck()
{
@Override
public boolean check()
{
return null != clientConn1.getRelayPullThread().getLastOpenConnection();
}
}, "relay connection1 present", 100, log);
// figure out connection details
final NettyHttpDatabusRelayConnection relayConn1 =
(NettyHttpDatabusRelayConnection)clientConn1.getRelayPullThread().getLastOpenConnection();
final NettyHttpDatabusRelayConnectionInspector relayConnInsp1 =
new NettyHttpDatabusRelayConnectionInspector(relayConn1);
relayConnInsp1.getHandler().getLog().setLevel(debugLevel);
// wait until client is connected
TestUtil.assertWithBackoff(new ConditionCheck()
{
@Override
public boolean check()
{
return null != relayConnInsp1.getChannel() && relayConnInsp1.getChannel().isConnected();
}
}, "client connected", 200, log);
//figure out which port we got connected to on the server side
Channel clientChannel1 = relayConnInsp1.getChannel();
InetSocketAddress relayAddr1 = (InetSocketAddress)clientChannel1.getRemoteAddress();
relayPort = relayAddr1.getPort();
log.info("relay selected: " + relayPort);
// do it again - no errors
// process the /sources request
captureAndReplySourcesRequest(objCapture, relay, clientAddr, clientConn1, log);
captureAndReplyRegisterRequest(objCapture, relay, clientAddr, clientConn1, log);
log.debug("process /stream call and return a response");
captureAndReplyStreamRequest(objCapture, relay, clientAddr, clientConn1, streamResPrefix, log);
LOG.info("*************>Message state after write complete is " + relayConnInsp1.getResponseHandlerMessageState().toString());
msgHistory = clientConn1.getRelayPullThread().getMessageHistoryLog();
log.debug("MSG HISTORY after: " + msgHistory);
Assert.assertEquals(countOccurencesOfWord(msgHistory, "_ERROR"), 1); //should be one error only
// make sure close channel event and future failure are propagated
TestUtil.sleep(3000);
// get the history and validate it
String expectedHistory = "[START, PICK_SERVER, REQUEST_SOURCES, SOURCES_RESPONSE_SUCCESS, REQUEST_REGISTER, REGISTER_RESPONSE_SUCCESS, REQUEST_STREAM, STREAM_REQUEST_SUCCESS, STREAM_RESPONSE_DONE, REQUEST_STREAM, STREAM_REQUEST_ERROR, PICK_SERVER, REQUEST_SOURCES]".trim();
msgHistory = clientConn.getRelayPullThread().getMessageHistoryLog().trim();
LOG.info("MSG HISTORY: " + msgHistory);
Assert.assertEquals(msgHistory, expectedHistory, "Puller thread message history doesn't match");
*/
} finally {
client.shutdown();
}
}
Aggregations