示例#1
0
  public void start() {
    for (Manageable manageable : consumers.values()) {
      manageable.start();
    }

    for (Manageable manageable : producers.values()) {
      manageable.start();
    }
  }
示例#2
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;
  }