org.springframework.cloud.stream.annotation.EnableBinding Java Examples

The following examples show how to use org.springframework.cloud.stream.annotation.EnableBinding. 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: BindingBeansRegistrar.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@Override
public void registerBeanDefinitions(AnnotationMetadata metadata,
		BeanDefinitionRegistry registry) {
	AnnotationAttributes attrs = AnnotatedElementUtils.getMergedAnnotationAttributes(
			ClassUtils.resolveClassName(metadata.getClassName(), null),
			EnableBinding.class);
	for (Class<?> type : collectClasses(attrs, metadata.getClassName())) {
		if (!registry.containsBeanDefinition(type.getName())) {
			BindingBeanDefinitionRegistryUtils.registerBindingTargetBeanDefinitions(
					type, type.getName(), registry);
			BindingBeanDefinitionRegistryUtils
					.registerBindingTargetsQualifiedBeanDefinitions(ClassUtils
							.resolveClassName(metadata.getClassName(), null), type,
							registry);
		}
	}
}
 
Example #2
Source File: TestChannelBinderConfiguration.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
/**
 * Utility operation to return an array of configuration classes defined in
 * {@link EnableBinding} annotation. Typically used for tests that do not rely on
 * creating an SCSt boot application annotated with {@link EnableBinding}, yet require
 * full {@link Binder} configuration.
 * @param additionalConfigurationClasses config classes to be added to the default
 * config
 * @return an array of configuration classes defined in {@link EnableBinding}
 * annotation
 */
public static Class<?>[] getCompleteConfiguration(
		Class<?>... additionalConfigurationClasses) {
	List<Class<?>> configClasses = new ArrayList<>();
	configClasses.add(TestChannelBinderConfiguration.class);
	Import annotation = AnnotationUtils.getAnnotation(EnableBinding.class,
			Import.class);
	Map<String, Object> annotationAttributes = AnnotationUtils
			.getAnnotationAttributes(annotation);
	configClasses
			.addAll(Arrays.asList((Class<?>[]) annotationAttributes.get("value")));
	configClasses.add(BindingServiceConfiguration.class);
	if (additionalConfigurationClasses != null) {
		configClasses.addAll(Arrays.asList(additionalConfigurationClasses));
	}
	return configClasses.toArray(new Class<?>[] {});
}
 
Example #3
Source File: AutoConfigureMessageVerifierTests.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldConfigureForNoOpWhenMissingImplementation() {
	this.contextRunner
			.withClassLoader(new FilteredClassLoader(org.apache.camel.Message.class,
					org.springframework.messaging.Message.class, JmsTemplate.class,
					KafkaTemplate.class, RabbitTemplate.class, EnableBinding.class))
			.run((context) -> {
				assertThat(context.getBeansOfType(MessageVerifierSender.class))
						.hasSize(1);
				assertThat(context.getBeansOfType(NoOpStubMessages.class)).hasSize(1);
			});
}
 
Example #4
Source File: FunctionConfiguration.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Bean
public InitializingBean functionInitializer(FunctionCatalog functionCatalog, FunctionInspector functionInspector,
		StreamFunctionProperties functionProperties, @Nullable BindableProxyFactory[] bindableProxyFactories,
		BindingServiceProperties serviceProperties, ConfigurableApplicationContext applicationContext,
		FunctionBindingRegistrar bindingHolder, StreamBridge streamBridge) {

	boolean shouldCreateInitializer = applicationContext.containsBean("output")
			|| ObjectUtils.isEmpty(applicationContext.getBeanNamesForAnnotation(EnableBinding.class));

	return shouldCreateInitializer
			? new FunctionToDestinationBinder(functionCatalog, functionProperties,
					serviceProperties, streamBridge)
					: null;
}
 
Example #5
Source File: FunctionConfiguration.java    From spring-cloud-stream with Apache License 2.0 4 votes vote down vote up
@Bean
InitializingBean supplierInitializer(FunctionCatalog functionCatalog, StreamFunctionProperties functionProperties,
		GenericApplicationContext context, BindingServiceProperties serviceProperties,
		@Nullable BindableFunctionProxyFactory[] proxyFactories, StreamBridge streamBridge,
		TaskScheduler taskScheduler) {

	if (!ObjectUtils.isEmpty(context.getBeanNamesForAnnotation(EnableBinding.class)) || proxyFactories == null) {
		return null;
	}

	return new InitializingBean() {

		@Override
		public void afterPropertiesSet() throws Exception {
			for (BindableFunctionProxyFactory proxyFactory : proxyFactories) {
				FunctionInvocationWrapper functionWrapper = functionCatalog.lookup(proxyFactory.getFunctionDefinition());
				if (functionWrapper != null && functionWrapper.isSupplier()) {
					// gather output content types
					List<String> contentTypes = new ArrayList<String>();
					Assert.isTrue(proxyFactory.getOutputs().size() == 1, "Supplier with multiple outputs is not supported at the moment.");
					String outputName  = proxyFactory.getOutputs().iterator().next();

					BindingProperties bindingProperties = serviceProperties.getBindingProperties(outputName);
					ProducerProperties producerProperties = bindingProperties.getProducer();
					if (!(bindingProperties.getProducer() != null && producerProperties.isUseNativeEncoding())) {
						contentTypes.add(bindingProperties.getContentType());
					}

					// obtain function wrapper with proper output content types
					functionWrapper = functionCatalog.lookup(proxyFactory.getFunctionDefinition(), contentTypes.toArray(new String[0]));
					Publisher<Object> beginPublishingTrigger = setupBindingTrigger(context);

					if (!functionProperties.isComposeFrom() && !functionProperties.isComposeTo()) {
						String integrationFlowName = proxyFactory.getFunctionDefinition() + "_integrationflow";
						PollableBean pollable = extractPollableAnnotation(functionProperties, context, proxyFactory);

						Type functionType = functionWrapper.getFunctionType();
						IntegrationFlow integrationFlow = integrationFlowFromProvidedSupplier(new PartitionAwareFunctionWrapper(functionWrapper, context, producerProperties),
								beginPublishingTrigger, pollable, context, taskScheduler, functionType)
								.route(Message.class, message -> {
									if (message.getHeaders().get("spring.cloud.stream.sendto.destination") != null) {
										String destinationName = (String) message.getHeaders().get("spring.cloud.stream.sendto.destination");
										return streamBridge.resolveDestination(destinationName, producerProperties);
										//return dynamicDestinationResolver.resolveDestination(destinationName);
									}
									return outputName;
								}).get();
						IntegrationFlow postProcessedFlow = (IntegrationFlow) context.getAutowireCapableBeanFactory()
								.applyBeanPostProcessorsBeforeInitialization(integrationFlow, integrationFlowName);
						context.registerBean(integrationFlowName, IntegrationFlow.class, () -> postProcessedFlow);
					}
				}
			}
		}
	};
}
 
Example #6
Source File: BindingBeansRegistrar.java    From spring-cloud-stream with Apache License 2.0 4 votes vote down vote up
private Class<?>[] collectClasses(AnnotationAttributes attrs, String className) {
	EnableBinding enableBinding = AnnotationUtils.synthesizeAnnotation(attrs,
			EnableBinding.class, ClassUtils.resolveClassName(className, null));
	return enableBinding.value();
}