예제 #1
0
  public void stop() {
    for (Manageable manageable : consumers.values()) {
      manageable.stop();
    }

    for (Manageable manageable : producers.values()) {
      manageable.stop();
    }
  }
예제 #2
0
  public void removeChannel(Channel channel) {
    lock.lock();
    try {
      if (channel.getDirection() == Direction.OUT) {
        BrokerHost registeredHost = null;
        for (Map.Entry<BrokerHost, List<Channel>> e : brokerHostToProducerChannelMap.entrySet()) {
          List<Channel> channels = e.getValue();
          Iterator<Channel> channelIterator = channels.iterator();
          while (channelIterator.hasNext()) {
            Channel c = channelIterator.next();
            if (c.equals(channel)) {
              registeredHost = e.getKey();
              channelIterator.remove();

              // if there are no more channels remove the producer
              if (channels.size() == 0) {
                Manageable producer = producers.remove(registeredHost);
                producer.stop();
              }
              break;
            }
          }
          if (registeredHost != null) {
            break;
          }
        }
      } else if (channel.getDirection() == Direction.IN) {
        BrokerHost registeredHost = null;
        for (Map.Entry<BrokerHost, List<Channel>> e : brokerHostToConsumerChannelMap.entrySet()) {
          List<Channel> channels = e.getValue();
          Iterator<Channel> channelIterator = channels.iterator();
          while (channelIterator.hasNext()) {
            Channel c = channelIterator.next();
            if (c.equals(channel)) {
              registeredHost = e.getKey();
              channelIterator.remove();

              // if there are no more channels remove the producer
              if (channels.size() == 0) {
                ConsumingWorker worker = consumingWorkers.remove(registeredHost);
                worker.stop();

                Manageable consumer = consumers.remove(registeredHost);
                consumer.stop();
              }
              break;
            }
          }
          if (registeredHost != null) {
            break;
          }
        }
      }
    } finally {
      lock.unlock();
    }
  }
예제 #3
0
  public BrokerHost addChannel(Channel channel) {
    lock.lock();
    try {
      // add the channel and return the broker host
      Manageable manageable;
      if (channel.getDirection() == Direction.OUT) {
        BrokerHost host = brokerHosts.get(brokerIndex);
        List<Channel> producerChannels = brokerHostToProducerChannelMap.get(host);
        BlockingQueue<MessageContext> channelOutQueue = producerQueues.get(host);

        if (!producers.containsKey(host)) {
          manageable =
              transport.registerProducer(
                  host, prefix, channel.getProperties(), producerQueues.get(host));
          producers.put(host, manageable);

          manageable.start();
        }

        // now register the channel with the brokers map
        // check weather you have a sender consumer for this host
        channel.setOutQueue(channelOutQueue);
        producerChannels.add(channel);
        producerChannels.add(new Channel("", Direction.OUT));

        LOG.info(
            "Registering channel {} with group {} and host {}",
            channel.getName(),
            name,
            host.toString());
        incrementProducerIndex();

        return host;

      } else if (channel.getDirection() == Direction.IN) {
        BrokerHost host = brokerHosts.get(brokerIndex);
        List<Channel> consumerChannels = brokerHostToConsumerChannelMap.get(host);
        if (!consumers.containsKey(host)) {
          BlockingQueue<MessageContext> channelInQueue = consumerQueues.get(host);
          manageable =
              transport.registerConsumer(host, prefix, channel.getProperties(), channelInQueue);
          consumers.put(host, manageable);

          ConsumingWorker worker;
          if (channel.isGrouped()) {
            worker = new ConsumingWorker(consumerChannels, channelInQueue);
          } else {
            worker = new ConsumingWorker(consumerChannels, channelInQueue, true);
          }
          Thread thread = new Thread(worker);
          thread.start();

          manageable.start();

          consumingWorkers.put(host, worker);
        }

        // now register the channel with the brokers map
        // check weather you have a sender consumer for this host
        consumerChannels.add(channel);

        LOG.info(
            "Registering channel {} with group {} and host {}",
            channel.getName(),
            name,
            host.toString());
        incrementConsumerIndex();

        return host;
      }
    } finally {
      lock.unlock();
    }
    return null;
  }