Apache MINA NIO support
I am new to using MINA. I have a program that uses MINA NIOconnector to connect to a host. I can send data as well as receive. This can be seen from the log4j log I am attaching below.
E:\>java TC4HostClient
[12:21:46] NioProcessor-1 INFO [] [] [org.apache.mina.filter.logging.LoggingFil
ter] - CREATED
[12:21:46] NioProcessor-1 INFO [] [] [org.apache.mina.filter.logging.LoggingFil
ter] - OPENED
Opened
CGS Sign On
[12:21:46] NioProcessor-1 INFO [] [] [org.apache.mina.filter.logging.LoggingFil
ter] - SENT: HeapBuffer[pos=0 lim=370 cap=512: 20 20 20 20 20 20 20 20 20 20 20
20 20 20 20 20...]
[12:21:46] NioProcessor-1 INFO [] [] [org.apache.mina.filter.logging.LoggingFil
ter] - SENT: HeapBuffer[pos=0 lim=0 cap=0: empty]
Message Sent 00000333CST 1001010 00000308000003080010000
000009600000000FTS O00000146TC4DS 001WSJTC41 ---001NTMU9001-I ---
-----000 0030000000012400000096500007013082015SATYA 500000
010165070000002200011
01800000000022000001241 172.16.25.122 02
[12:21:46] NioProcessor-1 INFO [] [] [org.apache.mina.filter.logging.LoggingFil
ter] - RECEIVED: HeapBuffer[pos=0 lim=36 cap=2048: 20 20 20 20 20 20 20 20 20 20
20 20 20 20 20 20...]
[12:21:46] NioProcessor-1 INFO [] [] [org.apache.mina.filter.logging.LoggingFil
ter] - RECEIVED: HeapBuffer[pos=0 lim=505 cap=2048: 31 20 20 20 20 20 20 20 20 3
0 30 30 30 30 34 38...]
After Writing
[12:21:52] NioProcessor-1 INFO [] [] [org.apache.mina.filter.logging.LoggingFil
ter] - CLOSED
Although I can see "RECEIVED" in the log my handler messageReceived method is not being called. Can someone please help me in this regard and tell me what I am doing wrong.
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.net.SocketAddress;
import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.core.future.*;
public class TC4HostClient
{
private static final int PORT = 9123;
public static void main( String[] args ) throws IOException,Exception
{
NioSocketConnector connector = new NioSocketConnector();
SocketAddress address = new InetSocketAddress("172.16.25.3", 8004);
connector.getSessionConfig().setReadBufferSize( 2048 );
connector.getFilterChain().addLast( "logger", new LoggingFilter() );
connector.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "UTF-8" ))));
connector.setHandler(new TC4HostClientHandler());
ConnectFuture future1 = connector.connect(address);
future1.awaitUninterruptibly();
if (!future1.isConnected()) {
return ;
}
IoSession session = future1.getSession();
System.out.println("CGS Sign On");
session.getConfig().setUseReadOperation(true);
session.write(" 00000333CST 1001010 00000308000003080010000000009600000000FTS O00000146TC4DS 001WSJTC41 ---001NTMU9001-I --------000 0030000000012400000096500007013082015SATYA 500000 010165070000002200011 01800000000022000001241 172.16.25.122 02");
session.getCloseFuture().awaitUninterruptibly();
System.out.println("After Writing");
connector.dispose();
}
}
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.core.buffer.IoBuffer;
public class TC4HostClientHandler extends IoHandlerAdapter
{
@Override
public void exceptionCaught( IoSession session, Throwable cause ) throws Exception
{
cause.printStackTrace();
}
@Override
public void messageSent( IoSession session, Object message ) throws Exception
{
String str = message.toString();
System.out.println("Message Sent" + str);
}
@Override
public void messageReceived( IoSession session, Object message ) throws Exception
{
IoBuffer buf = (IoBuffer) message;
// Print out read buffer content.
while (buf.hasRemaining()) {
System.out.print((char) buf.get());
}
System.out.flush();
}
/*
@Override
public void messageReceived( IoSession session, Object message ) throws Exception
{
String str = message.toString();
System.out.println("Message Received : " + str);
}*/
@Override
public void sessionIdle( IoSession session, IdleStatus status ) throws Exception
{
System.out.println( "IDLE " + session.getIdleCount( status ));
}
public void sessionClosed(IoSession session){
System.out.println( "Closed ");
}
public void sessionOpened(IoSession session){
System.out.println( "Opened ");
}
}
a source to share
I can't tell from your included logs, but the TextLineDecoder created by the TextLineCodecFactory will look (by default) for '\ r' (0x0d) or '\ n' (0x0a) to end the line and generate a filled message to be handled by your IoHandlerAdapter. Is the incoming data completed correctly?
a source to share
Today I was working on MINA for the first time, and the harsh reality I found is that help on MINA is hard to find. Even his own website is almost without it. With a few pieces of information I was able to gather, including yours, and with the documentation provided, I was able to connect, send and receive a response from the server. But to my surprise, the messageReceived (..) method was not called, only the logging proved that I received the required response. The log message was in Unicode (hex value). There were no exceptions or any indication of what went wrong. I was looking for it. Then I went through the source of the TextLineCodecFactory and it looked great too. I went through the server code and I found out that they are using their own Codec. So in my opinion your problem is the same as me.Make sure the codec on both sides is the same, or at least do the same conversions.
a source to share