使用MINA框架搭建服務端步驟:java
一、定義一個啓動服務的類MinaServer,並實現接口ServletContextListenerapache
二、定義一個處理業務邏輯的類MinaServerHandler,並繼承類IoHandlerAdaptersession
類MinaServer代碼例如如下:
框架
import java.net.InetSocketAddress; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; import org.apache.mina.core.session.IdleStatus; import org.apache.mina.filter.codec.ProtocolCodecFilter; import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory; import org.apache.mina.filter.executor.ExecutorFilter; import org.apache.mina.transport.socket.nio.NioSocketAcceptor; import com.utils.LogUtil; public class MinaServer implements ServletContextListener, HttpSessionListener { private static NioSocketAcceptor acceptor; private static final int port = 9999; private static final LogUtil logUtil = LogUtil.getLogUtil(MinaServer.class); // 中止MINA服務 public void contextDestroyed(ServletContextEvent sce) { try { MinaServer.acceptor.unbind(); MinaServer.acceptor.dispose(); logUtil.customLog("Mina服務中止..."); } catch (Exception e) { logUtil.customLog(e); } } // 啓動MINA服務 public void contextInitialized(ServletContextEvent sce) { try { // 建立一個非堵塞的server端的Socket acceptor = new NioSocketAcceptor(); // 設置過濾器 acceptor.getFilterChain().addLast( "serverCodec", new ProtocolCodecFilter( new ObjectSerializationCodecFactory())); acceptor.getFilterChain().addLast("ServerFilter", new ExecutorFilter()); // 設置讀取數據的緩衝區大小 acceptor.getSessionConfig().setReadBufferSize(1024*102400); // 讀寫通道10秒內無操做進入空暇狀態 acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10); // 加入邏輯處理器 acceptor.setHandler(new MinaServerHandler()); // 綁定端口 try { acceptor.bind(new InetSocketAddress(port)); } catch (Exception e) { } logUtil.customLog("服務端啓動成功... 端口號爲:" + port); } catch (Exception e) { logUtil.customLog("服務端啓動異常...."); } } public void sessionCreated(HttpSessionEvent arg0) { } public void sessionDestroyed(HttpSessionEvent arg0) { } }
import org.apache.mina.core.service.IoHandlerAdapter; import org.apache.mina.core.session.IdleStatus; import org.apache.mina.core.session.IoSession; import com.model.DeliverObject; import com.model.Orders; import com.model.Users; import com.service.GoodsService; import com.service.OrdersService; import com.service.UserService; import com.utils.GetBeanUtil; import com.utils.LogUtil; import com.utils.Params; public class MinaServerHandler extends IoHandlerAdapter { private static final LogUtil logUtil = LogUtil .getLogUtil(MinaServerHandler.class); @Override public void sessionCreated(IoSession session) throws Exception { logUtil.customLog("服務端與client建立鏈接..."); } @Override public void sessionOpened(IoSession session) throws Exception { logUtil.customLog("服務端與client鏈接打開..."); } @Override public void messageReceived(IoSession session, Object message) throws Exception { DeliverObject recvObj = (DeliverObject) message; int optionType = recvObj.getOptionType(); // 假設是登陸操做 if (optionType == Params.LOGIN) { UserService userService = (UserService) GetBeanUtil .getBean("userService"); userService.doLogin(session, (Users) recvObj.getObj()); } else if (optionType == Params.REGISTER) { // 假設是註冊操做 UserService userService = (UserService) GetBeanUtil .getBean("userService"); userService.doRegister(session, (Users) recvObj.getObj()); } else if (optionType == Params.UPDATEUSER) { // 假設是改動用戶信息操做 UserService userService = (UserService) GetBeanUtil .getBean("userService"); userService.doUpdateUser(session, (Users) recvObj.getObj()); } else if (optionType == Params.SEARCHGOODS) { // 假設是搜索商品操做 GoodsService goodsService = (GoodsService) GetBeanUtil .getBean("goodsService"); goodsService.doSearchGoods(session, (String) recvObj.getObj()); } else if (optionType == Params.SUBMITORDERS) { // 假設是提交訂單操做 OrdersService ordersService = (OrdersService) GetBeanUtil .getBean("ordersService"); ordersService.doSubmitOrders(session, (Orders) recvObj.getObj()); } else if (optionType == Params.SEARCHALLORDERS) { // 假設是查詢所有訂單操做 OrdersService ordersService = (OrdersService) GetBeanUtil .getBean("ordersService"); ordersService.doSearchAllOrders(session, (Users) recvObj.getObj()); } } @Override public void messageSent(IoSession session, Object message) throws Exception { logUtil.customLog("服務端發送信息成功..."); } @Override public void sessionClosed(IoSession session) throws Exception { logUtil.customLog("服務端與client鏈接關閉..."); session.close(true); } @Override public void sessionIdle(IoSession session, IdleStatus status) throws Exception { super.sessionIdle(session, status); logUtil.customLog("服務端進入空暇狀態..."); } @Override public void exceptionCaught(IoSession session, Throwable cause) throws Exception { logUtil.customLog("服務端發送異常..." + cause); session.close(true); } }