boolean isConnected() { this.lock.lock(); try { return connected(); } finally { this.lock.unlock(); } }
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 disconnect() { this.lock.lock(); try { if(!connected()) { return; } closeChannelsQuietly(); closeConnectionQuietly(); } finally { this.lock.unlock(); } }
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 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(); } }