1. package com.allinpay.mina; 
  2.  
  3. import java.io.IOException; 
  4. import java.net.InetSocketAddress; 
  5.  
  6. import org.apache.mina.common.IoAcceptor; 
  7. import org.apache.mina.filter.codec.ProtocolCodecFilter; 
  8. import org.apache.mina.transport.socket.nio.SocketAcceptor; 
  9. import org.apache.mina.transport.socket.nio.SocketAcceptorConfig; 
  10.  
  11.  
  12.  
  13. public class MyServer { 
  14.     /*服务器监听端口*/ 
  15.     private  int SERVER_PORT = 10000
  16.      
  17.     private  IoAcceptor acceptor=null
  18.  
  19.     /** 
  20.      * 接收报文的服务端Socket的idle时间(秒) 
  21.      */ 
  22.     public static int Server_Session_IdleSec = 5*60
  23.     /** 
  24.      * 构造函数,创建服务器监听线程 
  25.      * @param aServerPort 
  26.      * @throws IOException  
  27.      */ 
  28.     public MyServer(int aServerPort) throws IOException { 
  29.         SERVER_PORT=aServerPort; 
  30.         connect(); 
  31.     } 
  32.     /** 
  33.      * 创建服务器监听线程 
  34.      * @throws IOException  
  35.      * 
  36.      */ 
  37.     public void connect() throws IOException{ 
  38.         close(); 
  39.         acceptor = new SocketAcceptor(); 
  40.         SocketAcceptorConfig cfg = new SocketAcceptorConfig(); 
  41.         cfg.setReuseAddress(true); 
  42.         /** 
  43.          * 添加××× 
  44.          */ 
  45.         cfg.getFilterChain() 
  46.         .addLast("codec",new ProtocolCodecFilter(new MyCodecFactory())); 
  47.         /** 
  48.          * 添加系统日志 
  49.          */ 
  50.         //cfg.getFilterChain().addLast("logger", new LoggingFilter()); 
  51.         /** 
  52.          * 建立服务器监听 
  53.          */ 
  54.         acceptor.bind(new InetSocketAddress(SERVER_PORT),new MyServerIoSessionHandler(this), cfg); 
  55. //      LoggerUtil.info("Server start on port " + SERVER_PORT); 
  56.     } 
  57.     /** 
  58.      * 关闭连接 
  59.      * @return 
  60.      */ 
  61.     public boolean close(){ 
  62.         if(null != acceptor){ 
  63.             acceptor.unbindAll(); 
  64.             acceptor=null
  65.         } 
  66.         return true
  67.     } 
  68.      
  69.      
  70.     public static void main(String[] args){ 
  71.         try { 
  72.             new MyServer(10000); 
  73.         } catch (IOException e) { 
  74.             // TODO Auto-generated catch block 
  75.             e.printStackTrace(); 
  76.         } 
  77.     } 

 

 
  
  1. package com.allinpay.mina; 
  2.  
  3. import java.io.IOException; 
  4.  
  5. import org.apache.mina.common.IdleStatus; 
  6. import org.apache.mina.common.IoHandlerAdapter; 
  7. import org.apache.mina.common.IoSession; 
  8.  
  9. /** 
  10.  * Mina框架定义的ServerSessionHandler 
  11.  */ 
  12. public class MyServerIoSessionHandler extends IoHandlerAdapter { 
  13.     /** 
  14.      * 服务器线程 
  15.      */ 
  16.     private MyServer server_channel = null
  17.  
  18.     /** 
  19.      * 构造函数 
  20.      *  
  21.      * @param channel 
  22.      *            服务器线程 
  23.      */ 
  24.     public MyServerIoSessionHandler(MyServer channel) { 
  25.         super(); 
  26.         server_channel = channel; 
  27.     } 
  28.  
  29.     /** 
  30.      * 服务器socket的session打开时触发该方法,设置服务器的idle时间(5分钟) 
  31.      */ 
  32.     public void sessionOpened(IoSession session) { 
  33.         // 设置服务器idle时间 
  34.         session.setIdleTime(IdleStatus.READER_IDLE, 
  35.                 MyServer.Server_Session_IdleSec); 
  36.     } 
  37.  
  38.     /** 
  39.       *  
  40.       */ 
  41.     public void messageReceived(IoSession session, Object message) { 
  42.         String orderMsg = (String) message; 
  43.         System.out.println(orderMsg); 
  44.         // if(null == orderMsg){//表示空闲报文,直接返回,不需要做后续处理 
  45.         // // LoggerUtil.info("messageReceived message is null "); 
  46.         // return; 
  47.         // } 
  48.  
  49.         session.write(orderMsg); 
  50.  
  51.     } 
  52.  
  53.     public void sessionIdle(IoSession session, IdleStatus status) 
  54.             throws IOException { 
  55.         // disconnect an idle client 
  56.         session.close(); 
  57.         server_channel.connect(); 
  58.     } 
  59.  
  60.     public void exceptionCaught(IoSession session, Throwable cause) 
  61.             throws IOException { 
  62.         // SessionLog.info(session,cause.getMessage()); 
  63.         // disconnect an idle client 
  64.         session.close(); 
  65.         server_channel.connect(); 
  66.     } 
  67.  

 

 
  
  1. package com.allinpay.mina; 
  2.  
  3. import org.apache.mina.filter.codec.ProtocolCodecFactory; 
  4.  
  5. public class MyCodecFactory implements ProtocolCodecFactory { 
  6.      
  7.  
  8.     private final MyEncoder encoder; 
  9.     private final MyDecoder decoder; 
  10.  
  11.     public MyCodecFactory() { 
  12.         encoder = new MyEncoder(); 
  13.         decoder = new MyDecoder(); 
  14.     } 
  15.  
  16.     public MyEncoder getEncoder() { 
  17.         return encoder; 
  18.     } 
  19.  
  20.     public MyDecoder getDecoder() { 
  21.         return decoder; 
  22.     } 
  23.  
  24.      

 

 
  
  1. package com.allinpay.mina; 
  2.  
  3. import java.io.IOException; 
  4.  
  5. import org.apache.mina.common.ByteBuffer; 
  6. import org.apache.mina.common.IoSession; 
  7. import org.apache.mina.filter.codec.ProtocolDecoderAdapter; 
  8. import org.apache.mina.filter.codec.ProtocolDecoderOutput; 
  9.  
  10. public class MyDecoder extends ProtocolDecoderAdapter { 
  11.     
  12.     public void decode(IoSession session, ByteBuffer buf,ProtocolDecoderOutput out) throws Exception { 
  13.          
  14. //      LoggerUtil.info("GtsMessageDecoder start "); 
  15.         try
  16.             String request = recv(buf); 
  17. //          SpdbRequestParser requestParser = new SpdbRequestParser();   
  18. //          requestParser.initByXml(request); 
  19.             out.write(request);   
  20.         }catch (Exception e) { 
  21. //          LoggerUtil.error(e); 
  22.             throw e; 
  23.         } 
  24. //      LoggerUtil.info("GtsMessageDecoder End "); 
  25.     } 
  26.      
  27.     private String recv(ByteBuffer buf) throws IOException{ 
  28.         byte[] b=new byte[buf.limit()];   
  29.         buf.get(b); 
  30. //      LoggerUtil.info("recv  string["+new String(b,"GBK")+"], length :"+b.length); 
  31.         byte[] head = new byte[64]; 
  32.         System.arraycopy(b, 0, head, 064); 
  33.         String headstring = new String(head); 
  34. //      LoggerUtil.info("recv  head["+headstring+"]"); 
  35.         int length = Integer.parseInt(headstring.substring(8,16).trim()); 
  36. //      LoggerUtil.info("recv  body length["+length+"]"); 
  37.         byte[] body = new byte[length]; 
  38.         System.arraycopy(b, 64, body, 0, length); 
  39. //      LoggerUtil.info("recv  body ["+new String(body,"GBK")+"]"); 
  40.         return new String(body,"GBK"); 
  41.     } 
  42.      

 

 
  
  1. package com.allinpay.mina; 
  2.  
  3. import org.apache.mina.common.ByteBuffer; 
  4. import org.apache.mina.common.IoSession; 
  5. import org.apache.mina.filter.codec.ProtocolEncoderAdapter; 
  6. import org.apache.mina.filter.codec.ProtocolEncoderOutput; 
  7.  
  8.  
  9.  
  10.  
  11. public class MyEncoder extends ProtocolEncoderAdapter { 
  12.  
  13.     public MyEncoder() {   
  14.     }   
  15.      
  16.     public void encode(IoSession session, Object message,  
  17.             ProtocolEncoderOutput out) throws Exception { 
  18. //      LoggerUtil.info("GtsMessageEncoder start "); 
  19.         try
  20. //          LoggerUtil.info("message " + message); 
  21.             String response = (String)message; 
  22.          
  23. //          LoggerUtil.debug("Encode message : " + xml); 
  24.             byte[] bt = response.getBytes("GBK"); 
  25.             ByteBuffer buf = ByteBuffer.allocate(bt.length).setAutoExpand(true); 
  26.             buf.put(bt); 
  27.             buf.flip(); 
  28.             out.write(buf); 
  29.         }catch (Exception e) { 
  30. //          LoggerUtil.error("GtsMessageEncoder Error",e); 
  31.         } 
  32. //      LoggerUtil.info("GtsMessageEncoder End "); 
  33.     } 
  34.  
  35.      
  36. //  public byte[] send(byte[] data,String Fund_Id,String Trade_Code) throws IOException { 
  37. //      byte[] gbkData = new String(data,"UTF-8").getBytes("GBK"); 
  38. //      String h1_7 =Fund_Id;//Char(7) 
  39. //      String h8="0";//Char(1) 
  40. //      String h9_16="        "; //Char(8) 
  41. //      String h17_24 ="        "+Trade_Code; //Char(8) 
  42. //      String h25_32 = "00000000"; //Char(8) 
  43. //      String h33_64 = "00000000000000000000000000000000"; //Char(32) 
  44. //      h9_16=h9_16+gbkData.length; 
  45. //      String head = h1_7 + h8+h9_16.substring(h9_16.length()-8)+h17_24.substring(h17_24.length()-8)+h25_32+h33_64; 
  46. //      String str = head+new String(gbkData,"GBK"); 
  47. //      return str.getBytes("GBK"); 
  48. //  }