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

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

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

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

 

 public class SocketUnionPay implements SocketDataHander{

     @Override
public boolean onData(INonBlockingConnection connection) throws IOException,
BufferUnderflowException, ClosedChannelException,
MaxReadSizeExceededException {
// TODO Auto-generated method stub
start = System.currentTimeMillis();//获取当前时间
byte[] data = connection.readBytesByLength(connection.available()); //监控接收到的数据 //接收数据,转换为输出流,并且输出
receiveBufByte = new Iso8583_Send_OutputStream(null);
receiveBufByte.write(data, 0, data.length);
System.out.println("##############################长度:"+data.length); if(receiveBufByte.length() > 9){
LOG.info("接收数据:" + new String(receiveBufByte.getBufbyte()) + " 长度:" + receiveBufByte.length()); byte[] sendata = unionPay_processingCentre.sendTreatmentTrade(receiveBufByte.getBufbyte()); //处理发送并返回
byte[] rendata = unionPay_processingCentre.respTreatmentTrade(sendata);//数据返回处理
LOG.info("返回数据:" + new String(rendata));
connection.write(rendata);
} end = System.currentTimeMillis();
if(LOG.isTraceEnabled()){
LOG.trace("########################前置处理耗时:"+(end-start)+"#######################");
}
return true;
} @Override
public boolean onConnectException(INonBlockingConnection connection,
IOException e) throws IOException {
// TODO Auto-generated method stub
LOG.error("转发Bank连接异常..onConnectException...");
return true;
} @Override
public boolean onConnect(INonBlockingConnection connection) throws IOException,
BufferUnderflowException, MaxReadSizeExceededException {
// TODO Auto-generated method stub
LOG.info("Scoket前置打开连接..onConnect...");
return true;
} @Override
public boolean onDisconnect(INonBlockingConnection connection) throws IOException {
// TODO Auto-generated method stub
LOG.info("Scoket前置关闭连接..onDisconnect...");
connection.close();
return true;
} }
05-11 09:41