/** * Creates an instance of this publisher * * @param connection instance of AMQP {@link Connection} */ AMQPPublisher(Connection connection, ComponentLog processLog) { super(connection); this.processLog = processLog; getChannel().addReturnListener(new UndeliverableMessageLogger()); this.connectionString = connection.toString(); }
@Override public void open(Configuration config) throws Exception { ConnectionFactory factory = rmqConnectionConfig.getConnectionFactory(); try { connection = factory.newConnection(); channel = connection.createChannel(); if (channel == null) { throw new RuntimeException("None of RabbitMQ channels are available"); } setupQueue(); if (returnListener != null) { channel.addReturnListener(returnListener); } } catch (IOException e) { throw new RuntimeException("Error while creating the channel", e); } }
Channel chan = connection.createChannel(); pool.add(chan); chan.addReturnListener(new ReturnListener() { @Override public void handleReturn(int i, String s, String s2, String s3, AMQP.BasicProperties basicProperties, byte[] bytes) throws IOException {
/** * Added to the 5.0.x client. * @since 2.0 */ @Override public ReturnListener addReturnListener(ReturnCallback returnCallback) { return this.delegate.addReturnListener(returnCallback); }
/** * Added to the 5.0.x client. * @since 2.0 */ @Override public ReturnListener addReturnListener(ReturnCallback returnCallback) { return this.delegate.addReturnListener(returnCallback); }
@Override public void addReturnListener(ReturnListener listener) { this.delegate.addReturnListener(listener); }
@Override public void addReturnListener(ReturnListener listener) { this.delegate.addReturnListener(listener); }
/** * Add the listener and return the internal map of pending confirmations for that listener. * @param listener the listener. */ @Override public void addListener(Listener listener) { Assert.notNull(listener, "Listener cannot be null"); if (this.listeners.size() == 0) { this.delegate.addConfirmListener(this); this.delegate.addReturnListener(this); } if (this.listeners.putIfAbsent(listener.getUUID(), listener) == null) { this.pendingConfirms.put(listener, new ConcurrentSkipListMap<Long, PendingConfirm>()); if (this.logger.isDebugEnabled()) { this.logger.debug("Added listener " + listener); } } }
private Channel createNewChannel() throws IOException { Preconditions.checkState(this.connection!=null,"No connection available"); final Channel result = this.connection.createChannel(); Preconditions.checkState(result!=null,"No channel available"); result.addReturnListener(new LoggingReturnListener()); return result; }
/** * Add the listener and return the internal map of pending confirmations for that listener. * @param listener the listener. */ @Override public void addListener(Listener listener) { Assert.notNull(listener, "Listener cannot be null"); if (this.listeners.size() == 0) { this.delegate.addConfirmListener(this); this.delegate.addReturnListener(this); } if (this.listeners.putIfAbsent(listener.getUUID(), listener) == null) { this.pendingConfirms.put(listener, new ConcurrentSkipListMap<Long, PendingConfirm>()); if (this.logger.isDebugEnabled()) { this.logger.debug("Added listener " + listener); } } }
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(); } }
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(); } }
private void publishToRabbitMQ(final AMQP.BasicProperties properties, final byte[] body) throws IOException { // remove the OVERRIDE header so it does not propagate String exchangeName = (String) message.removeHeader(RabbitMQConstants.EXCHANGE_OVERRIDE_NAME); // If it is BridgeEndpoint we should ignore the message header of EXCHANGE_OVERRIDE_NAME if (exchangeName == null || endpoint.isBridgeEndpoint()) { exchangeName = endpoint.getExchangeName(); } else { LOG.debug("Overriding header: {} detected sending message to exchange: {}", RabbitMQConstants.EXCHANGE_OVERRIDE_NAME, exchangeName); } Boolean mandatory = camelExchange.getIn().getHeader(RabbitMQConstants.MANDATORY, endpoint.isMandatory(), Boolean.class); Boolean immediate = camelExchange.getIn().getHeader(RabbitMQConstants.IMMEDIATE, endpoint.isImmediate(), Boolean.class); LOG.debug("Sending message to exchange: {} with CorrelationId: {}", exchangeName, properties.getCorrelationId()); if (isPublisherAcknowledgements()) { channel.confirmSelect(); } if (endpoint.isGuaranteedDeliveries()) { basicReturnReceived = false; channel.addReturnListener(guaranteedDeliveryReturnListener); } try { channel.basicPublish(exchangeName, routingKey, mandatory, immediate, properties, body); if (isPublisherAcknowledgements()) { waitForConfirmation(); } } finally { if (endpoint.isGuaranteedDeliveries()) { channel.removeReturnListener(guaranteedDeliveryReturnListener); } } }
@Override public void openConnection() throws IOException { // Already connected? Do nothing this.logger.info( getId() + " is opening a connection to RabbitMQ." ); if( isConnected()) { this.logger.info( getId() + " has already a connection to RabbitMQ." ); return; } // Initialize the connection ConnectionFactory factory = new ConnectionFactory(); RabbitMqUtils.configureFactory( factory, this.configuration ); this.channel = factory.newConnection().createChannel(); this.logger.info( getId() + " established a new connection with RabbitMQ. Channel # " + this.channel.getChannelNumber()); // Be notified when a message does not arrive in a queue (i.e. nobody is listening) this.channel.addReturnListener( new RoboconfReturnListener()); // Add a recoverable listener (when broken connections are recovered). // Given the way the RabbitMQ factory is configured, the channel should be "recoverable". ((Recoverable) this.channel).addRecoveryListener( new RoboconfRecoveryListener()); // Declare the exchanges. RabbitMqUtils.declareGlobalExchanges( this.domain, this.channel ); RabbitMqUtils.declareApplicationExchanges( this.domain, this.applicationName, this.channel ); // Declare the dedicated queue. String queueName = getQueueName(); this.channel.queueDeclare( queueName, true, false, true, null ); // Start listening to messages. RoboconfConsumer consumer = new RoboconfConsumer( getId(), this.channel, this.messageQueue ); consumer.handleConsumeOk( queueName ); this.consumerTag = this.channel.basicConsume( queueName, true, consumer ); this.logger.finer( "A new consumer tag was created: " + this.consumerTag ); }
/** * Migrates the channel's configuration to the given {@code channel}. */ private void migrateConfiguration(Channel channel) throws Exception { channel.setDefaultConsumer(delegate.getDefaultConsumer()); if (flowBlocked) channel.flowBlocked(); if (basicQos != null) basicQos.invoke(channel); if (confirmSelect) channel.confirmSelect(); if (txSelect) channel.txSelect(); synchronized (shutdownListeners) { for (ShutdownListener listener : shutdownListeners) channel.addShutdownListener(listener); } for (ConfirmListener listener : confirmListeners) channel.addConfirmListener(listener); for (FlowListener listener : flowListeners) channel.addFlowListener(listener); for (ReturnListener listener : returnListeners) channel.addReturnListener(listener); }
/** * Migrates the channel's configuration to the given {@code channel}. */ private void migrateConfiguration(Channel channel) throws Exception { channel.setDefaultConsumer(delegate.getDefaultConsumer()); if (flowBlocked) channel.flowBlocked(); if (basicQos != null) basicQos.invoke(channel); if (confirmSelect) channel.confirmSelect(); if (txSelect) channel.txSelect(); synchronized (shutdownListeners) { for (ShutdownListener listener : shutdownListeners) channel.addShutdownListener(listener); } for (ConfirmListener listener : confirmListeners) channel.addConfirmListener(listener); for (FlowListener listener : flowListeners) channel.addFlowListener(listener); for (ReturnListener listener : returnListeners) channel.addReturnListener(listener); }