spring 整合mina
首先定义自定义过滤器
<bean id="codecFilter" value-ref="codecFilter" /></map></property></bean>
KaiComCodecFactory.java
public class KaiComCodecFactory implements ProtocolCodecFactory{private final KaiComDecoder decoder;private final KaiComEncoder encoder;@Overridepublic ProtocolEncoder getEncoder(IoSession session) throws Exception {// TODO Auto-generated method stubreturn encoder;}@Overridepublic ProtocolDecoder getDecoder(IoSession session) throws Exception {// TODO Auto-generated method stubreturn decoder;} public KaiComCodecFactory() { decoder = new KaiComDecoder(); encoder = new KaiComEncoder(); }}KaiComDecoder.jar
public class KaiComDecoder implements ProtocolDecoder { private final Charset charset; private int bufferLength = 128; public KaiComDecoder(Charset charset) { if (charset == null) { throw new IllegalArgumentException("charset parameter shuld not be null"); } this.charset = charset; // Convert delimiter to ByteBuffer if not done yet. } public KaiComDecoder() { this.charset = Charset.forName("UTF-8"); // Convert delimiter to ByteBuffer if not done yet. } @Overridepublic void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out)throws Exception {String str = ioBufferToString(in);if(str != null){String[] list = str.split("\\|");if(list.length>1){//判断自定义协议if(list[1].compareToIgnoreCase("98") == 0||list[1].compareToIgnoreCase("01") == 0){ writeText(session, str, out); return;}}}}public static String ioBufferToString(Object message) { if (!(message instanceof IoBuffer)) { return ""; } IoBuffer ioBuffer = (IoBuffer) message; byte[] b = new byte [ioBuffer.limit()]; ioBuffer.get(b); StringBuffer stringBuffer = new StringBuffer(); for (int i = 0; i < b.length; i++) { stringBuffer.append((char) b [i]); } return stringBuffer.toString(); } @Overridepublic void finishDecode(IoSession session, ProtocolDecoderOutput out)throws Exception {// TODO Auto-generated method stubInetSocketAddress isa = (InetSocketAddress) session.getRemoteAddress();System.out.println("客户端:" + isa.getAddress().getHostAddress() + ":"+ isa.getPort() + "连接关闭了!");}@Overridepublic void dispose(IoSession session) throws Exception {// TODO Auto-generated method stub} protected void writeText(IoSession session, String text, ProtocolDecoderOutput out) { out.write(text); }KaiComEncoder.java
public class KaiComEncoder extends ProtocolEncoderAdapter {@Overridepublic void encode(IoSession session, Object message,ProtocolEncoderOutput out) throws Exception {// TODO Auto-generated method stub out.write(stringToIoBuffer(message.toString()));}public static IoBuffer stringToIoBuffer(String str) { byte bt[] = str.getBytes(); IoBuffer ioBuffer = IoBuffer.allocate(bt.length); ioBuffer.put(bt, 0, bt.length); ioBuffer.flip(); return ioBuffer; } }配置监听地址和端口
<!-- 指定服务端地址和端口 --><bean id="address" value="125.118.54.228" /><constructor-arg index="1" value="6889}" /></bean>
配置监听事件
<!-- 这里是那个自定义的类 --><bean id="someServer" ref="someServer"></property></bean>
MinaServerHandler.java
public class MinaServerHandler extends IoHandlerAdapter {private SomeServer someServer;public void setSomeServer(SomeServer someServer) {this.someServer = someServer;}@Overridepublic void exceptionCaught(IoSession session, Throwable cause) {try {System.out.println("客户端 " + session.getRemoteAddress() + " 关闭了连接");} catch (Exception e) {// TODO: handle exception}}/** * 服务端接收消息 */@Overridepublic void messageReceived(IoSession session, Object message)throws Exception {int result = someServer.doSome(message);String msg = message.toString();if ("quit".equals(msg)) {session.close(false);return;}// 对客户端做出的响应// 正常收到回复 OK_RECV // 接收失败 OK_ERRORString response = "OK_RECV";if(result==-1){response = "OK_ERROR";}session.write(response);}/** * 客户端连接的会话创建 */@Overridepublic void sessionCreated(IoSession session) throws Exception {InetSocketAddress isa = (InetSocketAddress) session.getRemoteAddress();System.out.println("客户端:" + isa.getAddress().getHostAddress() + ":"+ isa.getPort() + "连接进来了。");//sessions.put(session.getRemoteAddress().toString(), session);}}