本文整理汇总了Java中org.apache.mina.transport.socket.SocketSessionConfig.setTcpNoDelay方法的典型用法代码示例。如果您正苦于以下问题:Java SocketSessionConfig.setTcpNoDelay方法的具体用法?Java SocketSessionConfig.setTcpNoDelay怎么用?Java SocketSessionConfig.setTcpNoDelay使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.mina.transport.socket.SocketSessionConfig
的用法示例。
在下文中一共展示了SocketSessionConfig.setTcpNoDelay方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: startListner
import org.apache.mina.transport.socket.SocketSessionConfig; //导入方法依赖的package包/类
public void startListner(IoHandler iohandler,int listenPort) throws Exception{
acceptor = new NioSocketAcceptor();
acceptor.setBacklog(100);
acceptor.setReuseAddress(true);
acceptor.setHandler(iohandler);
DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
IoFilter protocol = new ProtocolCodecFilter(new GameProtocolcodecFactory());
chain.addLast("codec", protocol);
threadpool = new OrderedThreadPoolExecutor(500);
threadpool.setThreadFactory(new ServerThreadFactory("OrderedThreadPool"));
chain.addLast("threadPool", new ExecutorFilter(threadpool));
int recsize = 5120;
int sendsize = 40480;
int timeout = 10;
SocketSessionConfig sc = acceptor.getSessionConfig();
sc.setReuseAddress(true);// 设置每一个非主监听连接的端口可以重用
sc.setReceiveBufferSize(recsize);// 设置输入缓冲区的大小
sc.setSendBufferSize(sendsize);// 设置输出缓冲区的大小
sc.setTcpNoDelay(true);// flush函数的调用 设置为非延迟发送,为true则不组装成大包发送,收到东西马上发出
sc.setSoLinger(0);
sc.setIdleTime(IdleStatus.READER_IDLE, timeout);
acceptor.bind(new InetSocketAddress(listenPort));
}
示例4: sessionCreated
import org.apache.mina.transport.socket.SocketSessionConfig; //导入方法依赖的package包/类
public void sessionCreated(IoSession session)
{
if (session.getConfig() instanceof SocketSessionConfig)
{
SocketSessionConfig cfg = (SocketSessionConfig) session.getConfig();
cfg.setReceiveBufferSize(1024);
cfg.setTcpNoDelay(true);
}
session.getConfig().setIdleTime(IdleStatus.READER_IDLE, timeout);
// We're going to use SSL negotiation notification.
session.setAttribute(SslFilter.USE_NOTIFICATION);
// Initialize POP3 protocol session attributes
MinaPop3Connection conn = new MinaPop3Connection(session, userManager);
session.setAttribute(CONNECTION, conn);
session.setAttribute(USING_APOP_AUTH, usingAPOPAuthMethod);
session.setAttribute(SECURED_AUTH_REQUIRED, secureAuthRequired);
session.setAttribute(PEER_IS_CLOSING_SESSION, Boolean.FALSE);
sendGreetings(conn);
}
示例5: SocketServer
import org.apache.mina.transport.socket.SocketSessionConfig; //导入方法依赖的package包/类
public SocketServer(int port, MessageHandler msgHandler, int threadPoolSize) {
mServerHandler = new ServerHandler(msgHandler);
acceptor = new NioSocketAcceptor();
acceptor.setHandler(mServerHandler);// 设置接收器的处理程序
acceptor.setReuseAddress(true) ;
// acceptor.getFilterChain().addLast("exector", new ExecutorFilter(new OrderedThreadPoolExecutor(10)));//建立有序线程池
acceptor.getFilterChain().addLast("exector", new ExecutorFilter(new OrderedThreadPoolExecutor(Runtime.getRuntime().availableProcessors()+1)));//建立有序线程池
// acceptor.getFilterChain().addLast("exector", new ExecutorFilter(new OrderedThreadPoolExecutor(128)));//建立有序线程池
/* Executor threadPool = Executors.newFixedThreadPool(threadPoolSize);// 建立线程池
acceptor.getFilterChain().addLast("exector",
new ExecutorFilter(threadPool));*/
acceptor.getFilterChain().addLast("codec",//解码\转码
new ProtocolCodecFilter(new MessageCodecFactory(Charset.forName("UTF-8"))));
try {
// 绑定端口
acceptor.bind(new InetSocketAddress(port));
logger.info("服务器启动正常,监听端口 " + port);
} catch (IOException e) {
e.printStackTrace();
}
SocketSessionConfig ssc = acceptor.getSessionConfig();// 建立连接的配置文件
ssc.setReadBufferSize(4096);// 设置接收最大字节默认4096
// ssc.setReceiveBufferSize(1024);// 设置输入缓冲区的大小
// ssc.setSendBufferSize(1024);// 设置输出缓冲区的大小
// ssc.setReuseAddress(true);// 设置每一个非主监听连接的端口可以重用
ssc.setTcpNoDelay(false);
}
示例6: initConnector
import org.apache.mina.transport.socket.SocketSessionConfig; //导入方法依赖的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: 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();
}
示例8: 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);
}
示例9: buildSocketAcceptor
import org.apache.mina.transport.socket.SocketSessionConfig; //导入方法依赖的package包/类
private static NioSocketAcceptor buildSocketAcceptor()
{
// Create SocketAcceptor with correct number of processors
final int processorCount = JiveGlobals.getIntProperty( "xmpp.processor.count", Runtime.getRuntime().availableProcessors() );
final NioSocketAcceptor socketAcceptor = new NioSocketAcceptor( processorCount );
// Set that it will be possible to bind a socket if there is a connection in the timeout state.
socketAcceptor.setReuseAddress( true );
// Set the listen backlog (queue) length. Default is 50.
socketAcceptor.setBacklog( JiveGlobals.getIntProperty( "xmpp.socket.backlog", 50 ) );
// Set default (low level) settings for new socket connections
final SocketSessionConfig socketSessionConfig = socketAcceptor.getSessionConfig();
//socketSessionConfig.setKeepAlive();
final int receiveBuffer = JiveGlobals.getIntProperty( "xmpp.socket.buffer.receive", -1 );
if ( receiveBuffer > 0 )
{
socketSessionConfig.setReceiveBufferSize( receiveBuffer );
}
final int sendBuffer = JiveGlobals.getIntProperty( "xmpp.socket.buffer.send", -1 );
if ( sendBuffer > 0 )
{
socketSessionConfig.setSendBufferSize( sendBuffer );
}
final int linger = JiveGlobals.getIntProperty( "xmpp.socket.linger", -1 );
if ( linger > 0 )
{
socketSessionConfig.setSoLinger( linger );
}
socketSessionConfig.setTcpNoDelay( JiveGlobals.getBooleanProperty( "xmpp.socket.tcp-nodelay", socketSessionConfig.isTcpNoDelay() ) );
return socketAcceptor;
}
示例10: sessionOpened
import org.apache.mina.transport.socket.SocketSessionConfig; //导入方法依赖的package包/类
/** {@inheritDoc} */
@Override
public void sessionOpened(IoSession session) throws Exception {
SocketSessionConfig ssc = (SocketSessionConfig) session.getConfig();
ssc.setTcpNoDelay(true);
super.sessionOpened(session);
}
示例11: sessionOpened
import org.apache.mina.transport.socket.SocketSessionConfig; //导入方法依赖的package包/类
/** {@inheritDoc} */
@Override
public void sessionOpened(IoSession session) throws Exception {
SocketSessionConfig ssc = (SocketSessionConfig) session.getConfig();
ssc.setTcpNoDelay(true);
//ssc.setReceiveBufferSize(2048);
//ssc.setSendBufferSize(2048);
super.sessionOpened(session);
}
示例12: bind
import org.apache.mina.transport.socket.SocketSessionConfig; //导入方法依赖的package包/类
@Override
public MinaAcceptance bind() {
// This socket acceptor will handle incoming connections
acceptor = new NioSocketAcceptor(transportConfig.getNioProcessorCount());
acceptor.setReuseAddress(true);
acceptor.setBacklog(128);
SocketSessionConfig sessionConfig = acceptor.getSessionConfig();
sessionConfig.setTcpNoDelay(true);
// set read buffer size
sessionConfig.setReceiveBufferSize(16 * 1024);
sessionConfig.setSendBufferSize(16 * 1024);
// get filter chain builder
DefaultIoFilterChainBuilder chainBuilder = acceptor.getFilterChain();
// add message codec filter
final TransportProtocolEncoder encoder = new TransportProtocolEncoder();
final TransportProtocolDecoder decoder = new TransportProtocolDecoder();
encoder.setMaxObjectSize(transportConfig.getMaxSize());
decoder.setMaxObjectSize(transportConfig.getMaxSize());
chainBuilder.addLast("codec", new ProtocolCodecFilter(encoder, decoder));
// add keep alive filter
KeepAliveFilter kaFilter = new KeepAliveFilter(new PassiveKeepAliveMessageFactory(), IdleStatus.BOTH_IDLE);
kaFilter.setRequestInterval(transportConfig.getHeartbeatIntervalSeconds());
kaFilter.setForwardEvent(true);
chainBuilder.addLast("keepAlive", kaFilter);
// add business handler
acceptor.setHandler(new MinaIoHandler());
try {
acceptor.bind(serviceAddress);
int processorCount = transportConfig.getBusinessProcessorCount();
if (processorCount > 0) {
NamedThreadFactory threadFactory = new NamedThreadFactory("Mina-BusinssProcessor");
executorService = Executors.newFixedThreadPool(processorCount, threadFactory);
}
} catch (Exception e) {
throw new RuntimeException("can't bind service on " + serviceAddress, e);
}
LOG.info("mina acceptance bind on {}", serviceAddress);
return this;
}
示例13: 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");
}