2 mqwsd mqwsd 于 2016.02.20 14:02 提问

mina 线程不知什么原因死了 5C

mina 写的客户端,在两台机器运行没有错误,这两台是有线网络,到第三台机器时不时线程就死了,第三台为无线网络,网络不稳定,但是已经加入了重连机制,目前在本地无法模拟出这种情况,大神可以看看是什么问题吗

@Service
public class MinaClient implements InitializingBean {

final Logger logger = Logger.getLogger(MinaClient.class);

private IoSession session;

private NioSocketConnector connector;

private int parkId;

public void start() throws IOException {
    // 读取配置文件停车场Id
    Properties prop = ConfUtils.loadConf(Constants.PROPERTIES_URL);
    parkId = Integer.parseInt(prop.getProperty("parkId"));
    String[] address = prop.getProperty("minaremoteaddress").split(":");

    String host = address[0];
    int port = Integer.parseInt(address[1]);

    connector = new NioSocketConnector();
    // connector.getFilterChain().addLast( "logger", new LoggingFilter() );

    connector.setConnectTimeoutMillis(10000);

    connector.getFilterChain().addLast("mdc", new MdcInjectionFilter());
    TextLineCodecFactory factory = new TextLineCodecFactory(
            Charset.forName("UTF-8"));

    factory.setDecoderMaxLineLength(10240);
    factory.setEncoderMaxLineLength(10240);
    // 加入解码器
    connector.getFilterChain().addLast("codec",
            new ProtocolCodecFilter(factory));

    connector.getSessionConfig().setReceiveBufferSize(10240); // 设置接收缓冲区的大小
    connector.getSessionConfig().setSendBufferSize(10240);// 设置输出缓冲区的大小

    connector.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 30); // 读写都空闲时间:30秒
    connector.getSessionConfig().setIdleTime(IdleStatus.READER_IDLE, 40);// 读(接收通道)空闲时间:40秒
    connector.getSessionConfig().setIdleTime(IdleStatus.WRITER_IDLE, 50);// 写(发送通道)空闲时间:50秒

    /** mina 心跳机制 */

    KeepAliveMessageFactory heartBeatFactory = new KeepAliveMessageFactoryImpl();
    KeepAliveFilter heartBeat = new KeepAliveFilter(heartBeatFactory,
            IdleStatus.READER_IDLE, KeepAliveRequestTimeoutHandler.CLOSE);
    /** 是否回发 */
    heartBeat.setForwardEvent(false);
    /** 发送频率 */
    heartBeat.setRequestInterval(10);
    heartBeat.setRequestTimeout(5);
    connector.getSessionConfig().setKeepAlive(true);
    connector.getFilterChain().addLast("heartbeat", heartBeat);

    // 添加处理器
    connector.setHandler(new MinaClientHandler());
    connector.setDefaultRemoteAddress(new InetSocketAddress(host, port));// 设置默认访问地址

    connector.getFilterChain().addFirst("reconnection",
            new IoFilterAdapter() {
                @Override
                public void sessionClosed(NextFilter nextFilter,
                        IoSession ioSession) throws Exception {
                    for (;;) {
                        try {
                            Thread.sleep(3000);
                            ConnectFuture future = connector.connect();
                            future.awaitUninterruptibly();// 等待连接创建成功
                            session = future.getSession();// 获取会话
                            JSONObject json = signJson(parkId);
                            session.write(json);
                            logger.error("断线重连["
                                    + connector.getDefaultRemoteAddress()
                                            .getHostName()
                                    + ":"
                                    + connector.getDefaultRemoteAddress()
                                            .getPort() + "]成功");
                            break;
                        } catch (Exception ex) {
                            logger.error("重连服务器登录失败,3秒再连接一次:"
                                    + ex.getMessage());
                        }
                    }
                }
            });
    for (;;) {
        try {
            ConnectFuture future = connector.connect();
            // 等待连接创建成功
            future.awaitUninterruptibly();
            // 获取会话
            session = future.getSession();
            JSONObject json = signJson(parkId);

            session.write(json);
            logger.error("连接服务端"
                    + host
                    + ":"
                    + port
                    + "[成功]"
                    + ",,时间:"
                    + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                            .format(new Date()));
            break;
        } catch (RuntimeIoException e) {
            logger.error(
                    "连接服务端"
                            + host
                            + ":"
                            + port
                            + "失败"
                            + ",,时间:"
                            + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                                    .format(new Date())
                            + ", 连接MSG异常,请检查MSG端口、IP是否正确,MSG服务是否启动,异常内容:"
                            + e.getMessage(), e);
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e1) {
                logger.error(
                        "连接服务端"
                                + host
                                + ":"
                                + port
                                + "失败"
                                + ",,时间:"
                                + new SimpleDateFormat(
                                        "yyyy-MM-dd HH:mm:ss")
                                        .format(new Date())
                                + ", 连接MSG异常,请检查MSG端口、IP是否正确,MSG服务是否启动,异常内容:"
                                + e.getMessage(), e1);
            }
        } catch (Exception e) {
            try {
                Thread.sleep(5000);
                logger.error("连接服务器失败", e);
            } catch (InterruptedException e1) {
                e1.printStackTrace();

            }
        }
    }

}

private JSONObject signJson(int parkId) {
    JSONObject json = new JSONObject();
    json.put("tag", "0001");
    json.put("parkId", parkId);
    JSONObject data = new JSONObject();
    data.put("data", json);
    String sign = SecurityUtil
            .MD5(json.toString() + Constants.PARKING_CODE);
    data.put("sign", sign);
    data.put("flag", parkId);
    return data;
}

@Override
public void afterPropertiesSet() throws Exception {
    try {
        //web项目启动后,连接mina服务器
        new MinaClient().start();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

1个回答

WinsenJiansbomber
WinsenJiansbomber   2016.03.08 13:49

没有任何异常吗?如果这样就应该检查程序逻辑

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!