@Override public synchronized void start() { if (!this.running) { if (!this.lazyConnect && this.connectionFactory != null) { try { Connection connection = this.connectionFactory.createConnection(); // NOSONAR (close) if (connection != null) { connection.close(); } } catch (RuntimeException e) { logger.error("Failed to eagerly establish the connection.", e); } } doStart(); this.running = true; } }
private boolean identicalConnectionFactories(ConnectionFactory cf, ConnectionFactory candidateCF) { String host = candidateCF.getHost(); String vhost = candidateCF.getVirtualHost(); int port = candidateCF.getPort(); return cf.getHost().equals(host) && cf.getVirtualHost().equals(vhost) && cf.getPort() == port; } }
@Override protected void onInit() { super.onInit(); if (!this.initialized && this.rabbitTemplate != null) { if (this.connectionFactory != null) { this.connectionFactory.addConnectionListener(this); } } this.initialized = true; }
/** * Create a connection with this connection factory and/or its publisher factory. * @param connectionFactory the connection factory. * @param publisherConnectionIfPossible true to use the publisher factory, if present. * @return the connection. * @since 2.0.2 */ public static Connection createConnection(final ConnectionFactory connectionFactory, final boolean publisherConnectionIfPossible) { if (publisherConnectionIfPossible) { ConnectionFactory publisherFactory = connectionFactory.getPublisherConnectionFactory(); if (publisherFactory != null) { return publisherFactory.createConnection(); } } return connectionFactory.createConnection(); }
@Bean public ConnectionFactory cf2() throws IOException { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); when(connectionFactory.createConnection()).thenReturn(conn2); when(conn2.createChannel(false)).thenReturn(channel2); when(channel2.queueDeclare("foo", true, false, false, null)) .thenReturn(new AMQImpl.Queue.DeclareOk("foo", 0, 0)); doAnswer(invocation -> { listener2 = invocation.getArgument(0); return null; }).when(connectionFactory).addConnectionListener(any(ConnectionListener.class)); return connectionFactory; }
private ConnectionFactory mockCF(final String address, final CountDownLatch latch) throws Exception { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Channel channel = mock(Channel.class); when(connectionFactory.createConnection()).thenReturn(connection); when(connection.createChannel(false)).thenReturn(channel); when(connection.isOpen()).thenReturn(true, false); when(channel.isOpen()).thenReturn(true, false); doAnswer(invocation -> { String tag = UUID.randomUUID().toString(); consumers.put(address, invocation.getArgument(6)); consumerTags.put(address, tag); if (latch != null) { latch.countDown(); } return tag; }).when(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); when(connectionFactory.getHost()).thenReturn(address); this.channels.put(address, channel); return connectionFactory; }
protected String getVirtualHost() { return connectionFactory.getVirtualHost(); }
public String getUrl(ConnectionFactory connectionFactory) { return String.format("amqp://%s/%s", connectionFactory.getHost(), connectionFactory.getVirtualHost()); }
@Test public void testPublisherConfirmsReturnsSetup() { org.springframework.amqp.rabbit.connection.ConnectionFactory cf = mock(org.springframework.amqp.rabbit.connection.ConnectionFactory.class); given(cf.isPublisherConfirms()).willReturn(true); given(cf.isPublisherReturns()).willReturn(true); org.springframework.amqp.rabbit.connection.Connection conn = mock(org.springframework.amqp.rabbit.connection.Connection.class); given(cf.createConnection()).willReturn(conn); PublisherCallbackChannel channel = mock(PublisherCallbackChannel.class); given(conn.createChannel(false)).willReturn(channel); RabbitTemplate template = new RabbitTemplate(cf); template.convertAndSend("foo"); verify(channel).addListener(template); }
@Override public String getHost() { return this.defaultConnectionFactory.getHost(); }
@Override public int getPort() { return this.defaultConnectionFactory.getPort(); }
@Override public void destroy() throws Exception { if (this.connectionFactory != null) { this.connectionFactory.removeConnectionListener(this); this.initialized = false; } }
@Bean public ConnectionFactory cf1() throws IOException { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); when(connectionFactory.createConnection()).thenReturn(conn1); when(conn1.createChannel(false)).thenReturn(channel1); when(channel1.queueDeclare("foo", true, false, false, new HashMap<>())) .thenReturn(new AMQImpl.Queue.DeclareOk("foo", 0, 0)); doAnswer(invocation -> { listener1 = invocation.getArgument(0); return null; }).when(connectionFactory).addConnectionListener(any(ConnectionListener.class)); return connectionFactory; }
/** * Create a connection with this connection factory and/or its publisher factory. * @param connectionFactory the connection factory. * @param publisherConnectionIfPossible true to use the publisher factory, if present. * @return the connection. * @since 2.0.2 */ public static Connection createConnection(final ConnectionFactory connectionFactory, final boolean publisherConnectionIfPossible) { if (publisherConnectionIfPossible) { ConnectionFactory publisherFactory = connectionFactory.getPublisherConnectionFactory(); if (publisherFactory != null) { return publisherFactory.createConnection(); } } return connectionFactory.createConnection(); }
protected String getVirtualHost() { return connectionFactory.getVirtualHost(); }
@Test public void url() { when(this.connectionFactory.getHost()).thenReturn("test-host"); when(this.connectionFactory.getVirtualHost()).thenReturn("test-virtual-host"); assertThat(this.rabbitUtils.getUrl(this.connectionFactory)).isEqualTo("amqp://test-host/test-virtual-host"); }
@Override public String getHost() { return this.defaultConnectionFactory.getHost(); }
@Override public int getPort() { return this.defaultConnectionFactory.getPort(); }
@Override public void destroy() throws Exception { if (this.connectionFactory != null) { this.connectionFactory.removeConnectionListener(this); this.initialized = false; } }