/** Calls {@link IoServiceListener#sessionDestroyed(IoSession)} for all registered listeners. */
  public void fireSessionDestroyed(IoSession session) {
    // Try to remove the remaining empty session set after removal.
    if (managedSessions.remove(Long.valueOf(session.getId())) == null) {
      return;
    }

    // Fire session events.
    session.getFilterChain().fireSessionClosed();

    // Fire listener events.
    try {
      for (IoServiceListener l : listeners) {
        try {
          l.sessionDestroyed(session);
        } catch (Throwable e) {
          ExceptionMonitor.getInstance().exceptionCaught(e);
        }
      }
    } finally {
      // Fire a virtual service deactivation event for the last session of the connector.
      if (session.getService() instanceof IoConnector) {
        boolean lastSession = false;
        synchronized (managedSessions) {
          lastSession = managedSessions.isEmpty();
        }
        if (lastSession) {
          fireServiceDeactivated();
        }
      }
    }
  }
Exemplo n.º 2
0
 /**
  * Title:
  *
  * <p>Desc:初始化客户端信息
  *
  * @param message
  * @throws UnsupportedEncodingException
  */
 public TongbuClient(Object message, String url, int port) {
   chain.addLast("logging", new LoggingFilter()); // 添加日志过滤器
   chain.addLast("myChin", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
   if (message instanceof String) {
     connector.setHandler(new ClientHandler((String) message));
   } else if (message instanceof CommEntity) {
     connector.setHandler(new ClientHandler((CommEntity) message));
   }
   connector.setConnectTimeoutMillis(10000);
   SocketSessionConfig cfg = connector.getSessionConfig();
   cfg.setUseReadOperation(true);
   IoSession session =
       connector.connect(new InetSocketAddress(url, port)).awaitUninterruptibly().getSession();
   ReadFuture readFuture = session.read();
   readFuture.awaitUninterruptibly();
   this.info = (CommResultInfo) readFuture.getMessage();
   session.close(true);
   session.getService().dispose();
 }
  /** Calls {@link IoServiceListener#sessionCreated(IoSession)} for all registered listeners. */
  public void fireSessionCreated(IoSession session) {
    boolean firstSession = false;
    if (session.getService() instanceof IoConnector) {
      synchronized (managedSessions) {
        firstSession = managedSessions.isEmpty();
      }
    }

    // If already registered, ignore.
    if (managedSessions.putIfAbsent(Long.valueOf(session.getId()), session) != null) {
      return;
    }

    // If the first connector session, fire a virtual service activation event.
    if (firstSession) {
      fireServiceActivated();
    }

    // Fire session events.
    session.getFilterChain().fireSessionCreated();
    session.getFilterChain().fireSessionOpened();

    int managedSessionCount = managedSessions.size();
    if (managedSessionCount > largestManagedSessionCount) {
      largestManagedSessionCount = managedSessionCount;
    }
    cumulativeManagedSessionCount++;

    // Fire listener events.
    for (IoServiceListener l : listeners) {
      try {
        l.sessionCreated(session);
      } catch (Throwable e) {
        ExceptionMonitor.getInstance().exceptionCaught(e);
      }
    }
  }