Java Code Examples for org.apache.mina.core.service.IoConnector#dispose()

The following examples show how to use org.apache.mina.core.service.IoConnector#dispose() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MinaClient.java    From java-study with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    // 创建一个非阻塞的客户端程序
    IoConnector connector = new NioSocketConnector();
    // 设置链接超时时间
    connector.setConnectTimeout(30000);
    ProtocolCodecFilter pf=new ProtocolCodecFilter((new MyTextLineCodecFactory(Charset .forName("utf-8"), "\r\n")));
    // 添加过滤器
    connector.getFilterChain().addLast("codec", pf);
    // 添加业务逻辑处理器类
    connector.setHandler(new MinaClientHandler());
    IoSession session = null;
    try {
        ConnectFuture future = connector.connect(new InetSocketAddress(
                HOST, PORT));// 创建连接
        future.awaitUninterruptibly();// 等待连接创建完成
        session = future.getSession();// 获得session
        String msg="hello \r\n";
        session.write(msg);// 发送消息
        logger.info("客户端与服务端建立连接成功...发送的消息为:"+msg);
    } catch (Exception e) {
    	e.printStackTrace();
        logger.error("客户端链接异常...", e);
    }
    session.getCloseFuture().awaitUninterruptibly();// 等待连接断开
    connector.dispose();
}
 
Example 2
Source File: MimaTimeClient.java    From frameworkAggregate with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
	IoConnector connector = new NioSocketConnector();
	connector.getFilterChain().addLast("logger", new LoggingFilter());
	connector.getFilterChain().addLast("codec",
			new ProtocolCodecFilter(new PrefixedStringCodecFactory(Charset.forName("UTF-8"))));
	connector.setHandler(new TimeClientHander());
	ConnectFuture connectFuture = connector.connect(new InetSocketAddress("127.0.0.1", PORT));
	// 等待建立连接
	connectFuture.awaitUninterruptibly();
	System.out.println("连接成功");
	IoSession session = connectFuture.getSession();
	Scanner sc = new Scanner(System.in);
	boolean quit = false;
	while (!quit) {
		String str = sc.next();
		if (str.equalsIgnoreCase("quit")) {
			quit = true;
		}
		session.write(str);
	}

	// 关闭
	if (session != null) {
		if (session.isConnected()) {
			session.getCloseFuture().awaitUninterruptibly();
		}
		connector.dispose(true);
	}

}
 
Example 3
Source File: CalculatorClient.java    From mina with Apache License 2.0 4 votes vote down vote up
@Override
    public void run() {
        IoConnector connector = new NioSocketConnector();
        connector.setConnectTimeoutMillis(CONNECT_TIMEOUT);//设置连接超时时间(毫秒数)
        connector.getFilterChain().addLast("logger", new LoggingFilter());
        connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new CommandCodecFactory("UTF-8")));

        KeepAliveMessageFactoryImpl kamfi = new KeepAliveMessageFactoryImpl();
        KeepAliveFilter kaf = new KeepAliveFilter(kamfi, IdleStatus.READER_IDLE, KeepAliveRequestTimeoutHandler.CLOSE);
        /** 是否回发 */
        kaf.setForwardEvent(true);
        connector.getFilterChain().addLast("heart", kaf);

        connector.setHandler(new CalculatorClientHander());
        ConnectFuture connectFuture = connector.connect(new InetSocketAddress(IP, PORT));
        //等待建立连接
        connectFuture.awaitUninterruptibly();

        if(!connectFuture.isConnected()){
            log.debug("连接失败");
            return ;
        }
        log.debug("连接成功");

        IoSession session = connectFuture.getSession();

//        try {
//            Cmd1003 cmd1003 = (Cmd1003) CommandFactory.createCommand(CidConst.C1003);
//            cmd1003.getReqMsg().setCpu(0.3f);
//            cmd1003.getReqMsg().setDisk(0.24f);
//            cmd1003.getReqMsg().setMemory(0.41f);
//            session.write(cmd1003);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }


        //关闭
        if (session != null) {
            if (session.isConnected()) {
                session.getCloseFuture().awaitUninterruptibly();
            }
            connector.dispose(true);
        }
    }
 
Example 4
Source File: MyClient.java    From jlogstash-input-plugin with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {    
    // 创建一个非组设的客户端客户端     
    IoConnector connector = new NioSocketConnector();    
    // 设置链接超时时间     
    connector.setConnectTimeoutMillis(30000);    
    // 添加过滤器     
    connector.getFilterChain().addLast( // 添加消息过滤器     
            "codec",    
            // Mina自带的根据文本换行符编解码的TextLineCodec过滤器 看到\r\n就认为一个完整的消息结束了  
            new ProtocolCodecFilter(new TextLineCodecFactory(Charset    
                    .forName("UTF-8"), LineDelimiter.MAC.getValue(),    
                    LineDelimiter.MAC.getValue())));    
    // 添加业务逻辑处理器类     
    connector.setHandler(new ClientMessageHandler());    
    IoSession session = null;    
    try {    
        ConnectFuture future = connector.connect(new InetSocketAddress(    
                HOST, PORT));    
        future.awaitUninterruptibly(); // 等待连接创建完成     
        session = future.getSession();
        long t1 = System.currentTimeMillis();
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<200000;i++){
        	System.out.println(i);
        	sb.append("ysqysq nginx_ccc [0050d2bf234311e6ba8cac853da49b78 type=nginx_access_log tag=\"mylog\"] /Users/sishuyss/ysq_access/$2016-04-05T11:12:24.230148+08:00/$100.97.184.152 - - [25/May/2016:01:10:07 +0800] \"GET /index.php?disp=dynamic HTTP/1.0\" 301 278 \"http://log.dtstack.com/\" \"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;Alibaba.Security.Heimdall.1142964)\" 121.42.0.85 - - - 0").append(LineDelimiter.UNIX.getValue());
        	if(i%200==0){
                session.write(sb.toString()); 
                sb =  new StringBuffer();
        	}
        }
        session.write(sb.toString()); 
        System.out.println("time:"+(System.currentTimeMillis()-t1));
    } catch (Exception e) {   
    	System.out.println(e.getCause());
        logger.info("客户端链接异常...");    
    }    

    session.getCloseFuture().awaitUninterruptibly();    
    logger.info("Mina要关闭了");    
    connector.dispose();    
}