日期:2014-05-16  浏览次数:20691 次

ApacheMina:ObjectSerializationCodecFactory的例子
1. 定义发送的RequestObject和返回的ResponseObject
RequestObject.java
public class RequestObject implements Serializable {
	
	private static final long serialVersionUID = 8891436114296586399L;

	private int id;
	private String name;
	private String description;
	private String others;
	
	
	public RequestObject(int id, String name, String description, String others) {
		super();
		this.id = id;
		this.name = name;
		this.description = description;
		this.others = others;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getOthers() {
		return others;
	}
	public void setOthers(String others) {
		this.others = others;
	}
	@Override
	public String toString() {
		return "RequestObject [id=" + id + ", name=" + name + ", description="
				+ description + ", others=" + others + "]";
	}

}


ResponseObject.java
public class ResponseObject implements Serializable {

	
	private static final long serialVersionUID = -6783592807728197249L;

	private int id;
	private String name;
	private String value;
	private String remarks;
	
	
	public ResponseObject(int id, String name, String value, String remarks) {
		super();
		this.id = id;
		this.name = name;
		this.value = value;
		this.remarks = remarks;
	}


	public int getId() {
		return id;
	}


	public void setId(int id) {
		this.id = id;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public String getValue() {
		return value;
	}


	public void setValue(String value) {
		this.value = value;
	}


	public String getRemarks() {
		return remarks;
	}


	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}


	@Override
	public String toString() {
		return "ResponseObject [id=" + id + ", name=" + name + ", value="
				+ value + ", remarks=" + remarks + "]";
	}

	
}

2. 定义Server端的链接接收和业务处理机制
DemoObjectServer.java
public class DemoObjectServer {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		
		final int PORT = 9123;
		
		// objects used to listen for incoming connection
		IoAcceptor acceptor = new NioSocketAcceptor();
		
		// filters:
		// 1. log all information
		acceptor.getFilterChain().addLast("logger", new LoggingFilter());
		// 2. translate binary or protocol specific data into message object
		acceptor.getFilterChain().addLast("codec",new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
		
		// define the handler used to service client connections and the requests for the current time
		acceptor.setHandler(new DemoObjectServerHandler());
		//NioSocketAcceptor configuration: for the socket that will be used to accept connections from client
		acceptor.getSessionConfig().setReadBufferSize(2048);
		acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
		
		// define the handler class and bind the NioSocketAcception to a port
		acceptor.bind(new InetSocketAddress(PORT));

	}

}



DemoObjectServerHandler.java
public class DemoObjectServerHandler extends IoHandlerAdapter {
	
	public DemoObjectServerHandler(){
		super();
	}
	
	@Override
	public void messageReceived(IoSession session, Object obj) throws Exception {
		System.out.println("message received");
		
		RequestObject ro = (RequestObject) obj;
		System.out.println("request body:" + ro.toString());
		
		String _name = "request_" + ro.getId();
		String _remarks = "description is " + ro.getDescription() + ", and others is " + ro.getOthers(