@Autowired CachingConnectionFactory cachingConnectionFactory; ... cachingConnectionFactory.resetConnection();
/** * Close the connection(s). This will impact any in-process operations. New * connection(s) will be created on demand after this method returns. This might be * used to force a reconnect to the primary broker after failing over to a secondary * broker. */ public void resetConnection() { synchronized (this.connectionMonitor) { if (this.connection.target != null) { this.connection.destroy(); } this.allocatedConnections.forEach(c -> c.destroy()); this.channelHighWaterMarks.values().forEach(count -> count.set(0)); this.connectionHighWaterMark.set(0); } if (this.publisherConnectionFactory != null) { this.publisherConnectionFactory.resetConnection(); } }
/** * Close the connection(s). This will impact any in-process operations. New * connection(s) will be created on demand after this method returns. This might be * used to force a reconnect to the primary broker after failing over to a secondary * broker. */ public void resetConnection() { synchronized (this.connectionMonitor) { if (this.connection.target != null) { this.connection.destroy(); } this.allocatedConnections.forEach(c -> c.destroy()); this.channelHighWaterMarks.values().forEach(count -> count.set(0)); this.connectionHighWaterMark.set(0); } if (this.publisherConnectionFactory != null) { this.publisherConnectionFactory.resetConnection(); } }
/** * Close the underlying shared connection. Use {@link #resetConnection()} to close the * connection while the application is still running. * <p> * As this bean implements DisposableBean, a bean factory will automatically invoke * this on destruction of its cached singletons. * <p> * If called after the context is closed, the connection factory can no longer server * up connections. */ @Override public final void destroy() { super.destroy(); resetConnection(); if (getContextStopped()) { this.stopped = true; if (this.channelsExecutor != null) { this.channelsExecutor.shutdownNow(); } } }
/** * Close the underlying shared connection. Use {@link #resetConnection()} to close the * connection while the application is still running. * <p> * As this bean implements DisposableBean, a bean factory will automatically invoke * this on destruction of its cached singletons. * <p> * If called after the context is closed, the connection factory can no longer server * up connections. */ @Override public final void destroy() { super.destroy(); resetConnection(); if (getContextStopped()) { this.stopped = true; if (this.channelsExecutor != null) { this.channelsExecutor.shutdownNow(); } } }
private void testBrokerNamedQueue(AbstractMessageListenerContainer container, CountDownLatch latch1, CountDownLatch latch2, Queue queue) throws Exception { container.start(); String firstActualName = queue.getActualName(); this.message.set(null); this.template.convertAndSend(firstActualName, "foo"); assertThat(latch1.await(10, TimeUnit.SECONDS)).isTrue(); assertThat(this.message.get().getBody()).isEqualTo("foo".getBytes()); final CountDownLatch newConnectionLatch = new CountDownLatch(2); this.cf.addConnectionListener(c -> newConnectionLatch.countDown()); this.cf.resetConnection(); assertThat(newConnectionLatch.await(10, TimeUnit.SECONDS)).isTrue(); String secondActualName = queue.getActualName(); assertThat(secondActualName).isNotEqualTo(firstActualName); this.message.set(null); this.template.convertAndSend(secondActualName, "bar"); assertThat(latch2.await(10, TimeUnit.SECONDS)).isTrue(); assertThat(this.message.get().getBody()).isEqualTo("bar".getBytes()); container.stop(); }
@Test public void testManualAckWithClosedChannel() throws Exception { final AtomicReference<IllegalStateException> exc = new AtomicReference<>(); final CountDownLatch latch = new CountDownLatch(1); this.container = createContainer((ChannelAwareMessageListener) (m, c) -> { if (exc.get() == null) { ((CachingConnectionFactory) this.template.getConnectionFactory()).resetConnection(); } try { c.basicAck(m.getMessageProperties().getDeliveryTag(), false); } catch (IllegalStateException e) { exc.set(e); } latch.countDown(); }, false, this.queue.getName()); this.container.setAcknowledgeMode(AcknowledgeMode.MANUAL); this.container.afterPropertiesSet(); this.container.start(); this.template.convertAndSend(this.queue.getName(), "foo"); assertTrue(latch.await(10, TimeUnit.SECONDS)); this.container.stop(); assertNotNull(exc.get()); assertThat(exc.get().getMessage(), equalTo("Channel closed; cannot ack/nack")); }