@@ -15,15 +15,45 @@ public class RoundTripTest {
1515 public static String getEnv (String property , String defaultValue ) {
1616 return System .getenv (property ) == null ? defaultValue : System .getenv (property );
1717 }
18+ public static String getEnv (String property ) {
19+ String value = System .getenv (property );
20+ if (value == null ) {
21+ throw new IllegalArgumentException ("Missing env variable " + property );
22+ }
23+ return value ;
24+ }
1825 public static void main (String args []) throws Exception {
1926 String hostname = getEnv ("RABBITMQ_HOSTNAME" , "localhost" );
2027 String port = getEnv ("RABBITMQ_AMQP_PORT" , "5672" );
2128 String scheme = getEnv ("RABBITMQ_AMQP_SCHEME" , "amqp" );
29+ String uri = scheme + "://" + hostname + ":" + port ;
2230 String username = args .length > 0 ? args [0 ] : getEnv ("RABBITMQ_AMQP_USERNAME" , "guest" );
2331 String password = args .length > 1 ? args [1 ] : getEnv ("RABBITMQ_AMQP_PASSWORD" , "guest" );
24- String uri = scheme + "://" + hostname + ":" + port ;
32+
33+ boolean usemtls = Boolean .parseBoolean (getEnv ("AMQP_USE_MTLS" , "false" ));
34+ String certsLocation = getEnv ("RABBITMQ_CERTS" );
35+
36+ if ("amqps" .equals (scheme )) {
37+ List <String > connectionParams = new ArrayList <String >();
38+
39+ connectionParams .add ("transport.trustStoreLocation=" + certsLocation + "/truststore.jks" );
40+ connectionParams .add ("transport.trustStorePassword=foobar" );
41+ connectionParams .add ("transport.verifyHost=true" );
42+ connectionParams .add ("transport.trustAll=true" );
2543
26- System .out .println ("AMQPS Roundrip using uri " + uri );
44+ if (usemtls ) {
45+ connectionParams .add ("amqp.saslMechanisms=EXTERNAL" );
46+ connectionParams .add ("transport.keyStoreLocation=" + certsLocation + "/client_rabbitmq.jks" );
47+ connectionParams .add ("transport.keyStorePassword=foobar" );
48+ connectionParams .add ("transport.keyAlias=client-rabbitmq-tls" );
49+ }
50+ if (!connectionParams .isEmpty ()) {
51+ uri = uri + "?" + String .join ("&" , connectionParams );
52+ System .out .println ("Using AMQP URI " + uri );
53+ }
54+ }
55+
56+ assertNotNull (uri );
2757
2858 Hashtable <Object , Object > env = new Hashtable <>();
2959 env .put (Context .INITIAL_CONTEXT_FACTORY , "org.apache.qpid.jms.jndi.JmsInitialContextFactory" );
@@ -33,12 +63,11 @@ public static void main(String args[]) throws Exception {
3363 env .put ("jms.requestTimeout" , 5 );
3464 javax .naming .Context context = new javax .naming .InitialContext (env );
3565
36- assertNotNull (uri );
37-
3866 ConnectionFactory factory = (ConnectionFactory ) context .lookup ("myFactoryLookup" );
3967 Destination queue = (Destination ) context .lookup ("myQueueLookup" );
4068
41- try (Connection connection = factory .createConnection (username , password )) {
69+ try (Connection connection =
70+ createConnection (factory , usemtls , username , password )) {
4271 connection .start ();
4372
4473 Session session = connection .createSession (false , Session .AUTO_ACKNOWLEDGE );
@@ -56,5 +85,12 @@ public static void main(String args[]) throws Exception {
5685
5786 assertEquals (message .getText (), receivedMessage .getText ());
5887 }
88+ }
89+ private static Connection createConnection (ConnectionFactory factory ,
90+ boolean usemtls , String username , String password ) throws jakarta .jms .JMSException {
91+ if (usemtls ) {
92+ return factory .createConnection ();
93+ }
94+ return factory .createConnection (username , password );
5995 }
6096}
0 commit comments