org.apache.mina.filter.codec.textline.TextLineCodecFactory Java Examples

The following examples show how to use org.apache.mina.filter.codec.textline.TextLineCodecFactory. 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: MinaTimeServer.java    From frameworkAggregate with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {

		IoAcceptor acceptor = new NioSocketAcceptor();
		// 这个过滤器用来记录所有的信息,比如创建session(会话),接收消息,发送消息,关闭会话等
		acceptor.getFilterChain().addLast("logger", new LoggingFilter());
		// 用来转换二进制或协议的专用数据到消息对象中
		acceptor.getFilterChain().addLast("codec",
				new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));

		// 实时处理客户端的连接和请求
		acceptor.setHandler(new TimeServerHandler());
		acceptor.getSessionConfig().setReadBufferSize(2048);
		// 方法将定时调用一次会话,保持空闲状态。来设定时间间隔。
		acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
		acceptor.bind(new InetSocketAddress(PORT));
	}
 
Example #2
Source File: MinaTimeServer.java    From java-tutorial with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    // 创建服务器端的监听器对象
    IoAcceptor acceptor = new NioSocketAcceptor();
    // 增加日志过滤器:用于日志存储
    acceptor.getFilterChain().addLast("logger", new LoggingFilter());
    // 增加消息编码过滤器,采用UTF-8编码
    acceptor.getFilterChain().addLast("codec",
            new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
    // 设置具体的事物逻辑处理器
    acceptor.setHandler(new TimeServerHandler());
    // 设置IoSession的一些属性
    acceptor.getSessionConfig().setReadBufferSize(2048);
    acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
    // 设置服务器监听的端口
    acceptor.bind(new InetSocketAddress(PORT));
}
 
Example #3
Source File: MinaTimeClient.java    From javabase with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
	// 创建客户端连接器.
	NioSocketConnector connector = new NioSocketConnector();
	connector.getFilterChain().addLast("logger", new LoggingFilter());
	connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8")))); // 设置编码过滤器
	connector.setConnectTimeout(30);
	connector.setHandler(new TimeClientHandler());// 设置事件处理器
	ConnectFuture cf = connector.connect(new InetSocketAddress("127.0.0.1", 9123));// 建立连接
	cf.awaitUninterruptibly();// 等待连接创建完成
	cf.getSession().write("hello");// 发送消息
	cf.getSession().write("quit");// 发送消息
	cf.getSession().getCloseFuture().awaitUninterruptibly();// 等待连接断开
	connector.dispose();
}
 
Example #4
Source File: MinaTimeServer.java    From javabase with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
	IoAcceptor acceptor = new NioSocketAcceptor();
	acceptor.getFilterChain().addLast("logger", new LoggingFilter());
	acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));// 指定编码过滤器
	acceptor.setHandler(new TimeServerHandler());// 指定业务逻辑处理器

	//读写 通道均在3 秒内无任何操作就进入空闲状态
	acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 3);

	acceptor.setDefaultLocalAddress(new InetSocketAddress(PORT));// 设置端口号
	acceptor.bind();// 启动监听
}
 
Example #5
Source File: Tcp.java    From jlogstash-input-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public void emit() {
	// TODO Auto-generated method stub
	try {
		// ssl 认证
		if (sslEnable) {
			SslFilter sslFilter = new SslFilter(getSslContext());
			acceptor.getFilterChain().addLast("sslFilter", sslFilter);
			logger.warn("ssl authenticate is open");
		}
		LoggingFilter loggingFilter = new LoggingFilter();
		acceptor.getFilterChain().addLast("logger", loggingFilter);
		TextLineCodecFactory textLineCodecFactory = new TextLineCodecFactory(
				Charset.forName(encodiing));
		textLineCodecFactory.setDecoderMaxLineLength(maxLineLength);
		textLineCodecFactory.setEncoderMaxLineLength(maxLineLength);
		acceptor.getFilterChain().addLast("codec",
				new ProtocolCodecFilter(textLineCodecFactory));
		acceptor.setHandler(minaBizHandler);
		acceptor.getSessionConfig().setReadBufferSize(bufSize);
		acceptor.getSessionConfig().setWriteTimeout(10);
		// acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE,
		// 10);//空闲状态
		acceptor.bind(new InetSocketAddress(InetAddress.getByName(host),
				port));
	} catch (Exception e) {
		// TODO Auto-generated catch block
		logger.error(e.getMessage());
		System.exit(1);
	}
}
 
Example #6
Source File: ConsoleServer.java    From MtgDesktopCompanion with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void start() throws IOException {
	acceptor = new NioSocketAcceptor();
	acceptor.getFilterChain().addLast("codec",
			new ProtocolCodecFilter(new TextLineCodecFactory(MTGConstants.DEFAULT_ENCODING)));
	acceptor.getSessionConfig().setReadBufferSize(getInt("BUFFER-SIZE"));
	acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, getInt("IDLE-TIME"));
	MTGConsoleHandler handler = new MTGConsoleHandler();
	handler.setWelcomeMessage(getString("STARTUP_MESSAGE"));
	acceptor.setHandler(handler);
	acceptor.bind(new InetSocketAddress(getInt(SERVER_PORT)));
	logger.info("Server started on port " + getString(SERVER_PORT));
}
 
Example #7
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();    
}