@Bean public ConnectionFactory connectionFactory() { return new CachingConnectionFactory(brokerRunning.getConnectionFactory()); }
@AfterClass public static void tearDown() { brokerRunning.removeTestQueues(); }
private BrokerRunning(boolean assumeOnline, boolean purge, boolean management, String... queues) { this.assumeOnline = assumeOnline; this.queues = queues; this.purge = purge; this.management = management; setPort(this.defaultPort); }
@After public void tearDown() { brokerIsRunning.deleteExchanges("si.fanout.foo", "si.fanout.channel", "si.fanout.pubSubWithEP"); brokerIsRunning.removeTestQueues(); }
public void isUp() throws IOException, TimeoutException, URISyntaxException { Connection connection = getConnectionFactory().newConnection(); // NOSONAR - closeResources() Channel channel = null; try { channel = createQueues(connection); } finally { closeResources(connection, channel); } }
/** * Delete arbitrary exchanges from the broker. * @param exchanges the exchanges to delete. */ public void deleteExchanges(String... exchanges) { Connection connection = null; // NOSONAR (closeResources()) Channel channel = null; try { connection = getConnection(getConnectionFactory()); connection.setId(generateId() + ".exchangeDelete"); channel = connection.createChannel(); for (String exchange : exchanges) { channel.exchangeDelete(exchange); } } catch (Exception e) { logger.warn("Failed to delete queues", e); } finally { closeResources(connection, channel); } }
connection = getConnection(getConnectionFactory()); channel = createQueues(connection); brokerOnline.put(this.port, false); if (this.assumeOnline) { if (fatal()) { fail("RabbitMQ Broker is required, but not available"); closeResources(connection, channel);
@Test public void testVars() { BrokerRunning brokerRunning = BrokerRunning.isBrokerAndManagementRunning(); brokerRunning.setAdminPassword("foo"); brokerRunning.setAdminUser("bar"); brokerRunning.setHostName("baz"); brokerRunning.setPassword("qux"); brokerRunning.setPort(1234); brokerRunning.setUser("fiz"); assertEquals("http://baz:15672/api/", brokerRunning.getAdminUri()); ConnectionFactory connectionFactory = brokerRunning.getConnectionFactory(); assertEquals("baz", connectionFactory.getHost()); assertEquals(1234, connectionFactory.getPort()); assertEquals("fiz", connectionFactory.getUsername()); assertEquals("qux", connectionFactory.getPassword()); }
@Bean BrokerRunning brokerRunning(final RabbitMqSetupService rabbitmqSetupService) { final BrokerRunning brokerRunning = BrokerRunning.isRunning(); brokerRunning.setHostName(rabbitmqSetupService.getHostname()); brokerRunning.getConnectionFactory().setUsername(rabbitmqSetupService.getUsername()); brokerRunning.getConnectionFactory().setPassword(rabbitmqSetupService.getPassword()); return brokerRunning; }
@Test public void testEnvironmentVars() { Map<String, String> vars = new HashMap<>(); vars.put("RABBITMQ_TEST_ADMIN_PASSWORD", "FOO"); vars.put("RABBITMQ_TEST_ADMIN_URI", "http://foo/bar"); vars.put("RABBITMQ_TEST_ADMIN_USER", "BAR"); vars.put("RABBITMQ_TEST_HOSTNAME", "BAZ"); vars.put("RABBITMQ_TEST_PASSWORD", "QUX"); vars.put("RABBITMQ_TEST_PORT", "2345"); vars.put("RABBITMQ_TEST_USER", "FIZ"); BrokerRunning.setEnvironmentVariableOverrides(vars); BrokerRunning brokerRunning = BrokerRunning.isBrokerAndManagementRunning(); assertEquals("http://foo/bar", brokerRunning.getAdminUri()); ConnectionFactory connectionFactory = brokerRunning.getConnectionFactory(); assertEquals("BAZ", connectionFactory.getHost()); assertEquals(2345, connectionFactory.getPort()); assertEquals("FIZ", connectionFactory.getUsername()); assertEquals("QUX", connectionFactory.getPassword()); DirectFieldAccessor dfa = new DirectFieldAccessor(brokerRunning); assertEquals("BAR", dfa.getPropertyValue("adminUser")); assertEquals("FOO", dfa.getPropertyValue("adminPassword")); BrokerRunning.clearEnvironmentVariableOverrides(); }
if (brokerRunning == null) { if (rabbit.management()) { brokerRunning = BrokerRunning.isBrokerAndManagementRunningWithEmptyQueues(queues); brokerRunning = BrokerRunning.isRunningWithEmptyQueues(queues); brokerRunning.isUp(); brokerRunningHolder.set(brokerRunning); Store store = getStore(context); if (BrokerRunning.fatal()) { throw new IllegalStateException("Required RabbitMQ is not available", e);
private Channel createQueues(Connection connection) throws IOException, MalformedURLException, URISyntaxException { Channel channel; channel = connection.createChannel(); for (String queueName : this.queues) { if (this.purge) { logger.debug("Deleting queue: " + queueName); // Delete completely - gets rid of consumers and bindings as well channel.queueDelete(queueName); } if (isDefaultQueue(queueName)) { // Just for test probe. channel.queueDelete(queueName); } else { channel.queueDeclare(queueName, true, false, false, null); } } brokerOffline.put(this.port, false); if (!this.assumeOnline) { Assume.assumeTrue(brokerOffline.get(this.port)); } if (this.management) { Client client = new Client(getAdminUri(), this.adminUser, this.adminPassword); if (!client.alivenessTest("/")) { throw new BrokerNotAliveException("Aliveness test failed for localhost:15672 guest/quest; " + "management not available"); } } return channel; }
@Test public void testArgumentsQueue() throws Exception { Queue queue = beanFactory.getBean("arguments", Queue.class); assertNotNull(queue); CachingConnectionFactory connectionFactory = new CachingConnectionFactory( brokerIsRunning.getConnectionFactory()); RabbitTemplate template = new RabbitTemplate(connectionFactory); RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory); rabbitAdmin.deleteQueue(queue.getName()); rabbitAdmin.declareQueue(queue); assertEquals(100L, queue.getArguments().get("x-message-ttl")); template.convertAndSend(queue.getName(), "message"); Properties props = rabbitAdmin.getQueueProperties("arguments"); if (props != null) { int n = 0; while (n++ < 200 && (Integer) props.get(RabbitAdmin.QUEUE_MESSAGE_COUNT) > 0) { Thread.sleep(50); props = rabbitAdmin.getQueueProperties("arguments"); } assertThat((Integer) props.get(RabbitAdmin.QUEUE_MESSAGE_COUNT), equalTo(0)); } connectionFactory.destroy(); brokerIsRunning.deleteQueues("arguments"); }
/** * @param queues the queues. * @return a new rule that assumes an existing broker with the management plugin with * the provided queues declared (and emptied if needed).. */ public static BrokerRunning isBrokerAndManagementRunningWithEmptyQueues(String...queues) { return new BrokerRunning(true, false, true, queues); }
@BeforeClass @AfterClass public static void setupAndCleanUp() { brokerIsRunning.deleteQueues("retry.test.queue"); brokerIsRunning.deleteExchanges("retry.test.exchange"); }
@BeforeClass @AfterClass public static void clean() { brokerIsRunning.deleteExchanges("fanoutTest", "directTest", "topicTest", "headersTest", "headersTestMulti"); }
/** * Delete arbitrary queues from the broker. * @param queues the queues to delete. */ public void deleteQueues(String... queues) { Connection connection = null; // NOSONAR (closeResources()) Channel channel = null; try { connection = getConnection(getConnectionFactory()); connection.setId(generateId() + ".queueDelete"); channel = connection.createChannel(); for (String queue : queues) { channel.queueDelete(queue); } } catch (Exception e) { logger.warn("Failed to delete queues", e); } finally { closeResources(connection, channel); } }
connection = getConnection(getConnectionFactory()); channel = createQueues(connection); brokerOnline.put(this.port, false); if (this.assumeOnline) { if (fatal()) { fail("RabbitMQ Broker is required, but not available"); closeResources(connection, channel);
public void isUp() throws IOException, TimeoutException, URISyntaxException { Connection connection = getConnectionFactory().newConnection(); // NOSONAR - closeResources() Channel channel = null; try { channel = createQueues(connection); } finally { closeResources(connection, channel); } }
@Bean BrokerRunning brokerRunning(final RabbitMqSetupService rabbitmqSetupService) { final BrokerRunning brokerRunning = BrokerRunning.isRunning(); brokerRunning.setHostName(rabbitmqSetupService.getHostname()); brokerRunning.getConnectionFactory().setUsername(rabbitmqSetupService.getUsername()); brokerRunning.getConnectionFactory().setPassword(rabbitmqSetupService.getPassword()); return brokerRunning; }