本文整理汇总了Java中org.apache.mina.transport.socket.SocketSessionConfig.setKeepAlive方法的典型用法代码示例。如果您正苦于以下问题:Java SocketSessionConfig.setKeepAlive方法的具体用法?Java SocketSessionConfig.setKeepAlive怎么用?Java SocketSessionConfig.setKeepAlive使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.mina.transport.socket.SocketSessionConfig
的用法示例。
在下文中一共展示了SocketSessionConfig.setKeepAlive方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.apache.mina.transport.socket.SocketSessionConfig; //导入方法依赖的package包/类
public static void main(String[] args) {
int port = U.getInt("cleaner.server.port");
int idleTime = U.getInt("cleaner.server.time");
NioSocketAcceptor clientAcceptor = new NioSocketAcceptor();
clientAcceptor.setReuseAddress(true);
DefaultIoFilterChainBuilder chain = clientAcceptor.getFilterChain();
SocketSessionConfig config = clientAcceptor.getSessionConfig();
config.setIdleTime(IdleStatus.READER_IDLE, idleTime);// 读空闲 10秒
config.setReuseAddress(true);
config.setReadBufferSize(4096);// 默认2048
config.setKeepAlive(true);
config.setTcpNoDelay(true);// 开启nagle算法
// 编解码
chain.addLast("coder", new ProtocolCodecFilter(new TextLineCodecFactory(U.UTF8)));
clientAcceptor.setHandler(new CleanerHandler());
try {
clientAcceptor.bind(new InetSocketAddress(port));
log.info("心跳模块启动..");
} catch (IOException e) {
log.error("心跳模块启动失败", e);
}
}
示例2: initialize
import org.apache.mina.transport.socket.SocketSessionConfig; //导入方法依赖的package包/类
public void initialize() {
idleTime = U.getInt("chat.server.idleTime");
System.out.println(idleTime);
clientAcceptor = new NioSocketAcceptor();
clientAcceptor.setReuseAddress(true);
DefaultIoFilterChainBuilder chain = clientAcceptor.getFilterChain();
SocketSessionConfig config = clientAcceptor.getSessionConfig();
config.setReuseAddress(true);
config.setIdleTime(IdleStatus.READER_IDLE, idleTime);// 读空闲 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());
}
示例3: getSessionConfig
import org.apache.mina.transport.socket.SocketSessionConfig; //导入方法依赖的package包/类
private SocketSessionConfig getSessionConfig() {
SocketSessionConfig config = new DefaultSocketSessionConfig();
config.setKeepAlive(true);
config.setReuseAddress(true);
return config;
}
示例4: main
import org.apache.mina.transport.socket.SocketSessionConfig; //导入方法依赖的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();
}
示例5: start
import org.apache.mina.transport.socket.SocketSessionConfig; //导入方法依赖的package包/类
@Override
public void start() throws SuspendExecution, Exception {
// Acceptor
acceptor = new PooledNioSocketAcceptor(cpool);
acceptor.setBacklog(1024);
acceptor.setSessionDataStructureFactory(new PooledIoSessionDataStructureFactory(cpool));
messageFactory =
new MinaMessageFactory(
// hostAx.getActorSystem().getCustomMessageMap(),
hostAx.getActorSystem().getCMsgMap(),
localInfo.getMinaMsgPoolSize(),
localInfo.getMinaMsgPoolInitSize(),
localInfo.getMinaMsgPoolMaxSize()
);
acceptor.getFilterChain().addLast("codec", new PooledProtocolCodecFilter(cpool, new MinaCodecFactory(messageFactory)));
// Handler
acceptor.setHandler(new MinaIoHandler(hostAx));
// Config
acceptor.setReuseAddress(true);
SocketSessionConfig sscfg = acceptor.getSessionConfig();
sscfg.setReadBufferSize(2048);
sscfg.setMinReadBufferSize(2048);
sscfg.setMaxReadBufferSize(2048);
sscfg.setIdleTime(IdleStatus.READER_IDLE, localInfo.getMinaIdleTime());
sscfg.setReceiveBufferSize(64 * 1024);
sscfg.setSendBufferSize(160 * 1024);
sscfg.setWriteTimeout(2);
sscfg.setTcpNoDelay(true);
sscfg.setSoLinger(-1);
sscfg.setKeepAlive(false);
// Bind
List<SocketAddress> socketAddresses = new ArrayList<SocketAddress>();
socketAddresses.add(new InetSocketAddress(localInfo.getHost(), localInfo.getPort()));
acceptor.bind(socketAddresses);
}
示例6: sessionCreated
import org.apache.mina.transport.socket.SocketSessionConfig; //导入方法依赖的package包/类
@Override
public void sessionCreated(IoSession session) throws Exception {
SocketSessionConfig cfg = (SocketSessionConfig) session.getConfig();
cfg.setReceiveBufferSize(2 * 1024 * 1024);
cfg.setReadBufferSize(2 * 1024 * 1024);
cfg.setKeepAlive(true);
cfg.setSoLinger(0);
}
示例7: start
import org.apache.mina.transport.socket.SocketSessionConfig; //导入方法依赖的package包/类
@Override
public void start() throws SuspendExecution, Exception {
connector = new PooledNioSocketConnector(cpool);
connector.setSessionDataStructureFactory(new PooledIoSessionDataStructureFactory(cpool));
messageFactory =
new MinaMessageFactory(
// hostAx.getActorSystem().getCustomMessageMap(),
hostAx.getActorSystem().getCMsgMap(),
remoteInfo.getMinaMsgPoolSize(),
remoteInfo.getMinaMsgPoolInitSize(),
remoteInfo.getMinaMsgPoolMaxSize()
);
connector.getFilterChain().addLast("codec", new PooledProtocolCodecFilter(cpool, new MinaCodecFactory(messageFactory)));
connector.setHandler(new MinaIoHandler(hostAx));
connector.setConnectTimeoutMillis(TimeUnit.SECONDS.toMillis(remoteInfo.getMinaConnectTimeout()));
SocketSessionConfig sscfg = connector.getSessionConfig();
sscfg.setReadBufferSize(2048);
sscfg.setMinReadBufferSize(2048);
sscfg.setMaxReadBufferSize(2048);
sscfg.setIdleTime(IdleStatus.READER_IDLE, remoteInfo.getMinaIdleTime());
sscfg.setReceiveBufferSize(64 * 1024);
sscfg.setSendBufferSize(160 * 1024);
sscfg.setWriteTimeout(2);
sscfg.setTcpNoDelay(true);
sscfg.setSoLinger(-1);
sscfg.setKeepAlive(false);
heartAid = hostAx.getActorSystem().spawn(hostAx, hostAx.getFiberScheduler(), new IFiberActorHandler() {
@Override
public void run(Actor self) throws SuspendExecution, InterruptedException {
// 心跳计时器Actor
Message cmsg = self.recv("INIT");
ActorId sireAid = cmsg.getSender();
Pattern pattern = new Pattern();
pattern.match("STOP");
pattern.after(remoteInfo.getHeartTimeout(), TimeUnit.SECONDS);
while (true){
try (PoolGuard guard = self.precv(pattern)){
Message msg = guard.get();
if (msg != null){
String type = msg.getType();
if (AtomCode.equals(type, "STOP")){
break;
}
}else{
self.send(sireAid, "HEART");
}
}catch (Throwable e){
logger.error("{} heart error: {}", getClass().getSimpleName(), ExceptionUtils.printStackTrace(e));
}
}
}
}, LinkType.MONITORED);
hostAx.send(heartAid, "INIT");
}