Skip to content

Commit 389efc3

Browse files
garyrussellartembilan
authored andcommitted
Add type-safe queue properties to AmqpAdmin
Retain (don't deprecate) the current method for JMX users. * Fix @SInCE
1 parent 1614a4b commit 389efc3

File tree

4 files changed

+123
-14
lines changed

4 files changed

+123
-14
lines changed

spring-amqp/src/main/java/org/springframework/amqp/core/AmqpAdmin.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ public interface AmqpAdmin {
118118
@Nullable
119119
Properties getQueueProperties(String queueName);
120120

121+
/**
122+
* Returns information about the queue, if it exists.
123+
* @param queueName the name of the queue.
124+
* @return the information or null if the queue doesn't exist.
125+
*/
126+
@Nullable
127+
QueueInformation getQueueInfo(String queueName);
128+
121129
/**
122130
* Initialize the admin.
123131
* @since 2.1
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.amqp.core;
18+
19+
/**
20+
* Information about a queue, resulting from a passive declaration.
21+
*
22+
* @author Gary Russell
23+
* @since 2.2
24+
*
25+
*/
26+
public class QueueInformation {
27+
28+
private final String name;
29+
30+
private final int messageCount;
31+
32+
private final int consumerCount;
33+
34+
public QueueInformation(String name, int messageCount, int consumerCount) {
35+
this.name = name;
36+
this.messageCount = messageCount;
37+
this.consumerCount = consumerCount;
38+
}
39+
40+
public String getName() {
41+
return this.name;
42+
}
43+
44+
public int getMessageCount() {
45+
return this.messageCount;
46+
}
47+
48+
public int getConsumerCount() {
49+
return this.consumerCount;
50+
}
51+
52+
@Override
53+
public int hashCode() {
54+
final int prime = 31;
55+
int result = 1;
56+
result = prime * result + ((this.name == null) ? 0 : this.name.hashCode());
57+
return result;
58+
}
59+
60+
@Override
61+
public boolean equals(Object obj) {
62+
if (this == obj) {
63+
return true;
64+
}
65+
if (obj == null) {
66+
return false;
67+
}
68+
if (getClass() != obj.getClass()) {
69+
return false;
70+
}
71+
QueueInformation other = (QueueInformation) obj;
72+
if (this.name == null) {
73+
if (other.name != null) {
74+
return false;
75+
}
76+
}
77+
else if (!this.name.equals(other.name)) {
78+
return false;
79+
}
80+
return true;
81+
}
82+
83+
@Override
84+
public String toString() {
85+
return "QueueInformation [name=" + this.name + ", messageCount=" + this.messageCount + ", consumerCount="
86+
+ this.consumerCount + "]";
87+
}
88+
89+
}

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitAdmin.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.springframework.amqp.core.Declarables;
4040
import org.springframework.amqp.core.Exchange;
4141
import org.springframework.amqp.core.Queue;
42+
import org.springframework.amqp.core.QueueInformation;
4243
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
4344
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory.CacheMode;
4445
import org.springframework.amqp.rabbit.connection.ChannelProxy;
@@ -249,7 +250,7 @@ public boolean deleteExchange(final String exchangeName) {
249250
try {
250251
channel.exchangeDelete(exchangeName);
251252
}
252-
catch (IOException e) {
253+
catch (@SuppressWarnings("unused") IOException e) {
253254
return false;
254255
}
255256
return true;
@@ -314,7 +315,7 @@ public boolean deleteQueue(final String queueName) {
314315
try {
315316
channel.queueDelete(queueName);
316317
}
317-
catch (IOException e) {
318+
catch (@SuppressWarnings("unused") IOException e) {
318319
return false;
319320
}
320321
return true;
@@ -397,17 +398,28 @@ public void removeBinding(final Binding binding) {
397398
*/
398399
@Override
399400
@ManagedOperation(description = "Get queue name, message count and consumer count")
400-
@Nullable
401401
public Properties getQueueProperties(final String queueName) {
402+
QueueInformation queueInfo = getQueueInfo(queueName);
403+
if (queueInfo != null) {
404+
Properties props = new Properties();
405+
props.put(QUEUE_NAME, queueInfo.getName());
406+
props.put(QUEUE_MESSAGE_COUNT, queueInfo.getMessageCount());
407+
props.put(QUEUE_CONSUMER_COUNT, queueInfo.getConsumerCount());
408+
return props;
409+
}
410+
else {
411+
return null;
412+
}
413+
}
414+
415+
@Override
416+
public QueueInformation getQueueInfo(String queueName) {
402417
Assert.hasText(queueName, "'queueName' cannot be null or empty");
403418
return this.rabbitTemplate.execute(channel -> {
404419
try {
405420
DeclareOk declareOk = channel.queueDeclarePassive(queueName);
406-
Properties props = new Properties();
407-
props.put(QUEUE_NAME, declareOk.getQueue());
408-
props.put(QUEUE_MESSAGE_COUNT, declareOk.getMessageCount());
409-
props.put(QUEUE_CONSUMER_COUNT, declareOk.getConsumerCount());
410-
return props;
421+
return new QueueInformation(declareOk.getQueue(), declareOk.getMessageCount(),
422+
declareOk.getConsumerCount());
411423
}
412424
catch (IllegalArgumentException e) {
413425
if (RabbitAdmin.this.logger.isDebugEnabled()) {
@@ -419,11 +431,11 @@ public Properties getQueueProperties(final String queueName) {
419431
((ChannelProxy) channel).getTargetChannel().close();
420432
}
421433
}
422-
catch (TimeoutException e1) {
434+
catch (@SuppressWarnings("unused") TimeoutException e1) {
423435
}
424436
return null;
425437
}
426-
catch (Exception e) {
438+
catch (@SuppressWarnings("unused") Exception e) {
427439
if (RabbitAdmin.this.logger.isDebugEnabled()) {
428440
RabbitAdmin.this.logger.debug("Queue '" + queueName + "' does not exist");
429441
}

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitAdminTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import org.springframework.amqp.core.DirectExchange;
6969
import org.springframework.amqp.core.Exchange;
7070
import org.springframework.amqp.core.Queue;
71+
import org.springframework.amqp.core.QueueInformation;
7172
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
7273
import org.springframework.amqp.rabbit.connection.Connection;
7374
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
@@ -185,10 +186,9 @@ public void testProperties() throws Exception {
185186
}
186187

187188
private int messageCount(RabbitAdmin rabbitAdmin, String queueName) {
188-
Properties props = rabbitAdmin.getQueueProperties(queueName);
189-
assertNotNull(props);
190-
assertNotNull(props.get(RabbitAdmin.QUEUE_MESSAGE_COUNT));
191-
return (Integer) props.get(RabbitAdmin.QUEUE_MESSAGE_COUNT);
189+
QueueInformation info = rabbitAdmin.getQueueInfo(queueName);
190+
assertNotNull(info);
191+
return info.getMessageCount();
192192
}
193193

194194
@Test

0 commit comments

Comments
 (0)