public static void configureFactory ( ConnectionFactory factory , Map < String , String > configuration ) throws IOException { final Logger logger = Logger . getLogger ( RabbitMqUtils . class . getName ( ) ) ; logger . fine ( "Configuring a connection factory for RabbitMQ." ) ; String messageServerIp = configuration . get ( RABBITMQ_SERVER_IP ) ; if ( messageServerIp != null ) { Map . Entry < String , Integer > entry = Utils . findUrlAndPort ( messageServerIp ) ; factory . setHost ( entry . getKey ( ) ) ; if ( entry . getValue ( ) > 0 ) factory . setPort ( entry . getValue ( ) ) ; } factory . setUsername ( configuration . get ( RABBITMQ_SERVER_USERNAME ) ) ; factory . setPassword ( configuration . get ( RABBITMQ_SERVER_PASSWORD ) ) ; // Timeout for connection establishment: 5s factory . setConnectionTimeout ( 5000 ) ; // Configure automatic reconnection factory . setAutomaticRecoveryEnabled ( true ) ; // Recovery interval: 10s factory . setNetworkRecoveryInterval ( 10000 ) ; // Exchanges and so on should be redeclared if necessary factory . setTopologyRecoveryEnabled ( true ) ; // SSL if ( Boolean . parseBoolean ( configuration . get ( RABBITMQ_USE_SSL ) ) ) { logger . fine ( "Connection factory for RabbitMQ: SSL is used." ) ; InputStream clientIS = null ; InputStream storeIS = null ; try { clientIS = new FileInputStream ( configuration . get ( RABBITMQ_SSL_KEY_STORE_PATH ) ) ; storeIS = new FileInputStream ( configuration . get ( RABBITMQ_SSL_TRUST_STORE_PATH ) ) ; char [ ] keyStorePassphrase = configuration . get ( RABBITMQ_SSL_KEY_STORE_PASSPHRASE ) . toCharArray ( ) ; KeyStore ks = KeyStore . getInstance ( getValue ( configuration , RABBITMQ_SSL_KEY_STORE_TYPE , DEFAULT_SSL_KEY_STORE_TYPE ) ) ; ks . load ( clientIS , keyStorePassphrase ) ; String value = getValue ( configuration , RABBITMQ_SSL_KEY_MNGR_FACTORY , DEFAULT_SSL_MNGR_FACTORY ) ; KeyManagerFactory kmf = KeyManagerFactory . getInstance ( value ) ; kmf . init ( ks , keyStorePassphrase ) ; char [ ] trustStorePassphrase = configuration . get ( RABBITMQ_SSL_TRUST_STORE_PASSPHRASE ) . toCharArray ( ) ; KeyStore tks = KeyStore . getInstance ( getValue ( configuration , RABBITMQ_SSL_TRUST_STORE_TYPE , DEFAULT_SSL_TRUST_STORE_TYPE ) ) ; tks . load ( storeIS , trustStorePassphrase ) ; value = getValue ( configuration , RABBITMQ_SSL_TRUST_MNGR_FACTORY , DEFAULT_SSL_MNGR_FACTORY ) ; TrustManagerFactory tmf = TrustManagerFactory . getInstance ( value ) ; tmf . init ( tks ) ; SSLContext c = SSLContext . getInstance ( getValue ( configuration , RABBITMQ_SSL_PROTOCOL , DEFAULT_SSL_PROTOCOL ) ) ; c . init ( kmf . getKeyManagers ( ) , tmf . getTrustManagers ( ) , null ) ; factory . useSslProtocol ( c ) ; } catch ( GeneralSecurityException e ) { throw new IOException ( "SSL configuration for the RabbitMQ factory failed." , e ) ; } finally { Utils . closeQuietly ( storeIS ) ; Utils . closeQuietly ( clientIS ) ; } } }
Configures the connection factory with the right settings .