8000 Create enum for exchange types · CassOnMars/rabbitmq-java-client@14acaf4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 14acaf4

Browse files
committed
Create enum for exchange types
Fixes rabbitmq#150
1 parent 9a0d02f commit 14acaf4

File tree

5 files changed

+226
-28
lines changed

5 files changed

+226
-28
lines changed

src/main/java/com/rabbitmq/client/Channel.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,17 @@ void basicPublish(String exchange, String routingKey, boolean mandatory, boolean
320320
*/
321321
Exchange.DeclareOk exchangeDeclare(String exchange, String type) throws IOException;
322322

323+
/**
324+
* Actively declare a non-autodelete, non-durable exchange with no extra arguments
325+
* @see com.rabbitmq.client.AMQP.Exchange.Declare
326+
* @see com.rabbitmq.client.AMQP.Exchange.DeclareOk
327+
* @param exchange the name of the exchange
328+
* @param type the exchange type
329+
* @return a declaration-confirm method to indicate the exchange was successfully declared
330+
* @throws java.io.IOException if an error is encountered
331+
*/
332+
Exchange.DeclareOk exchangeDeclare(String exchange, ExchangeType type) throws IOException;
333+
323334
/**
324335
* Actively declare a non-autodelete exchange with no extra arguments
325336
* @see com.rabbitmq.client.AMQP.Exchange.Declare
@@ -332,6 +343,18 @@ void basicPublish(String exchange, String routingKey, boolean mandatory, boolean
332343
*/
333344
Exchange.DeclareOk exchangeDeclare(String exchange, String type, boolean durable) throws IOException;
334345

346+
/**
347+
* Actively declare a non-autodelete exchange with no extra arguments
348+
* @see com.rabbitmq.client.AMQP.Exchange.Declare
349+
* @see com.rabbitmq.client.AMQP.Exchange.DeclareOk
350+
* @param exchange the name of the exchange
351+
* @param type the exchange type
352+
* @param durable true if we are declaring a durable exchange (the exchange will survive a server restart)
353+
* @throws java.io.IOException if an error is encountered
354+
* @return a declaration-confirm method to indicate the exchange was successfully declared
355+
*/
356+
Exchange.DeclareOk exchangeDeclare(String exchange, ExchangeType type, boolean durable) throws IOException;
357+
335358
/**
336359
* Declare an exchange.
337360
* @see com.rabbitmq.client.AMQP.Exchange.Declare
@@ -347,6 +370,21 @@ void basicPublish(String exchange, String routingKey, boolean mandatory, boolean
347370
Exchange.DeclareOk exchangeDeclare(String exchange, String type, boolean durable, boolean autoDelete,
348371
Map<String, Object> arguments) throws IOException;
349372

373+
/**
374+
* Declare an exchange.
375+
* @see com.rabbitmq.client.AMQP.Exchange.Declare
376+
* @see com.rabbitmq.client.AMQP.Exchange.DeclareOk
377+
* @param exchange the name of the exchange
378+
* @param type the exchange type
379+
* @param durable true if we are declaring a durable exchange (the exchange will survive a server restart)
380+
* @param autoDelete true if the server should delete the exchange when it is no longer in use
381+
* @param arguments other properties (construction arguments) for the exchange
382+
* @return a declaration-confirm method to indicate the exchange was successfully declared
383+
* @throws java.io.IOException if an error is encountered
384+
*/
385+
Exchange.DeclareOk exchangeDeclare(String exchange, ExchangeType type, boolean durable, boolean autoDelete,
386+
Map<String, Object> arguments) throws IOException;
387+
350388
/**
351389
* Declare an exchange, via an interface that allows the complete set of
352390
* arguments.
@@ -369,6 +407,28 @@ Exchange.DeclareOk exchangeDeclare(String exchange,
369407
boolean internal,
370408
Map<String, Object> arguments) throws IOException;
371409

410+
/**
411+
* Declare an exchange, via an interface that allows the complete set of
412+
* arguments.
413+
* @see com.rabbitmq.client.AMQP.Exchange.Declare
414+
* @see com.rabbitmq.client.AMQP.Exchange.DeclareOk
415+
* @param exchange the name of the exchange
416+
* @param type the exchange type
417+
* @param durable true if we are declaring a durable exchange (the exchange will survive a server restart)
418+
* @param autoDelete true if the server should delete the exchange when it is no longer in use
419+
* @param internal true if the exchange is internal, i.e. can't be directly
420+
* published to by a client.
421+
* @param arguments other properties (construction arguments) for the exchange
422+
* @return a declaration-confirm method to indicate the exchange was successfully declared
423+
* @throws java.io.IOException if an error is encountered
424+
*/
425+
Exchange.DeclareOk exchangeDeclare(String exchange,
426+
ExchangeType type,
427+
boolean durable,
428+
boolean autoDelete,
429+
boolean internal,
430+
Map<String, Object> arguments) throws IOException;
431+
372432
/**
373433
* Like {@link Channel#exchangeDeclare(String, String, boolean, boolean, java.util.Map)} but
374434
* sets nowait parameter to true and returns nothing (as there will be no response from
@@ -390,6 +450,27 @@ void exchangeDeclareNoWait(String exchange,
390450
boolean internal,
391451
Map<String, Object> arguments) throws IOException;
392452

453+
/**
454+
* Like {@link Channel#exchangeDeclare(String, String, boolean, boolean, java.util.Map)} but
455+
* sets nowait parameter to true and returns nothing (as there will be no response from
456+
* the server).
457+
*
458+
* @param exchange the name of the exchange
459+
* @param type the exchange type
460+
* @param durable true if we are declaring a durable exchange (the exchange will survive a server restart)
461+
* @param autoDelete true if the server should delete the exchange when it is no longer in use
462+
* @param internal true if the exchange is internal, i.e. can't be directly
463+
* published to by a client.
464+
* @param arguments other properties (construction arguments) for the exchange
465+
* @throws java.io.IOException if an error is encountered
466+
*/
467+
void exchangeDeclareNoWait(String exchange,
468+
ExchangeType type,
469+
boolean durable,
470+
boolean autoDelete,
471+
boolean internal,
472+
Map<String, Object> arguments) throws IOException;
473+
393474
/**
394475
* Declare an exchange passively; that is, check if the named exchange exists.
395476
* @param name check the existence of an exchange named this
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.rabbitmq.client;
2+
3+
/**
4+
*
5+
*/
6+
public enum ExchangeType {
7+
8+
DIRECT("direct"), FANOUT("fanout"), TOPIC("topic"), HEADERS("headers");
9+
10+
private final String type;
11+
12+
ExchangeType(String type) {
13+
this.type = type;
14+
}
15+
16+
public String getType() {
17+
return type;
18+
}
19+
}

src/main/java/com/rabbitmq/client/impl/ChannelN.java

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,10 @@
2525
import java.util.concurrent.CopyOnWriteArrayList;
2626
import java.util.concurrent.CountDownLatch;
2727
import java.util.concurrent.TimeoutException;
28-
import com.rabbitmq.client.AMQP;
28+
29+
import com.rabbitmq.client.*;
2930
import com.rabbitmq.client.AMQP.BasicProperties;
30-
import com.rabbitmq.client.Command;
31-
import com.rabbitmq.client.ConfirmListener;
32-
import com.rabbitmq.client.Connection;
33-
import com.rabbitmq.client.Consumer;
34-
import com.rabbitmq.client.Envelope;
35-
import com.rabbitmq.client.FlowListener;
36-
import com.rabbitmq.client.GetResponse;
3731
import com.rabbitmq.client.Method;
38-
import com.rabbitmq.client.MessageProperties;
39-
import com.rabbitmq.client.ReturnListener;
40-
import com.rabbitmq.client.ShutdownSignalException;
41-
import com.rabbitmq.client.UnexpectedMethodError;
4232
import com.rabbitmq.client.impl.AMQImpl.Basic;
4333
import com.rabbitmq.client.impl.AMQImpl.Channel;
4434
import com.rabbitmq.client.impl.AMQImpl.Confirm;
@@ -662,6 +652,8 @@ public void basicPublish(String exchange, String routingKey,
662652
useProps, body));
663653
}
664654

655+
656+
665657
/** Public API - {@inheritDoc} */
666658
public Exchange.DeclareOk exchangeDeclare(String exchange, String type,
667659
boolean durable, boolean autoDelete,
@@ -673,6 +665,17 @@ public Exchange.DeclareOk exchangeDeclare(String exchange, String type,
673665
arguments);
674666
}
675667

668+
/** Public API - {@inheritDoc} */
669+
public Exchange.DeclareOk exchangeDeclare(String exchange, ExchangeType type,
670+
boolean durable, boolean autoDelete,
671+
Map<String, Object> arguments)
672+
throws IOException
673+
{
674+
return exchangeDeclare(exchange, type.getType(),
675+
durable, autoDelete,
676+
arguments);
677+
}
678+
676679
public void exchangeDeclareNoWait(String exchange,
677680
String type,
678681
boolean durable,
@@ -691,6 +694,17 @@ public void exchangeDeclareNoWait(String exchange,
691694
.build()));
692695
}
693696

697+
public void exchangeDeclareNoWait(String exchange,
698+
ExchangeType type,
699+
boolean durable,
700+
boolean autoDelete,
701+
boolean internal,
702+
Map<String, Object> arguments) throws IOException {
703+
exchangeDeclareNoWait(exchange, type.getType(),
704+
durable, autoDelete, internal,
705+
arguments);
706+
}
707+
694708
/** Public API - {@inheritDoc} */
695709
public Exchange.DeclareOk exchangeDeclare(String exchange, String type,
696710
boolean durable,
@@ -711,6 +725,19 @@ public Exchange.DeclareOk exchangeDeclare(String exchange, String type,
711725
.getMethod();
712726
}
713727

728+
/** Public API - {@inheritDoc} */
729+
public Exchange.DeclareOk exchangeDeclare(String exchange, ExchangeType type,
730+
boolean durable,
731+
boolean autoDelete,
732+
boolean internal,
733+
Map<String, Object> arguments)
734+
throws IOException
735+
{
736+
return exchangeDeclare(exchange, type.getType(),
737+
durable, autoDelete, internal,
738+
arguments);
739+
}
740+
714741
/** Public API - {@inheritDoc} */
715742
public Exchange.DeclareOk exchangeDeclare(String exchange, String type,
716743
boolean durable)
@@ -719,13 +746,28 @@ public Exchange.DeclareOk exchangeDeclare(String exchange, String type,
719746
return exchangeDeclare(exchange, type, durable, false, null);
720747
}
721748

749+
/** Public API - {@inheritDoc} */
750+
public Exchange.DeclareOk exchangeDeclare(String exchange, ExchangeType type,
751+
boolean durable)
752+
throws IOException
753+
{
754+
return exchangeDeclare(exchange, type.getType(), durable);
755+
}
756+
722757
/** Public API - {@inheritDoc} */
723758
public Exchange.DeclareOk exchangeDeclare(String exchange, String type)
724759
throws IOException
725760
{
726761
return exchangeDeclare(exchange, type, false, false, null);
727762
}
728763

764+
/** Public API - {@inheritDoc} */
765+
public Exchange.DeclareOk exchangeDeclare(String exchange, ExchangeType type)
766+
throws IOException
767+
{
768+
return exchangeDeclare(exchange, type.getType());
769+
}
770+
729771
/** Public API - {@inheritDoc} */
730772
public Exchange.DeclareOk exchangeDeclarePassive(String exchange)
731773
throws IOException

src/main/java/com/rabbitmq/client/impl/recovery/AutorecoveringChannel.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,7 @@
1515

1616
package com.rabbitmq.client.impl.recovery;
1717

18-
import com.rabbitmq.client.AMQP;
19-
import com.rabbitmq.client.Channel;
20-
import com.rabbitmq.client.Command;
21-
import com.rabbitmq.client.ConfirmListener;
22-
import com.rabbitmq.client.Connection;
23-
import com.rabbitmq.client.Consumer;
24-
import com.rabbitmq.client.FlowListener;
25-
import com.rabbitmq.client.GetResponse;
26-
import com.rabbitmq.client.Method;
27-
import com.rabbitmq.client.Recoverable;
28-
import com.rabbitmq.client.RecoveryListener;
29-
import com.rabbitmq.client.ReturnListener;
30-
import com.rabbitmq.client.ShutdownListener;
31-
import com.rabbitmq.client.ShutdownSignalException;
18+
import com.rabbitmq.client.*;
3219

3320
import java.io.IOException;
3421
import java.util.ArrayList;
@@ -195,14 +182,26 @@ public AMQP.Exchange.DeclareOk exchangeDeclare(String exchange, String type) thr
195182
return exchangeDeclare(exchange, type, false, false, null);
196183
}
197184

185+
public AMQP.Exchange.DeclareOk exchangeDeclare(String exchange, ExchangeType type) throws IOException {
186+
return exchangeDeclare(exchange, type.getType());
187+
}
188+
198189
public AMQP.Exchange.DeclareOk exchangeDeclare(String exchange, String type, boolean durable) throws IOException {
199190
return exchangeDeclare(exchange, type, durable, false, null);
200191
}
201192

193+
public AMQP.Exchange.DeclareOk exchangeDeclare(String exchange, ExchangeType type, boolean durable) throws IOException {
194+
return exchangeDeclare(exchange, type.getType(), durable);
195+
}
196+
202197
public AMQP.Exchange.DeclareOk exchangeDeclare(String exchange, String type, boolean durable, boolean autoDelete, Map<String, Object> arguments) throws IOException {
203198
return exchangeDeclare(exchange, type, durable, autoDelete, false, arguments);
204199
}
205200

201+
public AMQP.Exchange.DeclareOk exchangeDeclare(String exchange, ExchangeType type, boolean durable, boolean autoDelete, Map<String, Object> arguments) throws IOException {
202+
return exchangeDeclare(exchange, type.getType(), durable, autoDelete, arguments);
203+
}
204+
206205
public AMQP.Exchange.DeclareOk exchangeDeclare(String exchange, String type, boolean durable, boolean autoDelete, boolean internal, Map<String, Object> arguments) throws IOException {
207206
final AMQP.Exchange.DeclareOk ok = delegate.exchangeDeclare(exchange, type, durable, autoDelete, internal, arguments);
208207
RecordedExchange x = new RecordedExchange(this, exchange).
@@ -214,6 +213,10 @@ public AMQP.Exchange.DeclareOk exchangeDeclare(String exchange, String type, boo
214213
return ok;
215214
}
216215

216+
public AMQP.Exchange.DeclareOk exchangeDeclare(String exchange, ExchangeType type, boolean durable, boolean autoDelete, boolean internal, Map<String, Object> arguments) throws IOException {
217+
return exchangeDeclare(exchange, type.getType(), durable, autoDelete, internal, arguments);
218+
}
219+
217220
@Override
218221
public void exchangeDeclareNoWait(String exchange, String type, boolean durable, boolean autoDelete, boolean internal, Map<String, Object> arguments) throws IOException {
219222
RecordedExchange x = new RecordedExchange(this, exchange).
@@ -225,6 +228,11 @@ public void exchangeDeclareNoWait(String exchange, String type, boolean durable,
225228
delegate.exchangeDeclareNoWait(exchange, type, durable, autoDelete, internal, arguments);
226229
}
227230

231+
@Override
232+
public void exchangeDeclareNoWait(String exchange, ExchangeType type, boolean durable, boolean autoDelete, boolean internal, Map<String, Object> arguments) throws IOException {
233+
exchangeDeclareNoWait(exchange, type.getType(), durable, autoDelete, internal, arguments);
234+
}
235+
228236
public AMQP.Exchange.DeclareOk exchangeDeclarePassive(String name) throws IOException {
229237
return delegate.exchangeDeclarePassive(name);
230238
}

src/test/java/com/rabbitmq/client/test/functional/ExchangeDeclare.java

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,15 @@
1616

1717
package com.rabbitmq.client.test.functional;
1818

19-
import java.util.Map;
20-
import java.util.HashMap;
19+
import com.rabbitmq.client.Channel;
20+
import com.rabbitmq.client.Connection;
21+
import com.rabbitmq.client.ConnectionFactory;
22+
import com.rabbitmq.client.ExchangeType;
23+
2124
import java.io.IOException;
25+
import java.util.HashMap;
26+
import java.util.Map;
27+
import java.util.concurrent.TimeoutException;
2228

2329
public class ExchangeDeclare extends ExchangeEquivalenceBase {
2430

@@ -71,4 +77,46 @@ public void testExchangeAutoDeleteNotEquivalent() throws IOException {
7177
channel.exchangeDeclare(NAME, "direct", false, false, null);
7278
verifyNotEquivalent(NAME, "direct", false, true, null);
7379
}
80+
81+
public void testExchangeDeclaredWithEnumerationEquivalentOnNonRecoverableConnection() throws IOException, InterruptedException {
82+
doTestExchangeDeclaredWithEnumerationEquivalent(channel);
83+
}
84+
85+
public void testExchangeDeclaredWithEnumerationEquivalentOnRecoverableConnection() throws IOException, TimeoutException, InterruptedException {
86+
ConnectionFactory connectionFactory = new ConnectionFactory();
87+
connectionFactory.setAutomaticRecoveryEnabled(true);
88+
connectionFactory.setTopologyRecoveryEnabled(false);
89+
Connection c = connectionFactory.newConnection();
90+
try {
91+
doTestExchangeDeclaredWithEnumerationEquivalent(c.createChannel());
92+
} finally {
93+
c.abort();
94+
}
95+
96+
}
97+
98+
private void doTestExchangeDeclaredWithEnumerationEquivalent(Channel channel) throws IOException, InterruptedException {
99+
assertEquals("There are 4 standard exchange types", 4, ExchangeType.values().length);
100+
for (ExchangeType exchangeType : ExchangeType.values()) {
101+
channel.exchangeDeclare(NAME, exchangeType);
102+
verifyEquivalent(NAME, exchangeType.getType(), false, false, null);
103+
channel.exchangeDelete(NAME);
104+
105+
channel.exchangeDeclare(NAME, exchangeType, false);
106+
verifyEquivalent(NAME, exchangeType.getType(), false, false, null);
107+
channel.exchangeDelete(NAME);
108+
109+
channel.exchangeDeclare(NAME, exchangeType, false, false, null);
110+
verifyEquivalent(NAME, exchangeType.getType(), false, false, null);
111+
channel.exchangeDelete(NAME);
112+
113+
channel.exchangeDeclare(NAME, exchangeType, false, false, false, null);
114+
verifyEquivalent(NAME, exchangeType.getType(), false, false, null);
115+
channel.exchangeDelete(NAME);
116+
117+
channel.exchangeDeclareNoWait(NAME, exchangeType, false, false, false, null);
118+
// no check, this one is asynchronous
119+
channel.exchangeDelete(NAME);
120+
}
121+
}
74122
}

0 commit comments

Comments
 (0)
0