本文整理匯總了Java中org.apache.mina.transport.socket.nio.NioSocketConnector.setConnectTimeoutCheckInterval方法的典型用法代碼示例。如果您正苦於以下問題:Java NioSocketConnector.setConnectTimeoutCheckInterval方法的具體用法?Java NioSocketConnector.setConnectTimeoutCheckInterval怎麽用?Java NioSocketConnector.setConnectTimeoutCheckInterval使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.mina.transport.socket.nio.NioSocketConnector
的用法示例。
在下文中一共展示了NioSocketConnector.setConnectTimeoutCheckInterval方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: 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();
}
示例2: 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;
}
}
}
}