@Override public Manageable registerConsumer( BrokerHost host, String prefix, Map channelConf, BlockingQueue<MessageContext> queue) { LOG.info("Registering consumer to host {}", host); String exchangeName = (String) channelConf.get(EXCHANGE_NAME_PROPERTY); String routingKey = (String) channelConf.get(ROUTING_KEY_PROPERTY); String queueName = (String) channelConf.get(QUEUE_NAME_PROPERTY); RabbitMQReceiver listener = new RabbitMQReceiver(queue, prefix + "." + queueName, host.getUrl()); if (executorService != null) { listener.setExecutorService(executorService); } listener.setExchangeName(exchangeName); listener.setRoutingKey(prefix + "." + routingKey); return listener; }
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; }