Java Code Examples for org.springframework.amqp.rabbit.connection.CachingConnectionFactory#resetConnection()

The following examples show how to use org.springframework.amqp.rabbit.connection.CachingConnectionFactory#resetConnection() . 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: RabbitBinderTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 5 votes vote down vote up
@Test
public void testProducerAckChannel() throws Exception {
	RabbitTestBinder binder = getBinder();
	CachingConnectionFactory ccf = this.rabbitAvailableRule.getResource();
	ccf.setPublisherReturns(true);
	ccf.setPublisherConfirms(true);
	ccf.resetConnection();
	DirectChannel moduleOutputChannel = createBindableChannel("output",
			new BindingProperties());
	ExtendedProducerProperties<RabbitProducerProperties> producerProps = createProducerProperties();
	producerProps.setErrorChannelEnabled(true);
	producerProps.getExtension().setConfirmAckChannel("acksChannel");
	Binding<MessageChannel> producerBinding = binder.bindProducer("acks.0",
			moduleOutputChannel, producerProps);
	final Message<?> message = MessageBuilder.withPayload("acksMessage".getBytes())
			.build();
	final AtomicReference<Message<?>> confirm = new AtomicReference<>();
	final CountDownLatch confirmLatch = new CountDownLatch(1);
	binder.getApplicationContext().getBean("acksChannel", DirectChannel.class)
			.subscribe(m -> {
				confirm.set(m);
				confirmLatch.countDown();
			});
	moduleOutputChannel.send(message);
	assertThat(confirmLatch.await(10, TimeUnit.SECONDS)).isTrue();
	assertThat(confirm.get().getPayload()).isEqualTo("acksMessage".getBytes());
	producerBinding.unbind();
}
 
Example 2
Source File: RabbitBinderModuleTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 4 votes vote down vote up
@Test
public void testParentConnectionFactoryInheritedByDefault() throws Exception {
	context = new SpringApplicationBuilder(SimpleProcessor.class)
			.web(WebApplicationType.NONE).run("--server.port=0",
					"--spring.cloud.stream.rabbit.binder.connection-name-prefix=foo",
					"--spring.cloud.stream.rabbit.bindings.input.consumer.single-active-consumer=true");
	BinderFactory binderFactory = context.getBean(BinderFactory.class);
	Binder<?, ?, ?> binder = binderFactory.getBinder(null, MessageChannel.class);
	assertThat(binder).isInstanceOf(RabbitMessageChannelBinder.class);
	DirectFieldAccessor binderFieldAccessor = new DirectFieldAccessor(binder);
	CachingConnectionFactory binderConnectionFactory = (CachingConnectionFactory) binderFieldAccessor
			.getPropertyValue("connectionFactory");
	assertThat(binderConnectionFactory).isInstanceOf(CachingConnectionFactory.class);
	ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class);
	assertThat(binderConnectionFactory).isSameAs(connectionFactory);

	CompositeHealthContributor bindersHealthIndicator = context
			.getBean("bindersHealthContributor", CompositeHealthContributor.class);

	assertThat(bindersHealthIndicator).isNotNull();

	RabbitHealthIndicator indicator = (RabbitHealthIndicator) bindersHealthIndicator.getContributor("rabbit");
	assertThat(indicator).isNotNull();
	assertThat(indicator.health().getStatus())
			.isEqualTo(Status.UP);

	ConnectionFactory publisherConnectionFactory = binderConnectionFactory
			.getPublisherConnectionFactory();
	assertThat(TestUtils.getPropertyValue(publisherConnectionFactory,
			"connection.target")).isNull();
	DirectChannel checkPf = new DirectChannel();
	Binding<MessageChannel> binding = ((RabbitMessageChannelBinder) binder)
			.bindProducer("checkPF", checkPf,
					new ExtendedProducerProperties<>(new RabbitProducerProperties()));
	checkPf.send(new GenericMessage<>("foo".getBytes()));
	binding.unbind();
	assertThat(TestUtils.getPropertyValue(publisherConnectionFactory,
			"connection.target")).isNotNull();

	CachingConnectionFactory cf = this.context
			.getBean(CachingConnectionFactory.class);
	ConnectionNameStrategy cns = TestUtils.getPropertyValue(cf,
			"connectionNameStrategy", ConnectionNameStrategy.class);
	assertThat(cns.obtainNewConnectionName(cf)).isEqualTo("foo#2");
	new RabbitAdmin(rabbitTestSupport.getResource()).deleteExchange("checkPF");
	checkCustomizedArgs();
	binderConnectionFactory.resetConnection();
	binderConnectionFactory.createConnection();
	checkCustomizedArgs();
}