|
63 | 63 | import com.rabbitmq.client.AMQP; |
64 | 64 | import com.rabbitmq.client.AlreadyClosedException; |
65 | 65 | import com.rabbitmq.client.Channel; |
| 66 | +import com.rabbitmq.client.Consumer; |
66 | 67 | import com.rabbitmq.client.DefaultConsumer; |
67 | 68 | import com.rabbitmq.client.Envelope; |
68 | 69 | import com.rabbitmq.client.Recoverable; |
@@ -239,10 +240,10 @@ public BlockingQueueConsumer(ConnectionFactory connectionFactory, |
239 | 240 | * @param queues The queues. |
240 | 241 | */ |
241 | 242 | public BlockingQueueConsumer(ConnectionFactory connectionFactory, |
242 | | - MessagePropertiesConverter messagePropertiesConverter, |
243 | | - ActiveObjectCounter<BlockingQueueConsumer> activeObjectCounter, AcknowledgeMode acknowledgeMode, |
244 | | - boolean transactional, int prefetchCount, boolean defaultRequeueRejected, |
245 | | - Map<String, Object> consumerArgs, boolean exclusive, String... queues) { |
| 243 | + MessagePropertiesConverter messagePropertiesConverter, |
| 244 | + ActiveObjectCounter<BlockingQueueConsumer> activeObjectCounter, AcknowledgeMode acknowledgeMode, |
| 245 | + boolean transactional, int prefetchCount, boolean defaultRequeueRejected, |
| 246 | + Map<String, Object> consumerArgs, boolean exclusive, String... queues) { |
246 | 247 | this(connectionFactory, messagePropertiesConverter, activeObjectCounter, acknowledgeMode, transactional, |
247 | 248 | prefetchCount, defaultRequeueRejected, consumerArgs, false, exclusive, queues); |
248 | 249 | } |
@@ -475,8 +476,8 @@ private Message handle(Delivery delivery) throws InterruptedException { |
475 | 476 | */ |
476 | 477 | public Message nextMessage() throws InterruptedException, ShutdownSignalException { |
477 | 478 | if (logger.isTraceEnabled()) { |
478 | | - logger.trace("Retrieving delivery for " + this); |
479 | | - } |
| 479 | + logger.trace("Retrieving delivery for " + this); |
| 480 | + } |
480 | 481 | return handle(this.queue.take()); |
481 | 482 | } |
482 | 483 |
|
@@ -666,8 +667,10 @@ private void addRecoveryListener() { |
666 | 667 |
|
667 | 668 | private void consumeFromQueue(String queue) throws IOException { |
668 | 669 | String consumerTag = this.channel.basicConsume(queue, this.acknowledgeMode.isAutoAck(), |
669 | | - (this.tagStrategy != null ? this.tagStrategy.createConsumerTag(queue) : ""), this.noLocal, this.exclusive, |
670 | | - this.consumerArgs, this.consumer); |
| 670 | + (this.tagStrategy != null ? this.tagStrategy.createConsumerTag(queue) : ""), this.noLocal, |
| 671 | + this.exclusive, this.consumerArgs, |
| 672 | + new ConsumerDecorator(queue, this.consumer, this.applicationEventPublisher)); |
| 673 | + |
671 | 674 | if (consumerTag != null) { |
672 | 675 | this.consumerTags.put(consumerTag, queue); |
673 | 676 | if (logger.isDebugEnabled()) { |
@@ -810,7 +813,7 @@ public boolean commitIfNecessary(boolean locallyTransacted) throws IOException { |
810 | 813 | */ |
811 | 814 | boolean isLocallyTransacted = locallyTransacted |
812 | 815 | || (this.transactional |
813 | | - && TransactionSynchronizationManager.getResource(this.connectionFactory) == null); |
| 816 | + && TransactionSynchronizationManager.getResource(this.connectionFactory) == null); |
814 | 817 | try { |
815 | 818 |
|
816 | 819 | boolean ackRequired = !this.acknowledgeMode.isAutoAck() && !this.acknowledgeMode.isManual(); |
@@ -874,11 +877,6 @@ public void handleConsumeOk(String consumerTag) { |
874 | 877 | if (logger.isDebugEnabled()) { |
875 | 878 | logger.debug("ConsumeOK: " + BlockingQueueConsumer.this); |
876 | 879 | } |
877 | | - if (BlockingQueueConsumer.this.applicationEventPublisher != null) { |
878 | | - String queueName = BlockingQueueConsumer.this.consumerTags.get(consumerTag); |
879 | | - BlockingQueueConsumer.this.applicationEventPublisher |
880 | | - .publishEvent(new ConsumeOkEvent(this, queueName, consumerTag)); |
881 | | - } |
882 | 880 | } |
883 | 881 |
|
884 | 882 | @Override |
@@ -952,6 +950,62 @@ public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProp |
952 | 950 |
|
953 | 951 | } |
954 | 952 |
|
| 953 | + private static final class ConsumerDecorator implements Consumer { |
| 954 | + |
| 955 | + private final String queue; |
| 956 | + |
| 957 | + private final Consumer delegate; |
| 958 | + |
| 959 | + private final ApplicationEventPublisher applicationEventPublisher; |
| 960 | + |
| 961 | + private String consumerTag; |
| 962 | + |
| 963 | + ConsumerDecorator(String queue, Consumer delegate, ApplicationEventPublisher applicationEventPublisher) { |
| 964 | + this.queue = queue; |
| 965 | + this.delegate = delegate; |
| 966 | + this.applicationEventPublisher = applicationEventPublisher; |
| 967 | + } |
| 968 | + |
| 969 | + |
| 970 | + public void handleConsumeOk(String consumerTag) { |
| 971 | + this.consumerTag = consumerTag; |
| 972 | + this.delegate.handleConsumeOk(consumerTag); |
| 973 | + if (this.applicationEventPublisher != null) { |
| 974 | + this.applicationEventPublisher.publishEvent(new ConsumeOkEvent(this.delegate, this.queue, consumerTag)); |
| 975 | + } |
| 976 | + } |
| 977 | + |
| 978 | + public void handleShutdownSignal(String consumerTag, ShutdownSignalException sig) { |
| 979 | + this.delegate.handleShutdownSignal(consumerTag, sig); |
| 980 | + } |
| 981 | + |
| 982 | + public void handleCancel(String consumerTag) throws IOException { |
| 983 | + this.delegate.handleCancel(consumerTag); |
| 984 | + } |
| 985 | + |
| 986 | + public void handleCancelOk(String consumerTag) { |
| 987 | + this.delegate.handleCancelOk(consumerTag); |
| 988 | + } |
| 989 | + |
| 990 | + public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, |
| 991 | + byte[] body) throws IOException { |
| 992 | + |
| 993 | + this.delegate.handleDelivery(consumerTag, envelope, properties, body); |
| 994 | + } |
| 995 | + |
| 996 | + public void handleRecoverOk(String consumerTag) { |
| 997 | + this.delegate.handleRecoverOk(consumerTag); |
| 998 | + } |
| 999 | + |
| 1000 | + @Override |
| 1001 | + public String toString() { |
| 1002 | + return "ConsumerDecorator{" + "queue='" + this.queue + '\'' + |
| 1003 | + ", consumerTag='" + this.consumerTag + '\'' + |
| 1004 | + '}'; |
| 1005 | + } |
| 1006 | + |
| 1007 | + } |
| 1008 | + |
955 | 1009 | @SuppressWarnings("serial") |
956 | 1010 | private static final class DeclarationException extends AmqpException { |
957 | 1011 |
|
|
0 commit comments