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

The following examples show how to use org.springframework.cloud.stream.binder.ConsumerProperties. 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: GlobalKTableBoundElementFactory.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 8 votes vote down vote up
@Override
public GlobalKTable createInput(String name) {
	BindingProperties bindingProperties = this.bindingServiceProperties.getBindingProperties(name);
	ConsumerProperties consumerProperties = bindingProperties.getConsumer();
	if (consumerProperties == null) {
		consumerProperties = this.bindingServiceProperties.getConsumerProperties(name);
		consumerProperties.setUseNativeDecoding(true);
	}
	else {
		if (!encodingDecodingBindAdviceHandler.isDecodingSettingProvided()) {
			consumerProperties.setUseNativeDecoding(true);
		}
	}
	// Always set multiplex to true in the kafka streams binder
	consumerProperties.setMultiplex(true);

	// @checkstyle:off
	GlobalKTableBoundElementFactory.GlobalKTableWrapperHandler wrapper = new GlobalKTableBoundElementFactory.GlobalKTableWrapperHandler();
	// @checkstyle:on
	ProxyFactory proxyFactory = new ProxyFactory(
			GlobalKTableBoundElementFactory.GlobalKTableWrapper.class,
			GlobalKTable.class);
	proxyFactory.addAdvice(wrapper);

	return (GlobalKTable) proxyFactory.getProxy();
}
 
Example #2
Source File: KStreamBoundElementFactory.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
@Override
public KStream createInput(String name) {
	BindingProperties bindingProperties = this.bindingServiceProperties.getBindingProperties(name);
	ConsumerProperties consumerProperties = bindingProperties.getConsumer();
	if (consumerProperties == null) {
		consumerProperties = this.bindingServiceProperties.getConsumerProperties(name);
		consumerProperties.setUseNativeDecoding(true);
	}
	else {
		if (!encodingDecodingBindAdviceHandler.isDecodingSettingProvided()) {
			consumerProperties.setUseNativeDecoding(true);
		}
	}
	// Always set multiplex to true in the kafka streams binder
	consumerProperties.setMultiplex(true);
	return createProxyForKStream(name);
}
 
Example #3
Source File: TestConsumer.java    From spring-cloud-consul with Apache License 2.0 6 votes vote down vote up
@Override
public void run(ApplicationArguments args) throws Exception {
	logger.info("Consumer running with binder {}", this.binder);
	SubscribableChannel consumerChannel = new ExecutorSubscribableChannel();
	consumerChannel.subscribe(new MessageHandler() {
		@Override
		public void handleMessage(Message<?> message) throws MessagingException {
			TestConsumer.this.messagePayload = (String) message.getPayload();
			logger.info("Received message: {}", TestConsumer.this.messagePayload);
		}
	});
	String group = null;

	if (args.containsOption("group")) {
		group = args.getOptionValues("group").get(0);
	}

	this.binder.bindConsumer(ConsulBinderTests.BINDING_NAME, group, consumerChannel,
			new ConsumerProperties());
	this.isBound = true;
}
 
Example #4
Source File: RetryTemplateTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
@Test
public void testSpecificCustomRetryTemplate() throws Exception {
	ApplicationContext context = new SpringApplicationBuilder(
			SpecificCustomRetryTemplateConfiguration.class)
					.web(WebApplicationType.NONE).run("--spring.jmx.enabled=false",
							"--spring.cloud.stream.bindings.input.consumer.retry-template-name=retryTemplateTwo");

	RetryTemplate retryTemplateTwo = context.getBean("retryTemplateTwo",
			RetryTemplate.class);
	BindingServiceProperties bindingServiceProperties = context
			.getBean(BindingServiceProperties.class);
	ConsumerProperties consumerProperties = bindingServiceProperties
			.getConsumerProperties("input");
	AbstractBinder binder = context.getBean(AbstractBinder.class);

	Method m = AbstractBinder.class.getDeclaredMethod("buildRetryTemplate",
			ConsumerProperties.class);
	m.setAccessible(true);
	RetryTemplate retryTemplate = (RetryTemplate) m.invoke(binder,
			consumerProperties);
	assertThat(retryTemplate).isEqualTo(retryTemplateTwo);
}
 
Example #5
Source File: BindingServiceTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@Test
public void testConsumerPropertiesValidation() {
	BindingServiceProperties serviceProperties = new BindingServiceProperties();
	Map<String, BindingProperties> bindingProperties = new HashMap<>();
	BindingProperties props = new BindingProperties();
	ConsumerProperties consumerProperties = new ConsumerProperties();
	consumerProperties.setConcurrency(0);
	props.setDestination("foo");
	props.setConsumer(consumerProperties);
	final String inputChannelName = "input";
	bindingProperties.put(inputChannelName, props);
	serviceProperties.setBindings(bindingProperties);
	DefaultBinderFactory binderFactory = createMockBinderFactory();
	BindingService service = new BindingService(serviceProperties, binderFactory);
	MessageChannel inputChannel = new DirectChannel();
	try {
		service.bindConsumer(inputChannel, inputChannelName);
		fail("Consumer properties should be validated.");
	}
	catch (IllegalStateException e) {
		assertThat(e)
				.hasMessageContaining("Concurrency should be greater than zero.");
	}
}
 
Example #6
Source File: BindingService.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
public <T> void reschedulePollableConsumerBinding(final T input,
		final String inputName, final Binder<T, ConsumerProperties, ?> binder,
		final ConsumerProperties consumerProperties, final String target,
		final LateBinding<T> late, RuntimeException exception) {
	assertNotIllegalException(exception);
	this.log.error("Failed to create consumer binding; retrying in "
			+ this.bindingServiceProperties.getBindingRetryInterval() + " seconds",
			exception);
	this.scheduleTask(() -> {
		try {
			late.setDelegate(((PollableConsumerBinder) binder).bindPollableConsumer(
					target, this.bindingServiceProperties.getGroup(inputName),
					(PollableSource) input, consumerProperties));
		}
		catch (RuntimeException e) {
			reschedulePollableConsumerBinding(input, inputName, binder,
					consumerProperties, target, late, e);
		}
	});
}
 
Example #7
Source File: BindingService.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
public <T> Binding<T> doBindPollableConsumer(T input, String inputName,
		Binder<T, ConsumerProperties, ?> binder,
		ConsumerProperties consumerProperties, String target) {
	if (this.taskScheduler == null
			|| this.bindingServiceProperties.getBindingRetryInterval() <= 0) {
		return ((PollableConsumerBinder) binder).bindPollableConsumer(target,
				this.bindingServiceProperties.getGroup(inputName),
				(PollableSource) input, consumerProperties);
	}
	else {
		try {
			return ((PollableConsumerBinder) binder).bindPollableConsumer(target,
					this.bindingServiceProperties.getGroup(inputName),
					(PollableSource) input, consumerProperties);
		}
		catch (RuntimeException e) {
			LateBinding<T> late = new LateBinding<T>(target,
					e.getCause() == null ? e.toString() : e.getCause().getMessage(), consumerProperties, true);
			reschedulePollableConsumerBinding(input, inputName, binder,
					consumerProperties, target, late, e);
			return late;
		}
	}
}
 
Example #8
Source File: BindingService.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
public <T> void rescheduleConsumerBinding(final T input, final String inputName,
		final Binder<T, ConsumerProperties, ?> binder,
		final ConsumerProperties consumerProperties, final String target,
		final LateBinding<T> late, RuntimeException exception) {
	assertNotIllegalException(exception);
	this.log.error("Failed to create consumer binding; retrying in "
			+ this.bindingServiceProperties.getBindingRetryInterval() + " seconds",
			exception);
	this.scheduleTask(() -> {
		try {
			late.setDelegate(binder.bindConsumer(target,
					this.bindingServiceProperties.getGroup(inputName), input,
					consumerProperties));
		}
		catch (RuntimeException e) {
			rescheduleConsumerBinding(input, inputName, binder, consumerProperties,
					target, late, e);
		}
	});
}
 
Example #9
Source File: BindingService.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
public <T> Binding<T> doBindConsumer(T input, String inputName,
		Binder<T, ConsumerProperties, ?> binder,
		ConsumerProperties consumerProperties, String target) {
	if (this.taskScheduler == null
			|| this.bindingServiceProperties.getBindingRetryInterval() <= 0) {
		return binder.bindConsumer(target,
				this.bindingServiceProperties.getGroup(inputName), input,
				consumerProperties);
	}
	else {
		try {
			return binder.bindConsumer(target,
					this.bindingServiceProperties.getGroup(inputName), input,
					consumerProperties);
		}
		catch (RuntimeException e) {
			LateBinding<T> late = new LateBinding<T>(target,
					e.getCause() == null ? e.toString() : e.getCause().getMessage(), consumerProperties, true);
			rescheduleConsumerBinding(input, inputName, binder, consumerProperties,
					target, late, e);
			this.consumerBindings.put(inputName, Collections.singletonList(late));
			return late;
		}
	}
}
 
Example #10
Source File: KeyValueSerdeResolver.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
public Serde<?> getInboundValueSerde(ConsumerProperties consumerProperties,
									KafkaStreamsConsumerProperties extendedConsumerProperties,
									ResolvableType resolvableType) {
	Serde<?> valueSerde;

	String valueSerdeString = extendedConsumerProperties.getValueSerde();
	try {
		if (consumerProperties != null && consumerProperties.isUseNativeDecoding()) {
			valueSerde = getValueSerde(valueSerdeString, resolvableType);
		}
		else {
			valueSerde = Serdes.ByteArray();
		}
	}
	catch (ClassNotFoundException ex) {
		throw new IllegalStateException("Serde class not found: ", ex);
	}
	return valueSerde;
}
 
Example #11
Source File: KeyValueSerdeResolver.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
/**
 * Provide the {@link Serde} for inbound value.
 * @param consumerProperties {@link ConsumerProperties} on binding
 * @param extendedConsumerProperties binding level extended
 * {@link KafkaStreamsConsumerProperties}
 * @return configurd {@link Serde} for the inbound value.
 */
public Serde<?> getInboundValueSerde(ConsumerProperties consumerProperties,
		KafkaStreamsConsumerProperties extendedConsumerProperties) {
	Serde<?> valueSerde;

	String valueSerdeString = extendedConsumerProperties.getValueSerde();
	try {
		if (consumerProperties != null && consumerProperties.isUseNativeDecoding()) {
			valueSerde = getValueSerde(valueSerdeString);
		}
		else {
			valueSerde = Serdes.ByteArray();
		}
	}
	catch (ClassNotFoundException ex) {
		throw new IllegalStateException("Serde class not found: ", ex);
	}
	return valueSerde;
}
 
Example #12
Source File: KTableBoundElementFactory.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
@Override
public KTable createInput(String name) {
	BindingProperties bindingProperties = this.bindingServiceProperties.getBindingProperties(name);
	ConsumerProperties consumerProperties = bindingProperties.getConsumer();
	if (consumerProperties == null) {
		consumerProperties = this.bindingServiceProperties.getConsumerProperties(name);
		consumerProperties.setUseNativeDecoding(true);
	}
	else {
		if (!encodingDecodingBindAdviceHandler.isDecodingSettingProvided()) {
			consumerProperties.setUseNativeDecoding(true);
		}
	}
	// Always set multiplex to true in the kafka streams binder
	consumerProperties.setMultiplex(true);

	KTableBoundElementFactory.KTableWrapperHandler wrapper = new KTableBoundElementFactory.KTableWrapperHandler();
	ProxyFactory proxyFactory = new ProxyFactory(
			KTableBoundElementFactory.KTableWrapper.class, KTable.class);
	proxyFactory.addAdvice(wrapper);

	return (KTable) proxyFactory.getProxy();
}
 
Example #13
Source File: PartitionedConsumerTest.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testBindingPartitionedConsumer() {
	Binder binder = this.binderFactory.getBinder(null, MessageChannel.class);
	ArgumentCaptor<ConsumerProperties> argumentCaptor = ArgumentCaptor
			.forClass(ConsumerProperties.class);
	verify(binder).bindConsumer(eq("partIn"), isNull(), eq(this.testSink.input()),
			argumentCaptor.capture());
	assertThat(argumentCaptor.getValue().getInstanceIndex()).isEqualTo(0);
	assertThat(argumentCaptor.getValue().getInstanceCount()).isEqualTo(2);
	verifyNoMoreInteractions(binder);
}
 
Example #14
Source File: TestChannelBinderConfiguration.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Bean
public Binder<T, ? extends ConsumerProperties, ? extends ProducerProperties> springIntegrationChannelBinder(
		TestChannelBinderProvisioner provisioner) {
	return (Binder<T, ? extends ConsumerProperties, ? extends ProducerProperties>) new TestChannelBinder(
			provisioner);
}
 
Example #15
Source File: TestChannelBinder.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Override
protected MessageProducer createConsumerEndpoint(ConsumerDestination destination,
		String group, ConsumerProperties properties) throws Exception {
	ErrorMessageStrategy errorMessageStrategy = new DefaultErrorMessageStrategy();
	SubscribableChannel siBinderInputChannel = ((SpringIntegrationConsumerDestination) destination)
			.getChannel();

	IntegrationMessageListeningContainer messageListenerContainer = new IntegrationMessageListeningContainer();
	IntegrationBinderInboundChannelAdapter adapter = new IntegrationBinderInboundChannelAdapter(
			messageListenerContainer);

	String groupName = StringUtils.hasText(group) ? group : "anonymous";
	ErrorInfrastructure errorInfrastructure = registerErrorInfrastructure(destination,
			groupName, properties);
	if (properties.getMaxAttempts() > 1) {
		adapter.setRetryTemplate(buildRetryTemplate(properties));
		adapter.setRecoveryCallback(errorInfrastructure.getRecoverer());
	}
	else {
		adapter.setErrorMessageStrategy(errorMessageStrategy);
		adapter.setErrorChannel(errorInfrastructure.getErrorChannel());
	}

	siBinderInputChannel.subscribe(messageListenerContainer);

	return adapter;
}
 
Example #16
Source File: TestChannelBinder.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Override
protected PolledConsumerResources createPolledConsumerResources(String name,
		String group, ConsumerDestination destination,
		ConsumerProperties consumerProperties) {
	return new PolledConsumerResources(this.messageSourceDelegate,
			registerErrorInfrastructure(destination, group, consumerProperties));
}
 
Example #17
Source File: BindingServiceTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testExplicitGroup() throws Exception {
	BindingServiceProperties properties = new BindingServiceProperties();
	Map<String, BindingProperties> bindingProperties = new HashMap<>();
	BindingProperties props = new BindingProperties();
	props.setDestination("foo");
	props.setGroup("fooGroup");
	final String inputChannelName = "input";
	bindingProperties.put(inputChannelName, props);
	properties.setBindings(bindingProperties);
	DefaultBinderFactory binderFactory = createMockBinderFactory();
	Binder binder = binderFactory.getBinder("mock", MessageChannel.class);
	BindingService service = new BindingService(properties, binderFactory);
	MessageChannel inputChannel = new DirectChannel();
	Binding<MessageChannel> mockBinding = Mockito.mock(Binding.class);
	when(binder.bindConsumer(eq("foo"), eq("fooGroup"), same(inputChannel),
			any(ConsumerProperties.class))).thenReturn(mockBinding);
	Collection<Binding<MessageChannel>> bindings = service.bindConsumer(inputChannel,
			inputChannelName);
	assertThat(bindings).hasSize(1);
	Binding<MessageChannel> binding = bindings.iterator().next();
	assertThat(binding).isSameAs(mockBinding);

	service.unbindConsumers(inputChannelName);
	verify(binder).bindConsumer(eq("foo"), eq(props.getGroup()), same(inputChannel),
			any(ConsumerProperties.class));
	verify(binding).unbind();
	binderFactory.destroy();
}
 
Example #18
Source File: TestChannelBinder.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Override
protected MessageHandler getErrorMessageHandler(ConsumerDestination destination,
		String group, ConsumerProperties consumerProperties) {
	return m -> {
		this.logger.debug("Error handled: " + m);
		this.lastError = m;
	};
}
 
Example #19
Source File: TestChannelBinderProvisioner.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
/**
 * Will provision consumer destination as SI {@link DirectChannel}.
 */
@Override
public ConsumerDestination provisionConsumerDestination(String name, String group,
		ConsumerProperties properties) throws ProvisioningException {
	SubscribableChannel destination = this.provisionDestination(name, false);
	if (this.source != null) {
		this.source.setChannel(destination);
	}
	return new SpringIntegrationConsumerDestination(name, destination);
}
 
Example #20
Source File: BindingServiceTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testDefaultGroup() throws Exception {
	BindingServiceProperties properties = new BindingServiceProperties();
	Map<String, BindingProperties> bindingProperties = new HashMap<>();
	BindingProperties props = new BindingProperties();
	props.setDestination("foo");
	final String inputChannelName = "input";
	bindingProperties.put(inputChannelName, props);
	properties.setBindings(bindingProperties);
	DefaultBinderFactory binderFactory = createMockBinderFactory();
	Binder binder = binderFactory.getBinder("mock", MessageChannel.class);
	BindingService service = new BindingService(properties, binderFactory);
	MessageChannel inputChannel = new DirectChannel();
	Binding<MessageChannel> mockBinding = Mockito.mock(Binding.class);
	when(binder.bindConsumer(eq("foo"), isNull(), same(inputChannel),
			any(ConsumerProperties.class))).thenReturn(mockBinding);
	Collection<Binding<MessageChannel>> bindings = service.bindConsumer(inputChannel,
			inputChannelName);
	assertThat(bindings).hasSize(1);
	Binding<MessageChannel> binding = bindings.iterator().next();
	assertThat(binding).isSameAs(mockBinding);
	service.unbindConsumers(inputChannelName);
	verify(binder).bindConsumer(eq("foo"), isNull(), same(inputChannel),
			any(ConsumerProperties.class));
	verify(binding).unbind();
	binderFactory.destroy();
}
 
Example #21
Source File: TestSupportBinderAutoConfiguration.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Bean
@SuppressWarnings("unchecked")
public BinderFactory binderFactory(final Binder<MessageChannel, ?, ?> binder) {
	return new BinderFactory() {
		@Override
		public <T> Binder<T, ? extends ConsumerProperties, ? extends ProducerProperties> getBinder(
				String configurationName, Class<? extends T> bindableType) {
			return (Binder<T, ? extends ConsumerProperties, ? extends ProducerProperties>) binder;
		}
	};
}
 
Example #22
Source File: MessageConverterConfigurer.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
private boolean isNativeEncodingNotSet(ProducerProperties producerProperties,
		ConsumerProperties consumerProperties, boolean input) {
	if (input) {
		return consumerProperties == null
				|| !consumerProperties.isUseNativeDecoding();
	}
	else {
		return producerProperties == null
				|| !producerProperties.isUseNativeEncoding();
	}
}
 
Example #23
Source File: MessageConverterConfigurer.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
/**
 * Setup data-type and message converters for the given message channel.
 * @param channel message channel to set the data-type and message converters
 * @param channelName the channel name
 * @param inbound inbound (i.e., "input") or outbound channel
 */
private void configureMessageChannel(MessageChannel channel, String channelName,
		boolean inbound) {
	Assert.isAssignable(AbstractMessageChannel.class, channel.getClass());
	AbstractMessageChannel messageChannel = (AbstractMessageChannel) channel;
	BindingProperties bindingProperties = this.bindingServiceProperties
			.getBindingProperties(channelName);
	String contentType = bindingProperties.getContentType();
	ProducerProperties producerProperties = bindingProperties.getProducer();
	boolean partitioned = !inbound && producerProperties != null && producerProperties.isPartitioned();
	boolean functional = streamFunctionProperties != null
			&& (StringUtils.hasText(streamFunctionProperties.getDefinition()) || StringUtils.hasText(bindingServiceProperties.getSource()));

	if (partitioned) {
		if (inbound || !functional) {
			messageChannel.addInterceptor(new PartitioningInterceptor(bindingProperties));
		}
	}

	ConsumerProperties consumerProperties = bindingProperties.getConsumer();
	if (this.isNativeEncodingNotSet(producerProperties, consumerProperties, inbound)) {
		if (inbound) {
			messageChannel.addInterceptor(
					new InboundContentTypeEnhancingInterceptor(contentType));
		}
		else {
			messageChannel.addInterceptor(
					new OutboundContentTypeConvertingInterceptor(contentType,
							this.compositeMessageConverter));
		}
	}
}
 
Example #24
Source File: MessageConverterConfigurer.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Override
public void configurePolledMessageSource(PollableMessageSource binding, String name) {
	BindingProperties bindingProperties = this.bindingServiceProperties
			.getBindingProperties(name);
	String contentType = bindingProperties.getContentType();
	ConsumerProperties consumerProperties = bindingProperties.getConsumer();
	if ((consumerProperties == null || !consumerProperties.isUseNativeDecoding())
			&& binding instanceof DefaultPollableMessageSource) {
		((DefaultPollableMessageSource) binding).addInterceptor(
				new InboundContentTypeEnhancingInterceptor(contentType));
	}
}
 
Example #25
Source File: KafkaStreamsBindingInformationCatalogue.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
/**
 * Is native decoding is enabled on this {@link KStream}.
 * @param bindingTarget binding target for KStream
 * @return true if native decoding is enabled, fasle otherwise.
 */
boolean isUseNativeDecoding(KStream<?, ?> bindingTarget) {
	BindingProperties bindingProperties = this.bindingProperties.get(bindingTarget);
	if (bindingProperties.getConsumer() == null) {
		bindingProperties.setConsumer(new ConsumerProperties());
	}
	return bindingProperties.getConsumer().isUseNativeDecoding();
}
 
Example #26
Source File: ConsulBinder.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
@Override
protected Binding<MessageChannel> doBindConsumer(String name, String group,
		MessageChannel inputChannel, ConsumerProperties properties) {
	ConsulInboundMessageProducer messageProducer = new ConsulInboundMessageProducer(
			this.eventService);
	messageProducer.setOutputChannel(inputChannel);
	messageProducer.setBeanFactory(this.getBeanFactory());
	messageProducer.afterPropertiesSet();
	messageProducer.start();

	return new DefaultBinding<>(name, group, inputChannel, messageProducer);
}
 
Example #27
Source File: FunctionConfiguration.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
FunctionWrapper(Function function, ConsumerProperties consumerProperties,
		ProducerProperties producerProperties, ConfigurableApplicationContext applicationContext) {

	isRoutingFunction = ((FunctionInvocationWrapper) function).getTarget() instanceof RoutingFunction;
	this.applicationContext = applicationContext;
	this.function = new PartitionAwareFunctionWrapper((FunctionInvocationWrapper) function, this.applicationContext, producerProperties);
	this.consumerProperties = consumerProperties;
	this.producerProperties = producerProperties;
	this.headersField = ReflectionUtils.findField(MessageHeaders.class, "headers");
	this.headersField.setAccessible(true);
}
 
Example #28
Source File: ChannelBindingServiceProperties.java    From gem with MIT License 4 votes vote down vote up
public ConsumerProperties getConsumerProperties(String inputChannelName) {
	return bindingServiceProperties.getConsumerProperties(inputChannelName);
}
 
Example #29
Source File: TestSupportBinder.java    From spring-cloud-stream with Apache License 2.0 4 votes vote down vote up
@Override
public Binding<MessageChannel> bindConsumer(String name, String group,
		MessageChannel inboundBindTarget, ConsumerProperties properties) {
	return new TestBinding(inboundBindTarget, null);
}
 
Example #30
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");
	}
}