0

我有一个 Spring 应用程序,我需要使用 Apache MINA 库实现非常简单的通信通道。

我的 Spring 应用程序正在运行但 MINA 服务器没有运行,在 netstat 中这个端口没有使用。

我遵循了本教程: http: //mina.apache.org/mina-project/userguide/ch17-spring-integration/ch17-spring-integration.html

我究竟做错了什么?

这是一个处理程序类:

public class ServerHandler extends IoHandlerAdapter {

    @Override
    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
        cause.printStackTrace();
    }

    @Override
    public void messageReceived(IoSession session, Object message) throws Exception {

        String str = message.toString();
        if (str.trim().equalsIgnoreCase("quit")) {
            session.close(true);
            return;
        }
        Date date = new Date();
        session.write(date.toString());
        System.out.println("Message written...");
    }

    @Override
    public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
        System.out.println("IDLE " + session.getIdleCount(status));
    }
}

这是来自 applicationContext.xml 的 XML 配置

<bean id="trapHandler" class="ServerHandler"></bean>

    <bean id="snmpCodecFilter" class="org.apache.mina.filter.codec.ProtocolCodecFilter">
        <constructor-arg>
            <bean class="org.apache.mina.filter.codec.textline.TextLineCodecFactory" />
        </constructor-arg>
    </bean>

    <bean id="loggingFilter" class="org.apache.mina.filter.logging.LoggingFilter" />

    <!-- The filter chain. -->
    <bean id="filterChainBuilder" class="org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder">
        <property name="filters">
            <map>
                <entry key="loggingFilter" value-ref="loggingFilter"/>
                <entry key="codecFilter" value-ref="snmpCodecFilter"/>
            </map>
        </property>
    </bean>

    <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <property name="customEditors">
            <map>
                <entry key="java.net.SocketAddress">
                    <bean class="org.apache.mina.integration.beans.InetSocketAddressEditor" />
                </entry>
            </map>
        </property>
    </bean>

    <!-- The IoAcceptor which binds to port 161 -->
    <bean id="ioAcceptor" class="org.apache.mina.transport.socket.nio.NioDatagramAcceptor" init-method="bind" destroy-method="unbind">
        <!--<property name="defaultLocalAddress" value="${interface.ip_address}:${interface.communication.port}" />-->
        <property name="defaultLocalAddress" value=":6001" />
        <property name="handler" ref="trapHandler" />
        <property name="filterChainBuilder" ref="filterChainBuilder" />
    </bean>  
4

1 回答 1

0

服务器正在运行,但不在 TPC 上,而是在 UDP 上。

对于 UDP 协议:

  <bean id="ioAcceptor" class="org.apache.mina.transport.socket.nio.NioDatagramAcceptor" init-method="bind" destroy-method="unbind">
        <property name="defaultLocalAddress" value=":6001" />
        <property name="handler" ref="trapHandler" />
        <property name="filterChainBuilder" ref="filterChainBuilder" />
    </bean> 

对于 TCP 协议:

  <bean id="ioAcceptor" class="org.apache.mina.transport.socket.nio.NioSocketAcceptor" init-method="bind" destroy-method="unbind">
        <property name="defaultLocalAddress" value=":6001" />
        <property name="handler" ref="trapHandler" />
        <property name="filterChainBuilder" ref="filterChainBuilder" />
    </bean> 

不同之处在于使用的 Acceptor 类的类型。

于 2013-06-28T15:24:23.370 回答