Java Code Examples for org.springframework.cloud.stream.binder.Binding#start()

The following examples show how to use org.springframework.cloud.stream.binder.Binding#start() . 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: BindingsEndpoint.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@WriteOperation
public void changeState(@Selector String name, State state) {
	Binding<?> binding = BindingsEndpoint.this.locateBinding(name);
	if (binding != null) {
		switch (state) {
		case STARTED:
			binding.start();
			break;
		case STOPPED:
			binding.stop();
			break;
		case PAUSED:
			binding.pause();
			break;
		case RESUMED:
			binding.resume();
			break;
		default:
			break;
		}
	}
}
 
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");
	}
}