Exemplo n.º 1
0
  public Channel removeChannel(String channelId, boolean timeout) {
    if (channelId == null) return null;

    // Remove existing channel id/subscriptions in distributed data (clustering).
    try {
      DistributedData gdd = graniteConfig.getDistributedDataFactory().getInstance();
      if (gdd != null) {
        log.debug("Removing channel id from distributed data: %s", channelId);
        gdd.removeChannelId(channelId);
      }
    } catch (Exception e) {
      log.error(e, "Could not remove channel id from distributed data: %s", channelId);
    }

    TimeChannel<?> timeChannel = channels.get(channelId);
    Channel channel = null;
    if (timeChannel != null) {
      try {
        if (timeChannel.getTimerTask() != null) timeChannel.getTimerTask().cancel();
      } catch (Exception e) {
        // Should never happen...
      }

      channel = timeChannel.getChannel();

      try {
        for (Subscription subscription : channel.getSubscriptions()) {
          try {
            Message message = subscription.getUnsubscribeMessage();
            handleMessage(channel.getFactory(), message, true);
          } catch (Exception e) {
            log.error(
                e,
                "Error while unsubscribing channel: %s from subscription: %s",
                channel,
                subscription);
          }
        }
      } finally {
        channels.remove(channelId);
        channel.destroy(timeout);
      }
    }
    return channel;
  }
Exemplo n.º 2
0
  public boolean access(String channelId) {
    if (channelId != null) {
      TimeChannel<?> timeChannel = channels.get(channelId);
      if (timeChannel != null) {
        synchronized (timeChannel) {
          TimerTask timerTask = timeChannel.getTimerTask();
          if (timerTask != null) {
            log.debug("Canceling TimerTask: %s", timerTask);
            timerTask.cancel();
            timeChannel.setTimerTask(null);
          }

          timerTask = new ChannelTimerTask(this, channelId);
          timeChannel.setTimerTask(timerTask);

          long timeout = gravityConfig.getChannelIdleTimeoutMillis();
          log.debug("Scheduling TimerTask: %s for %s ms.", timerTask, timeout);
          channelsTimer.schedule(timerTask, timeout);
          return true;
        }
      }
    }
    return false;
  }