org.springframework.cloud.stream.binding.BindingService Java Examples

The following examples show how to use org.springframework.cloud.stream.binding.BindingService. 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: KafkaBinderActuatorTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
@Test
public void testKafkaBinderMetricsWhenNoMicrometer() {
	new ApplicationContextRunner().withUserConfiguration(KafkaMetricsTestConfig.class)
			.withClassLoader(new FilteredClassLoader("io.micrometer.core"))
			.run(context -> {
				assertThat(context.getBeanNamesForType(MeterRegistry.class))
						.isEmpty();
				assertThat(context.getBeanNamesForType(MeterBinder.class)).isEmpty();

				DirectFieldAccessor channelBindingServiceAccessor = new DirectFieldAccessor(
						context.getBean(BindingService.class));
				@SuppressWarnings("unchecked")
				Map<String, List<Binding<MessageChannel>>> consumerBindings =
					(Map<String, List<Binding<MessageChannel>>>) channelBindingServiceAccessor
						.getPropertyValue("consumerBindings");
				assertThat(new DirectFieldAccessor(
						consumerBindings.get("input").get(0)).getPropertyValue(
								"lifecycle.messageListenerContainer.beanName"))
										.isEqualTo("setByCustomizer:input");
				assertThat(new DirectFieldAccessor(
						consumerBindings.get("input").get(0)).getPropertyValue(
								"lifecycle.beanName"))
										.isEqualTo("setByCustomizer:input");
				assertThat(new DirectFieldAccessor(
						consumerBindings.get("source").get(0)).getPropertyValue(
								"lifecycle.beanName"))
										.isEqualTo("setByCustomizer:source");

				@SuppressWarnings("unchecked")
				Map<String, Binding<MessageChannel>> producerBindings =
					(Map<String, Binding<MessageChannel>>) channelBindingServiceAccessor
						.getPropertyValue("producerBindings");

				assertThat(new DirectFieldAccessor(
						producerBindings.get("output")).getPropertyValue(
						"lifecycle.beanName"))
						.isEqualTo("setByCustomizer:output");
			});
}
 
Example #2
Source File: BindingServiceConfiguration.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Bean
// This conditional is intentionally not in an autoconfig (usually a bad idea) because
// it is used to detect a BindingService in the parent context (which we know
// already exists).
@ConditionalOnMissingBean(search = SearchStrategy.CURRENT)
public BindingService bindingService(
		BindingServiceProperties bindingServiceProperties,
		BinderFactory binderFactory, TaskScheduler taskScheduler) {

	return new BindingService(bindingServiceProperties, binderFactory, taskScheduler);
}
 
Example #3
Source File: BindingServiceConfiguration.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Bean
@DependsOn("bindingService")
public OutputBindingLifecycle outputBindingLifecycle(BindingService bindingService,
		Map<String, Bindable> bindables) {

	return new OutputBindingLifecycle(bindingService, bindables);
}
 
Example #4
Source File: BindingServiceConfiguration.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Bean
public BinderAwareChannelResolver binderAwareChannelResolver(
		BindingService bindingService,
		AbstractBindingTargetFactory<? extends MessageChannel> bindingTargetFactory,
		DynamicDestinationsBindable dynamicDestinationsBindable,
		@Nullable BinderAwareChannelResolver.NewDestinationBindingCallback callback) {

	return new BinderAwareChannelResolver(bindingService, bindingTargetFactory,
			dynamicDestinationsBindable, callback);
}
 
Example #5
Source File: BinderAwareChannelResolverTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void propertyPassthrough() {
	Map<String, BindingProperties> bindings = new HashMap<>();
	BindingProperties genericProperties = new BindingProperties();
	genericProperties.setContentType("text/plain");
	bindings.put("foo", genericProperties);
	this.bindingServiceProperties.setBindings(bindings);
	Binder binder = mock(Binder.class);
	Binder binder2 = mock(Binder.class);
	BinderFactory mockBinderFactory = Mockito.mock(BinderFactory.class);
	Binding<MessageChannel> fooBinding = Mockito.mock(Binding.class);
	Binding<MessageChannel> barBinding = Mockito.mock(Binding.class);
	when(binder.bindProducer(matches("foo"), any(DirectChannel.class),
			any(ProducerProperties.class))).thenReturn(fooBinding);
	when(binder2.bindProducer(matches("bar"), any(DirectChannel.class),
			any(ProducerProperties.class))).thenReturn(barBinding);
	when(mockBinderFactory.getBinder(null, DirectWithAttributesChannel.class))
			.thenReturn(binder);
	when(mockBinderFactory.getBinder("someTransport",
			DirectWithAttributesChannel.class)).thenReturn(binder2);
	BindingService bindingService = new BindingService(this.bindingServiceProperties,
			mockBinderFactory);
	BinderAwareChannelResolver resolver = new BinderAwareChannelResolver(
			bindingService, this.bindingTargetFactory,
			new DynamicDestinationsBindable());
	resolver.setBeanFactory(this.context.getBeanFactory());
	SubscribableChannel resolved = (SubscribableChannel) resolver
			.resolveDestination("foo");
	verify(binder).bindProducer(eq("foo"), any(MessageChannel.class),
			any(ProducerProperties.class));
	assertThat(resolved).isSameAs(this.context.getBean("foo"));
	this.context.close();
}
 
Example #6
Source File: RabbitBinderModuleTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testParentConnectionFactoryInheritedByDefaultAndRabbitSettingsPropagated() {
	context = new SpringApplicationBuilder(SimpleProcessor.class)
			.web(WebApplicationType.NONE).run("--server.port=0",
					"--spring.cloud.stream.bindings.source.group=someGroup",
					"--spring.cloud.stream.bindings.input.group=someGroup",
					"--spring.cloud.stream.rabbit.bindings.input.consumer.transacted=true",
					"--spring.cloud.stream.rabbit.bindings.output.producer.transacted=true");
	BinderFactory binderFactory = context.getBean(BinderFactory.class);
	Binder<?, ?, ?> binder = binderFactory.getBinder(null, MessageChannel.class);
	assertThat(binder).isInstanceOf(RabbitMessageChannelBinder.class);
	BindingService bindingService = context.getBean(BindingService.class);
	DirectFieldAccessor channelBindingServiceAccessor = new DirectFieldAccessor(
			bindingService);
	// @checkstyle:off
	Map<String, List<Binding<MessageChannel>>> consumerBindings = (Map<String, List<Binding<MessageChannel>>>) channelBindingServiceAccessor
			.getPropertyValue("consumerBindings");
	// @checkstyle:on
	Binding<MessageChannel> inputBinding = consumerBindings.get("input").get(0);
	assertThat(TestUtils.getPropertyValue(inputBinding, "lifecycle.beanName"))
			.isEqualTo("setByCustomizer:someGroup");
	SimpleMessageListenerContainer container = TestUtils.getPropertyValue(
			inputBinding, "lifecycle.messageListenerContainer",
			SimpleMessageListenerContainer.class);
	assertThat(TestUtils.getPropertyValue(container, "beanName"))
			.isEqualTo("setByCustomizerForQueue:input.someGroup,andGroup:someGroup");
	assertThat(TestUtils.getPropertyValue(container, "transactional", Boolean.class))
			.isTrue();
	Map<String, Binding<MessageChannel>> producerBindings = (Map<String, Binding<MessageChannel>>) TestUtils
			.getPropertyValue(bindingService, "producerBindings");
	Binding<MessageChannel> outputBinding = producerBindings.get("output");
	assertThat(TestUtils.getPropertyValue(outputBinding,
			"lifecycle.amqpTemplate.transactional", Boolean.class)).isTrue();
	assertThat(TestUtils.getPropertyValue(outputBinding, "lifecycle.beanName"))
			.isEqualTo("setByCustomizer:output");
	DirectFieldAccessor binderFieldAccessor = new DirectFieldAccessor(binder);
	ConnectionFactory binderConnectionFactory = (ConnectionFactory) 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);

	CachingConnectionFactory cf = this.context
			.getBean(CachingConnectionFactory.class);
	ConnectionNameStrategy cns = TestUtils.getPropertyValue(cf,
			"connectionNameStrategy", ConnectionNameStrategy.class);
	assertThat(cns.obtainNewConnectionName(cf)).startsWith("rabbitConnectionFactory");
	assertThat(TestUtils.getPropertyValue(consumerBindings.get("source").get(0),
			"target.source.h.advised.targetSource.target.beanName"))
		.isEqualTo("setByCustomizer:someGroup");
}
 
Example #7
Source File: BindingServiceConfiguration.java    From spring-cloud-stream with Apache License 2.0 4 votes vote down vote up
@Bean
@DependsOn("bindingService")
public InputBindingLifecycle inputBindingLifecycle(BindingService bindingService,
		Map<String, Bindable> bindables) {
	return new InputBindingLifecycle(bindingService, bindables);
}