Quantcast
Channel: Apache Timeline
Viewing all articles
Browse latest Browse all 5648

Session closed unexpectedly when using Camel and Mina2

$
0
0
Hi,

We are using Camel 2.13 and Mina2 to write a server for a custom TCP
protocol. The protocol is bundled as a JAR and deployed into Apache Karaf.
Things are working almost as expected except that the server is unexpectedly
closing the session after it sends a response. To explain this I have
provided my code below.

We have a filter that listens for the incoming connection, when it receives
a message it sends an ACK.:

public class HELO_IN extends IoFilterAdapter {
@Override
public void messageReceived(NextFilter nextFilter, IoSession session,
Object message)
throws Exception {
String msg = message.toString();
String msgId = "";

if (msg.startsWith("00"))
msgId = msg.split("\n")[0].split("|")[6];
else if (msg.startsWith("FULL"))
msgId = msg.split("|")[1];

session.write(String.format("\u0002ACK|%s|OK|", msgId));

super.messageReceived(nextFilter, session, message);

This works as I would expect, we receive a message and send the appropriate
ACK back.

The Server code looks like the following:

public class Test extends RouteBuilder {

private Mina2Component minaComp = new Mina2Component();
private ArrayList<IoFilter> filters = new ArrayList<IoFilter>();

@Override
public void configure() throws Exception {
minaComp.setCamelContext(getContext());
filters.add(new HELO_IN());

// Configure connection
Mina2Configuration minaCfg = new Mina2Configuration();
minaCfg.setProtocol("tcp");
minaCfg.setHost("0.0.0.0");
minaCfg.setPort(20001);
minaCfg.setDisconnectOnNoReply(false);
minaCfg.setFilters(filters);
minaCfg.setCodec(
new Mina2TextLineCodecFactory(Charset.defaultCharset(), new
LineDelimiter("\u0003"))
);

//TEST IN
from(minaComp.createEndpoint(minaCfg))
.choice()
.when(body().startsWith("\u0002HELO")).stop() //Filter
out HELO messages
.when(body().startsWith("\u0004")).stop() //Filter out
EOT messages
.when(body().startsWith("\u0005")).stop() //Filter out
ENQ messages
.otherwise()
.to("file:blah")
.endChoice();

Again this works, if Camel receives a HELO message they are filtered out as
expected. If we receive a message with some other content this is logged to
the "blah" folder (only for testing purposes - will be a rabbit queue).

However, the issue is that once the message has been received and processed
(or filtered out) the connection is closed (extract from Karaf log file):

2014-04-29 15:24:02,537 | INFO | NioProcessor-1 | LoggingFilter
| 43 - org.apache.mina.core - 2.0.7 | CREATED
2014-04-29 15:24:02,541 | INFO | pool-23-thread-1 | LoggingFilter
| 43 - org.apache.mina.core - 2.0.7 | OPENED
2014-04-29 15:24:02,541 | INFO | pool-23-thread-1 | LoggingFilter
| 43 - org.apache.mina.core - 2.0.7 | RECEIVED: HELO2|||
2014-04-29 15:24:02,563 | INFO | pool-23-thread-1 | LoggingFilter
| 43 - org.apache.mina.core - 2.0.7 | SENT: ACK||OK|
2014-04-29 15:24:02,563 | INFO | pool-23-thread-1 | LoggingFilter
| 43 - org.apache.mina.core - 2.0.7 | CLOSED
2014-04-29 15:24:05,322 | INFO | NioProcessor-2 | LoggingFilter
| 43 - org.apache.mina.core - 2.0.7 | CREATED
2014-04-29 15:24:05,322 | INFO | pool-23-thread-2 | LoggingFilter
| 43 - org.apache.mina.core - 2.0.7 | OPENED
2014-04-29 15:24:05,322 | INFO | pool-23-thread-2 | LoggingFilter
| 43 - org.apache.mina.core - 2.0.7 | RECEIVED: HELO2|||
2014-04-29 15:24:05,359 | INFO | pool-23-thread-2 | LoggingFilter
| 43 - org.apache.mina.core - 2.0.7 | SENT: ACK||OK|
2014-04-29 15:24:05,359 | INFO | pool-23-thread-2 | LoggingFilter
| 43 - org.apache.mina.core - 2.0.7 | CLOSED

How can I prevent the connection from being closed? This needs to be a log
running connection due to limitations on the client side.

Any help would be greatly appreciated as I am truly stuck on this issue!

Viewing all articles
Browse latest Browse all 5648

Trending Articles