private int availableInternal() throws IOException { return channel.queueDeclarePassive(this.queueName).getMessageCount(); } }
public static boolean isQueueAvailable(Channel channel, String queueName) throws IOException { try { // check availability of the named queue // if an error is encountered, including if the queue does not exist and if the // queue is exclusively owned by another connection channel.queueDeclarePassive(queueName); return true; } catch (IOException e) { return false; } }
@Override public com.rabbitmq.client.AMQP.Queue.DeclareOk queueDeclarePassive( String queue) throws IOException { return this.delegate.queueDeclarePassive(queue); }
@Override public com.rabbitmq.client.AMQP.Queue.DeclareOk queueDeclarePassive( String queue) throws IOException { return this.delegate.queueDeclarePassive(queue); }
@Override public AMQP.Queue.DeclareOk queueDeclarePassive(String queue) throws IOException { return delegate.queueDeclarePassive(queue); }
/** * for other function, it borrow a outer channel * * @param queueName * @param outerChannel * @return */ private boolean innerExists(String queueName, Channel outerChannel) { boolean result = true; try { outerChannel.queueDeclarePassive(queueName); } catch (IOException e) { result = false; } return result; }
private static int getNumberOfMessages(Channel channel, String destQueueName) { try { // It turns out that the messages take an indeterminate time to get to their queues, // so this passive declare may not give a result that agrees with our expectations // (even though it is *accurate* in some rabbitmq-server defined sense). // Heuristics, like issuing the passive declare twice in a row, with or without a pause, // do not always work---there appear to be some erratic delays occasionally---and so // the decision has been taken to *not* try to circumvent this. There is, after all, // nothing in the JMS spec that makes any guarantees about what a QueueBrowser will see. // Our integration tests have to be less dogmatic, therefore. // In the code below, there are commented out sections used in test to explore the behaviour. // int mc1 = channel.queueDeclarePassive(destQueueName).getMessageCount(); // Thread.sleep(100); int mc2 = channel.queueDeclarePassive(destQueueName).getMessageCount(); // if (mc1!=mc2) System.out.println(String.format("q='%s', msgcount=%s/%s", destQueueName, mc1, mc2)); // System.out.println(String.format("q='%s', msgcount=%s", destQueueName, mc2)); return mc2; } catch (Exception e) { // ignore errors---we assume no messages in the queue in this case. } return 0; // default drop-through value }
private boolean queueExists(String queueName) throws IOException { boolean result = true; try { channel.queueDeclarePassive(queueName); } catch (IOException e) { result = false; if (!channel.isOpen()) { super.init(); } } return result; }
public boolean exists(String queueName) throws IOException { super.init(); boolean result = true; try { this.channel.queueDeclarePassive(queueName); } catch (IOException e) { result = false; } super.close(); return result; }
public int getMessageCount(final String queue) throws IOException { Connection connection = connectionFactory.createConnection(); final Channel channel = connection.createChannel(false); final AMQP.Queue.DeclareOk declareOk = channel.queueDeclarePassive(queue); return declareOk.getMessageCount(); } }
private void declareQueueIfMissing(Connection connection) throws IOException, TimeoutException { Channel channel = connection.createChannel(); try { // check if queue already exists channel.queueDeclarePassive(builder.queue); channel.close(); } catch (IOException maybeQueueDoesNotExist) { if (maybeQueueDoesNotExist.getCause() != null && maybeQueueDoesNotExist.getCause().getMessage().contains("NOT_FOUND")) { channel = connection.createChannel(); channel.queueDeclare(builder.queue, true, false, false, null); channel.close(); } else { throw maybeQueueDoesNotExist; } } } }
/** * Checks if all preconditions are fulfilled on the broker to * successfully register a consumer there. One important precondition * is the existence of the queue the consumer shall consume from. * * @param consumerHolders The consumer holders * @throws IOException if the precondition check fails */ protected void checkPreconditions(List<ConsumerHolder> consumerHolders) throws IOException { Channel channel = createChannel(); for (ConsumerHolder consumerHolder : consumerHolders) { String queue = consumerHolder.getConfiguration().getQueueName(); try { channel.queueDeclarePassive(queue); LOGGER.debug("Queue {} found on broker", queue); } catch (IOException e) { LOGGER.error("Queue {} not found on broker", queue); throw e; } } channel.close(); }
private void ensureQueuesExist(final Channel channel) { final Collection<String> missingQueues = new ArrayList<>(); getSourcesOrEmptySet().forEach(consumer -> consumer.getAddresses().forEach(address -> { try { channel.queueDeclarePassive(address); } catch (final IOException e) { missingQueues.add(address); log.warning("The queue <{}> does not exist.", address); } }) ); if (!missingQueues.isEmpty()) { log.warning("Stopping RMQ client actor for connection <{}> as queues to connect to are missing: <{}>", connectionId(), missingQueues); throw ConnectionFailedException.newBuilder(connectionId()) .description("The queues " + missingQueues + " to connect to are missing.") .build(); } }
private void ensureQueuesExist(final Channel channel) { final Collection<String> missingQueues = new ArrayList<>(); getSourcesOrEmptySet().forEach(consumer -> consumer.getAddresses().forEach(address -> { try { channel.queueDeclarePassive(address); } catch (final IOException e) { missingQueues.add(address); log.warning("The queue <{}> does not exist.", address); } }) ); if (!missingQueues.isEmpty()) { log.warning("Stopping RMQ client actor for connection <{}> as queues to connect to are missing: <{}>", connectionId(), missingQueues); throw ConnectionFailedException.newBuilder(connectionId()) .description("The queues " + missingQueues + " to connect to are missing.") .build(); } }
static void declareTestQueue(RabbitTemplate template, final String routingKey) { // declare and bind queue template.execute(channel -> { Queue.DeclareOk res = channel.queueDeclarePassive(TestConstants.QUEUE_NAME); String queueName = res.getQueue(); System .out .println("Queue Name = " + queueName); channel.queueBind(queueName, TestConstants.EXCHANGE_NAME, routingKey); return queueName; }); }
private void declareAndBindQueue(final Channel channel, final String queue, final String exchange, final String routingKey, final Map<String, Object> queueArgs, final Map<String, Object> bindingArgs) throws IOException { if (endpoint.isPassive()) { channel.queueDeclarePassive(queue); } else { channel.queueDeclare(queue, endpoint.isDurable(), endpoint.isExclusive(), endpoint.isAutoDelete(), queueArgs); } if (shouldBindQueue()) { channel.queueBind(queue, exchange, emptyIfNull(routingKey), bindingArgs); } }
/** * Verify that a queue exists using the native Rabbit API to bypass all the connection and * channel caching and callbacks in Spring AMQP. * * @param queue The queue to verify * @return True if the queue exists */ private boolean queueExists(final Queue queue) throws Exception { ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost("localhost"); connectionFactory.setPort(BrokerTestUtils.getPort()); Connection connection = connectionFactory.newConnection(); Channel channel = connection.createChannel(); try { DeclareOk result = channel.queueDeclarePassive(queue.getName()); return result != null; } catch (IOException e) { return e.getCause().getMessage().contains("RESOURCE_LOCKED"); } finally { connection.close(); } }
@Test public void test(ConnectionFactory connectionFactory) throws Exception { Connection conn = connectionFactory.newConnection(); Channel channel = conn.createChannel(); DeclareOk declareOk = channel.queueDeclarePassive("rabbitAvailableTests.queue"); assertEquals(0, declareOk.getConsumerCount()); channel.close(); conn.close(); }
@Nullable private SimpleConsumer consume(String queue, Connection connection) { Channel channel = null; SimpleConsumer consumer = null; try { channel = connection.createChannel(isChannelTransacted()); channel.basicQos(getPrefetchCount()); consumer = new SimpleConsumer(connection, channel, queue); channel.queueDeclarePassive(queue); consumer.consumerTag = channel.basicConsume(queue, getAcknowledgeMode().isAutoAck(), (getConsumerTagStrategy() != null ? getConsumerTagStrategy().createConsumerTag(queue) : ""), // NOSONAR never null isNoLocal(), isExclusive(), getConsumerArguments(), consumer); } catch (AmqpApplicationContextClosedException e) { throw new AmqpConnectException(e); } catch (Exception e) { RabbitUtils.closeChannel(channel); RabbitUtils.closeConnection(connection); consumer = handleConsumeException(queue, consumer, e); } return consumer; }
@Test public void test(ConnectionFactory cf) throws Exception { assertSame(cf, this.connectionFactory); Connection conn = this.connectionFactory.newConnection(); Channel channel = conn.createChannel(); DeclareOk declareOk = channel.queueDeclarePassive("rabbitAvailableTests.queue"); assertEquals(0, declareOk.getConsumerCount()); channel.close(); conn.close(); }