由Java开发数据编码的客户端场景并不多见,通常的作法一般直接创建socket套接字来完成。但是考虑到需要定义N个数组以及数组之间的转换来完成编包,所以干脆采用mina框架来实现客户端的开发。
开发前通常要考虑日志的跟踪,以及后续的容灾策略。此作法在接触大数据后变的尤为重要,此处暂不考虑容灾。
1、日志采用的是slf4j的2个jar包,因为整个应用都需要日志的来跟踪,因此日志类应该作为基类来定义,所以在mina主程序中自定义的核心处理类如:handle、filter等必须采用接口实现或者直接定义匿名类。
ApplicationLogging :
package com.jp.system; import org.apache.commons.lang.exception.ExceptionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * 日志基类 * @author zh.h * */ public abstract class ApplicationLogging { protected final Logger log; public ApplicationLogging() { super(); log = LoggerFactory.getLogger(this.getClass()); } protected final void debug(Object o) { log.debug(String.valueOf(o)); } protected final void debug(String msg) { log.debug(msg); } protected final void debug(String msg, Object... objects) { log.debug(msg, objects); } protected final void debug(Throwable ex) { log.debug(ex.getMessage(), ex); Throwable re = ExceptionUtils.getRootCause(ex); if (re != null && ex != re) { log.debug("root cause", re); } } protected final void error(String msg) { log.error(msg); } protected final void error(String msg, Object... objects) { log.error(msg, objects); } protected final void error(Throwable ex) { log.error(ex.getMessage(), ex); Throwable re = ExceptionUtils.getRootCause(ex); if (re != null && ex != re) { log.error("root cause", re); } } protected final void info(String msg) { log.info(msg); } protected final void info(String msg, Object... objects) { log.info(msg, objects); } protected final void info(Throwable ex) { log.info(ex.getMessage(), ex); Throwable re = ExceptionUtils.getRootCause(ex); if (re != null && ex != re) { log.info("root cause", re); } } protected final void trace(Object o) { log.trace(String.valueOf(o)); } protected final void trace(String msg) { log.trace(msg); } protected final void trace(String msg, Object... objects) { log.trace(msg, objects); } protected final void trace(Throwable ex) { log.trace(ex.getMessage(), ex); Throwable re = ExceptionUtils.getRootCause(ex); if (re != null && ex != re) { log.trace("root cause", re); } } protected final void warn(String msg) { log.warn(msg); } protected final void warn(String msg, Object... objects) { log.warn(msg, objects); } protected final void warn(Throwable ex) { log.warn(ex.getMessage(), ex); Throwable re = ExceptionUtils.getRootCause(ex); if (re != null && ex != re) { log.warn("root cause", re); } } }
2、主程序入口
socket与socketServer连接时需要考虑两点:
a) connect 重连 ->代码已实现
b) session 重连 :需要在connector中声明监听器,并实现或者重写IoServiceListener中的sessionDestroyed方法
package com.jp.main.client; import java.net.InetSocketAddress; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Queue; import java.util.concurrent.LinkedBlockingQueue; import org.apache.mina.core.future.ConnectFuture; import org.apache.mina.core.service.IoConnector; import org.apache.mina.core.service.IoHandlerAdapter; import org.apache.mina.core.session.IoSession; import org.apache.mina.filter.codec.ProtocolCodecFilter; import org.apache.mina.filter.logging.LoggingFilter; import org.apache.mina.transport.socket.nio.NioSocketConnector; import com.jp.main.entity.CarTakeVo; import com.jp.main.listener.IoListener; import com.jp.main.protocolFiter.ProcessCodecFactory; import com.jp.main.service.ConvertHelper; import com.jp.system.ApplicationLogging; import com.jp.system.ConvergeTransmit; /** * TCP短连接 * @author zh.h * */ public class TcpShortClient extends ApplicationLogging{ public static Queue<CarTakeVo> queue=new LinkedBlockingQueue<CarTakeVo>(); private IoConnector connector = null; private ConnectFuture future = null; private IoSession session = null; private ConvertHelper helper; public TcpShortClient(){ } public TcpShortClient(ConvertHelper helper){ this.helper = helper; } public void config(){ log.info(">>>>>>>>>>>>>>>>>>>>>>>TcpShortClient contextInitialized config<
mina 二进制协议客户端开发
最新推荐文章于 2021-03-01 21:11:17 发布
本文介绍了在Java开发中使用mina框架来实现二进制协议客户端的详细步骤,包括日志跟踪、连接重试、session重连策略以及协议编码与解码的注意事项。强调了IoBuffer在动态增长和数据截取中的使用技巧。
摘要由CSDN通过智能技术生成