在客戶端和服務(wù)端的調(diào)用如下:
客戶端;
package cn.com.unutrip.java.opensource.mina.sumup;
import java.net.InetSocketAddress;
import org.apache.mina.core.RuntimeIoException; import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder; import org.apache.mina.core.future.ConnectFuture; import org.apache.mina.core.session.IoSession; import org.apache.mina.filter.codec.ProtocolCodecFilter; import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory; import org.apache.mina.filter.logging.LoggingFilter; import org.apache.mina.transport.socket.nio.NioSocketConnector;
import cn.com.unutrip.java.opensource.mina.sumup.codec.SumUpProtocolCodecFactory; /** * TCP/ID 客戶端的應(yīng)用 * @author longgangbai * */ public class Client { private static final String HOSTNAME = "localhost"; private static final int PORT = 8123; private static final long CONNECT_TIMEOUT = 30*1000L; // 30 seconds // Set this to false to use object serialization instead of custom codec. private static final boolean USE_CUSTOM_CODEC = true; public static void main(String[] args) throws Throwable { // prepare values to sum up int[] values = new int[10]; for (int i = 0; i < args.length; i++) { values[i] = Integer.parseInt(args[i]); } //創(chuàng)建一個(gè)客戶端對(duì)象 NioSocketConnector connector = new NioSocketConnector(); // Configure the service. //設(shè)置連接超時(shí)時(shí)間 connector.setConnectTimeoutMillis(CONNECT_TIMEOUT); //獲取過(guò)濾器鏈工廠 DefaultIoFilterChainBuilder filterchain=connector.getFilterChain(); //添加相應(yīng)的過(guò)濾鏈 if (USE_CUSTOM_CODEC) { //采用自定義的協(xié)議編碼工廠類過(guò)濾器連 filterchain.addLast("codec",new ProtocolCodecFilter(new SumUpProtocolCodecFactory(false))); } else { //采用對(duì)象序列化編碼工廠類過(guò)濾器連 filterchain.addLast("codec",new ProtocolCodecFilter(new ObjectSerializationCodecFactory())); } //添加日志過(guò)濾器連 filterchain.addLast("logger", new LoggingFilter()); //進(jìn)行業(yè)務(wù)處理邏輯 connector.setHandler(new ClientSessionHandler(values)); IoSession session=null; for (;;) { try { ConnectFuture future = connector.connect(new InetSocketAddress( HOSTNAME, PORT)); future.awaitUninterruptibly(); session = future.getSession(); break; } catch (RuntimeIoException e) { System.err.println("Failed to connect."); e.printStackTrace(); Thread.sleep(5000); } } // wait until the summation is done session.getCloseFuture().awaitUninterruptibly(); connector.dispose(); } }
服務(wù)端使用如下:
package cn.com.unutrip.java.opensource.mina.sumup;
import java.net.InetSocketAddress;
import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder; import org.apache.mina.filter.codec.ProtocolCodecFilter; import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory; import org.apache.mina.filter.logging.LoggingFilter; import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import cn.com.unutrip.java.opensource.mina.sumup.codec.SumUpProtocolCodecFactory; /** *TCP /IP 服務(wù)端的代碼的應(yīng)用 * @author longgangbai * */ public class Server { private static final int SERVER_PORT = 8123; // Set this to false to use object serialization instead of custom codec. private static final boolean USE_CUSTOM_CODEC = true; public static void main(String[] args) throws Throwable { //創(chuàng)建服務(wù)端 NioSocketAcceptor acceptor = new NioSocketAcceptor(); //獲取過(guò)濾鏈的信息 DefaultIoFilterChainBuilder filterchain=acceptor.getFilterChain(); // Prepare the service configuration. if (USE_CUSTOM_CODEC) { filterchain.addLast("codec",new ProtocolCodecFilter(new SumUpProtocolCodecFactory(true))); } else { filterchain.addLast("codec",new ProtocolCodecFilter(new ObjectSerializationCodecFactory())); } filterchain.addLast("logger", new LoggingFilter()); //業(yè)務(wù)處理 acceptor.setHandler(new ServerSessionHandler()); //綁定服務(wù)端口 acceptor.bind(new InetSocketAddress(SERVER_PORT)); System.out.println("Listening on port " + SERVER_PORT); }
}
希望大家交流。
|