@Bean(ZipkinAutoConfiguration.SENDER_BEAN_NAME) Sender rabbitSender(CachingConnectionFactory connectionFactory, RabbitProperties config) { return RabbitMQSender.newBuilder() .connectionFactory(connectionFactory.getRabbitConnectionFactory()) .queue(this.queue).addresses(config.determineAddresses()).build(); }
/** * Create a new CachingConnectionFactory given a host name * and port. * @param hostNameArg the host name to connect to * @param port the port number */ public CachingConnectionFactory(@Nullable String hostNameArg, int port) { super(newRabbitConnectionFactory()); String hostname = hostNameArg; if (!StringUtils.hasText(hostname)) { hostname = getDefaultHostName(); } setHost(hostname); setPort(port); this.publisherConnectionFactory = new CachingConnectionFactory(getRabbitConnectionFactory(), true); setPublisherConnectionFactory(this.publisherConnectionFactory); }
/** * Create a new CachingConnectionFactory given a {@link URI}. * @param uri the amqp uri configuring the connection * @since 1.5 */ public CachingConnectionFactory(URI uri) { super(newRabbitConnectionFactory()); setUri(uri); this.publisherConnectionFactory = new CachingConnectionFactory(getRabbitConnectionFactory(), true); setPublisherConnectionFactory(this.publisherConnectionFactory); }
/** * Create a new CachingConnectionFactory given a {@link URI}. * @param uri the amqp uri configuring the connection * @since 1.5 */ public CachingConnectionFactory(URI uri) { super(newRabbitConnectionFactory()); setUri(uri); this.publisherConnectionFactory = new CachingConnectionFactory(getRabbitConnectionFactory(), true); setPublisherConnectionFactory(this.publisherConnectionFactory); }
@Bean Sender rabbitSender(CachingConnectionFactory connectionFactory, RabbitProperties config) { return RabbitMQSender.newBuilder() .connectionFactory(connectionFactory.getRabbitConnectionFactory()) .queue(this.queue) .addresses(config.determineAddresses()) .build(); } }
/** * Create a new CachingConnectionFactory given a host name * and port. * @param hostNameArg the host name to connect to * @param port the port number */ public CachingConnectionFactory(@Nullable String hostNameArg, int port) { super(newRabbitConnectionFactory()); String hostname = hostNameArg; if (!StringUtils.hasText(hostname)) { hostname = getDefaultHostName(); } setHost(hostname); setPort(port); this.publisherConnectionFactory = new CachingConnectionFactory(getRabbitConnectionFactory(), true); setPublisherConnectionFactory(this.publisherConnectionFactory); }
/** * Create a new CachingConnectionFactory for the given target ConnectionFactory. * @param rabbitConnectionFactory the target ConnectionFactory * @param isPublisherFactory true if this is the publisher sub-factory. */ private CachingConnectionFactory(com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory, boolean isPublisherFactory) { super(rabbitConnectionFactory); if (!isPublisherFactory) { if (rabbitConnectionFactory.isAutomaticRecoveryEnabled()) { rabbitConnectionFactory.setAutomaticRecoveryEnabled(false); logger.warn("***\nAutomatic Recovery was Enabled in the provided connection factory;\n" + "while Spring AMQP is generally compatible with this feature, there\n" + "are some corner cases where problems arise. Spring AMQP\n" + "prefers to use its own recovery mechanisms; when this option is true, you may receive\n" + "'AutoRecoverConnectionNotCurrentlyOpenException's until the connection is recovered.\n" + "It has therefore been disabled; if you really wish to enable it, use\n" + "'getRabbitConnectionFactory().setAutomaticRecoveryEnabled(true)',\n" + "but this is discouraged."); } this.publisherConnectionFactory = new CachingConnectionFactory(getRabbitConnectionFactory(), true); setPublisherConnectionFactory(this.publisherConnectionFactory); } else { this.publisherConnectionFactory = null; } }
@Bean(ZipkinAutoConfiguration.SENDER_BEAN_NAME) Sender rabbitSender(CachingConnectionFactory connectionFactory, RabbitProperties config) { return RabbitMQSender.newBuilder() .connectionFactory(connectionFactory.getRabbitConnectionFactory()) .queue(this.queue).addresses(config.determineAddresses()).build(); }
/** * Create a new CachingConnectionFactory for the given target ConnectionFactory. * @param rabbitConnectionFactory the target ConnectionFactory * @param isPublisherFactory true if this is the publisher sub-factory. */ private CachingConnectionFactory(com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory, boolean isPublisherFactory) { super(rabbitConnectionFactory); if (!isPublisherFactory) { if (rabbitConnectionFactory.isAutomaticRecoveryEnabled()) { rabbitConnectionFactory.setAutomaticRecoveryEnabled(false); logger.warn("***\nAutomatic Recovery was Enabled in the provided connection factory;\n" + "while Spring AMQP is generally compatible with this feature, there\n" + "are some corner cases where problems arise. Spring AMQP\n" + "prefers to use its own recovery mechanisms; when this option is true, you may receive\n" + "'AutoRecoverConnectionNotCurrentlyOpenException's until the connection is recovered.\n" + "It has therefore been disabled; if you really wish to enable it, use\n" + "'getRabbitConnectionFactory().setAutomaticRecoveryEnabled(true)',\n" + "but this is discouraged."); } this.publisherConnectionFactory = new CachingConnectionFactory(getRabbitConnectionFactory(), true); setPublisherConnectionFactory(this.publisherConnectionFactory); } else { this.publisherConnectionFactory = null; } }
@After public void close() { if (!this.connectionFactory.getVirtualHost().equals("non-existent")) { this.brokerIsRunning.removeTestQueues(); } assertEquals("bar", connectionFactory.getRabbitConnectionFactory().getClientProperties().get("foo")); connectionFactory.destroy(); }
@Before public void open() { connectionFactory = new CachingConnectionFactory("localhost"); connectionFactory.setPort(BrokerTestUtils.getPort()); connectionFactory.getRabbitConnectionFactory().getClientProperties().put("foo", "bar"); connectionFactory.setConnectionNameStrategy(cf -> CF_INTEGRATION_CONNECTION_NAME); }
@Test(expected = AmqpResourceNotAvailableException.class) public void testChannelMax() { this.connectionFactory.getRabbitConnectionFactory().setRequestedChannelMax(1); Connection connection = this.connectionFactory.createConnection(); connection.createChannel(true); connection.createChannel(false); }
connectionFactory.setChannelCacheSize(3); connectionFactory.getRabbitConnectionFactory().setAutomaticRecoveryEnabled(false); List<Connection> connections = new ArrayList<Connection>(); connections.add(connectionFactory.createConnection());
@Test public void testListenerRecoversFromClosedConnection() throws Exception { ConnectionFactory connectionFactory1 = createConnectionFactory(); RabbitTemplate template = new RabbitTemplate(connectionFactory1); CountDownLatch latch = new CountDownLatch(messageCount); CachingConnectionFactory connectionFactory2 = createConnectionFactory(); // this test closes the underlying connection normally; it won't automatically recover. connectionFactory2.getRabbitConnectionFactory().setAutomaticRecoveryEnabled(false); container = createContainer(queue.getName(), new CloseConnectionListener((ConnectionProxy) connectionFactory2.createConnection(), latch), connectionFactory2); for (int i = 0; i < messageCount; i++) { template.convertAndSend(queue.getName(), i + "foo"); } int timeout = Math.min(4 + messageCount / (4 * concurrentConsumers), 30); logger.debug("Waiting for messages with timeout = " + timeout + " (s)"); boolean waited = latch.await(timeout, TimeUnit.SECONDS); assertTrue("Timed out waiting for message", waited); assertNull(template.receiveAndConvert(queue.getName())); this.container.stop(); ((DisposableBean) connectionFactory1).destroy(); ((DisposableBean) connectionFactory2).destroy(); }
BrokerTestUtils.getPort()); connectionFactory.getRabbitConnectionFactory().setAutomaticRecoveryEnabled(false);
@Test public void testHardErrorAndReconnectNoAuto() throws Exception { this.connectionFactory.getRabbitConnectionFactory().setAutomaticRecoveryEnabled(false); RabbitTemplate template = new RabbitTemplate(connectionFactory); RabbitAdmin admin = new RabbitAdmin(connectionFactory);