protected AbstractConsumer(@Nonnull final ConnectionFactory connectionFactory, @Nonnull final AmqpConfig amqpConfig, @Nonnull final String name, @Nonnull final ConsumerCallback consumerCallback) { super(connectionFactory, amqpConfig, name); this.consumerCallback = consumerCallback; this.tickTimeout = getConfig().getTickTimeout().getMillis(); }
@Override protected boolean process() throws IOException, InterruptedException { channelConnect(); final QueueingConsumer consumer = consumerHolder.get(); if (consumer == null) { // When the channel is not connected, this can happen. Simulate a tick. LOG.warn("Channel not connected!"); Thread.sleep(tickTimeout); return true; } else { final QueueingConsumer.Delivery delivery = consumer.nextDelivery(tickTimeout); if (delivery != null) { try { return consumerCallback.withDelivery(delivery); } finally { final Channel channel = getChannel(); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } } else { LOG.trace("Tick..."); } return true; } } }
@Override protected void connectCallback(@Nonnull final Channel channel) throws IOException { super.connectCallback(channel); if (getConfig().isDeclaring()) { channel.exchangeDeclare(getName(), getConfig().getExchangeType(), getConfig().isDurable(), getConfig().isAutoDelete(), null); } final String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, getName(), routingKey); channel.basicConsume(queueName, false, getConsumer()); } }
@Override protected void connectCallback(final Channel channel) throws IOException { super.connectCallback(channel); if (getConfig().isDeclaring()) { channel.queueDeclare(getName(), getConfig().isDurable(), getConfig().isExclusive(), getConfig().isAutoDelete(), null); } channel.basicConsume(getName(), false, getConsumer()); } }