Skip to content

Commit 8c26ebd

Browse files
committed
Finalize naming strategy
References #513 (cherry picked from commit f1e6926) Conflicts: src/main/java/com/rabbitmq/jms/admin/RMQConnectionFactory.java src/main/java/com/rabbitmq/jms/client/RMQConnection.java
1 parent 77f0f8e commit 8c26ebd

23 files changed

+354
-134
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.rabbitmq.jms.admin;
2+
3+
/**
4+
* Default implementation for {@link NamingStrategy}.
5+
*/
6+
final class DefaultNamingStrategy implements NamingStrategy {
7+
8+
private final String topicExchangeName = "jms.durable.topic";
9+
10+
private final String temporaryTopicExchangeName = "jms.temp.topic";
11+
12+
private final String queueExchangeName = "jms.durable.queues";
13+
14+
private final String temporaryQueueExchangeName = "jms.temp.queues";
15+
16+
private final String topicSubscriberQueuePrefix = "jms-cons-";
17+
18+
private final String durableSubscriberTopicSelectorExchangePrefix = "jms-dutop-slx-";
19+
20+
private final String nonDurableSubscriberTopicSelectorExchangePrefix = "jms-ndtop-slx-";
21+
22+
private final String temporaryQueuePrefix = "jms-temp-queue-";
23+
24+
private final String temporaryTopicPrefix = "jms-temp-queue-";
25+
26+
@Override
27+
public String topicExchangeName() {
28+
return this.topicExchangeName;
29+
}
30+
31+
@Override
32+
public String temporaryTopicExchangeName() {
33+
return this.temporaryTopicExchangeName;
34+
}
35+
36+
@Override
37+
public String queueExchangeName() {
38+
return this.queueExchangeName;
39+
}
40+
41+
@Override
42+
public String temporaryQueueExchangeName() {
43+
return this.temporaryQueueExchangeName;
44+
}
45+
46+
@Override
47+
public String topicSubscriberQueuePrefix() {
48+
return this.topicSubscriberQueuePrefix;
49+
}
50+
51+
@Override
52+
public String durableSubscriberTopicSelectorExchangePrefix() {
53+
return this.durableSubscriberTopicSelectorExchangePrefix;
54+
}
55+
56+
@Override
57+
public String nonDurableSubscriberTopicSelectorExchangePrefix() {
58+
return this.nonDurableSubscriberTopicSelectorExchangePrefix;
59+
}
60+
61+
@Override
62+
public String temporaryQueuePrefix() {
63+
return this.temporaryQueuePrefix;
64+
}
65+
66+
@Override
67+
public String temporaryTopicPrefix() {
68+
return this.temporaryTopicPrefix;
69+
}
70+
}

src/main/java/com/rabbitmq/jms/admin/DestinationsStrategy.java

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.rabbitmq.jms.admin;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* Contract to control the name of AMQP entities (exchanges, queues).
7+
*
8+
* <p>Naming is considered an implementation details of RabbitMQ JMS, so this interface and its
9+
* methods can change at any time between releases.
10+
*
11+
* <p>A custom implementation can be used to customize the name of some entities (e.g. for security
12+
* reasons), but implementators must be prepared to API changes.
13+
*
14+
* @since 3.4.0
15+
* @see RMQConnectionFactory#setNamingStrategy(NamingStrategy)
16+
*/
17+
public interface NamingStrategy extends Serializable {
18+
19+
NamingStrategy DEFAULT = new DefaultNamingStrategy();
20+
21+
/**
22+
* Name of the exchange for non-temporary topics.
23+
*
24+
* @return exchange name
25+
*/
26+
String topicExchangeName();
27+
28+
/**
29+
* Name of the exchange for temporary topics.
30+
*
31+
* @return exchange name
32+
*/
33+
String temporaryTopicExchangeName();
34+
35+
/**
36+
* Name of the exchange for non-temporary queues.
37+
*
38+
* @return exchange name
39+
*/
40+
String queueExchangeName();
41+
42+
/**
43+
* Name of the exchange for temporary queues.
44+
*
45+
* @return exchange name
46+
*/
47+
String temporaryQueueExchangeName();
48+
49+
/**
50+
* Prefix for the AMQP queue name of topic subscribers.
51+
*
52+
* @return queue prefix
53+
*/
54+
String topicSubscriberQueuePrefix();
55+
56+
/**
57+
* Prefix for the selector exchange name of durable topic subscribers.
58+
*
59+
* @return exchange prefix
60+
*/
61+
String durableSubscriberTopicSelectorExchangePrefix();
62+
63+
/**
64+
* Prefix for the selector exchange name of non-durable topic subscribers.
65+
*
66+
* @return exchange prefix
67+
*/
68+
String nonDurableSubscriberTopicSelectorExchangePrefix();
69+
70+
/**
71+
* Prefix for the AMQP queue name of temporary queues.
72+
*
73+
* @return queue prefix
74+
*/
75+
String temporaryQueuePrefix();
76+
77+
/**
78+
* Prefix for the AMQP queue name of temporary topics.
79+
*
80+
* @return queue prefix
81+
*/
82+
String temporaryTopicPrefix();
83+
}

src/main/java/com/rabbitmq/jms/admin/RMQConnectionFactory.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,11 @@ public class RMQConnectionFactory implements ConnectionFactory, Referenceable, S
290290
private AuthenticationMechanism authenticationMechanism = AuthenticationMechanism.PLAIN;
291291

292292
/**
293-
* The destinations name strategy to use.
293+
* The entity name strategy to use.
294294
*
295295
* @since 3.4.0
296296
*/
297-
private DestinationsStrategy destinationsStrategy = new DestinationsStrategy() {};
297+
private NamingStrategy namingStrategy = NamingStrategy.DEFAULT;
298298

299299
/**
300300
* {@inheritDoc}
@@ -384,7 +384,7 @@ protected Connection createConnection(String username, String password, Connecti
384384
.setKeepTextMessageType(this.keepTextMessageType)
385385
.setValidateSubscriptionNames(this.validateSubscriptionNames)
386386
.setReplyToStrategy(replyToStrategy)
387-
.setDestinationsStrategy(destinationsStrategy)
387+
.setNamingStrategy(namingStrategy)
388388
);
389389
logger.debug("Connection {} created.", conn);
390390
return conn;
@@ -1224,9 +1224,22 @@ public void setValidateSubscriptionNames(boolean validateSubscriptionNames) {
12241224
this.validateSubscriptionNames = validateSubscriptionNames;
12251225
}
12261226

1227-
public void setDestinationsStrategy(DestinationsStrategy destinationsStrategy) {
1228-
this.destinationsStrategy = destinationsStrategy;
1229-
}
1227+
/**
1228+
* Naming strategy for AMQP entities.
1229+
*
1230+
* <p>Most applications should not worry about this setting, the default is fine.
1231+
*
1232+
* <p>Note {@link NamingStrategy} is not considered a public and stable API.
1233+
*
1234+
* @param namingStrategy naming strategy
1235+
* @see NamingStrategy
1236+
*/
1237+
public void setNamingStrategy(NamingStrategy namingStrategy) {
1238+
if (namingStrategy == null) {
1239+
throw new IllegalArgumentException("Naming strategy cannot be null");
1240+
}
1241+
this.namingStrategy = namingStrategy;
1242+
}
12301243

12311244
@FunctionalInterface
12321245
private interface ConnectionCreator {

src/main/java/com/rabbitmq/jms/admin/RMQDestination.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,41 @@ public RMQDestination() {
6060
/**
6161
* Creates a destination for RJMS
6262
* @param destName the name of the topic or queue
63-
* @param isQueue true if this represent a queue
63+
* @param isQueue true if this represents a queue
6464
* @param isTemporary true if this is a temporary destination
6565
*/
66-
public RMQDestination(String destName, boolean isQueue, boolean isTemporary, DestinationsStrategy destinationsStrategy) {
67-
this(destName, isQueue, isTemporary, null, destinationsStrategy);
66+
public RMQDestination(String destName, boolean isQueue, boolean isTemporary) {
67+
this(destName, isQueue, isTemporary, null, NamingStrategy.DEFAULT);
6868
}
6969

7070
/**
7171
* Creates a destination for RJMS with arguments to declare the AMQP queue
7272
* @param destName the name of the topic or queue
73-
* @param isQueue true if this represent a queue
73+
* @param isQueue true if this represents a queue
7474
* @param isTemporary true if this is a temporary destination
7575
* @param queueDeclareArguments arguments to use when declaring the AMQP queue
7676
*/
77-
public RMQDestination(String destName, boolean isQueue, boolean isTemporary, Map<String, Object> queueDeclareArguments, DestinationsStrategy destinationsStrategy) {
78-
this(destName, false, queueOrTopicExchangeName(isQueue, isTemporary, destinationsStrategy), destName, destName, isQueue, isTemporary, queueDeclareArguments);
77+
public RMQDestination(String destName, boolean isQueue, boolean isTemporary, Map<String, Object> queueDeclareArguments) {
78+
this(destName, isQueue, isTemporary, queueDeclareArguments, NamingStrategy.DEFAULT);
7979
}
8080

81-
private static String queueOrTopicExchangeName(boolean isQueue, boolean isTemporary, DestinationsStrategy destinationsStrategy) {
82-
if (isQueue & isTemporary) return destinationsStrategy.getTempQueueExchangeName();
83-
else if (isQueue & !isTemporary) return destinationsStrategy.getDurableQueueExchangeName();
84-
else if (!isQueue & isTemporary) return destinationsStrategy.getTempTopicExchangeName();
85-
else /* if (!isQueue & !isTemporary) */ return destinationsStrategy.getDurableTopicExchangeName();
81+
/**
82+
* Creates a destination for RJMS with arguments to declare the AMQP queue
83+
* @param destName the name of the topic or queue
84+
* @param isQueue true if this represents a queue
85+
* @param isTemporary true if this is a temporary destination
86+
* @param queueDeclareArguments arguments to use when declaring the AMQP queue
87+
* @param namingStrategy entity naming strategy
88+
*/
89+
public RMQDestination(String destName, boolean isQueue, boolean isTemporary, Map<String, Object> queueDeclareArguments, NamingStrategy namingStrategy) {
90+
this(destName, false, queueOrTopicExchangeName(isQueue, isTemporary, namingStrategy), destName, destName, isQueue, isTemporary, queueDeclareArguments);
91+
}
92+
93+
private static String queueOrTopicExchangeName(boolean isQueue, boolean isTemporary, NamingStrategy namingStrategy) {
94+
if (isQueue & isTemporary) return namingStrategy.temporaryQueueExchangeName();
95+
else if (isQueue & !isTemporary) return namingStrategy.queueExchangeName();
96+
else if (!isQueue & isTemporary) return namingStrategy.temporaryTopicExchangeName();
97+
else /* if (!isQueue & !isTemporary) */ return namingStrategy.topicExchangeName();
8698
}
8799

88100
private static String queueOrTopicExchangeType(boolean isQueue) {

src/main/java/com/rabbitmq/jms/admin/RMQObjectFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public class RMQObjectFactory implements ObjectFactory {
125125

126126
private static final Logger LOGGER = LoggerFactory.getLogger(RMQObjectFactory.class);
127127

128-
private final DestinationsStrategy destinationsStrategy = new DestinationsStrategy(){};
128+
private final NamingStrategy namingStrategy = NamingStrategy.DEFAULT;
129129

130130
/**
131131
* {@inheritDoc}
@@ -264,7 +264,7 @@ public Object createDestination(Reference ref, Hashtable<?, ?> environment, Name
264264
String amqpQueueName = getStringProperty(ref, environment, "amqpQueueName", true, null);
265265
return new RMQDestination(dname, amqpExchangeName, amqpRoutingKey, amqpQueueName);
266266
} else {
267-
return new RMQDestination(dname, !topic, false, queueDeclareArguments, destinationsStrategy);
267+
return new RMQDestination(dname, !topic, false, queueDeclareArguments, namingStrategy);
268268
}
269269
}
270270

src/main/java/com/rabbitmq/jms/client/ConnectionParams.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import com.rabbitmq.client.AMQP;
99
import com.rabbitmq.client.Connection;
10-
import com.rabbitmq.jms.admin.DestinationsStrategy;
10+
import com.rabbitmq.jms.admin.NamingStrategy;
1111
import com.rabbitmq.jms.util.WhiteListObjectInputStream;
1212

1313
import javax.jms.Message;
@@ -133,7 +133,7 @@ public class ConnectionParams {
133133
*
134134
* @since 3.4.0
135135
*/
136-
private DestinationsStrategy destinationsStrategy = new DestinationsStrategy() {};
136+
private NamingStrategy namingStrategy = NamingStrategy.DEFAULT;
137137

138138
public Connection getRabbitConnection() {
139139
return rabbitConnection;
@@ -297,12 +297,12 @@ public ReplyToStrategy getReplyToStrategy() {
297297
return replyToStrategy;
298298
}
299299

300-
public ConnectionParams setDestinationsStrategy(DestinationsStrategy destinationsStrategy) {
301-
this.destinationsStrategy = destinationsStrategy;
300+
public ConnectionParams setNamingStrategy(NamingStrategy namingStrategy) {
301+
this.namingStrategy = namingStrategy;
302302
return this;
303303
}
304304

305-
public DestinationsStrategy getDestinationsStrategy() {
306-
return destinationsStrategy;
305+
NamingStrategy getNamingStrategy() {
306+
return namingStrategy;
307307
}
308308
}

0 commit comments

Comments
 (0)