apache mina框架小试
1.server
2.handler
package
server;
import java.net.InetSocketAddress;
import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
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.transport.socket.SocketAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
public class MainServer
{
private static MainServer mainServer = new MainServer();
private SocketAcceptor acceptor;
private int bindPort = 8888;
public static MainServer getInstances()
{
return mainServer;
}
private MainServer()
{
this.init();
}
private void init()
{
try
{
acceptor = new NioSocketAcceptor();
acceptor.getSessionConfig().setReadBufferSize(2048*3);
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
chain.addLast("myChain",new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
acceptor.setHandler(ServerHandler.getInstances());
acceptor.bind(new InetSocketAddress(bindPort));
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
public void shutDown()
{
acceptor.dispose();
}
public static void main(String[] args) throws Exception
{
MainServer.getInstances();
}
}
import java.net.InetSocketAddress;
import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
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.transport.socket.SocketAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
public class MainServer
{
private static MainServer mainServer = new MainServer();
private SocketAcceptor acceptor;
private int bindPort = 8888;
public static MainServer getInstances()
{
return mainServer;
}
private MainServer()
{
this.init();
}
private void init()
{
try
{
acceptor = new NioSocketAcceptor();
acceptor.getSessionConfig().setReadBufferSize(2048*3);
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
chain.addLast("myChain",new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
acceptor.setHandler(ServerHandler.getInstances());
acceptor.bind(new InetSocketAddress(bindPort));
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
public void shutDown()
{
acceptor.dispose();
}
public static void main(String[] args) throws Exception
{
MainServer.getInstances();
}
}
2.handler
package
server;
import org.apache.mina.core.filterchain.IoFilterAdapter;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.transport.socket.SocketSessionConfig;
public class ServerHandler extends IoFilterAdapter implements IoHandler
{
private static ServerHandler samplMinaServerHandler = new ServerHandler();
public static ServerHandler getInstances()
{
return samplMinaServerHandler;
}
private ServerHandler()
{}
public void sessionOpened(IoSession session) throws Exception
{
System.out.println("连接找开");
}
public void sessionClosed(IoSession session)
{
session.close(true);
System.out.println("关闭");
}
public void messageReceived(IoSession session, Object message)
throws Exception
{
String msg = (String) message;
System.out.println("收到的信息长度" + msg.length());
session.write("echo");
}
public void exceptionCaught(IoSession session, Throwable arg1)
throws Exception
{
session.close(true);
}
public void messageSent(IoSession arg0, Object arg1) throws Exception
{
//System.out.println(arg1);
}
public void sessionCreated(IoSession session) throws Exception
{
SocketSessionConfig cfg = (SocketSessionConfig) session.getConfig();
cfg.setReceiveBufferSize(2 * 1024 * 1024);
cfg.setReadBufferSize(2 * 1024 * 1024);
cfg.setKeepAlive(true);
cfg.setSoLinger(0);
}
public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception
{}
}
import org.apache.mina.core.filterchain.IoFilterAdapter;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.transport.socket.SocketSessionConfig;
public class ServerHandler extends IoFilterAdapter implements IoHandler
{
private static ServerHandler samplMinaServerHandler = new ServerHandler();
public static ServerHandler getInstances()
{
return samplMinaServerHandler;
}
private ServerHandler()
{}
public void sessionOpened(IoSession session) throws Exception
{
System.out.println("连接找开");
}
public void sessionClosed(IoSession session)
{
session.close(true);
System.out.println("关闭");
}
public void messageReceived(IoSession session, Object message)
throws Exception
{
String msg = (String) message;
System.out.println("收到的信息长度" + msg.length());
session.write("echo");
}
public void exceptionCaught(IoSession session, Throwable arg1)
throws Exception
{
session.close(true);
}
public void messageSent(IoSession arg0, Object arg1) throws Exception
{
//System.out.println(arg1);
}
public void sessionCreated(IoSession session) throws Exception
{
SocketSessionConfig cfg = (SocketSessionConfig) session.getConfig();
cfg.setReceiveBufferSize(2 * 1024 * 1024);
cfg.setReadBufferSize(2 * 1024 * 1024);
cfg.setKeepAlive(true);
cfg.setSoLinger(0);
}
public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception
{}
}
3.client
package
client;
import java.net.InetSocketAddress;
import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
public class MsgSender
{
private static MsgSender msgSender = new MsgSender();
NioSocketConnector connector;
DefaultIoFilterChainBuilder chain;
public static MsgSender getInstances()
{
return msgSender;
}
private MsgSender()
{
connector = new NioSocketConnector();
chain = connector.getFilterChain();
chain.addLast("myChain",new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
connector.setHandler(ClientHandler.getInstances());
connector.setConnectTimeoutMillis(1000);
}
public void sendMsg(String host, int port, String msg)
{
//chain.addLast("codec", new ProtocolCodecFilter( new TextLineCodecFactory()));
ConnectFuture cf = connector.connect(new InetSocketAddress(host, port));
cf.awaitUninterruptibly();
try
{
cf.getSession().write(msg);
}
catch (Exception ex)
{
ex.printStackTrace();
}
cf.getSession().getCloseFuture().awaitUninterruptibly();
}
public void shutDown()
{
connector.dispose();
}
public static void main(String args[])
{
StringBuffer s = new StringBuffer();
for(int i=0;i<100;i++)
{
s.append(i);
}
for(int i=0;i<200;i++)
{
MsgSender.getInstances().sendMsg("127.0.0.1", 8888, s.toString());
}
}
}
import java.net.InetSocketAddress;
import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
public class MsgSender
{
private static MsgSender msgSender = new MsgSender();
NioSocketConnector connector;
DefaultIoFilterChainBuilder chain;
public static MsgSender getInstances()
{
return msgSender;
}
private MsgSender()
{
connector = new NioSocketConnector();
chain = connector.getFilterChain();
chain.addLast("myChain",new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
connector.setHandler(ClientHandler.getInstances());
connector.setConnectTimeoutMillis(1000);
}
public void sendMsg(String host, int port, String msg)
{
//chain.addLast("codec", new ProtocolCodecFilter( new TextLineCodecFactory()));
ConnectFuture cf = connector.connect(new InetSocketAddress(host, port));
cf.awaitUninterruptibly();
try
{
cf.getSession().write(msg);
}
catch (Exception ex)
{
ex.printStackTrace();
}
cf.getSession().getCloseFuture().awaitUninterruptibly();
}
public void shutDown()
{
connector.dispose();
}
public static void main(String args[])
{
StringBuffer s = new StringBuffer();
for(int i=0;i<100;i++)
{
s.append(i);
}
for(int i=0;i<200;i++)
{
MsgSender.getInstances().sendMsg("127.0.0.1", 8888, s.toString());
}
}
}
4.handler
package
client;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
public class ClientHandler extends IoHandlerAdapter
{
private static ClientHandler samplMinaClientHandler = new ClientHandler();
public static ClientHandler getInstances()
{
return samplMinaClientHandler;
}
private ClientHandler()
{
}
public void sessionOpened(IoSession session) throws Exception
{
}
public void sessionClosed(IoSession session)
{}
public void messageReceived(IoSession session, Object message)
throws Exception
{
System.out.println("收到到消息" + (String) message);
session.close(true);
}
public void messageSent(IoSession arg0, Object arg1) throws Exception
{
//System.out.println(arg1);
}
public void exceptionCaught(IoSession session, Throwable cause) throws Exception
{
session.close(true);
}
}
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
public class ClientHandler extends IoHandlerAdapter
{
private static ClientHandler samplMinaClientHandler = new ClientHandler();
public static ClientHandler getInstances()
{
return samplMinaClientHandler;
}
private ClientHandler()
{
}
public void sessionOpened(IoSession session) throws Exception
{
}
public void sessionClosed(IoSession session)
{}
public void messageReceived(IoSession session, Object message)
throws Exception
{
System.out.println("收到到消息" + (String) message);
session.close(true);
}
public void messageSent(IoSession arg0, Object arg1) throws Exception
{
//System.out.println(arg1);
}
public void exceptionCaught(IoSession session, Throwable cause) throws Exception
{
session.close(true);
}
}