Apache MINA NIO 连接器帮助

发布于 2024-08-30 21:45:42 字数 5195 浏览 8 评论 0原文

我刚开始使用 MINA。 我有一个使用 MINA NIOconnector 连接到主机的程序。 我能够发送数据,也能够接收数据。从我在下面附上的 log4j 日志可以清楚地看出这一点。

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

虽然我在日志中看到“RECEIVED”,但我的处理程序 messageReceived 方法没有被调用。 谁能在这方面帮助我并告诉我我做错了什么

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 ");
    }
}

I'm new to using MINA.
I've a program which uses MINA NIOconnector to connect to host.
I'm able to send data and also receive. This is clear from log4j log which i'm 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

Though i see "RECEIVED" in log my handler messageReceived method is not being called.
Can anyone please help me in this regard and tell me what i'm 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 ");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鹤舞 2024-09-06 21:45:42

我无法从您包含的日志中看出,但 TextLineCodecFactory 创建的 TextLineDecoder 将寻找(默认情况下)“\r”(0x0d)或“\n”(0x0a)来结束该行并生成完整的消息由您的 IoHandlerAdapter 处理。输入数据是否正确终止?

I can't tell from your included logs, but the TextLineDecoder created by TextLineCodecFactory will be looking for (by default) a '\r' (0x0d) or '\n' (0x0a) to end the line and generate the completed message to be handled by your IoHandlerAdapter. Is the incoming data properly terminated?

幽梦紫曦~ 2024-09-06 21:45:42

今天我第一次在 MINA 上工作,我发现残酷的现实是 MINA 上的帮助很难找到。就连它自己的网站也几乎没有。在我设法收集的一些信息(包括您的信息)的帮助下,并在提供的文档的帮助下,我设法连接、发送和接收来自服务器的响应。但令我惊讶的是,messageReceived(..) 方法没有被调用,只是日志记录证明我收到了所需的响应。日志消息采用 unicode(十六进制值)格式。没有任何异常,也没有任何迹象表明出了什么问题。我搜索了一下。然后我查看了 TextLineCodecFactory 的源代码,它看起来也很好。我查看了服务器代码,发现他们使用的是自定义编解码器。所以在我看来你的问题和我遇到的一样。确保双方的编解码器相同或至少执行相同的转换。

Today I worked on MINA for the first time and the harsh reality I found is that the help on MINA is hard to find. Even its own site is almost without it. With the help of few pieces of info i managed to gather including yours and with the help of the documentation provided I managed to connect, send and receive the response from the server. But to my surprise the messageReceived(..) method was not called, just the logging proved that I received the required response. The log message was in unicode(hex value). There was no exception or any indication about what went wrong. I searched for it. Then I went through the source of TextLineCodecFactory and it looked fine too. I went through the server code and I found out that they were using a custom Codec. So in my opinion yours problem is same as I encountered. Make sure that the codec on both sides is the same or atleast perform same conversions.

白日梦 2024-09-06 21:45:42

它与编解码器工厂有关。我更改为自定义编解码器,因为我们在项目中有自定义消息格式。谢谢大家的回复。

It is related to codec factory. I changed to custom codec as we have custom message formats in the project.. thank you all for your responses.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文