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


Java NioSocketConnector.setConnectTimeoutMillis方法代码示例

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


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

示例1: MinaDestination

import org.apache.mina.transport.socket.nio.NioSocketConnector; //导入方法依赖的package包/类
public MinaDestination(String host, int port, boolean withLogging) throws Exception {
    socketAddress = new InetSocketAddress(host, port);
    connector = new NioSocketConnector(Runtime.getRuntime().availableProcessors() + 1);
    connector.setConnectTimeoutMillis(10000L);
    connectorConfig = connector.getSessionConfig();
    executor = new OrderedThreadPoolExecutor(5);

    connector.getFilterChain().addLast("threadPool", new ExecutorFilter(executor));
    if (withLogging) {
        connector.getFilterChain().addLast("logger", new LoggingFilter());
    }

    SslFilter filter = new SslFilter(SSLContext.getDefault(), true);
    filter.setUseClientMode(true);
    connector.getFilterChain().addFirst("sslFilter", filter);
}
 
开发者ID:oehf,项目名称:ipf-oht-atna,代码行数:17,代码来源:MinaTLSSyslogSenderImpl.java

示例2: connect

import org.apache.mina.transport.socket.nio.NioSocketConnector; //导入方法依赖的package包/类
/**
 * 执行连接操作的方法
 * 
 * @throws RedisProtocolException
 *             当连接出现问题时抛出该异常
 */
private void connect() throws RedisProtocolException {
	connector = new NioSocketConnector();
	connector.setConnectTimeoutMillis(connectionTimeOut);
	connector.getFilterChain().addFirst("redis-protocol", new ProtocolCodecFilter(new RedisProtocolCodecFactory()));
	connector.setHandler(this);
	connector.connect(new InetSocketAddress(address, port));
}
 
开发者ID:wmz7year,项目名称:Redis-Synyed,代码行数:14,代码来源:DefaultRedisConnection.java

示例3: Work

import org.apache.mina.transport.socket.nio.NioSocketConnector; //导入方法依赖的package包/类
public Work(){
			NioSocketConnector connector = new NioSocketConnector(); 
			connector.getFilterChain().addLast( "logger", new LoggingFilter() ); 
			connector.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new MessageCodecFactory())); //设置编码过滤器 
			connector.setConnectTimeoutMillis(30000); 
			connector.setHandler(new ClientMessageHandler());//设置事件处理器 
			ConnectFuture cf = connector.connect( 
					new InetSocketAddress("127.0.0.1", 50000));//建立连接 
			cf.awaitUninterruptibly();//等待连接创建完成 
			ByteArray ba = new ByteArray();
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("parseId", 1);
			jsonObject.put("command", 1);
			jsonObject.put("text", "涂鸦");
			ba.writeUTF(jsonObject.toJSONString());
			cf.getSession().write(ba.toArray());
//			cf.getSession().write("涂鸦");//发送消息 
//			cf.getSession().write("quit");//发送消息 
//			cf.getSession().getCloseFuture().awaitUninterruptibly();//等待连接断开 
//			connector.dispose();
		}
 
开发者ID:mklm1525,项目名称:GameServer_demo,代码行数:22,代码来源:TestClient.java

示例4: connect

import org.apache.mina.transport.socket.nio.NioSocketConnector; //导入方法依赖的package包/类
private synchronized void connect (boolean bIsReconnect, boolean bUseReadOperation) {
	try {
		if (mFuture != null && mFuture.isConnected()) {
			return;
		}
		if (mPort == 0) {
			return;
		}
		mConnector = new NioSocketConnector();
		//设置连接超时时间
		mConnector.setConnectTimeoutMillis(2000);
		mConnector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MessageCodecFactory(Charset.forName("UTF-8"))));
		mConnector.setHandler(mClientHandler);
//		logger.info("尝试连接 Server " + mAddr + ":" + mPort+"--------------------------------------------");
		mFuture = mConnector.connect(new InetSocketAddress(mAddr, mPort));
		//设置IDLE时间
		mConnector.getSessionConfig().setIdleTime(IdleStatus.READER_IDLE, 40);
		mConnector.getSessionConfig().setTcpNoDelay(false);
		mConnector.getSessionConfig().setUseReadOperation(bUseReadOperation);
		mFuture.awaitUninterruptibly();
		boolean connected=mFuture.isConnected();
		if (!connected && bIsReconnect) {
//			logger.info("connect fail mFuture=[ " + mFuture + "],mAddr =[" + mAddr + ":" + mPort);
			reset();
			mConnector.dispose(true);
			//如果链接失败,则重新启动链接重试线程
			(new Thread(this)).start();
		} else {
			//如果连接成功,则发送所有队列中的消息
			logger.info("connect success " + mAddr + ":" + mPort);
			sendAllMsgInQueue();
		}
	} catch (Exception e) {
		logger.fatal("SocketClient ["+mAddr+"  :  "+ mPort+"] connect()失败    Exception: " + e + " " + Arrays.toString(e.getStackTrace()));
		//如果链接失败,则重新启动链接重试线程
		(new Thread(this)).start();
	}
}
 
开发者ID:langxianwei,项目名称:iot-plat,代码行数:39,代码来源:SocketClient.java

示例5: MinaCubeClient

import org.apache.mina.transport.socket.nio.NioSocketConnector; //导入方法依赖的package包/类
public MinaCubeClient(String hostname, int port) {
    this.hostname = hostname;
    this.port = port;
    connector = new NioSocketConnector(1);
    connector.setConnectTimeoutMillis(2000);

    connector.getFilterChain().addLast("logger", new LoggingFilter());
    TextLineCodecFactory codecFactory = new TextLineCodecFactory(UTF_8, LineDelimiter.CRLF, LineDelimiter.AUTO);
    codecFactory.setDecoderMaxLineLength(4096);
    codecFactory.setEncoderMaxLineLength(4096);
    connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(codecFactory));

    handler = new ConnectHandler();
    connector.setHandler(handler);
}
 
开发者ID:spinscale,项目名称:maxcube-java,代码行数:16,代码来源:MinaCubeClient.java

示例6: 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

示例7: 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

示例8: main

import org.apache.mina.transport.socket.nio.NioSocketConnector; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
//		if (args.length != 2) {
//			System.out.println(Main.class.getName() + " <hostname> <port>");
//			return;
//		}
		DateTime endTime = DateTime.now().withTimeAtStartOfDay();
		DateTime startTime = endTime.minusMonths(1);
		List<DateTime> dayStarts=dayStarts(startTime, endTime);
		for (DateTime dateTime : dayStarts) {
			System.out.println(dateTime.toString("yyyy-MM-dd HH:mm:ss"));
		}
		
		
		// Create TCP/IP connector.
		NioSocketConnector connector = new NioSocketConnector();

		// Set connect timeout.
		connector.setConnectTimeoutMillis(30 * 1000L);

		// Start communication.
		connector.setHandler(new NetCatProtocolHandler());
		ConnectFuture cf = connector.connect(new InetSocketAddress(args[0], Integer.parseInt(args[1])));

		// Wait for the connection attempt to be finished.
		cf.awaitUninterruptibly();
		cf.getSession().getCloseFuture().awaitUninterruptibly();
		connector.dispose();
	}
 
开发者ID:East196,项目名称:maker,代码行数:29,代码来源:Main.java

示例9: 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

示例10: TetrisClient

import org.apache.mina.transport.socket.nio.NioSocketConnector; //导入方法依赖的package包/类
public TetrisClient(String host, int port) {
	this.host = host;
	this.port = port;
	connector = new NioSocketConnector();
	connector.setConnectTimeoutMillis(CONNECT_TIMEOUT);
	connector.getFilterChain().addLast("logger", new LoggingFilter());
	connector.getFilterChain().addLast("protocol",
			new ProtocolCodecFilter(new TetrisCodecFactory()));
}
 
开发者ID:jptiancai,项目名称:TetrisMina,代码行数:10,代码来源:TetrisClient.java

示例11: CIMConnectorManager

import org.apache.mina.transport.socket.nio.NioSocketConnector; //导入方法依赖的package包/类
private CIMConnectorManager(Context ctx) {
    context = ctx;
    executor = Executors.newFixedThreadPool(3);

    connector = new NioSocketConnector();
    connector.setConnectTimeoutMillis(10 * 1000);
    connector.getSessionConfig().setBothIdleTime(180);
    connector.getSessionConfig().setKeepAlive(true);
    connector.getFilterChain().addLast("logger", new LoggingFilter());
    connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ClientMessageCodecFactory()));
    connector.setHandler(iohandler);
}
 
开发者ID:SatanFu,项目名称:CimChat,代码行数:13,代码来源:CIMConnectorManager.java

示例12: createConnector

import org.apache.mina.transport.socket.nio.NioSocketConnector; //导入方法依赖的package包/类
private NioSocketConnector createConnector() {
    NioSocketConnector connector = new NioSocketConnector();
    connector.setConnectTimeoutMillis(tcpClientSettings.getTcpTransactionTimeoutSec() * 1000);
    connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TransactionCodecFactory()));
    connector.setHandler(new TCPSessionHandler());
    return connector;
}
 
开发者ID:guardtime,项目名称:ksi-java-sdk,代码行数:8,代码来源:KSITCPClient.java

示例13: connect

import org.apache.mina.transport.socket.nio.NioSocketConnector; //导入方法依赖的package包/类
public void connect(String host, int port) throws IOException, ExecutionException, InterruptedException {
	this.host = host;
	this.port = port;
	connector = new NioSocketConnector();
	connector.setConnectTimeoutMillis(4000);
	connector.getFilterChain().addLast("codec",
			new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
	connector.getSessionConfig().setIdleTime(IdleStatus.WRITER_IDLE, 10);
	connector.getSessionConfig().setIdleTime(IdleStatus.READER_IDLE, 10);
	connector.getSessionConfig().setReceiveBufferSize(2048);
	connector.setHandler(this);
	System.out.println("Connecting.. " + host + ":" + port + DateTime.now().toString());
	future = connector.connect(new InetSocketAddress(host, port));
}
 
开发者ID:induwarabas,项目名称:simple-event-loop,代码行数:15,代码来源:Client.java

示例14: getSocketConnector

import org.apache.mina.transport.socket.nio.NioSocketConnector; //导入方法依赖的package包/类
private static final NioSocketConnector getSocketConnector(long timeout, IoHandler handler) {
    // Create a new NioSocketConnector
    //NioSocketConnector connector = new NioSocketConnector(m_executor, m_processor);

    // To address bug NMS-6412, I'm changing this to use the default constructor so that the
    // Executor pool and IoProcessor pools are created and destroyed for every connector.
    // This slows things down but might be an acceptable tradeoff for more reliable detection.
    //
    // @see http://issues.opennms.org/browse/NMS-6412
    //
    NioSocketConnector connector = new NioSocketConnector(Runtime.getRuntime().availableProcessors());

    // Enable SO_REUSEADDR on the socket so that TIMED_WAIT connections that are bound on the
    // same port do not block new outgoing connections. If the connections are blocked, then
    // the following exception will be thrown under heavy load:
    //
    // Caused by: java.net.SocketException: Invalid argument
    //   at sun.nio.ch.Net.connect(Native Method)
    //   at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:500)
    //   at org.apache.mina.transport.socket.nio.NioSocketConnector.connect(NioSocketConnector.java:188)
    //   ...
    //
    // @see http://issues.opennms.org/browse/NMS-5469
    //
    connector.getSessionConfig().setReuseAddress(true);

    // Setting SO_LINGER will prevent TIME_WAIT sockets altogether because TCP connections will be
    // forcefully terminated with RST packets. However, this doesn't seem to be necessary to maintain
    // performance of the outgoing connections. As long as SO_REUSEADDR is set, the operating system
    // and Java appear to recycle the ports effectively although some will remain in TIME_WAIT state.
    //
    // @see http://issues.opennms.org/browse/NMS-5469
    //
    //connector.getSessionConfig().setSoLinger(0);

    connector.setHandler(handler);
    connector.setConnectTimeoutMillis(timeout);
    return connector;
}
 
开发者ID:qoswork,项目名称:opennmszh,代码行数:40,代码来源:ConnectionFactoryNewConnectorImpl.java

示例15: getSocketConnector

import org.apache.mina.transport.socket.nio.NioSocketConnector; //导入方法依赖的package包/类
private static final NioSocketConnector getSocketConnector(long timeout, IoHandler handler) {
    // Create a new NioSocketConnector
    NioSocketConnector connector = new NioSocketConnector(m_executor, m_processor);

    // Enable SO_REUSEADDR on the socket so that TIMED_WAIT connections that are bound on the
    // same port do not block new outgoing connections. If the connections are blocked, then
    // the following exception will be thrown under heavy load:
    //
    // Caused by: java.net.SocketException: Invalid argument
    //   at sun.nio.ch.Net.connect(Native Method)
    //   at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:500)
    //   at org.apache.mina.transport.socket.nio.NioSocketConnector.connect(NioSocketConnector.java:188)
    //   ...
    //
    // @see http://issues.opennms.org/browse/NMS-5469
    //
    connector.getSessionConfig().setReuseAddress(true);

    // Setting SO_LINGER will prevent TIME_WAIT sockets altogether because TCP connections will be
    // forcefully terminated with RST packets. However, this doesn't seem to be necessary to maintain
    // performance of the outgoing connections. As long as SO_REUSEADDR is set, the operating system
    // and Java appear to recycle the ports effectively although some will remain in TIME_WAIT state.
    //
    // @see http://issues.opennms.org/browse/NMS-5469
    //
    //connector.getSessionConfig().setSoLinger(0);

    connector.setHandler(handler);
    connector.setConnectTimeoutMillis(timeout);
    return connector;
}
 
开发者ID:vishwaabhinav,项目名称:OpenNMS,代码行数:32,代码来源:ConnectionFactoryNewConnectorImpl.java


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