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()); }
void disconnect() { this.lock.lock(); try { if(!connected()) { return; } closeChannelsQuietly(); closeConnectionQuietly(); } finally { this.lock.unlock(); } }
private void closeChannelsQuietly() { closeQuietly(this.channel); for(final Channel tmpChannel:this.channels.values()) { closeQuietly(tmpChannel); } this.channels.clear(); this.channel=null; }
private void createChannel() throws ControllerException { try { this.channel = createNewChannel(); } catch (final ControllerException e) { closeConnectionQuietly(); throw e; } }
Channel channel() throws ControllerException { this.lock.lock(); try { checkState(connected(),"Not connected"); if(!this.channel.isOpen()) { createChannel(); } return this.channel; } finally { this.lock.unlock(); } }
void connect() throws ControllerException { this.lock.lock(); try { if(connected()) { return; } final ConnectionFactory factory=new ConnectionFactory(); factory.setHost(this.brokerHost); factory.setPort(this.brokerPort); factory.setVirtualHost(this.virtualHost); factory.setThreadFactory(brokerThreadFactory()); factory.setExceptionHandler(new ConnectionManagerExceptionHandler(this)); this.connection = factory.newConnection(); createChannel(); } catch(IOException | TimeoutException e) { final String message = String.format("Could not connect to broker at %s:%s using virtual host %s",this.brokerHost,this.brokerPort,this.virtualHost); throw new ControllerException(this.brokerHost,this.brokerPort,this.virtualHost,message,e); } finally { this.lock.unlock(); } }
void connect() throws ControllerException { this.write.lock(); try { checkState(!this.manager.isConnected(),"Already connected"); this.manager.connect(); declareExchange(); prepareQueue(); } catch(final ControllerException e) { disconnectGracefully(); throw e; } finally { this.write.unlock(); } }
boolean isConnected() { this.lock.lock(); try { return connected(); } finally { this.lock.unlock(); } }
Channel currentChannel() throws ControllerException { final long threadId = Thread.currentThread().getId(); Channel result; synchronized(this.channels) { result=this.channels.get(threadId); if(result==null) { result=createNewChannel(); this.channels.put(threadId,result); } } return result; }
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 Channel createNewChannel() throws ControllerException { this.lock.lock(); try { checkState(connected(),"No connection available"); final Channel result = this.connection.createChannel(); checkNotNull(result,"No channel available"); return result; } catch(final NullPointerException | IOException e) { final String message = String.format( "Could not create channel using connection %08X to broker at %s:%s using virtual host %s", this.connection.hashCode(), this.brokerHost, this.brokerPort, this.virtualHost); throw new ControllerException(this.brokerHost,this.brokerPort,this.virtualHost,message,e); } finally { this.lock.unlock(); } }
/** * 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); } }
void discardChannel() { final long threadId = Thread.currentThread().getId(); synchronized(this.channels) { final Channel aChannel=this.channels.get(threadId); if(aChannel!=null) { closeQuietly(aChannel); this.channels.remove(threadId); } } }
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); } } }