com.rabbitmq.http.client.Client Java Examples

The following examples show how to use com.rabbitmq.http.client.Client. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RabbitBinderModuleTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 6 votes vote down vote up
private void checkCustomizedArgs() throws MalformedURLException, URISyntaxException, InterruptedException {
	Client client = new Client("http://guest:guest@localhost:15672/api");
	List<BindingInfo> bindings = client.getBindingsBySource("/", "input");
	int n = 0;
	while (n++ < 100 && bindings == null || bindings.size() < 1) {
		Thread.sleep(100);
		bindings = client.getBindingsBySource("/", "input");
	}
	assertThat(bindings).isNotNull();
	assertThat(bindings.get(0).getArguments()).contains(entry("added.by", "customizer"));
	ExchangeInfo exchange = client.getExchange("/", "input");
	assertThat(exchange.getArguments()).contains(entry("added.by", "customizer"));
	QueueInfo queue = client.getQueue("/", bindings.get(0).getDestination());
	assertThat(queue.getArguments()).contains(entry("added.by", "customizer"));
	assertThat(queue.getArguments()).contains(entry("x-single-active-consumer", Boolean.TRUE));
}
 
Example #2
Source File: RabbitBindingCleaner.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 6 votes vote down vote up
private List<String> findExchanges(Client client, String vhost, String binderPrefix, String entity) {
	List<ExchangeInfo> exchanges = client.getExchanges(vhost);
	String exchangeNamePrefix = adjustPrefix(AbstractBinder.applyPrefix(binderPrefix, entity));
	List<String> exchangesToRemove = exchanges.stream()
			.filter(e -> e.getName().startsWith(exchangeNamePrefix))
			.map(e -> {
				System.out.println(e.getName());
				List<BindingInfo> bindingsBySource = client.getBindingsBySource(vhost, e.getName());
				return Collections.singletonMap(e.getName(), bindingsBySource);
			})
			.map(bindingsMap -> hasNoForeignBindings(bindingsMap, exchangeNamePrefix))
			.collect(Collectors.toList());
	exchangesToRemove.stream()
			.map(exchange -> client.getExchangeBindingsByDestination(vhost, exchange))
			.forEach(bindings -> {
				if (bindings.size() > 0) {
					throw new RabbitAdminException("Cannot delete exchange "
							+ bindings.get(0).getDestination() + "; it is a destination: " + bindings);
				}
			});
	return exchangesToRemove;
}
 
Example #3
Source File: RabbitBinderTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 5 votes vote down vote up
@Test
public void testConsumerPropertiesWithUserInfrastructureNoBind() throws Exception {
	RabbitAdmin admin = new RabbitAdmin(this.rabbitAvailableRule.getResource());
	Queue queue = new Queue("propsUser1.infra");
	admin.declareQueue(queue);
	DirectExchange exchange = new DirectExchange("propsUser1");
	admin.declareExchange(exchange);
	admin.declareBinding(BindingBuilder.bind(queue).to(exchange).with("foo"));

	RabbitTestBinder binder = getBinder();
	ExtendedConsumerProperties<RabbitConsumerProperties> properties = createConsumerProperties();
	properties.getExtension().setDeclareExchange(false);
	properties.getExtension().setBindQueue(false);

	Binding<MessageChannel> consumerBinding = binder.bindConsumer("propsUser1",
			"infra", createBindableChannel("input", new BindingProperties()),
			properties);
	Lifecycle endpoint = extractEndpoint(consumerBinding);
	SimpleMessageListenerContainer container = TestUtils.getPropertyValue(endpoint,
			"messageListenerContainer", SimpleMessageListenerContainer.class);
	assertThat(TestUtils.getPropertyValue(container, "missingQueuesFatal",
			Boolean.class)).isFalse();
	assertThat(container.isRunning()).isTrue();
	consumerBinding.unbind();
	assertThat(container.isRunning()).isFalse();
	Client client = new Client("http://guest:guest@localhost:15672/api/");
	List<?> bindings = client.getBindingsBySource("/", exchange.getName());
	assertThat(bindings.size()).isEqualTo(1);
}
 
Example #4
Source File: RabbitBindingCleaner.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 5 votes vote down vote up
public Map<String, List<String>> clean(String adminUri, String user, String pw,
		String vhost, String binderPrefix, String entity, boolean isJob) {

	try {
		Client client = new Client(adminUri, user, pw);
		return doClean(client,
				vhost == null ? "/" : vhost,
				binderPrefix == null ? BINDER_PREFIX : binderPrefix, entity, isJob);
	}
	catch (MalformedURLException | URISyntaxException e) {
		throw new RabbitAdminException("Couldn't create a Client", e);
	}
}
 
Example #5
Source File: RabbitBindingCleaner.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 5 votes vote down vote up
private Map<String, List<String>> doClean(Client client,
		String vhost, String binderPrefix, String entity, boolean isJob) {

	LinkedList<String> removedQueues = isJob ? null
			: findStreamQueues(client, vhost, binderPrefix, entity);
	List<String> removedExchanges = findExchanges(client, vhost, binderPrefix, entity);
	// Delete the queues in reverse order to enable re-running after a partial
	// success.
	// The queue search above starts with 0 and terminates on a not found.
	if (removedQueues != null) {
		removedQueues.descendingIterator().forEachRemaining(q -> {
			client.deleteQueue(vhost, q);
			if (logger.isDebugEnabled()) {
				logger.debug("deleted queue: " + q);
			}
		});
	}
	Map<String, List<String>> results = new HashMap<>();
	if (removedQueues.size() > 0) {
		results.put("queues", removedQueues);
	}
	// Fanout exchanges for taps
	removedExchanges.forEach(exchange -> {
		client.deleteExchange(vhost, exchange);
		if (logger.isDebugEnabled()) {
			logger.debug("deleted exchange: " + exchange);
		}
	});
	if (removedExchanges.size() > 0) {
		results.put("exchanges", removedExchanges);
	}
	return results;
}
 
Example #6
Source File: RabbitBindingCleaner.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 5 votes vote down vote up
private LinkedList<String> findStreamQueues(Client client, String vhost, String binderPrefix, String stream) {
	String queueNamePrefix = adjustPrefix(AbstractBinder.applyPrefix(binderPrefix, stream));
	List<QueueInfo> queues = client.getQueues(vhost);
	return queues.stream()
		.filter(q -> q.getName().startsWith(queueNamePrefix))
		.map(q -> checkNoConsumers(q))
		.collect(Collectors.toCollection(LinkedList::new));
}
 
Example #7
Source File: DataSubscriberRabbitMQImpl.java    From kkbinlog with Apache License 2.0 4 votes vote down vote up
public DataSubscriberRabbitMQImpl (ConnectionFactory connectionFactory, Client rabbitHttpClient, RedissonClient redissonClient) throws Exception {
    this.rabbitMQClient = RabbitMQClient.getInstance(connectionFactory);
    this.rabbitHttpClient = rabbitHttpClient;
    this.vhost = this.rabbitMQClient.getConnectionFactory().getVirtualHost();
    this.redissonClient = redissonClient;
}
 
Example #8
Source File: RabbitBinderTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 4 votes vote down vote up
@Test
public void testConsumerPropertiesWithUserInfrastructureCustomExchangeAndRK()
		throws Exception {
	RabbitTestBinder binder = getBinder();
	ExtendedConsumerProperties<RabbitConsumerProperties> properties = createConsumerProperties();
	properties.getExtension().setExchangeType(ExchangeTypes.DIRECT);
	properties.getExtension().setBindingRoutingKey("foo,bar");
	properties.getExtension().setBindingRoutingKeyDelimiter(",");
	properties.getExtension().setQueueNameGroupOnly(true);
	// properties.getExtension().setDelayedExchange(true); // requires delayed message
	// exchange plugin; tested locally

	String group = "infra";
	Binding<MessageChannel> consumerBinding = binder.bindConsumer("propsUser2", group,
			createBindableChannel("input", new BindingProperties()), properties);
	Lifecycle endpoint = extractEndpoint(consumerBinding);
	SimpleMessageListenerContainer container = TestUtils.getPropertyValue(endpoint,
			"messageListenerContainer", SimpleMessageListenerContainer.class);
	assertThat(container.isRunning()).isTrue();
	consumerBinding.unbind();
	assertThat(container.isRunning()).isFalse();
	assertThat(container.getQueueNames()[0]).isEqualTo(group);
	Client client = new Client("http://guest:guest@localhost:15672/api/");
	List<BindingInfo> bindings = client.getBindingsBySource("/", "propsUser2");
	int n = 0;
	while (n++ < 100 && bindings == null || bindings.size() < 1) {
		Thread.sleep(100);
		bindings = client.getBindingsBySource("/", "propsUser2");
	}
	assertThat(bindings.size()).isEqualTo(2);
	assertThat(bindings.get(0).getSource()).isEqualTo("propsUser2");
	assertThat(bindings.get(0).getDestination()).isEqualTo(group);
	assertThat(bindings.get(0).getRoutingKey()).isIn("foo", "bar");
	assertThat(bindings.get(1).getSource()).isEqualTo("propsUser2");
	assertThat(bindings.get(1).getDestination()).isEqualTo(group);
	assertThat(bindings.get(1).getRoutingKey()).isIn("foo", "bar");
	assertThat(bindings.get(1).getRoutingKey()).isNotEqualTo(bindings.get(0).getRoutingKey());

	ExchangeInfo exchange = client.getExchange("/", "propsUser2");
	while (n++ < 100 && exchange == null) {
		Thread.sleep(100);
		exchange = client.getExchange("/", "propsUser2");
	}
	assertThat(exchange.getType()).isEqualTo("direct");
	assertThat(exchange.isDurable()).isEqualTo(true);
	assertThat(exchange.isAutoDelete()).isEqualTo(false);
}
 
Example #9
Source File: RabbitBinderTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 4 votes vote down vote up
@Test
public void testConsumerPropertiesWithHeaderExchanges() throws Exception {
	RabbitTestBinder binder = getBinder();
	ExtendedConsumerProperties<RabbitConsumerProperties> properties = createConsumerProperties();
	properties.getExtension().setExchangeType(ExchangeTypes.HEADERS);
	properties.getExtension().setAutoBindDlq(true);
	properties.getExtension().setDeadLetterExchange(ExchangeTypes.HEADERS);
	properties.getExtension().setDeadLetterExchange("propsHeader.dlx");
	Map<String, String> queueBindingArguments = new HashMap<>();
	queueBindingArguments.put("x-match", "any");
	queueBindingArguments.put("foo", "bar");
	properties.getExtension().setQueueBindingArguments(queueBindingArguments);
	properties.getExtension().setDlqBindingArguments(queueBindingArguments);

	String group = "bindingArgs";
	Binding<MessageChannel> consumerBinding = binder.bindConsumer("propsHeader", group,
			createBindableChannel("input", new BindingProperties()), properties);
	Lifecycle endpoint = extractEndpoint(consumerBinding);
	SimpleMessageListenerContainer container = TestUtils.getPropertyValue(endpoint,
			"messageListenerContainer", SimpleMessageListenerContainer.class);
	assertThat(container.isRunning()).isTrue();
	consumerBinding.unbind();
	assertThat(container.isRunning()).isFalse();
	assertThat(container.getQueueNames()[0]).isEqualTo("propsHeader." + group);
	Client client = new Client("http://guest:guest@localhost:15672/api/");
	List<BindingInfo> bindings = client.getBindingsBySource("/", "propsHeader");
	int n = 0;
	while (n++ < 100 && bindings == null || bindings.size() < 1) {
		Thread.sleep(100);
		bindings = client.getBindingsBySource("/", "propsHeader");
	}
	assertThat(bindings.size()).isEqualTo(1);
	assertThat(bindings.get(0).getSource()).isEqualTo("propsHeader");
	assertThat(bindings.get(0).getDestination()).isEqualTo("propsHeader." + group);
	assertThat(bindings.get(0).getArguments()).hasEntrySatisfying("x-match", v -> assertThat(v).isEqualTo("any"));
	assertThat(bindings.get(0).getArguments()).hasEntrySatisfying("foo", v -> assertThat(v).isEqualTo("bar"));

	bindings = client.getBindingsBySource("/", "propsHeader.dlx");
	n = 0;
	while (n++ < 100 && bindings == null || bindings.size() < 1) {
		Thread.sleep(100);
		bindings = client.getBindingsBySource("/", "propsHeader.dlx");
	}
	assertThat(bindings.size()).isEqualTo(1);
	assertThat(bindings.get(0).getSource()).isEqualTo("propsHeader.dlx");
	assertThat(bindings.get(0).getDestination()).isEqualTo("propsHeader." + group + ".dlq");
	assertThat(bindings.get(0).getArguments()).hasEntrySatisfying("x-match", v -> assertThat(v).isEqualTo("any"));
	assertThat(bindings.get(0).getArguments()).hasEntrySatisfying("foo", v -> assertThat(v).isEqualTo("bar"));
}