org.springframework.cloud.stream.binder.ExtendedPropertiesBinder Java Examples

The following examples show how to use org.springframework.cloud.stream.binder.ExtendedPropertiesBinder. 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 5 votes vote down vote up
@Test
public void testExtendedProperties() {
	context = new SpringApplicationBuilder(SimpleProcessor.class)
			.web(WebApplicationType.NONE).run("--server.port=0",
					"--spring.cloud.stream.rabbit.default.producer.routing-key-expression=fooRoutingKey",
					"--spring.cloud.stream.rabbit.default.consumer.exchange-type=direct",
					"--spring.cloud.stream.rabbit.bindings.output.producer.batch-size=512",
					"--spring.cloud.stream.rabbit.default.consumer.max-concurrency=4",
					"--spring.cloud.stream.rabbit.bindings.input.consumer.exchange-type=fanout");
	BinderFactory binderFactory = context.getBean(BinderFactory.class);
	Binder<?, ?, ?> rabbitBinder = binderFactory.getBinder(null,
			MessageChannel.class);

	RabbitProducerProperties rabbitProducerProperties = (RabbitProducerProperties) ((ExtendedPropertiesBinder) rabbitBinder)
			.getExtendedProducerProperties("output");

	assertThat(
			rabbitProducerProperties.getRoutingKeyExpression().getExpressionString())
					.isEqualTo("fooRoutingKey");
	assertThat(rabbitProducerProperties.getBatchSize()).isEqualTo(512);

	RabbitConsumerProperties rabbitConsumerProperties = (RabbitConsumerProperties) ((ExtendedPropertiesBinder) rabbitBinder)
			.getExtendedConsumerProperties("input");

	assertThat(rabbitConsumerProperties.getExchangeType())
			.isEqualTo(ExchangeTypes.FANOUT);
	assertThat(rabbitConsumerProperties.getMaxConcurrency()).isEqualTo(4);
}
 
Example #2
Source File: BindingService.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> Binding<T> bindProducer(T output, String outputName, boolean cache) {
	String bindingTarget = this.bindingServiceProperties
			.getBindingDestination(outputName);
	Class<?> outputClass = output.getClass();
	if (output instanceof Advised) {
		outputClass = Stream.of(((Advised) output).getProxiedInterfaces()).filter(c -> !c.getName().contains("org.springframework")).findFirst()
				.orElse(outputClass);
	}
	Binder<T, ?, ProducerProperties> binder = (Binder<T, ?, ProducerProperties>) getBinder(
			outputName, outputClass);
	ProducerProperties producerProperties = this.bindingServiceProperties
			.getProducerProperties(outputName);
	if (binder instanceof ExtendedPropertiesBinder) {
		Object extension = ((ExtendedPropertiesBinder) binder)
				.getExtendedProducerProperties(outputName);
		ExtendedProducerProperties extendedProducerProperties = new ExtendedProducerProperties<>(
				extension);
		BeanUtils.copyProperties(producerProperties, extendedProducerProperties);

		producerProperties = extendedProducerProperties;
	}
	validate(producerProperties);
	Binding<T> binding = doBindProducer(output, bindingTarget, binder,
			producerProperties);
	if (cache) {
		this.producerBindings.put(outputName, binding);
	}
	return binding;
}
 
Example #3
Source File: BindingService.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
public Object getExtendedProducerProperties(Object output, String outputName) {
	Binder binder = getBinder(outputName, output.getClass());
	if (binder instanceof ExtendedPropertiesBinder) {
		return ((ExtendedPropertiesBinder) binder)
				.getExtendedProducerProperties(outputName);
	}
	return null;
}
 
Example #4
Source File: MockExtendedBinderConfiguration.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Bean
public Binder<?, ?, ?> extendedPropertiesBinder() {
	Binder mock = Mockito.mock(Binder.class,
			Mockito.withSettings().defaultAnswer(Mockito.RETURNS_MOCKS)
					.extraInterfaces(ExtendedPropertiesBinder.class));
	ConfigurableEnvironment environment = new StandardEnvironment();
	Map<String, Object> propertiesToAdd = new HashMap<>();
	propertiesToAdd.put("spring.cloud.stream.foo.default.consumer.extendedProperty",
			"someFancyExtension");
	propertiesToAdd.put("spring.cloud.stream.foo.default.producer.extendedProperty",
			"someFancyExtension");
	environment.getPropertySources()
			.addLast(new MapPropertySource("extPropertiesConfig", propertiesToAdd));

	ConfigurableApplicationContext applicationContext = new GenericApplicationContext();
	applicationContext.setEnvironment(environment);

	FooExtendedBindingProperties fooExtendedBindingProperties = new FooExtendedBindingProperties();
	fooExtendedBindingProperties.setApplicationContext(applicationContext);

	final FooConsumerProperties fooConsumerProperties = fooExtendedBindingProperties
			.getExtendedConsumerProperties("input");
	final FooProducerProperties fooProducerProperties = fooExtendedBindingProperties
			.getExtendedProducerProperties("output");

	when(((ExtendedPropertiesBinder) mock).getExtendedConsumerProperties("input"))
			.thenReturn(fooConsumerProperties);

	when(((ExtendedPropertiesBinder) mock).getExtendedProducerProperties("output"))
			.thenReturn(fooProducerProperties);

	return mock;
}
 
Example #5
Source File: KafkaBinderExtendedPropertiesTest.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 4 votes vote down vote up
@Test
public void testKafkaBinderExtendedProperties() throws Exception {

	BinderFactory binderFactory = context.getBeanFactory()
			.getBean(BinderFactory.class);
	Binder<MessageChannel, ? extends ConsumerProperties, ? extends ProducerProperties> kafkaBinder = binderFactory
			.getBinder("kafka", MessageChannel.class);

	KafkaProducerProperties kafkaProducerProperties = (KafkaProducerProperties) ((ExtendedPropertiesBinder) kafkaBinder)
			.getExtendedProducerProperties("standard-out");

	// binding "standard-out" gets FooSerializer defined on the binding itself
	// and BarSerializer through default property.
	assertThat(kafkaProducerProperties.getConfiguration().get("key.serializer"))
			.isEqualTo("FooSerializer.class");
	assertThat(kafkaProducerProperties.getConfiguration().get("value.serializer"))
			.isEqualTo("BarSerializer.class");

	assertThat(kafkaProducerProperties.getConfiguration().get("foo"))
			.isEqualTo("bindingSpecificPropertyShouldWinOverDefault");

	// @checkstyle:off
	KafkaConsumerProperties kafkaConsumerProperties = (KafkaConsumerProperties) ((ExtendedPropertiesBinder) kafkaBinder)
			.getExtendedConsumerProperties("standard-in");
	// @checkstyle:on
	// binding "standard-in" gets FooSerializer defined on the binding itself
	// and BarSerializer through default property.
	assertThat(kafkaConsumerProperties.getConfiguration().get("key.serializer"))
			.isEqualTo("FooSerializer.class");
	assertThat(kafkaConsumerProperties.getConfiguration().get("value.serializer"))
			.isEqualTo("BarSerializer.class");

	// @checkstyle:off
	KafkaProducerProperties customKafkaProducerProperties = (KafkaProducerProperties) ((ExtendedPropertiesBinder) kafkaBinder)
			.getExtendedProducerProperties("custom-out");
	// @checkstyle:on

	// binding "standard-out" gets BarSerializer and BarSerializer for
	// key.serializer/value.serializer through default properties.
	assertThat(customKafkaProducerProperties.getConfiguration().get("key.serializer"))
			.isEqualTo("BarSerializer.class");
	assertThat(
			customKafkaProducerProperties.getConfiguration().get("value.serializer"))
					.isEqualTo("BarSerializer.class");

	// through default properties.
	assertThat(customKafkaProducerProperties.getConfiguration().get("foo"))
			.isEqualTo("bar");

	// @checkstyle:off
	KafkaConsumerProperties customKafkaConsumerProperties = (KafkaConsumerProperties) ((ExtendedPropertiesBinder) kafkaBinder)
			.getExtendedConsumerProperties("custom-in");
	// @checkstyle:on
	// binding "standard-in" gets BarSerializer and BarSerializer for
	// key.serializer/value.serializer through default properties.
	assertThat(customKafkaConsumerProperties.getConfiguration().get("key.serializer"))
			.isEqualTo("BarSerializer.class");
	assertThat(
			customKafkaConsumerProperties.getConfiguration().get("value.serializer"))
					.isEqualTo("BarSerializer.class");

	assertThat(kafkaConsumerProperties.isAckEachRecord()).isEqualTo(true);
	assertThat(customKafkaConsumerProperties.isAckEachRecord()).isEqualTo(false);

	RebalanceListener rebalanceListener = context.getBean(RebalanceListener.class);
	assertThat(rebalanceListener.latch.await(10, TimeUnit.SECONDS)).isTrue();
	assertThat(rebalanceListener.bindings.keySet()).contains("standard-in",
			"custom-in");
	assertThat(rebalanceListener.bindings.values()).containsExactly(Boolean.TRUE,
			Boolean.TRUE);
}