com.rabbitmq.client.Recoverable Java Examples

The following examples show how to use com.rabbitmq.client.Recoverable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RabbitMQConsumer.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * Register a consumer to the queue
 *
 * @throws IOException
 */
private void initConsumer() throws IOException, RabbitMQException {
    if (connection == null) {
        connection = rabbitMQConnectionFactory.createConnection();
    }

    channel = connection.createChannel();
    ((Recoverable) this.channel).addRecoveryListener(new RabbitMQRecoveryListener());

    // set the qos value
    int qos = NumberUtils.toInt(rabbitMQProperties.get(RabbitMQConstants.CONSUMER_QOS),
            RabbitMQConstants.DEFAULT_CONSUMER_QOS);
    channel.basicQos(qos);

    // declaring queue, exchange and binding
    queueName = rabbitMQProperties.get(RabbitMQConstants.QUEUE_NAME);
    String exchangeName = rabbitMQProperties.get(RabbitMQConstants.EXCHANGE_NAME);
    RabbitMQUtils.declareQueuesExchangesAndBindings(channel, queueName, exchangeName, rabbitMQProperties);

    // get max dead-lettered count
    maxDeadLetteredCount =
            NumberUtils.toLong(rabbitMQProperties.get(RabbitMQConstants.MESSAGE_MAX_DEAD_LETTERED_COUNT));

    // get consumer tag if given
    String consumerTag = rabbitMQProperties.get(RabbitMQConstants.CONSUMER_TAG);

    autoAck = BooleanUtils.toBooleanDefaultIfNull(BooleanUtils.toBooleanObject(rabbitMQProperties
            .get(RabbitMQConstants.QUEUE_AUTO_ACK)), true);

    if (StringUtils.isNotEmpty(consumerTag)) {
        channel.basicConsume(queueName, autoAck, consumerTag, this);
    } else {
        channel.basicConsume(queueName, autoAck, this);
    }
}
 
Example #2
Source File: RabbitMQRecoveryListener.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRecovery(Recoverable recoverable) {
    if (recoverable instanceof Connection) {
        String connectionId = ((Connection) recoverable).getId();
        log.info("Connection with id " + connectionId + " was recovered.");
    }

    if (recoverable instanceof Channel) {
        int channelNumber = ((Channel) recoverable).getChannelNumber();
        log.info("Connection to channel number " + channelNumber + " was recovered.");
    }
}
 
Example #3
Source File: RabbitMQRecoveryListener.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRecoveryStarted(Recoverable recoverable) {
    if (recoverable instanceof Connection) {
        String connectionId = ((Connection) recoverable).getId();
        log.info("Connection with id " + connectionId + " started to recover.");
    }

    if (recoverable instanceof Channel) {
        int channelNumber = ((Channel) recoverable).getChannelNumber();
        log.info("Connection to channel number " + channelNumber + " started to recover.");
    }
}
 
Example #4
Source File: ConsumerHolder.java    From rabbitmq-cdi with MIT License 5 votes vote down vote up
@Override
public void handleRecovery(Recoverable recoverable) {
  LOGGER.debug("Handle recovery");
  if (recoverable != null && recoverable.equals(channel)) {
    recoverRunning = false;
    synchronized (pendingAckActions) {
      pendingAckActions.removeIf(this::invokePendingAckAction);
    }
  }
}
 
Example #5
Source File: ConsumerHolder.java    From rabbitmq-cdi with MIT License 5 votes vote down vote up
@Override
public void handleRecoveryStarted(Recoverable recoverable) {
  LOGGER.debug("Handle recovery started");
  if (recoverable != null && recoverable.equals(channel)) {
    recoverRunning = true;
  }
}
 
Example #6
Source File: RabbitMqClient.java    From roboconf-platform with Apache License 2.0 5 votes vote down vote up
@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 );
}
 
Example #7
Source File: RoboconfRecoveryListener.java    From roboconf-platform with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRecovery( Recoverable recoverable ) {

	if( recoverable instanceof Channel ) {
		int channelNumber = ((Channel) recoverable).getChannelNumber();
		this.logger.fine( "Connection to channel #" + channelNumber + " was recovered." );
	}
}
 
Example #8
Source File: RoboconfRecoveryListenerTest.java    From roboconf-platform with Apache License 2.0 5 votes vote down vote up
@Test
public void testHandleRecovery() {

	// Channel being recovered
	RoboconfRecoveryListener listener = new RoboconfRecoveryListener();
	AutorecoveringChannel ch = Mockito.mock( AutorecoveringChannel.class );

	listener.handleRecovery( ch );
	Mockito.verify( ch, Mockito.only()).getChannelNumber();

	// Not a channel (e.g. a connection)
	Recoverable recoverable = Mockito.mock( Recoverable.class );
	listener.handleRecovery( recoverable );
	Mockito.verifyZeroInteractions( recoverable );
}