private CollectorController(final CollectorConfiguration collector, final String queueName, final BlockingQueue<SuspendedNotification> notificationQueue) { checkNotNull(collector,"Collector cannot be null"); this.collector=collector; this.manager=new ConnectionManager(collector.getInstance(),collector.getBrokerHost(),collector.getBrokerPort(),collector.getVirtualHost()); this.queueName=queueName; this.notificationQueue=notificationQueue; final ReadWriteLock lock=new ReentrantReadWriteLock(); this.write=lock.writeLock(); this.cleaners=Lists.newLinkedList(); this.callbacks=Lists.newArrayList(); }
private String queueName(final CollectorConfiguration collector) { final Integer hash = Objects.hash( collector.getBrokerHost(), collector.getBrokerPort(), collector.getVirtualHost(), collector.getExchangeName()); return String.format("%s.collector.hash%08X",this.name,hash); }
void publishEvent(final Event event) throws ControllerException { try { publishEvent(EventUtil.marshall(event), event.getClass().getSimpleName()); } catch (final IOException e) { throw new ControllerException( this.collector.getBrokerHost(), this.collector.getBrokerPort(), this.collector.getVirtualHost(), "Could not serialize event "+event, e); } }
/** * The declared queues are durable, exclusive, and auto-delete, and expire if no * client uses them after 1 second. */ private String declareQueue() throws ControllerException { final String targetQueueName=Optional.fromNullable(this.queueName).or(""); try { final Map<String, Object> args= ImmutableMap. <String, Object>builder(). put("x-expires",1000). build(); final DeclareOk ok = this.manager.channel().queueDeclare(targetQueueName,true,true,true,args); final String declaredQueueName = ok.getQueue(); this.cleaners.push(CleanerFactory.queueDelete(declaredQueueName)); return declaredQueueName; } catch (final IOException e) { throw new ControllerException(this.collector.getBrokerHost(),this.collector.getBrokerPort(),this.collector.getVirtualHost(),"Could not create queue named '"+targetQueueName+"'",e); } }
"Could not publish message [%s] to exchange '%s' and routing key '%s' using broker %s:%s%s: %s", payload,exchangeName,routingKey, this.collector.getBrokerHost(), this.collector.getBrokerPort(), this.collector.getVirtualHost(), LOGGER.error(errorMessage,e); throw new ControllerException( this.collector.getBrokerHost(), this.collector.getBrokerPort(), this.collector.getVirtualHost(), "Unexpected failure while publishing message [%s] to exchange '%s' and routing key '%s' using broker %s:%s%s: %s", payload,exchangeName,routingKey, this.collector.getBrokerHost(), this.collector.getBrokerPort(), this.collector.getVirtualHost(), LOGGER.error(errorMessage,e); throw new ControllerException( this.collector.getBrokerHost(), this.collector.getBrokerPort(), this.collector.getVirtualHost(),
private void bindQueue(final String queueName) throws ControllerException { try { this.manager.channel().queueBind(queueName,this.collector.getExchangeName(),Notifications.ROUTING_KEY_PATTERN); this.cleaners.push(CleanerFactory.queueUnbind(this.collector.getExchangeName(),queueName,Notifications.ROUTING_KEY_PATTERN)); } catch (final IOException e) { throw new ControllerException(this.collector.getBrokerHost(),this.collector.getBrokerPort(),this.collector.getVirtualHost(),"Could not bind queue '"+queueName+"' to exchange '"+this.collector.getExchangeName()+"' using routing key '"+Notifications.ROUTING_KEY_PATTERN+"'",e); } }
private void prepareQueue() throws ControllerException { if(this.notificationQueue!=null) { this.actualQueueName = declareQueue(); bindQueue(this.actualQueueName); try { final Channel currentChannel = this.manager.channel(); final NotificationConsumer callback = new NotificationConsumer(currentChannel,this.notificationQueue); currentChannel. basicConsume( this.actualQueueName, false, callback ); this.callbacks.add(callback); } catch (final IOException e) { throw new ControllerException(this.collector.getBrokerHost(),this.collector.getBrokerPort(),this.collector.getVirtualHost(),"Could not register consumer for queue '"+this.actualQueueName+"'",e); } } }
/** * The a durable topic exchange is always declared */ private void declareExchange() throws ControllerException { try { this.manager.channel().exchangeDeclare(this.collector.getExchangeName(),EXCHANGE_TYPE,true); } catch (final IOException e) { if(!FailureAnalyzer.isExchangeDeclarationRecoverable(e)) { throw new ControllerException(this.collector.getBrokerHost(),this.collector.getBrokerPort(),this.collector.getVirtualHost(),"Could not create exchange named '"+this.collector.getExchangeName()+"'",e); } } }