本文整理汇总了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);
}
示例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));
}
示例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();
}
示例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();
}
}
示例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);
}
示例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);
}
示例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)));
}
示例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();
}
示例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();
}
示例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()));
}
示例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);
}
示例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;
}
示例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));
}
示例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;
}
示例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;
}