Example usage for com.rabbitmq.client Channel confirmSelect

List of usage examples for com.rabbitmq.client Channel confirmSelect

Introduction

In this page you can find the example usage for com.rabbitmq.client Channel confirmSelect.

Prototype

Confirm.SelectOk confirmSelect() throws IOException;

Source Link

Document

Enables publisher acknowledgements on this channel.

Usage

From source file:acromusashi.stream.component.rabbitmq.CachingConnectionFactory.java

License:Apache License

private synchronized Channel createBareChannel(boolean transactional) {
    // if attempt to fail back, close the caching connection ant channels
    // in this time, others thread may throw exception for the connection loss.
    boolean hasCachedConnection = this.connection != null;
    boolean shouldFailback = shouldFailback();
    if (hasCachedConnection && shouldFailback) {
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Closing cached Rabbit Connection to failback");
        }/*from  w  w  w.  ja  v a 2 s. com*/
        this.connection.destroy();
    }

    if (hasCachedConnection == false || this.connection.isOpen() == false) {
        // Use createConnection here not doCreateConnection so that the old one is properly disposed
        createConnection();
    }
    Channel channel = this.connection.createBareChannel(transactional);
    if (this.publisherConfirms) {
        try {
            channel.confirmSelect();
        } catch (IOException e) {
            this.logger.error("Could not configure the channel to receive publisher confirms", e);
        }
    }
    if (this.publisherConfirms || this.publisherReturns) {
        if (!(channel instanceof PublisherCallbackChannelImpl)) {
            channel = new PublisherCallbackChannelImpl(channel);
        }
    }
    return channel;
}

From source file:com.avricot.avrilog.rabbitmq.HaConnectionFactory.java

License:Apache License

/**
 * Create a new {@link Channel} from the current connection.
 *
 * @param confirm true if the channel is selected as confirmed (so that we can
 *                wait brocker ack)/*from w w w. ja va 2 s  .c  o m*/
 */
public Channel createChannel(final boolean confirm) throws RabbitMQException {
    if (reconnectionInProgress.get()) {
        throw new RabbitMQReconnectionException();
    }
    try {
        Channel channel = connection.createChannel();
        if (channel == null) {
            throw new RabbitMQException(
                    "Can't create the channel (get a null channel from the connection). Your application is probably requesting too many channel. Try using/not using localthread.");
        }
        if (confirm) {
            channel.confirmSelect();
        }
        return channel;
    } catch (IOException e) {
        throw new RabbitMQException("Can't create the channel.", e);
    }
}

From source file:info.pancancer.arch3.utils.Utilities.java

License:Open Source License

/**
 * Clears database state and known queues for testing.
 *
 * @param settings//  w w  w  .  jav a 2s .c o m
 * @throws IOException
 * @throws java.util.concurrent.TimeoutException
 */
public static void clearState(HierarchicalINIConfiguration settings) throws IOException, TimeoutException {
    File configFile = FileUtils.getFile("src", "test", "resources", "config");
    HierarchicalINIConfiguration parseConfig = Utilities.parseConfig(configFile.getAbsolutePath());
    PostgreSQL postgres = new PostgreSQL(parseConfig);
    // clean up the database
    postgres.clearDatabase();

    String server = settings.getString(Constants.RABBIT_HOST);
    String user = settings.getString(Constants.RABBIT_USERNAME);
    String pass = settings.getString(Constants.RABBIT_PASSWORD);

    Channel channel;

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(server);
    factory.setUsername(user);
    factory.setPassword(pass);
    factory.setAutomaticRecoveryEnabled(true);
    Connection connection = factory.newConnection();
    channel = connection.createChannel();
    channel.basicQos(1);
    channel.confirmSelect();

    String prefix = settings.getString(Constants.RABBIT_QUEUE_NAME);
    String[] queues = { prefix + "_jobs", prefix + "_orders", prefix + "_vms", prefix + "_for_CleanupJobs",
            prefix + "_for_CleanupVMs" };
    for (String queue : queues) {
        try {
            channel.queueDelete(queue);
        } catch (IOException e) {
            Log.info("Could not delete " + queue);
        }
    }
}

From source file:info.pancancer.arch3.utils.Utilities.java

License:Open Source License

public static Channel setupQueue(HierarchicalINIConfiguration settings, String queue)
        throws IOException, TimeoutException {

    String server = settings.getString(Constants.RABBIT_HOST);
    String user = settings.getString(Constants.RABBIT_USERNAME);
    String pass = settings.getString(Constants.RABBIT_PASSWORD);

    Channel channel = null;

    try {//from w  ww  .j  a va  2s. c o  m

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(server);
        factory.setUsername(user);
        factory.setPassword(pass);
        factory.setAutomaticRecoveryEnabled(true);
        Connection connection = factory.newConnection();
        channel = connection.createChannel();
        channel.basicQos(1);
        channel.queueDeclare(queue, true, false, false, null);
        channel.confirmSelect();
        // channel.queueDeclarePassive(queue);

    } catch (IOException | TimeoutException ex) {
        // Logger.getLogger(Master.class.getName()).log(Level.SEVERE, null, ex);
        LOG.error("Error setting up queue connections to queue:" + queue + " on host: " + server
                + "; error is: " + ex.getMessage(), ex);
    }
    return channel;

}

From source file:info.pancancer.arch3.utils.Utilities.java

License:Open Source License

public static Channel setupExchange(HierarchicalINIConfiguration settings, String queue)
        throws IOException, TimeoutException {

    String server = settings.getString(Constants.RABBIT_HOST);
    String user = settings.getString(Constants.RABBIT_USERNAME);
    String pass = settings.getString(Constants.RABBIT_PASSWORD);

    Channel channel = null;

    try {/*from  w w  w.j  a  va 2  s  .c o  m*/

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(server);
        factory.setUsername(user);
        factory.setPassword(pass);
        factory.setAutomaticRecoveryEnabled(true);
        Connection connection = factory.newConnection();
        channel = connection.createChannel();
        channel.exchangeDeclare(queue, "fanout", true, false, null);
        channel.confirmSelect();

    } catch (IOException | TimeoutException ex) {
        // Logger.getLogger(Master.class.getName()).log(Level.SEVERE, null, ex);
        LOG.error("Error setting up queue connections: " + ex.getMessage(), ex);
        throw ex;
    }
    return channel;
}

From source file:ms.dew.core.cluster.spi.rabbit.RabbitClusterMQ.java

License:Apache License

/**
 * MQ ??  ?.//from www  .  j  a  v a  2s .  c  om
 * <p>
 * exchange = fanout
 * ??? topic ?
 *
 * @param topic   
 * @param message ?
 * @param confirm ??
 * @return ???rabbit confirm ?????
 */
public boolean doPublish(String topic, String message, boolean confirm) {
    Connection connection = rabbitAdapter.getConnection();
    Channel channel = connection.createChannel(false);
    Object funResult = null;
    try {
        if (confirm) {
            channel.confirmSelect();
        }
        channel.exchangeDeclare(topic, BuiltinExchangeType.FANOUT, true);
        AMQP.BasicProperties properties = new AMQP.BasicProperties("text/plain", null, getMQHeader(topic), 2, 0,
                null, null, null, null, null, null, null, null, null);
        funResult = sendBeforeFun.invoke(topic, "", properties);
        channel.basicPublish(topic, "", properties, message.getBytes());
        if (confirm) {
            try {
                return channel.waitForConfirms();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                logger.error("[MQ] Rabbit publish error.", e);
                sendErrorFun.invoke(e, funResult);
                return false;
            }
        } else {
            return true;
        }
    } catch (IOException e) {
        logger.error("[MQ] Rabbit publish error.", e);
        sendErrorFun.invoke(e, funResult);
        return false;
    } finally {
        try {
            channel.close();
            sendFinishFun.invoke(funResult);
        } catch (IOException | TimeoutException e) {
            logger.error("[MQ] Rabbit publish error.", e);
        }
        connection.close();
    }
}

From source file:ms.dew.core.cluster.spi.rabbit.RabbitClusterMQ.java

License:Apache License

/**
 * MQ ??  ./*from   w  w  w .  j a  v  a 2s  .c  o m*/
 * <p>
 * exchange = fanout
 *
 * @param address ?
 * @param message ?
 * @param confirm ??
 * @return ??rabbit confirm ?????
 */
public boolean doRequest(String address, String message, boolean confirm) {
    Connection connection = rabbitAdapter.getConnection();
    Channel channel = connection.createChannel(false);
    Object funResult = null;
    try {
        if (confirm) {
            channel.confirmSelect();
        }
        channel.queueDeclare(address, true, false, false, null);
        AMQP.BasicProperties properties = new AMQP.BasicProperties("text/plain", null, getMQHeader(address), 2,
                0, null, null, null, null, null, null, null, null, null);
        funResult = sendBeforeFun.invoke("", address, properties);
        channel.basicPublish("", address, properties, message.getBytes());
        if (confirm) {
            try {
                return channel.waitForConfirms();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                logger.error("[MQ] Rabbit request error.", e);
                sendErrorFun.invoke(e, funResult);
                return false;
            }
        } else {
            return true;
        }
    } catch (IOException e) {
        logger.error("[MQ] Rabbit request error.", e);
        sendErrorFun.invoke(e, funResult);
        return false;
    } finally {
        try {
            channel.close();
            sendFinishFun.invoke(funResult);
        } catch (IOException | TimeoutException e) {
            logger.error("[MQ] Rabbit request error.", e);
        }
        connection.close();
    }
}

From source file:ms.dew.core.cluster.spi.rabbit.RabbitClusterMQ.java

License:Apache License

/**
 * MQ ??  ?.//from w w  w. j a va  2 s .c o  m
 * <p>
 * exchange = topic
 *
 * @param topic      
 * @param routingKey Key
 * @param queueName  ??
 * @param message    ?
 * @param confirm    ??
 * @return ???rabbit confirm ?????
 */
public boolean publishWithTopic(String topic, String routingKey, String queueName, String message,
        boolean confirm) {
    logger.trace("[MQ] publishWithTopic {}:{}", topic, message);
    Connection connection = rabbitAdapter.getConnection();
    Channel channel = connection.createChannel(false);
    Object funResult = null;
    try {
        if (confirm) {
            channel.confirmSelect();
        }
        channel.queueDeclare(queueName, true, false, false, null);
        channel.exchangeDeclare(topic, BuiltinExchangeType.TOPIC, true);
        AMQP.BasicProperties properties = new AMQP.BasicProperties("text/plain", null, getMQHeader(topic), 2, 0,
                null, null, null, null, null, null, null, null, null);
        funResult = sendBeforeFun.invoke(topic, routingKey, properties);
        channel.basicPublish(topic, routingKey, properties, message.getBytes());
        if (confirm) {
            try {
                return channel.waitForConfirms();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                logger.error("[MQ] Rabbit publishWithTopic error.", e);
                sendErrorFun.invoke(e, funResult);
                return false;
            }
        } else {
            return true;
        }
    } catch (IOException e) {
        logger.error("[MQ] Rabbit publishWithTopic error.", e);
        sendErrorFun.invoke(e, funResult);
        return false;
    } finally {
        try {
            channel.close();
            sendFinishFun.invoke(funResult);
        } catch (IOException | TimeoutException e) {
            logger.error("[MQ] Rabbit publishWithTopic error.", e);
        }
        connection.close();
    }
}

From source file:net.lshift.accent.AccentConfirmPublisher.java

License:Apache License

@Override
public void channelCreated(Channel c) throws IOException {
    // Enable publisher confirmations on channel
    c.confirmSelect();

    // Add listeners to allow us to process publisher acks
    c.addConfirmListener(feedbackListener);
}

From source file:org.axonframework.amqp.eventhandling.RabbitMQBenchmark.java

License:Apache License

public static void main(String[] args) throws IOException, InterruptedException, TimeoutException {
    final Connection connection = new ConnectionFactory().newConnection();
    final Channel channel = connection.createChannel();
    String queueName = channel.queueDeclare().getQueue();
    execute("Transactional and Channel pooling", createChannelPoolSharingThreads(connection, queueName));
    queueName = refreshQueue(channel, queueName);
    execute("Transactional, new Channel per tx", createChannelCreatingThreads(connection, queueName, true));
    queueName = refreshQueue(channel, queueName);
    execute("Non-transactional, new Channel per tx",
            createChannelCreatingThreads(connection, queueName, false));
    queueName = refreshQueue(channel, queueName);
    execute("Non-transactional, single Channel", createChannelSharingThreads(connection, queueName));
    channel.confirmSelect();
    connection.close();//from  ww  w .jav  a2  s.c o  m
}