Java Code Examples for org.springframework.cloud.stream.config.BindingServiceProperties#getBindings()

The following examples show how to use org.springframework.cloud.stream.config.BindingServiceProperties#getBindings() . 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: BindingServiceTests.java    From spring-cloud-stream with Apache License 2.0 4 votes vote down vote up
@Test
public void testDefaultPropertyBehavior() {
	ConfigurableApplicationContext run = SpringApplication.run(
			DefaultConsumerPropertiesTestSink.class,
			"--server.port=0",
			"--spring.cloud.stream.default.contentType=text/plain",
			"--spring.cloud.stream.bindings.input1.contentType=application/json",
			"--spring.cloud.stream.default.group=foo",
			"--spring.cloud.stream.bindings.input2.group=bar",
			"--spring.cloud.stream.default.consumer.concurrency=5",
			"--spring.cloud.stream.bindings.input2.consumer.concurrency=1",
			"--spring.cloud.stream.bindings.input1.consumer.partitioned=true",
			"--spring.cloud.stream.default.producer.partitionCount=10",
			"--spring.cloud.stream.bindings.output2.producer.partitionCount=1",
			"--spring.cloud.stream.bindings.inputXyz.contentType=application/json",
			"--spring.cloud.stream.bindings.inputFooBar.contentType=application/avro",
			"--spring.cloud.stream.bindings.input_snake_case.contentType=application/avro");

	BindingServiceProperties bindingServiceProperties = run.getBeanFactory()
			.getBean(BindingServiceProperties.class);
	Map<String, BindingProperties> bindings = bindingServiceProperties.getBindings();

	assertThat(bindings.get("input1").getContentType()).isEqualTo("application/json");
	assertThat(bindings.get("input2").getContentType()).isEqualTo("text/plain");
	assertThat(bindings.get("input1").getGroup()).isEqualTo("foo");
	assertThat(bindings.get("input2").getGroup()).isEqualTo("bar");
	assertThat(bindings.get("input1").getConsumer().getConcurrency()).isEqualTo(5);
	assertThat(bindings.get("input2").getConsumer().getConcurrency()).isEqualTo(1);
	assertThat(bindings.get("input1").getConsumer().isPartitioned()).isEqualTo(true);
	assertThat(bindings.get("input2").getConsumer().isPartitioned()).isEqualTo(false);
	assertThat(bindings.get("output1").getProducer().getPartitionCount())
			.isEqualTo(10);
	assertThat(bindings.get("output2").getProducer().getPartitionCount())
			.isEqualTo(1);

	assertThat(bindings.get("inputXyz").getContentType())
			.isEqualTo("application/json");
	assertThat(bindings.get("inputFooBar").getContentType())
			.isEqualTo("application/avro");
	assertThat(bindings.get("inputFooBarBuzz").getContentType())
			.isEqualTo("text/plain");
	assertThat(bindings.get("input_snake_case").getContentType())
			.isEqualTo("application/avro");

	run.close();
}
 
Example 2
Source File: FunctionBindingTestUtils.java    From spring-cloud-stream with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("rawtypes")
public static void bind(ConfigurableApplicationContext applicationContext, Object function) {
	try {
		Object targetFunction = function;
		if (function instanceof FunctionRegistration) {
			targetFunction = ((FunctionRegistration) function).getTarget();
		}
		String functionName = targetFunction instanceof Function ? "function" : (targetFunction instanceof Consumer ? "consumer" : "supplier");

		System.setProperty("spring.cloud.function.definition", functionName);
		applicationContext.getBeanFactory().registerSingleton(functionName, function);

		Object actualFunction =  ((FunctionInvocationWrapper) applicationContext
				.getBean(FunctionCatalog.class).lookup(functionName)).getTarget();

		InitializingBean functionBindingRegistrar = applicationContext.getBean("functionBindingRegistrar", InitializingBean.class);
		functionBindingRegistrar.afterPropertiesSet();

		BindableProxyFactory bindingProxy = applicationContext.getBean("&" + functionName + "_binding", BindableProxyFactory.class);
		bindingProxy.afterPropertiesSet();

		InitializingBean functionBinder = applicationContext.getBean("functionInitializer", InitializingBean.class);
		functionBinder.afterPropertiesSet();

		BindingServiceProperties bindingProperties = applicationContext.getBean(BindingServiceProperties.class);
		String inputBindingName = functionName + "-in-0";
		String outputBindingName = functionName + "-out-0";
		Map<String, BindingProperties> bindings = bindingProperties.getBindings();
		BindingProperties inputProperties = bindings.get(inputBindingName);
		BindingProperties outputProperties = bindings.get(outputBindingName);
		ConsumerProperties consumerProperties = inputProperties.getConsumer();
		ProducerProperties producerProperties = outputProperties.getProducer();

		TestChannelBinder binder = applicationContext.getBean(TestChannelBinder.class);
		if (actualFunction instanceof Supplier || actualFunction instanceof Function) {
			Binding<MessageChannel> bindProducer = binder.bindProducer(outputProperties.getDestination(),
					applicationContext.getBean(outputBindingName, MessageChannel.class),
					producerProperties == null ? new ProducerProperties() : producerProperties);
			bindProducer.start();
		}
		if (actualFunction instanceof Consumer || actualFunction instanceof Function) {
			Binding<MessageChannel> bindConsumer = binder.bindConsumer(inputProperties.getDestination(), null,
					applicationContext.getBean(inputBindingName, MessageChannel.class),
					consumerProperties == null ? new ConsumerProperties() : consumerProperties);
			bindConsumer.start();
		}
	}
	catch (Exception e) {
		throw new IllegalStateException("Failed to bind function", e);
	}
	finally {
		System.clearProperty("spring.cloud.function.definition");
	}
}