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 }

 

posted @ 2016-08-25 16:21  一念花开满天下  阅读(2322)  评论(0编辑  收藏  举报