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(); } }
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); } }
e.getMessage()); LOGGER.error(errorMessage,e); throw new ControllerException( this.collector.getBrokerHost(), this.collector.getBrokerPort(), e.getMessage()); LOGGER.error(errorMessage,e); throw new ControllerException( this.collector.getBrokerHost(), this.collector.getBrokerPort(),
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); } }
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(); } }
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); } } }