private void cleanUp() { LOGGER.debug("Cleaning up broker ({})...",this.cleaners.size()); while(!this.cleaners.isEmpty()) { final Cleaner cleaner=this.cleaners.pop(); try { cleaner.clean(this.manager.channel()); LOGGER.trace("{} completed",cleaner); } catch (final Exception e) { LOGGER.warn("{} failed. Full stacktrace follows",cleaner,e); } } LOGGER.debug("Broker clean-up completed.",this.cleaners.size()); }
/** * 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 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); } } }