Apace Mina Hello world

2279 단어 Hello world


public class TotalCallable implements Callable<Integer> {

private NioSocketConnector connector;
private ConnectFuture cf;

public TotalCallable(String ip, int port, Parameter parameter) {

                connector = new NioSocketConnector();
		//     
		int connectTimeout = p.getConnectTimeout();
		if (connectTimeout == 0) {
			connectTimeout = Parameter.TIMEOUT_CONNECT;
		}
		connector.setConnectTimeoutMillis(connectTimeout * 1000L);
		DefaultIoFilterChainBuilder chain = connector.getFilterChain();
		chain.addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
		SocketSessionConfig cfg = connector.getSessionConfig();
		cfg.setUseReadOperation(true);
		//     
		cf = connector.connect(new InetSocketAddress(ip, port));
		//         
		cf.awaitUninterruptibly();

	public Integer call() {
		IoSession session = cf.getSession();
		try {
			long s = System.currentTimeMillis();

			int readTimeout = p.getSearchTimeout();
			if (readTimeout == 0) {
				readTimeout = Parameter.TIMEOUT_SEARCH;
			}
			//   
			session.write(p).awaitUninterruptibly();
			//   
			ReadFuture readFuture = session.read();
			if (readFuture.awaitUninterruptibly(readTimeout, TimeUnit.SECONDS)) {
				Object msg = readFuture.getMessage();
				if (msg instanceof Object[]) {
					Object[] rt = (Object[]) msg;
					if (rt.length > 1 && rt[0] instanceof Integer) {
						Integer total = (Integer) rt[0];
						return total;
					}
				}
			} else {
				//    
			}
			long e = System.currentTimeMillis();
			e = System.currentTimeMillis();
			log.info("      :" + (e - s));
		} catch (Exception e) {
			log.error("ip " + ip + " port : " + port);
			log.error(e.getMessage(), e);
			//       
			cf = null;
		} finally {
			//   
			session.close(true);
			session.getService().dispose();
			connector.dispose();
		}
		return 0;
	}
}



좋은 웹페이지 즐겨찾기