当前位置: 首页>>代码示例>>Java>>正文


Java NioSocketConnector.getFilterChain方法代码示例

本文整理汇总了Java中org.apache.mina.transport.socket.nio.NioSocketConnector.getFilterChain方法的典型用法代码示例。如果您正苦于以下问题:Java NioSocketConnector.getFilterChain方法的具体用法?Java NioSocketConnector.getFilterChain怎么用?Java NioSocketConnector.getFilterChain使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.mina.transport.socket.nio.NioSocketConnector的用法示例。


在下文中一共展示了NioSocketConnector.getFilterChain方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: initConnector

import org.apache.mina.transport.socket.nio.NioSocketConnector; //导入方法依赖的package包/类
private void initConnector(InetSocketAddress isa, TransportConfig config) {
    // create connector
    ioConnector = new NioSocketConnector(1);
    ioConnector.setConnectTimeoutMillis(config.getConnectTimeout());

    SocketSessionConfig sessionConfig = ioConnector.getSessionConfig();
    sessionConfig.setTcpNoDelay(true);
    sessionConfig.setReceiveBufferSize(8 * 1024);
    sessionConfig.setSendBufferSize(8 * 1024);

    DefaultIoFilterChainBuilder chainBuilder = ioConnector.getFilterChain();

    final TransportProtocolEncoder encoder = new TransportProtocolEncoder();
    final TransportProtocolDecoder decoder = new TransportProtocolDecoder();
    encoder.setMaxObjectSize(config.getMaxSize());
    decoder.setMaxObjectSize(config.getMaxSize());
    // add filter
    chainBuilder.addLast("codec", new ProtocolCodecFilter(encoder, decoder));

    // add keep alive filter
    ActiveKeepAliveMessageFactory messageFactory = new ActiveKeepAliveMessageFactory(config.getSerializeType());
    KeepAliveFilter kaFilter = new KeepAliveFilter(messageFactory, IdleStatus.BOTH_IDLE);
    kaFilter.setRequestInterval(config.getHeartbeatIntervalSeconds());
    kaFilter.setForwardEvent(true);
    chainBuilder.addLast("keepAlive", kaFilter);

    // set handler
    ioConnector.setHandler(new MinaIoHandler());

    ioConnector.setDefaultRemoteAddress(isa);
}
 
开发者ID:dinstone,项目名称:jrpc,代码行数:32,代码来源:MinaConnector.java

示例2: connect

import org.apache.mina.transport.socket.nio.NioSocketConnector; //导入方法依赖的package包/类
/**
 * �����ͻ�������
 */
public boolean connect() {
	// ʵ���� �������� Socket����
	client = new NioSocketConnector();
	// �������ݹ�����
	DefaultIoFilterChainBuilder filterChain = client.getFilterChain();
	//filterChain.addLast("textCode", new ProtocolCodecFilter(
	//		new TextLineCodecFactory(Charset.forName("UTF-8"))));
	filterChain.addLast("myChin", new ProtocolCodecFilter(
			new ObjectSerializationCodecFactory()));
	// �ͻ��˴������
	ClientIoHandler clientIoHandler = new ClientIoHandler(loginFrame,client);
	client.setHandler(clientIoHandler);
	clientIoHandler.setRegisterFrame(registerFrame);
	// ���ӷ�����
	ConnectFuture future = client.connect(new InetSocketAddress(
			IP, Port));
	// �ȴ�
	future.awaitUninterruptibly();
	// �õ��Ự����
	try {
		session = future.getSession();
		return true;
	} catch (Exception e) {
		Tools.show(loginFrame, "�޷����ӷ�������������û������");
		client.dispose();
		if(registerFrame!=null)
		registerFrame.dispose();
		return false;
	}
	// session.getCloseFuture().awaitUninterruptibly();

}
 
开发者ID:ganhang,项目名称:My-ChatSystem,代码行数:36,代码来源:Client.java

示例3: start

import org.apache.mina.transport.socket.nio.NioSocketConnector; //导入方法依赖的package包/类
public void start() throws Exception {
	buildExecutors();
	IoProcessor<NioSession> processor = new SimpleIoProcessorPool<NioSession>(
			NioProcessor.class, ioExecutor, coreSize);
	connector = new NioSocketConnector(connectorExecutor, processor);
	connector.setConnectTimeoutMillis((Integer) rule.get("timeout")); // 设置连接超时。见AbstractPollingIoConnector.processTimedOutSessions()与ConnectionRequest类
	// connector.getSessionConfig().setUseReadOperation(true); //
	// 亦可使用该方式实现同步发送并接收数据,这样无须设置Handler,通过session.read()获取
	handler = new ShortConnectorHandler();
	connector.setHandler(handler);
	DefaultIoFilterChainBuilder filterChain = connector.getFilterChain();
	filterChain.addLast("codec", new ProtocolCodecFilter(
			ProtocolCodecFactoryFactory.getInstance(rule)));
}
 
开发者ID:dreajay,项目名称:jcode,代码行数:15,代码来源:TcpConnector.java

示例4: main

import org.apache.mina.transport.socket.nio.NioSocketConnector; //导入方法依赖的package包/类
public static void main(String[] args) {
	NioSocketConnector connector = new NioSocketConnector();
	DefaultIoFilterChainBuilder chain = connector.getFilterChain();
	SocketSessionConfig config = connector.getSessionConfig();
	config.setReuseAddress(true);
	config.setIdleTime(IdleStatus.READER_IDLE, 20);// 读空闲 20秒
	config.setReadBufferSize(4096);// 默认2048
	config.setKeepAlive(true);
	config.setTcpNoDelay(true);// 禁用/开启nagle算法
	// 编解码
	chain.addLast("coder", new ProtocolCodecFilter(new MessageCodecFactory()));
	// 日志
	chain.addLast("logger", new LoggingFilter());
	// 业务逻辑处理线程池
	chain.addLast("threadPool", new ExecutorFilter());
	connector.setHandler(new MinaMessageHandler());
	connector.setConnectTimeoutMillis(15 * 1000); // 设置连接超时 ,15秒
	connector.setConnectTimeoutCheckInterval(300);
	// 建立连接
	ConnectFuture cf = connector.connect(new InetSocketAddress("localhost", 8000));
	// 等待连接创建完成
	cf.awaitUninterruptibly();
	Packet packet = Packet.newBuilder().setCmdId(0).setChatMsgAck(ChatMsgAck.newBuilder().setResult(123)).build();
	cf.getSession().write(packet.toByteArray());
	packet = Packet.newBuilder().setCmdId(1).setChatMsgAck(ChatMsgAck.newBuilder().setResult(123)).build();
	cf.getSession().write(packet.toByteArray());

	// 等待连接断开
	cf.getSession().getCloseFuture().awaitUninterruptibly();
	// 释放连接
	connector.dispose();

}
 
开发者ID:East196,项目名称:maker,代码行数:34,代码来源:TcpClient.java

示例5: MinaConnector

import org.apache.mina.transport.socket.nio.NioSocketConnector; //导入方法依赖的package包/类
public MinaConnector(CodecFactory codecFactory, int maxIoThreads, int maxTaskThreads, TaskMapper taskMapper) {
	Objects.requireNonNull(codecFactory);

	log.info("Initializing connector {max I/O threads: {}, max task threads: {}}",
			Integer.toString(maxIoThreads),
			maxTaskThreads > 0
					? Integer.toString(maxTaskThreads)
					: "unbounded");

	connector = new NioSocketConnector(maxIoThreads);
	DefaultIoFilterChainBuilder filters = connector.getFilterChain();
	
	// encoding
	filters.addLast("codec", new ProtocolCodecFilter(new MinaCodecFactory(codecFactory)));
	
	// thread model
	executor = newExecutor(maxTaskThreads, taskMapper);
	filters.addLast("threadPool", new ExecutorFilter(executor));

	// logging
	filters.addLast("logger", new LoggingFilter());

	// session handler
	minaAdapter = new MinaAdapter();
	connector.setHandler(minaAdapter);
	
	// connector configuration
	connector.getSessionConfig().setReuseAddress(true);
	connector.getSessionConfig().setKeepAlive(true);
	connector.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, DEFAULT_SESSION_IDLE_SECONDS);
	// no Nagle's algorithm
	connector.getSessionConfig().setTcpNoDelay(true);
}
 
开发者ID:ugcs,项目名称:ugcs-java-sdk,代码行数:34,代码来源:MinaConnector.java

示例6: start

import org.apache.mina.transport.socket.nio.NioSocketConnector; //导入方法依赖的package包/类
private void start() {
	Config conf = ConfigFactory.load();
	String check_server_beat_ip = conf.getString("check_server_beat_ip");
	int check_server_beat_port = conf.getInt("check_server_beat_port");
	if (U.isEmpty(check_server_beat_ip)) {
		log.error("BeatClient 连接心跳服务器 没有配置!");
		return;
	}

	NioSocketConnector connector = new NioSocketConnector();
	DefaultIoFilterChainBuilder chain = connector.getFilterChain();
	chain.addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
	connector.setHandler(new BeatHanlder());
	connector.setConnectTimeoutMillis(15 * 1000); // 设置连接超时 ,15秒
	connector.setConnectTimeoutCheckInterval(300);

	long waitTimeConnect = 2 * 1000;
	final long waitTimeMAX = 30 * 1000;
	final long waitTimeSpace = 2 * 1000;
	for (;;) {
		try {
			ConnectFuture cf = connector
					.connect(new InetSocketAddress(check_server_beat_ip, check_server_beat_port));
			cf.awaitUninterruptibly();
			IoSession session = cf.getSession();// 获取会话
			if (session.isConnected()) {
				log.info("BeatClient 连接心跳服务器OK IP:" + check_server_beat_ip + ",PORT:" + check_server_beat_port);
				break;
			}
			Thread.sleep(waitTimeConnect);
		} catch (Exception e) {
			log.error("ERROR_connect :" + e.getMessage());
			try {
				Thread.sleep(waitTimeConnect);
			} catch (InterruptedException e1) {
			}
		} finally {
			waitTimeConnect += waitTimeSpace;
			if (waitTimeConnect > waitTimeMAX) {
				waitTimeConnect = waitTimeSpace;
			}
		}
	}
}
 
开发者ID:East196,项目名称:maker,代码行数:45,代码来源:BeatClient.java

示例7: MyClient

import org.apache.mina.transport.socket.nio.NioSocketConnector; //导入方法依赖的package包/类
/**
     * Constructor
     */
    public MyClient() {
        connector = new NioSocketConnector();
        /**
         * 设置信息交换的IoHandler,负责接收和发送信息的处理
         */
        connector.setHandler(new ClientIoHandler());
        //配置过滤器
        DefaultIoFilterChainBuilder chain = connector.getFilterChain();
        //增加日志过滤器
        chain.addLast("logger", new LoggingFilter());
        //增加字符编码过滤器以及设置编码器和解码器
        //chain.addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
        /**
         * 默认编码器,解码器,遇到\n默认消息结束
         * 当然可以加参数指定解码字符,但解码字符会被截掉
         * 例如:new TextLineCodecFactory(Charset.forName("UTF-8"),"]","]");
         * 则会认为"]"为一条消息结束,遇到"]"则截取
         * 比如服务器给你发送的消息是aaaa]aaaa]
         * 会收到两条消息:
         * 1、aaaa
         * 2、aaaa
         * 后面的"]"则去掉了
         */
//        acceptor.getFilterChain().addLast(
//                "codec",
//                new ProtocolCodecFilter(new TextLineCodecFactory(Charset
//                        .forName("UTF-8"))));// 指定编码过滤器
        DemuxingProtocolCodecFactory pcf = new DemuxingProtocolCodecFactory();
        //自定义编码器
        pcf.addMessageEncoder(String.class, new MyMessageEncoder());
        //自定义解码器
        pcf.addMessageDecoder(new MyMessageDecoder());
        ProtocolCodecFilter codec = new ProtocolCodecFilter(pcf);
        chain.addLast("codec",codec);// 指定编码过滤器
        //设置默认连接的地址和端口
        connector.setDefaultRemoteAddress(new InetSocketAddress("localhost", 8888));
        /**
         * 重连机制
         * 如果没有连接,则过30 * 1000毫秒客户端会尝试重新连接服务器
         * 如果连接,则下面的代码不会执行
         */
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                if (null != connector && !connector.isActive()) {
                    try {
                        //尝试连接默认的地址和端口
                        ConnectFuture connFuture = connector.connect();
                        connFuture.awaitUninterruptibly();
                    } catch (Exception e) {
                        // TODO: handle exception
                        e.printStackTrace();
                    }
                }
            }
        }, new Date(), 30 * 1000);
    }
 
开发者ID:joezhjiang,项目名称:ics-mina,代码行数:61,代码来源:MyClient.java


注:本文中的org.apache.mina.transport.socket.nio.NioSocketConnector.getFilterChain方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。