當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。