|
62 | 62 | import com.rabbitmq.client.AMQP;
|
63 | 63 | import com.rabbitmq.client.AlreadyClosedException;
|
64 | 64 | import com.rabbitmq.client.Channel;
|
| 65 | +import com.rabbitmq.client.Consumer; |
65 | 66 | import com.rabbitmq.client.DefaultConsumer;
|
66 | 67 | import com.rabbitmq.client.Envelope;
|
67 | 68 | import com.rabbitmq.client.Recoverable;
|
@@ -237,10 +238,11 @@ public BlockingQueueConsumer(ConnectionFactory connectionFactory,
|
237 | 238 | * @param queues The queues.
|
238 | 239 | */
|
239 | 240 | public BlockingQueueConsumer(ConnectionFactory connectionFactory,
|
240 |
| - MessagePropertiesConverter messagePropertiesConverter, |
241 |
| - ActiveObjectCounter<BlockingQueueConsumer> activeObjectCounter, AcknowledgeMode acknowledgeMode, |
242 |
| - boolean transactional, int prefetchCount, boolean defaultRequeueRejected, |
243 |
| - Map<String, Object> consumerArgs, boolean exclusive, String... queues) { |
| 241 | + MessagePropertiesConverter messagePropertiesConverter, |
| 242 | + ActiveObjectCounter<BlockingQueueConsumer> activeObjectCounter, AcknowledgeMode acknowledgeMode, |
| 243 | + boolean transactional, int prefetchCount, boolean defaultRequeueRejected, |
| 244 | + Map<String, Object> consumerArgs, boolean exclusive, String... queues) { |
| 245 | + |
244 | 246 | this(connectionFactory, messagePropertiesConverter, activeObjectCounter, acknowledgeMode, transactional,
|
245 | 247 | prefetchCount, defaultRequeueRejected, consumerArgs, false, exclusive, queues);
|
246 | 248 | }
|
@@ -483,7 +485,9 @@ private Message handle(Delivery delivery) throws InterruptedException {
|
483 | 485 | * @throws ShutdownSignalException if the connection is shut down while waiting
|
484 | 486 | */
|
485 | 487 | public Message nextMessage() throws InterruptedException, ShutdownSignalException {
|
486 |
| - logger.trace("Retrieving delivery for " + this); |
| 488 | + if (logger.isTraceEnabled()) { |
| 489 | + logger.trace("Retrieving delivery for " + this); |
| 490 | + } |
487 | 491 | return handle(this.queue.take());
|
488 | 492 | }
|
489 | 493 |
|
@@ -673,8 +677,10 @@ private void addRecoveryListener() {
|
673 | 677 |
|
674 | 678 | private void consumeFromQueue(String queue) throws IOException {
|
675 | 679 | String consumerTag = this.channel.basicConsume(queue, this.acknowledgeMode.isAutoAck(),
|
676 |
| - (this.tagStrategy != null ? this.tagStrategy.createConsumerTag(queue) : ""), this.noLocal, this.exclusive, |
677 |
| - this.consumerArgs, this.consumer); |
| 680 | + (this.tagStrategy != null ? this.tagStrategy.createConsumerTag(queue) : ""), this.noLocal, |
| 681 | + this.exclusive, this.consumerArgs, |
| 682 | + new ConsumerDecorator(queue, this.consumer, this.applicationEventPublisher)); |
| 683 | + |
678 | 684 | if (consumerTag != null) {
|
679 | 685 | this.consumerTags.put(consumerTag, queue);
|
680 | 686 | if (logger.isDebugEnabled()) {
|
@@ -817,7 +823,7 @@ public boolean commitIfNecessary(boolean locallyTransacted) throws IOException {
|
817 | 823 | */
|
818 | 824 | boolean isLocallyTransacted = locallyTransacted
|
819 | 825 | || (this.transactional
|
820 |
| - && TransactionSynchronizationManager.getResource(this.connectionFactory) == null); |
| 826 | + && TransactionSynchronizationManager.getResource(this.connectionFactory) == null); |
821 | 827 | try {
|
822 | 828 |
|
823 | 829 | boolean ackRequired = !this.acknowledgeMode.isAutoAck() && !this.acknowledgeMode.isManual();
|
@@ -884,11 +890,6 @@ public void handleConsumeOk(String consumerTag) {
|
884 | 890 | if (logger.isDebugEnabled()) {
|
885 | 891 | logger.debug("ConsumeOK: " + BlockingQueueConsumer.this);
|
886 | 892 | }
|
887 |
| - if (BlockingQueueConsumer.this.applicationEventPublisher != null) { |
888 |
| - String queueName = BlockingQueueConsumer.this.consumerTags.get(consumerTag); |
889 |
| - BlockingQueueConsumer.this.applicationEventPublisher |
890 |
| - .publishEvent(new ConsumeOkEvent(this, queueName, consumerTag)); |
891 |
| - } |
892 | 893 | }
|
893 | 894 |
|
894 | 895 | @Override
|
@@ -962,6 +963,68 @@ public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProp
|
962 | 963 |
|
963 | 964 | }
|
964 | 965 |
|
| 966 | + private static final class ConsumerDecorator implements Consumer { |
| 967 | + |
| 968 | + private final String queue; |
| 969 | + |
| 970 | + private final Consumer delegate; |
| 971 | + |
| 972 | + private final ApplicationEventPublisher applicationEventPublisher; |
| 973 | + |
| 974 | + private String consumerTag; |
| 975 | + |
| 976 | + ConsumerDecorator(String queue, Consumer delegate, ApplicationEventPublisher applicationEventPublisher) { |
| 977 | + this.queue = queue; |
| 978 | + this.delegate = delegate; |
| 979 | + this.applicationEventPublisher = applicationEventPublisher; |
| 980 | + } |
| 981 | + |
| 982 | + |
| 983 | + @Override |
| 984 | + public void handleConsumeOk(String consumerTag) { |
| 985 | + this.consumerTag = consumerTag; |
| 986 | + this.delegate.handleConsumeOk(consumerTag); |
| 987 | + if (this.applicationEventPublisher != null) { |
| 988 | + this.applicationEventPublisher.publishEvent(new ConsumeOkEvent(this.delegate, this.queue, consumerTag)); |
| 989 | + } |
| 990 | + } |
| 991 | + |
| 992 | + @Override |
| 993 | + public void handleShutdownSignal(String consumerTag, ShutdownSignalException sig) { |
| 994 | + this.delegate.handleShutdownSignal(consumerTag, sig); |
| 995 | + } |
| 996 | + |
| 997 | + @Override |
| 998 | + public void handleCancel(String consumerTag) throws IOException { |
| 999 | + this.delegate.handleCancel(consumerTag); |
| 1000 | + } |
| 1001 | + |
| 1002 | + @Override |
| 1003 | + public void handleCancelOk(String consumerTag) { |
| 1004 | + this.delegate.handleCancelOk(consumerTag); |
| 1005 | + } |
| 1006 | + |
| 1007 | + @Override |
| 1008 | + public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, |
| 1009 | + byte[] body) throws IOException { |
| 1010 | + |
| 1011 | + this.delegate.handleDelivery(consumerTag, envelope, properties, body); |
| 1012 | + } |
| 1013 | + |
| 1014 | + @Override |
| 1015 | + public void handleRecoverOk(String consumerTag) { |
| 1016 | + this.delegate.handleRecoverOk(consumerTag); |
| 1017 | + } |
| 1018 | + |
| 1019 | + @Override |
| 1020 | + public String toString() { |
| 1021 | + return "ConsumerDecorator{" + "queue='" + this.queue + '\'' + |
| 1022 | + ", consumerTag='" + this.consumerTag + '\'' + |
| 1023 | + '}'; |
| 1024 | + } |
| 1025 | + |
| 1026 | + } |
| 1027 | + |
965 | 1028 | @SuppressWarnings("serial")
|
966 | 1029 | private static final class DeclarationException extends AmqpException {
|
967 | 1030 |
|
|
0 commit comments