private synchronized ConnectionFactory nodeConnectionFactory(String queue, String node, String address) { if (this.logger.isInfoEnabled()) { this.logger.info("Queue: " + queue + " is on node: " + node + " at: " + address); } ConnectionFactory cf = this.nodeFactories.get(node); if (cf == null) { cf = createConnectionFactory(address, node); if (this.logger.isInfoEnabled()) { this.logger.info("Created new connection factory: " + cf); } this.nodeFactories.put(node, cf); } return cf; }
@Override public ConnectionFactory getTargetConnectionFactory(Object key) { String queue = ((String) key); queue = queue.substring(1, queue.length() - 1); Assert.isTrue(!queue.contains(","), () -> "Cannot use LocalizedQueueConnectionFactory with more than one queue: " + key); ConnectionFactory connectionFactory = determineConnectionFactory(queue); if (connectionFactory == null) { return this.defaultConnectionFactory; } else { return connectionFactory; } }
/** * @param defaultConnectionFactory the fallback connection factory to use if the queue can't be located. * @param addresses the rabbitmq server addresses (host:port, ...). * @param adminUris the rabbitmq admin addresses (http://host:port, ...). * @param nodes the rabbitmq nodes corresponding to addresses (rabbit@server1, ...) must be the same length * as addresses. * @param vhost the virtual host. * @param username the user name. * @param password the password. * @param useSSL use SSL. * @param keyStore the key store resource (e.g. "file:/foo/keystore"). * @param trustStore the trust store resource (e.g. "file:/foo/truststore"). * @param keyStorePassPhrase the pass phrase for the key store. * @param trustStorePassPhrase the pass phrase for the trust store. */ public LocalizedQueueConnectionFactory(ConnectionFactory defaultConnectionFactory, String[] addresses, String[] adminUris, String[] nodes, String vhost, String username, String password, boolean useSSL, String keyStore, String trustStore, String keyStorePassPhrase, String trustStorePassPhrase) { this(defaultConnectionFactory, adminUris, nodesAddressesToMap(nodes, addresses), vhost, username, password, useSSL, null, keyStore, trustStore, keyStorePassPhrase, trustStorePassPhrase); }
Client client = createClient(adminUri, this.username, this.password); QueueInfo queueInfo = client.getQueue(this.vhost, queue); if (queueInfo != null) { String uri = this.nodeToAddress.get(node); if (uri != null) { return nodeConnectionFactory(queue, node, uri);
@Test public void test2Queues() throws Exception { try { String rabbit1 = "localhost:1235"; String rabbit2 = "localhost:1236"; String[] addresses = new String[]{rabbit1, rabbit2}; String[] adminUris = new String[]{"http://localhost:11235", "http://localhost:11236"}; String[] nodes = new String[]{"rabbit@foo", "rabbit@bar"}; String vhost = "/"; String username = "guest"; String password = "guest"; LocalizedQueueConnectionFactory lqcf = new LocalizedQueueConnectionFactory(mockCF("localhost:1234", null), addresses, adminUris, nodes, vhost, username, password, false, null); lqcf.getTargetConnectionFactory("[foo, bar]"); } catch (IllegalArgumentException e) { assertThat(e.getMessage(), containsString("Cannot use LocalizedQueueConnectionFactory with more than one queue: [foo, bar]")); } }
@Before public void setup() { this.defaultConnectionFactory = new CachingConnectionFactory("localhost"); String[] addresses = new String[] { "localhost:9999", "localhost:5672" }; String[] adminUris = new String[] { "http://localhost:15672", "http://localhost:15672" }; String[] nodes = new String[] { "foo@bar", "rabbit@localhost" }; String vhost = "/"; String username = "guest"; String password = "guest"; this.lqcf = new LocalizedQueueConnectionFactory(defaultConnectionFactory, addresses, adminUris, nodes, vhost, username, password, false, null); }
@After public void tearDown() throws Exception { this.lqcf.destroy(); this.defaultConnectionFactory.destroy(); }
@Test public void testConnect() throws Exception { RabbitAdmin admin = new RabbitAdmin(this.lqcf); Queue queue = new Queue(UUID.randomUUID().toString(), false, false, true); admin.declareQueue(queue); ConnectionFactory targetConnectionFactory = this.lqcf.getTargetConnectionFactory("[" + queue.getName() + "]"); RabbitTemplate template = new RabbitTemplate(targetConnectionFactory); template.convertAndSend("", queue.getName(), "foo"); assertEquals("foo", template.receiveAndConvert(queue.getName())); admin.deleteQueue(queue.getName()); }
Client client = createClient(adminUri, this.username, this.password); QueueInfo queueInfo = client.getQueue(this.vhost, queue); if (queueInfo != null) { String uri = this.nodeToAddress.get(node); if (uri != null) { return nodeConnectionFactory(queue, node, uri);
@Override public void onInit() throws Exception { super.onInit(); if (this.clustered) { String[] addresses = StringUtils.commaDelimitedListToStringArray(this.rabbitProperties.getAddresses()); Assert.state(addresses.length == this.adminAddresses.length && addresses.length == this.nodes.length, "'addresses', 'adminAddresses', and 'nodes' properties must have equal length"); this.connectionFactory = new LocalizedQueueConnectionFactory(this.connectionFactory, addresses, this.adminAddresses, this.nodes, this.rabbitProperties.getVirtualHost(), this.rabbitProperties.getUsername(), this.rabbitProperties.getPassword(), this.rabbitProperties.getSsl().isEnabled(), this.rabbitProperties.getSsl().getKeyStore(), this.rabbitProperties.getSsl().getTrustStore(), this.rabbitProperties.getSsl().getKeyStorePassword(), this.rabbitProperties.getSsl().getTrustStorePassword()); this.destroyConnectionFactory = true; } }
@Override public void onInit() throws Exception { super.onInit(); if (this.clustered) { String[] addresses = StringUtils.commaDelimitedListToStringArray(this.rabbitProperties.getAddresses()); Assert.state(addresses.length == this.adminAddresses.length && addresses.length == this.nodes.length, "'addresses', 'adminAddresses', and 'nodes' properties must have equal length"); this.connectionFactory = new LocalizedQueueConnectionFactory(this.connectionFactory, addresses, this.adminAddresses, this.nodes, this.rabbitProperties.getVirtualHost(), this.rabbitProperties.getUsername(), this.rabbitProperties.getPassword(), this.rabbitProperties.getSsl().isEnabled(), this.rabbitProperties.getSsl().getKeyStore(), this.rabbitProperties.getSsl().getTrustStore(), this.rabbitProperties.getSsl().getKeyStorePassword(), this.rabbitProperties.getSsl().getTrustStorePassword()); this.destroyConnectionFactory = true; } }
/** * @param defaultConnectionFactory the fallback connection factory to use if the queue can't be located. * @param addresses the rabbitmq server addresses (host:port, ...). * @param adminUris the rabbitmq admin addresses (http://host:port, ...). * @param nodes the rabbitmq nodes corresponding to addresses (rabbit@server1, ...) must be the same length * as addresses. * @param vhost the virtual host. * @param username the user name. * @param password the password. * @param useSSL use SSL. * @param keyStore the key store resource (e.g. "file:/foo/keystore"). * @param trustStore the trust store resource (e.g. "file:/foo/truststore"). * @param keyStorePassPhrase the pass phrase for the key store. * @param trustStorePassPhrase the pass phrase for the trust store. */ public LocalizedQueueConnectionFactory(ConnectionFactory defaultConnectionFactory, String[] addresses, String[] adminUris, String[] nodes, String vhost, String username, String password, boolean useSSL, String keyStore, String trustStore, String keyStorePassPhrase, String trustStorePassPhrase) { this(defaultConnectionFactory, adminUris, nodesAddressesToMap(nodes, addresses), vhost, username, password, useSSL, null, keyStore, trustStore, keyStorePassPhrase, trustStorePassPhrase); }
@Override public ConnectionFactory getTargetConnectionFactory(Object key) { String queue = ((String) key); queue = queue.substring(1, queue.length() - 1); Assert.isTrue(!queue.contains(","), () -> "Cannot use LocalizedQueueConnectionFactory with more than one queue: " + key); ConnectionFactory connectionFactory = determineConnectionFactory(queue); if (connectionFactory == null) { return this.defaultConnectionFactory; } else { return connectionFactory; } }
private synchronized ConnectionFactory nodeConnectionFactory(String queue, String node, String address) { if (this.logger.isInfoEnabled()) { this.logger.info("Queue: " + queue + " is on node: " + node + " at: " + address); } ConnectionFactory cf = this.nodeFactories.get(node); if (cf == null) { cf = createConnectionFactory(address, node); if (this.logger.isInfoEnabled()) { this.logger.info("Created new connection factory: " + cf); } this.nodeFactories.put(node, cf); } return cf; }
mockCFs.put(rabbit1, mockCF(rabbit1, latch1)); mockCFs.put(rabbit2, mockCF(rabbit2, latch2)); LocalizedQueueConnectionFactory lqcf = new LocalizedQueueConnectionFactory(defaultConnectionFactory, addresses, adminUris, nodes, vhost, username, password, false, null) {
/** * @param defaultConnectionFactory the fallback connection factory to use if the queue * can't be located. * @param addresses the rabbitmq server addresses (host:port, ...). * @param adminUris the rabbitmq admin addresses (http://host:port, ...) * @param nodes the rabbitmq nodes corresponding to addresses (rabbit@server1, ...) * must be the same length as addresses. * @param vhost the virtual host. * @param username the user name. * @param password the password. * @param useSSL use SSL. * @param sslPropertiesLocation the SSL properties location. */ public LocalizedQueueConnectionFactory(ConnectionFactory defaultConnectionFactory, String[] addresses, String[] adminUris, String[] nodes, String vhost, String username, String password, boolean useSSL, @Nullable Resource sslPropertiesLocation) { this(defaultConnectionFactory, adminUris, nodesAddressesToMap(nodes, addresses), vhost, username, password, useSSL, sslPropertiesLocation, null, null, null, null); }
/** * @param defaultConnectionFactory the fallback connection factory to use if the queue * can't be located. * @param addresses the rabbitmq server addresses (host:port, ...). * @param adminUris the rabbitmq admin addresses (http://host:port, ...) * @param nodes the rabbitmq nodes corresponding to addresses (rabbit@server1, ...) * must be the same length as addresses. * @param vhost the virtual host. * @param username the user name. * @param password the password. * @param useSSL use SSL. * @param sslPropertiesLocation the SSL properties location. */ public LocalizedQueueConnectionFactory(ConnectionFactory defaultConnectionFactory, String[] addresses, String[] adminUris, String[] nodes, String vhost, String username, String password, boolean useSSL, @Nullable Resource sslPropertiesLocation) { this(defaultConnectionFactory, adminUris, nodesAddressesToMap(nodes, addresses), vhost, username, password, useSSL, sslPropertiesLocation, null, null, null, null); }