Skip to content

Commit 12b6d16

Browse files
committed
Add post-processor for AMQP connection factory
For advanced customization (TLS, metrics, threading, etc), just before the AMQP connection is created. Fixes #57 (cherry picked from commit da91f5d) Conflicts: src/main/java/com/rabbitmq/jms/admin/RMQConnectionFactory.java
1 parent 191c091 commit 12b6d16

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.rabbitmq.client.Address;
55
import com.rabbitmq.client.MetricsCollector;
66
import com.rabbitmq.client.NoOpMetricsCollector;
7+
import com.rabbitmq.jms.client.AmqpConnectionFactoryPostProcessor;
78
import com.rabbitmq.jms.client.AmqpPropertiesCustomiser;
89
import com.rabbitmq.jms.client.ConnectionParams;
910
import com.rabbitmq.jms.client.RMQConnection;
@@ -107,6 +108,17 @@ public class RMQConnectionFactory implements ConnectionFactory, Referenceable, S
107108
*/
108109
private MetricsCollector metricsCollector = new NoOpMetricsCollector();
109110

111+
/**
112+
* For post-processor the {@link com.rabbitmq.client.ConnectionFactory} before creating the AMQP connection.
113+
*
114+
* @since 1.10.0
115+
*/
116+
private AmqpConnectionFactoryPostProcessor amqpConnectionFactoryPostProcessor = new AmqpConnectionFactoryPostProcessor() {
117+
118+
@Override
119+
public void postProcess(com.rabbitmq.client.ConnectionFactory connectionFactory) { }
120+
};
121+
110122
/** Default not to use ssl */
111123
private boolean ssl = false;
112124
private String tlsProtocol;
@@ -222,6 +234,9 @@ protected Connection createConnection(String username, String password, Connecti
222234
maybeEnableTLS(cf);
223235
maybeEnableHostnameVerification(cf);
224236
cf.setMetricsCollector(this.metricsCollector);
237+
if (this.amqpConnectionFactoryPostProcessor != null) {
238+
this.amqpConnectionFactoryPostProcessor.postProcess(cf);
239+
}
225240
com.rabbitmq.client.Connection rabbitConnection = instantiateNodeConnection(cf, connectionCreator);
226241

227242
RMQConnection conn = new RMQConnection(new ConnectionParams()
@@ -859,6 +874,20 @@ public List<String> getUris() {
859874
return urisAsStrings;
860875
}
861876

877+
/**
878+
* Set a post-processor for the AMQP {@link com.rabbitmq.client.ConnectionFactory}.
879+
* <p>
880+
* The post-processor is called before the AMQP creation. This callback can be
881+
* useful to customize the {@link com.rabbitmq.client.ConnectionFactory}:
882+
* TLS-related configuration, metrics collection, etc.
883+
*
884+
* @param amqpConnectionFactoryPostProcessor
885+
* @since 1.10.0
886+
*/
887+
public void setAmqpConnectionFactoryPostProcessor(AmqpConnectionFactoryPostProcessor amqpConnectionFactoryPostProcessor) {
888+
this.amqpConnectionFactoryPostProcessor = amqpConnectionFactoryPostProcessor;
889+
}
890+
862891
private interface ConnectionCreator {
863892
com.rabbitmq.client.Connection create(com.rabbitmq.client.ConnectionFactory cf) throws Exception;
864893
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* Copyright (c) 2018 Pivotal Software, Inc. All rights reserved. */
2+
3+
package com.rabbitmq.jms.client;
4+
5+
import com.rabbitmq.client.ConnectionFactory;
6+
7+
/**
8+
* Callback to post-process the AMQP {@link com.rabbitmq.client.ConnectionFactory}.
9+
*
10+
* <p>
11+
* This callback is enforced just before the creation of the AMQP connection.
12+
* It can be used to for any customisation: TLS, threading, etc.
13+
*
14+
* @since 1.10.0
15+
*/
16+
public interface AmqpConnectionFactoryPostProcessor {
17+
18+
/**
19+
* Customise the {@link ConnectionFactory} before AMQP connection creation.
20+
*
21+
* @param connectionFactory
22+
*/
23+
void postProcess(ConnectionFactory connectionFactory);
24+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@
88

99
/**
1010
* Callback to customise properties of outbound AMQP messages.
11+
*
1112
* @since 1.9.0
1213
*/
1314
public interface AmqpPropertiesCustomiser {
1415

1516
/**
1617
* Customise AMQP message properties.
17-
* @param builder the AMQP properties builder
18+
*
19+
* @param builder the AMQP properties builder
1820
* @param jmsMessage the outbound JMS message
1921
* @return the customised or a new AMQP properties builder
2022
*/
2123
AMQP.BasicProperties.Builder customise(AMQP.BasicProperties.Builder builder, Message jmsMessage);
22-
2324
}

src/test/java/com/rabbitmq/jms/admin/RMQConnectionFactoryTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.rabbitmq.client.AddressResolver;
77
import com.rabbitmq.client.Connection;
88
import com.rabbitmq.client.ConnectionFactory;
9+
import com.rabbitmq.jms.client.AmqpConnectionFactoryPostProcessor;
910
import org.hamcrest.Matchers;
1011
import org.junit.Before;
1112
import org.junit.Test;
@@ -19,6 +20,7 @@
1920
import java.util.List;
2021
import java.util.Properties;
2122
import java.util.concurrent.ExecutorService;
23+
import java.util.concurrent.atomic.AtomicInteger;
2224

2325
import static java.util.Arrays.asList;
2426
import static org.hamcrest.Matchers.both;
@@ -274,6 +276,21 @@ public void shouldUseSeveralAddressesWhenUrisIsUsed() throws Exception {
274276
assertEquals(10000, passedInAddressResolver.getAddresses().get(1).getPort());
275277
}
276278

279+
@Test public void amqpConnectionFactoryIsCalled() throws Exception {
280+
final AtomicInteger callCount = new AtomicInteger(0);
281+
rmqCf.setAmqpConnectionFactoryPostProcessor(new AmqpConnectionFactoryPostProcessor() {
282+
283+
@Override
284+
public void postProcess(ConnectionFactory cf) {
285+
callCount.incrementAndGet();
286+
}
287+
});
288+
rmqCf.createConnection();
289+
assertEquals(1, callCount.get());
290+
rmqCf.createConnection();
291+
assertEquals(2, callCount.get());
292+
}
293+
277294
class TestRmqConnectionFactory extends RMQConnectionFactory {
278295

279296
@Override

0 commit comments

Comments
 (0)