private static List<CollectorConfiguration> toConfiguration(final URI instance, final Notifications notifications) { final ImmutableList.Builder<CollectorConfiguration> builder=ImmutableList.builder(); if(notifications!=null) { final CollectorConfiguration result = new CollectorConfiguration(); result.setInstance(instance.toString()); result.setBrokerHost(notifications.getBrokerHost()); result.setBrokerPort(notifications.getBrokerPort()); result.setVirtualHost(notifications.getVirtualHost()); result.setExchangeName(notifications.getExchangeName()); LOGGER.debug("Using collector configuration {}",result); builder.add(result); } return builder.build(); }
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); }
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(); }
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); } }
/** * Shutdown the notification publisher. Upon shutdown, the user will not be * able to push notifications. */ public void shutdown() { LOGGER.info("Shutting down notification publisher for {}...",this.configuration.getInstance()); this.controller.disconnect(); LOGGER.info("Notification manager {} shutdown",this.configuration.getInstance()); }
void publishEvent(final String event, final String eventType) throws ControllerException { this.write.lock(); try { checkState(this.manager.isConnected(),"Not connected"); final Channel aChannel = this.manager.currentChannel(); aChannel.addReturnListener(new LoggingReturnListener()); publish( aChannel, this.collector.getExchangeName(), Notifications.routingKey(eventType), event); } finally { this.write.unlock(); } }
"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(), e.getMessage()); LOGGER.error(errorMessage,e); throw new ControllerException( this.collector.getBrokerHost(), this.collector.getBrokerPort(), this.collector.getVirtualHost(), errorMessage, e); "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(), e.getMessage()); LOGGER.error(errorMessage,e); throw new ControllerException( this.collector.getBrokerHost(), this.collector.getBrokerPort(), this.collector.getVirtualHost(), errorMessage, e);
private void addCollector(final CollectorConfiguration collector, final String queueName) throws ControllerException { final boolean isNew=!this.brokerCollectors.containsKey(queueName); this.brokerInstances.put(queueName,collector.getInstance()); this.instanceBroker.put(collector.getInstance(),queueName); this.brokerCollectors.put(queueName,collector); if(isNew) { final CollectorController controller=startController(collector,queueName); this.brokerController.put(queueName, controller); this.connectedControllers.add(controller); } }
final CollectorConfiguration amqpConfig = new CollectorConfiguration(); amqpConfig.setInstance(instance); amqpConfig.setBrokerHost(brokerHost); amqpConfig.setBrokerPort(brokerPort); amqpConfig.setVirtualHost(virtualHost); amqpConfig.setExchangeName(exchangeName); final Notifications notifications = new Notifications(); notifications.setBrokerHost(brokerHost);
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); } }
/** * 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); } }
private void verifyCollectorIsNotConfigured(final CollectorConfiguration collector) { if(this.brokerInstances.containsValue(collector.getInstance())) { shutdownGracefully(); throw new IllegalArgumentException("Multiple configurations found for collector "+collector.getInstance()); } }
/** * 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); } } }
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); } } }
/** * Start the notification publisher. Upon this point, the user will be able * to push notifications. * * @throws IOException * if the notification publisher cannot connect to the broker * used by the specified Collector configuration */ public void start() throws IOException { LOGGER.info("Starting notification publisher for {}...",this.configuration.getInstance()); try { this.controller.connect(); LOGGER.info("Notification publisher for {} started",this.configuration.getInstance()); } catch (final ControllerException e) { LOGGER.warn("Could not start publisher using {}. Full stacktrace follows",this.configuration,e); throw new IOException("Could not start publisher using "+this.configuration,e); } }
private CollectorController startController(final CollectorConfiguration collector, final String queueName) throws ControllerException { final CollectorController controller = CollectorController.createNamedReceiver(collector,queueName,this.notificationQueue); LOGGER.info("Connecting controller for collector {}...",collector.getInstance()); try { controller.connect(); return controller; } catch (final ControllerException e) { LOGGER.warn("Could not connect controller for collector {}. Full stacktrace follows",collector.getInstance(),e); shutdownGracefully(); throw e; } }