Skip to content

Commit ae8550d

Browse files
artembilangaryrussell
authored andcommitted
AMQP-812: Deprecate RabbitManagementTemplate (#779)
* AMQP-812: Deprecate RabbitManagementTemplate JIRA: https://jira.spring.io/browse/AMQP-812 * Fix `RabbitRestApiTests`
1 parent 8fa1eee commit ae8550d

File tree

9 files changed

+80
-104
lines changed

9 files changed

+80
-104
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ project('spring-rabbit') {
254254
compile project(":spring-amqp")
255255

256256
compile "com.rabbitmq:amqp-client:$rabbitmqVersion"
257-
compile "com.rabbitmq:http-client:$rabbitmqHttpClientVersion"
257+
compile ("com.rabbitmq:http-client:$rabbitmqHttpClientVersion", optional)
258258

259259
compile ("org.springframework:spring-aop:$springVersion", optional)
260260
compile "org.springframework:spring-context:$springVersion"

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2016 the original author or authors.
2+
* Copyright 2015-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,7 +24,9 @@
2424
* @author Gary Russell
2525
* @since 1.5
2626
*
27+
* @deprecated since 2.1 in favor of direct usage of target REST API client.
2728
*/
29+
@Deprecated
2830
public interface AmqpManagementOperations {
2931

3032
/**

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2017 the original author or authors.
2+
* Copyright 2015-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,7 +21,6 @@
2121

2222
import org.springframework.amqp.AmqpException;
2323
import org.springframework.amqp.core.AbstractExchange;
24-
import org.springframework.amqp.core.AmqpManagementOperations;
2524
import org.springframework.amqp.core.Binding;
2625
import org.springframework.amqp.core.Binding.DestinationType;
2726
import org.springframework.amqp.core.DirectExchange;
@@ -48,8 +47,10 @@
4847
*
4948
* @since 1.5
5049
*
50+
* @deprecated since 2.1 in favor of direct {@link Client} usage.
5151
*/
52-
public class RabbitManagementTemplate implements AmqpManagementOperations {
52+
@Deprecated
53+
public class RabbitManagementTemplate implements org.springframework.amqp.core.AmqpManagementOperations {
5354

5455
private static final String DEFAULT_VHOST = "/";
5556

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/annotation/EnableRabbitIntegrationTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@
7777
import org.springframework.amqp.rabbit.connection.ConnectionNameStrategy;
7878
import org.springframework.amqp.rabbit.connection.SimplePropertyValueConnectionNameStrategy;
7979
import org.springframework.amqp.rabbit.core.RabbitAdmin;
80-
import org.springframework.amqp.rabbit.core.RabbitManagementTemplate;
8180
import org.springframework.amqp.rabbit.core.RabbitTemplate;
8281
import org.springframework.amqp.rabbit.junit.BrokerRunning;
8382
import org.springframework.amqp.rabbit.listener.ConditionalRejectingErrorHandler;
@@ -140,6 +139,8 @@
140139
import org.springframework.util.ErrorHandler;
141140

142141
import com.rabbitmq.client.Channel;
142+
import com.rabbitmq.http.client.Client;
143+
import com.rabbitmq.http.client.domain.QueueInfo;
143144

144145
/**
145146
*
@@ -718,8 +719,8 @@ public void deadLetterOnDefaultExchange() {
718719
this.rabbitTemplate.convertAndSend("amqp656", "foo");
719720
assertEquals("foo", this.rabbitTemplate.receiveAndConvert("amqp656dlq", 10000));
720721
try {
721-
RabbitManagementTemplate rmt = new RabbitManagementTemplate();
722-
org.springframework.amqp.core.Queue amqp656 = rmt.getQueue("amqp656");
722+
Client rabbitRestClient = new Client("http://localhost:15672/api/", "guest", "guest");
723+
QueueInfo amqp656 = rabbitRestClient.getQueue("/", "amqp656");
723724
if (amqp656 != null) {
724725
assertEquals("", amqp656.getArguments().get("test-empty"));
725726
assertEquals("undefined", amqp656.getArguments().get("test-null"));

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
1717
package org.springframework.amqp.rabbit.core;
1818

1919
import static org.hamcrest.Matchers.greaterThan;
20-
import static org.hamcrest.Matchers.instanceOf;
2120
import static org.junit.Assert.assertEquals;
2221
import static org.junit.Assert.assertFalse;
2322
import static org.junit.Assert.assertNotNull;
@@ -41,6 +40,7 @@
4140
import org.springframework.amqp.core.Binding.DestinationType;
4241
import org.springframework.amqp.core.DirectExchange;
4342
import org.springframework.amqp.core.Exchange;
43+
import org.springframework.amqp.core.ExchangeTypes;
4444
import org.springframework.amqp.core.Message;
4545
import org.springframework.amqp.core.MessageBuilder;
4646
import org.springframework.amqp.core.MessageProperties;
@@ -56,6 +56,8 @@
5656
import com.rabbitmq.client.Channel;
5757
import com.rabbitmq.client.Connection;
5858
import com.rabbitmq.client.ConnectionFactory;
59+
import com.rabbitmq.http.client.Client;
60+
import com.rabbitmq.http.client.domain.ExchangeInfo;
5961

6062
/**
6163
* @author Dave Syer
@@ -260,8 +262,8 @@ public void testDeleteExchangeWithInternalOption() throws Exception {
260262
exchange.setInternal(true);
261263
rabbitAdmin.declareExchange(exchange);
262264

263-
Exchange exchange2 = getExchange(exchangeName);
264-
assertThat(exchange2, instanceOf(DirectExchange.class));
265+
ExchangeInfo exchange2 = getExchange(exchangeName);
266+
assertEquals(ExchangeTypes.DIRECT, exchange2.getType());
265267
assertTrue(exchange2.isInternal());
266268

267269
boolean result = rabbitAdmin.deleteExchange(exchangeName);
@@ -416,22 +418,22 @@ public void testDeclareDelayedExchange() throws Exception {
416418
assertEquals(Integer.valueOf(1000), received.getMessageProperties().getReceivedDelay());
417419
assertThat(System.currentTimeMillis() - t1, greaterThan(950L));
418420

419-
Exchange exchange2 = getExchange(exchangeName);
421+
ExchangeInfo exchange2 = getExchange(exchangeName);
420422
assertNotNull(exchange2);
421-
assertThat(exchange2, instanceOf(DirectExchange.class));
422-
assertTrue(exchange2.isDelayed());
423+
assertEquals(ExchangeTypes.DIRECT, exchange2.getArguments().get("x-delayed-type"));
424+
assertEquals("x-delayed-message", exchange2.getType());
423425

424426
this.rabbitAdmin.deleteQueue(queue.getName());
425427
this.rabbitAdmin.deleteExchange(exchangeName);
426428
}
427429

428-
private Exchange getExchange(String exchangeName) throws InterruptedException {
429-
RabbitManagementTemplate rmt = new RabbitManagementTemplate();
430+
private ExchangeInfo getExchange(String exchangeName) throws Exception {
431+
Client rabbitRestClient = new Client("http://localhost:15672/api/", "guest", "guest");
430432
int n = 0;
431-
Exchange exchange = rmt.getExchange(exchangeName);
433+
ExchangeInfo exchange = rabbitRestClient.getExchange("/", exchangeName);
432434
while (n++ < 100 && exchange == null) {
433435
Thread.sleep(100);
434-
exchange = rmt.getExchange(exchangeName);
436+
exchange = rabbitRestClient.getExchange("/", exchangeName);
435437
}
436438
return exchange;
437439
}
Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2017 the original author or authors.
2+
* Copyright 2015-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,8 @@
2525
import static org.junit.Assert.assertThat;
2626
import static org.junit.Assert.assertTrue;
2727

28+
import java.net.MalformedURLException;
29+
import java.net.URISyntaxException;
2830
import java.util.Collections;
2931
import java.util.List;
3032
import java.util.Map;
@@ -45,6 +47,9 @@
4547

4648
import com.rabbitmq.client.Channel;
4749
import com.rabbitmq.client.DefaultConsumer;
50+
import com.rabbitmq.http.client.Client;
51+
import com.rabbitmq.http.client.domain.BindingInfo;
52+
import com.rabbitmq.http.client.domain.ExchangeInfo;
4853
import com.rabbitmq.http.client.domain.QueueInfo;
4954

5055
/**
@@ -54,53 +59,58 @@
5459
* @since 1.5
5560
*
5661
*/
57-
public class RabbitManagementTemplateTests {
62+
public class RabbitRestApiTests {
5863

5964
private final CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");
6065

61-
private final RabbitManagementTemplate template = new RabbitManagementTemplate();
66+
private final Client rabbitRestClient;
67+
6268

6369
@ClassRule
6470
public static BrokerRunning brokerAndManagementRunning = BrokerRunning.isBrokerAndManagementRunning();
6571

72+
public RabbitRestApiTests() throws MalformedURLException, URISyntaxException {
73+
this.rabbitRestClient = new Client("http://localhost:15672/api/", "guest", "guest");
74+
}
75+
6676
@After
6777
public void tearDown() {
6878
connectionFactory.destroy();
6979
}
7080

7181
@Test
7282
public void testExchanges() {
73-
List<Exchange> list = this.template.getExchanges();
83+
List<ExchangeInfo> list = this.rabbitRestClient.getExchanges();
7484
assertTrue(list.size() > 0);
7585
}
7686

7787
@Test
7888
public void testExchangesVhost() {
79-
List<Exchange> list = this.template.getExchanges("/");
89+
List<ExchangeInfo> list = this.rabbitRestClient.getExchanges("/");
8090
assertTrue(list.size() > 0);
8191
}
8292

8393
@Test
8494
public void testBindings() {
85-
List<Binding> list = this.template.getBindings();
95+
List<BindingInfo> list = this.rabbitRestClient.getBindings();
8696
assertTrue(list.size() > 0);
8797
}
8898

8999
@Test
90100
public void testBindingsVhost() {
91-
List<Binding> list = this.template.getBindings("/");
101+
List<BindingInfo> list = this.rabbitRestClient.getBindings("/");
92102
assertTrue(list.size() > 0);
93103
}
94104

95105
@Test
96106
public void testQueues() {
97-
List<Queue> list = this.template.getQueues();
107+
List<QueueInfo> list = this.rabbitRestClient.getQueues();
98108
assertTrue(list.size() > 0);
99109
}
100110

101111
@Test
102112
public void testQueuesVhost() {
103-
List<Queue> list = this.template.getQueues("/");
113+
List<QueueInfo> list = this.rabbitRestClient.getQueues("/");
104114
assertTrue(list.size() > 0);
105115
}
106116

@@ -125,12 +135,12 @@ public void testBindingsDetail() {
125135
.with("bar");
126136
admin.declareBinding(binding2);
127137

128-
List<Binding> bindings = this.template.getBindingsForExchange("/", exchange1.getName());
138+
List<BindingInfo> bindings = this.rabbitRestClient.getBindingsBySource("/", exchange1.getName());
129139
assertEquals(2, bindings.size());
130-
assertEquals(exchange1.getName(), bindings.get(0).getExchange());
140+
assertEquals(exchange1.getName(), bindings.get(0).getSource());
131141
assertThat("foo", anyOf(equalTo(bindings.get(0).getRoutingKey()), equalTo(bindings.get(1).getRoutingKey())));
132-
Binding qout = null;
133-
Binding eout = null;
142+
BindingInfo qout = null;
143+
BindingInfo eout = null;
134144
if (bindings.get(0).getRoutingKey().equals("foo")) {
135145
qout = bindings.get(0);
136146
eout = bindings.get(1);
@@ -139,12 +149,12 @@ public void testBindingsDetail() {
139149
eout = bindings.get(0);
140150
qout = bindings.get(1);
141151
}
142-
assertEquals(Binding.DestinationType.QUEUE, qout.getDestinationType());
152+
assertEquals("queue", qout.getDestinationType());
143153
assertEquals(queue.getName(), qout.getDestination());
144154
assertNotNull(qout.getArguments());
145155
assertEquals("", qout.getArguments().get("alternate-exchange"));
146156

147-
assertEquals(Binding.DestinationType.EXCHANGE, eout.getDestinationType());
157+
assertEquals("exchange", eout.getDestinationType());
148158
assertEquals(exchange2.getName(), eout.getDestination());
149159

150160
admin.deleteExchange(exchange1.getName());
@@ -157,7 +167,7 @@ public void testSpecificExchange() {
157167
Map<String, Object> args = Collections.<String, Object>singletonMap("alternate-exchange", "");
158168
Exchange exchange = new DirectExchange(UUID.randomUUID().toString(), true, true, args);
159169
admin.declareExchange(exchange);
160-
Exchange exchangeOut = this.template.getExchange("/", exchange.getName());
170+
ExchangeInfo exchangeOut = this.rabbitRestClient.getExchange("/", exchange.getName());
161171
assertTrue(exchangeOut.isDurable());
162172
assertTrue(exchangeOut.isAutoDelete());
163173
assertEquals(exchange.getName(), exchangeOut.getName());
@@ -180,13 +190,13 @@ public void testSpecificQueue() throws Exception {
180190
admin.declareQueue(queue2);
181191
Channel channel = this.connectionFactory.createConnection().createChannel(false);
182192
String consumer = channel.basicConsume(queue1.getName(), false, "", false, true, null, new DefaultConsumer(channel));
183-
QueueInfo qi = this.template.getClient().getQueue("/", queue1.getName());
193+
QueueInfo qi = this.rabbitRestClient.getQueue("/", queue1.getName());
184194
int n = 0;
185195
while (n++ < 100 && (qi.getExclusiveConsumerTag() == null || qi.getExclusiveConsumerTag().equals(""))) {
186196
Thread.sleep(100);
187-
qi = this.template.getClient().getQueue("/", queue1.getName());
197+
qi = this.rabbitRestClient.getQueue("/", queue1.getName());
188198
}
189-
Queue queueOut = this.template.getQueue("/", queue1.getName());
199+
QueueInfo queueOut = this.rabbitRestClient.getQueue("/", queue1.getName());
190200
assertFalse(queueOut.isDurable());
191201
assertFalse(queueOut.isExclusive());
192202
assertTrue(queueOut.isAutoDelete());
@@ -196,7 +206,7 @@ public void testSpecificQueue() throws Exception {
196206
channel.basicCancel(consumer);
197207
channel.close();
198208

199-
queueOut = this.template.getQueue("/", queue2.getName());
209+
queueOut = this.rabbitRestClient.getQueue("/", queue2.getName());
200210
assertTrue(queueOut.isDurable());
201211
assertFalse(queueOut.isExclusive());
202212
assertFalse(queueOut.isAutoDelete());
@@ -211,12 +221,17 @@ public void testSpecificQueue() throws Exception {
211221
public void testDeleteExchange() {
212222
String exchangeName = "testExchange";
213223
Exchange testExchange = new DirectExchange(exchangeName);
214-
this.template.addExchange(testExchange);
215-
Exchange exchangeToAssert = this.template.getExchange(exchangeName);
224+
ExchangeInfo info = new ExchangeInfo();
225+
info.setArguments(testExchange.getArguments());
226+
info.setAutoDelete(testExchange.isAutoDelete());
227+
info.setDurable(testExchange.isDurable());
228+
info.setType(testExchange.getType());
229+
this.rabbitRestClient.declareExchange("/", testExchange.getName(), info);
230+
ExchangeInfo exchangeToAssert = this.rabbitRestClient.getExchange("/", exchangeName);
216231
assertEquals(testExchange.getName(), exchangeToAssert.getName());
217232
assertEquals(testExchange.getType(), exchangeToAssert.getType());
218-
this.template.deleteExchange(testExchange);
219-
assertNull(this.template.getExchange(exchangeName));
233+
this.rabbitRestClient.deleteExchange("/", testExchange.getName());
234+
assertNull(this.rabbitRestClient.getExchange("/", exchangeName));
220235
}
221236

222237
}

0 commit comments

Comments
 (0)