• Xsocket学习


    1.xsocket是一个轻量级的基于NIO的服务器框架,用于开发高性能、可扩展、多线程的服务器。该框架封装了线程处理,异步读写等方面的操作。

      定义一个借口,继承IDataHandler,IConnectExceptionHandler, IConnectHandler, IDisconnectHandler:

    public interface SocketDataHander extends IDataHandler,
        IConnectExceptionHandler, IConnectHandler, IDisconnectHandler{
    
    }
    View Code

      写出这个接口的实现方法:

     

     1 public class SocketUnionPay implements SocketDataHander{
     2 
     3     
     4     @Override
     5     public boolean onData(INonBlockingConnection connection) throws IOException,
     6             BufferUnderflowException, ClosedChannelException,
     7             MaxReadSizeExceededException {
     8         // TODO Auto-generated method stub
     9         start = System.currentTimeMillis();//获取当前时间
    10         byte[] data = connection.readBytesByLength(connection.available());  //监控接收到的数据
    11         
    12         //接收数据,转换为输出流,并且输出
    13         receiveBufByte = new Iso8583_Send_OutputStream(null);
    14         receiveBufByte.write(data, 0, data.length);
    15         System.out.println("##############################长度:"+data.length);
    16         
    17         if(receiveBufByte.length() > 9){
    18             LOG.info("接收数据:" + new String(receiveBufByte.getBufbyte()) + "  长度:" + receiveBufByte.length());
    19             
    20             byte[] sendata = unionPay_processingCentre.sendTreatmentTrade(receiveBufByte.getBufbyte());  //处理发送并返回
    21             byte[] rendata = unionPay_processingCentre.respTreatmentTrade(sendata);//数据返回处理
    22             LOG.info("返回数据:" + new String(rendata));
    23             connection.write(rendata);
    24         }
    25         
    26         end = System.currentTimeMillis();
    27         if(LOG.isTraceEnabled()){
    28             LOG.trace("########################前置处理耗时:"+(end-start)+"#######################");
    29         }
    30         return true;
    31     }
    32 
    33     @Override
    34     public boolean onConnectException(INonBlockingConnection connection,
    35             IOException e) throws IOException {
    36         // TODO Auto-generated method stub
    37         LOG.error("转发Bank连接异常..onConnectException...");
    38         return true;
    39     }
    40 
    41     @Override
    42     public boolean onConnect(INonBlockingConnection connection) throws IOException,
    43             BufferUnderflowException, MaxReadSizeExceededException {
    44         // TODO Auto-generated method stub
    45         LOG.info("Scoket前置打开连接..onConnect...");
    46         return true;
    47     }
    48 
    49     @Override
    50     public boolean onDisconnect(INonBlockingConnection connection) throws IOException {
    51         // TODO Auto-generated method stub
    52         LOG.info("Scoket前置关闭连接..onDisconnect...");
    53         connection.close();
    54         return true;
    55     }
    56 
    57 }
  • 相关阅读:
    关于BigDecimal转String的准确性问题
    MyBatis动态Sql之if标签的注意事项
    Servlet与通信协议概述
    关于ThreadLocal的那些事
    MyBatis 中 @Param 注解的四种使用场景
    Mybatis中#{}与${}的区别
    如何重新加载 Spring Boot 上的更改,而无需重新启动服务器?
    jsp有哪些动作?作用分别是什么?
    forward 和redirect的区别 ?
    Eureka和ZooKeeper都可以提供服务注册与发现的功能,请说说两个的区别?
  • 原文地址:https://www.cnblogs.com/feitianshaoxai/p/5807319.html
Copyright © 2020-2023  润新知