本文整理匯總了Java中org.apache.mina.core.session.IdleStatus類的典型用法代碼示例。如果您正苦於以下問題:Java IdleStatus類的具體用法?Java IdleStatus怎麽用?Java IdleStatus使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
IdleStatus類屬於org.apache.mina.core.session包,在下文中一共展示了IdleStatus類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: main
import org.apache.mina.core.session.IdleStatus; //導入依賴的package包/類
public static void main(String[] args) throws IOException {
IoAcceptor acceptor = new NioSocketAcceptor();
// 這個過濾器用來記錄所有的信息,比如創建session(會話),接收消息,發送消息,關閉會話等
acceptor.getFilterChain().addLast("logger", new LoggingFilter());
// 用來轉換二進製或協議的專用數據到消息對象中
acceptor.getFilterChain().addLast("codec",
new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
// 實時處理客戶端的連接和請求
acceptor.setHandler(new TimeServerHandler());
acceptor.getSessionConfig().setReadBufferSize(2048);
// 方法將定時調用一次會話,保持空閑狀態。來設定時間間隔。
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
acceptor.bind(new InetSocketAddress(PORT));
}
示例2: deliverEvent
import org.apache.mina.core.session.IdleStatus; //導入依賴的package包/類
/**
* Static method which effectively delivers the specified event to the next filter
* <code>nextFilter</code> on the <code>session</code>.
*
* @param nextFilter the next filter
* @param session the session on which the event occured
* @param type the event type
* @param status the idle status should only be non null only if the event type is
* {@link IoSessionEventType#IDLE}
*/
private static void deliverEvent(final NextFilter nextFilter, final IoSession session,
final IoSessionEventType type, final IdleStatus status) {
switch (type) {
case CREATED:
nextFilter.sessionCreated(session);
break;
case OPENED:
nextFilter.sessionOpened(session);
break;
case IDLE:
nextFilter.sessionIdle(session, status);
break;
case CLOSED:
nextFilter.sessionClosed(session);
break;
}
}
示例3: KeepAliveFilter
import org.apache.mina.core.session.IdleStatus; //導入依賴的package包/類
/**
* Creates a new instance.
*/
public KeepAliveFilter(KeepAliveMessageFactory messageFactory, IdleStatus interestedIdleStatus,
KeepAliveRequestTimeoutHandler policy, int keepAliveRequestInterval, int keepAliveRequestTimeout) {
if (messageFactory == null) {
throw new IllegalArgumentException("messageFactory");
}
if (interestedIdleStatus == null) {
throw new IllegalArgumentException("interestedIdleStatus");
}
if (policy == null) {
throw new IllegalArgumentException("policy");
}
this.messageFactory = messageFactory;
this.interestedIdleStatus = interestedIdleStatus;
requestTimeoutHandler = policy;
setRequestInterval(keepAliveRequestInterval);
setRequestTimeout(keepAliveRequestTimeout);
}
示例4: sessionIdle
import org.apache.mina.core.session.IdleStatus; //導入依賴的package包/類
public void sessionIdle(IoSession session, IdleStatus status) {
//向服務端發送心跳消息
/*IoBuffer buffer1 = IoBuffer.allocate(28).setAutoExpand(true);
buffer1.put("<aa><aa><aa><ab>".getBytes());
buffer1.put(new byte[]{0, 10, 0, 0, 0, 0, 0, 0, 0, 0});
buffer1.flip();
session.write(buffer1);*/
// logger.debug("發送IDLE消息:" + session);
if (System.currentTimeMillis() - session.getLastReadTime() > 60000) {
String sErrMsg = (System.currentTimeMillis() - session.getLastReadTime()) + "毫秒沒有收到消息,主動斷開連接" + session;
logger.fatal(sErrMsg);
mSockClient.close(true);
// EMail email = new EMail(new String[]{"[email protected]"}, sErrMsg, sErrMsg);
// email.send();
}
}
示例5: sessionIdle
import org.apache.mina.core.session.IdleStatus; //導入依賴的package包/類
public void sessionIdle(IoSession session, IdleStatus status)throws Exception {
logger.info("客戶端sessionIdle, " + (System.currentTimeMillis() - session.getLastReadTime()) + "毫秒沒有上行。" + session.getRemoteAddress());
if((System.currentTimeMillis()-session.getLastReadTime())>1000*60*10){
Object userID = (Object)0;
if(session.containsAttribute("UserID")){
userID = session.getAttribute("UserID");
}
logger.info(" Player["+userID.toString()+"] -------------------------------------------Out of time, close user session lastReadTime:"+session.getLastReadTime()+"["+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:ms").format(new Date(session.getLastReadTime()))+"] now:"+System.currentTimeMillis()+"["+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:ms").format(new Date(System.currentTimeMillis()))+"] session:"+session.getRemoteAddress());
logger.info(session.getId()+" -----------------------------------------Out of time, close user session");
logger.info("System.currentTimeMillis():"+format(System.currentTimeMillis()));
logger.info("session.getLastIoTime():"+format(session.getLastIoTime()));
logger.info("session.getLastBothIdleTime():"+format(session.getLastBothIdleTime()));
logger.info("session.getLastReaderIdleTime():"+format(session.getLastReaderIdleTime()));
logger.info("session.getLastReadTime():"+format(session.getLastReadTime()));
logger.info("session.getLastWriterIdleTime():"+format(session.getLastWriterIdleTime()));
logger.info("session.getLastWriteTime():"+format(session.getLastWriteTime()));
session.closeOnFlush();
}
}
示例6: Channel
import org.apache.mina.core.session.IdleStatus; //導入依賴的package包/類
public Channel(final int world, final int channel) {
this.world = world;
this.channel = channel;
this.mapFactory = new MapleMapFactory(MapleDataProviderFactory.getDataProvider(new File(System.getProperty("wzpath") + "/Map.wz")), MapleDataProviderFactory.getDataProvider(new File(System.getProperty("wzpath") + "/String.wz")), world, channel);
try {
eventSM = new EventScriptManager(this, ServerConstants.EVENTS.split(" "));
port = 7575 + this.channel - 1;
port += (world * 100);
ip = ServerConstants.HOST + ":" + port;
IoBuffer.setUseDirectBuffer(false);
IoBuffer.setAllocator(new SimpleBufferAllocator());
acceptor = new NioSocketAcceptor();
TimerManager.getInstance().register(new respawnMaps(), 10000);
acceptor.setHandler(new MapleServerHandler(world, channel));
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 30);
acceptor.getFilterChain().addLast("codec", (IoFilter) new ProtocolCodecFilter(new MapleCodecFactory()));
acceptor.bind(new InetSocketAddress(port));
((SocketSessionConfig) acceptor.getSessionConfig()).setTcpNoDelay(true);
eventSM.init();
System.out.println(" Channel " + getId() + ": Listening on port " + port);
} catch (Exception e) {
e.printStackTrace();
}
}
示例7: main
import org.apache.mina.core.session.IdleStatus; //導入依賴的package包/類
public static void main(String[] args) throws IOException {
// 創建服務器端的監聽器對象
IoAcceptor acceptor = new NioSocketAcceptor();
// 增加日誌過濾器:用於日誌存儲
acceptor.getFilterChain().addLast("logger", new LoggingFilter());
// 增加消息編碼過濾器,采用UTF-8編碼
acceptor.getFilterChain().addLast("codec",
new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
// 設置具體的事物邏輯處理器
acceptor.setHandler(new TimeServerHandler());
// 設置IoSession的一些屬性
acceptor.getSessionConfig().setReadBufferSize(2048);
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
// 設置服務器監聽的端口
acceptor.bind(new InetSocketAddress(PORT));
}
示例8: main
import org.apache.mina.core.session.IdleStatus; //導入依賴的package包/類
public static void main(String[] args) throws IOException {
IoAcceptor acceptor = new NioSocketAcceptor();
// acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, IDELTIMEOUT);
acceptor.getFilterChain().addLast("logger", new LoggingFilter());
acceptor.getFilterChain().addLast("myfliter", new MyFilter());
acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new CommandCodecFactory("UTF-8")));
KeepAliveMessageFactoryImpl kamfi = new KeepAliveMessageFactoryImpl();
KeepAliveFilter kaf = new KeepAliveFilter(kamfi, IdleStatus.BOTH_IDLE);
/** 是否回發 */
kaf.setForwardEvent(true);
acceptor.getFilterChain().addLast("heart", kaf);
acceptor.setHandler(new CalculatorHandler());
acceptor.bind(new InetSocketAddress(PORT));
log.debug("socket通信服務端已啟動,端口是" + PORT);
}
示例9: sessionIdle
import org.apache.mina.core.session.IdleStatus; //導入依賴的package包/類
@Override
public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
com.lts.remoting.Channel channel = new MinaChannel(session);
final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(channel);
if (IdleStatus.BOTH_IDLE == status) {
LOGGER.info("{}: IDLE [{}]", sideType, remoteAddress);
RemotingHelper.closeChannel(channel);
}
if (remoting.getChannelEventListener() != null) {
RemotingEventType remotingEventType = null;
if (IdleStatus.BOTH_IDLE == status) {
remotingEventType = RemotingEventType.ALL_IDLE;
} else if (IdleStatus.READER_IDLE == status) {
remotingEventType = RemotingEventType.READER_IDLE;
} else if (IdleStatus.WRITER_IDLE == status) {
remotingEventType = RemotingEventType.WRITER_IDLE;
}
remoting.putRemotingEvent(new RemotingEvent(remotingEventType,
remoteAddress, channel));
}
}
示例10: init
import org.apache.mina.core.session.IdleStatus; //導入依賴的package包/類
private void init(Config config) {
address = new InetSocketAddress(config.sourcePort);
acceptor = new NioSocketAcceptor();
if (acceptor.getFilterChain().get(LOGGER) == null) {
acceptor.getFilterChain().addLast(LOGGER, new LoggingFilter());
}
if (acceptor.getFilterChain().get(CODEC) == null) {
ProtocolCodecFactory codecFactory = config.codec == null ?
MinaUtil.getTextLineCodecFactory() : (ProtocolCodecFactory) config.codec;
acceptor.getFilterChain().addLast(CODEC, new ProtocolCodecFilter(codecFactory));
}
acceptor.setHandler(new MinaServerHandler());
acceptor.getSessionConfig().setReadBufferSize(config.bufferSize);
acceptor.getSessionConfig().setIdleTime(IdleStatus.WRITER_IDLE, 10);
acceptor.setReuseAddress(true); //避免重啟時提示地址被占用
//設置主服務監聽端口的監聽隊列的最大值為50,如果當前已經有50個連接,新的連接將被服務器拒絕
acceptor.setBacklog(50);
KeepAliveFilter keepAliveFilter = MinaUtil.initServerKeepAlive(config, this);
if (keepAliveFilter != null) {
acceptor.getFilterChain().addLast(HEARTBEAT, keepAliveFilter);
}
}
示例11: init
import org.apache.mina.core.session.IdleStatus; //導入依賴的package包/類
private void init(Config config) {
connector = new NioDatagramConnector();
connector.setHandler(new MinaClientHandler());
DefaultIoFilterChainBuilder chain = connector.getFilterChain();
if (connector.getFilterChain().get(LOGGER) == null) {
chain.addLast(LOGGER, new LoggingFilter());
}
if (connector.getFilterChain().get(CODEC) == null) {
ProtocolCodecFactory codecFactory = config.codec == null ?
MinaUtil.getTextLineCodecFactory() : (ProtocolCodecFactory) config.codec;
chain.addLast(CODEC, new ProtocolCodecFilter(codecFactory));
}
connector.getSessionConfig().setReadBufferSize(config.bufferSize);
connector.getSessionConfig().setIdleTime(IdleStatus.WRITER_IDLE, 10);
connector.getSessionConfig().setBroadcast(true);
KeepAliveFilter keepAliveFilter = MinaUtil.initClientKeepAlive(config, this);
if (keepAliveFilter != null) {
connector.getFilterChain().addLast(HEARTBEAT, keepAliveFilter);
}
}
示例12: init
import org.apache.mina.core.session.IdleStatus; //導入依賴的package包/類
private void init(Config config) {
address = new InetSocketAddress(config.targetIP, config.targetPort);
connector = new NioSocketConnector();
connector.setDefaultRemoteAddress(address);
if (connector.getFilterChain().get(LOGGER) == null) {
connector.getFilterChain().addLast(LOGGER, new LoggingFilter());
}
if (connector.getFilterChain().get(CODEC) == null) {
ProtocolCodecFactory codecFactory = config.codec == null ?
MinaUtil.getTextLineCodecFactory() : (ProtocolCodecFactory) config.codec;
connector.getFilterChain().addLast(CODEC, new ProtocolCodecFilter(codecFactory));
}
connector.setHandler(new MinaClientHandler());
connector.getSessionConfig().setReadBufferSize(config.bufferSize);
connector.getSessionConfig().setIdleTime(IdleStatus.WRITER_IDLE, 10);
KeepAliveFilter keepAliveFilter = MinaUtil.initClientKeepAlive(config, this);
if (keepAliveFilter != null) {
connector.getFilterChain().addLast(HEARTBEAT, keepAliveFilter);
}
}
示例13: init
import org.apache.mina.core.session.IdleStatus; //導入依賴的package包/類
private void init(Config config) {
address = new InetSocketAddress(config.sourcePort);
acceptor = new NioDatagramAcceptor();
acceptor.setHandler(new MinaServerHandler());
DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
if (acceptor.getFilterChain().get(LOGGER) == null) {
chain.addLast(LOGGER, new LoggingFilter());
}
if (acceptor.getFilterChain().get(CODEC) == null) {
ProtocolCodecFactory codecFactory = config.codec == null ?
MinaUtil.getTextLineCodecFactory() : (ProtocolCodecFactory) config.codec;
chain.addLast(CODEC, new ProtocolCodecFilter(codecFactory));
}
DatagramSessionConfig dcfg = acceptor.getSessionConfig();
dcfg.setReuseAddress(true);
dcfg.setReadBufferSize(config.bufferSize);
dcfg.setIdleTime(IdleStatus.WRITER_IDLE, 10);
dcfg.setBroadcast(true);
KeepAliveFilter keepAliveFilter = MinaUtil.initServerKeepAlive(config, this);
if (keepAliveFilter != null) {
acceptor.getFilterChain().addLast(HEARTBEAT, keepAliveFilter);
}
}
示例14: main
import org.apache.mina.core.session.IdleStatus; //導入依賴的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);
}
}
示例15: initialize
import org.apache.mina.core.session.IdleStatus; //導入依賴的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());
}