Java Code Examples for org.springframework.beans.factory.ObjectProvider#ifAvailable()

The following examples show how to use org.springframework.beans.factory.ObjectProvider#ifAvailable() . 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: GcpPubSubAutoConfiguration.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public PublisherFactory defaultPublisherFactory(
		@Qualifier("publisherExecutorProvider") ExecutorProvider executorProvider,
		@Qualifier("publisherBatchSettings") ObjectProvider<BatchingSettings> batchingSettings,
		@Qualifier("publisherRetrySettings") ObjectProvider<RetrySettings> retrySettings,
		TransportChannelProvider transportChannelProvider) {
	DefaultPublisherFactory factory = new DefaultPublisherFactory(this.finalProjectIdProvider);
	factory.setExecutorProvider(executorProvider);
	factory.setCredentialsProvider(this.finalCredentialsProvider);
	factory.setHeaderProvider(this.headerProvider);
	factory.setChannelProvider(transportChannelProvider);
	retrySettings.ifAvailable(factory::setRetrySettings);
	batchingSettings.ifAvailable(factory::setBatchingSettings);
	return factory;
}
 
Example 2
Source File: BatchConfiguration.java    From spring-graalvm-native with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean(JobOperator.class)
public JobOperator jobOperator(ObjectProvider<JobParametersConverter> jobParametersConverter,
		JobExplorer jobExplorer, JobLauncher jobLauncher, ListableJobLocator jobRegistry,
		JobRepository jobRepository) throws Exception {
	System.out.println("FOOBAR");
	SimpleJobOperator factory = new SimpleJobOperator();
	factory.setJobExplorer(jobExplorer);
	factory.setJobLauncher(jobLauncher);
	factory.setJobRegistry(jobRegistry);
	factory.setJobRepository(jobRepository);
	jobParametersConverter.ifAvailable(factory::setJobParametersConverter);
	return factory;
}
 
Example 3
Source File: MultiModuleConfigServiceBootstrapConfiguration.java    From spring-cloud-formula with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean(ConfigServicePropertySourceLocator.class)
@ConditionalOnProperty(value = "spring.cloud.config.enabled", matchIfMissing = true)
public ConfigServicePropertySourceLocator configServicePropertySource(
        ConfigClientProperties client, ObjectProvider<ConfigEnvironmentPreprocessor> preprocessorObjectProvider) {
    MultiModuleConfigServicePropertySourceLocator l = new MultiModuleConfigServicePropertySourceLocator(client);
    preprocessorObjectProvider.ifAvailable(l::setPreprocessor);
    return l;
}
 
Example 4
Source File: GcpPubSubAutoConfiguration.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public SubscriberFactory defaultSubscriberFactory(
		@Qualifier("subscriberExecutorProvider") ExecutorProvider executorProvider,
		@Qualifier("subscriberSystemExecutorProvider")
		ObjectProvider<ExecutorProvider> systemExecutorProvider,
		@Qualifier("subscriberFlowControlSettings")
				ObjectProvider<FlowControlSettings> flowControlSettings,
		@Qualifier("subscriberApiClock") ObjectProvider<ApiClock> apiClock,
		@Qualifier("subscriberRetrySettings") ObjectProvider<RetrySettings> retrySettings,
		TransportChannelProvider transportChannelProvider) {
	DefaultSubscriberFactory factory = new DefaultSubscriberFactory(this.finalProjectIdProvider);
	factory.setExecutorProvider(executorProvider);
	factory.setCredentialsProvider(this.finalCredentialsProvider);
	factory.setHeaderProvider(this.headerProvider);
	factory.setChannelProvider(transportChannelProvider);
	systemExecutorProvider.ifAvailable(factory::setSystemExecutorProvider);
	flowControlSettings.ifAvailable(factory::setFlowControlSettings);
	apiClock.ifAvailable(factory::setApiClock);
	retrySettings.ifAvailable(factory::setSubscriberStubRetrySettings);
	if (this.gcpPubSubProperties.getSubscriber().getMaxAckExtensionPeriod() != null) {
		factory.setMaxAckExtensionPeriod(Duration.ofSeconds(
				this.gcpPubSubProperties.getSubscriber().getMaxAckExtensionPeriod()));
	}
	if (this.gcpPubSubProperties.getSubscriber().getParallelPullCount() != null) {
		factory.setParallelPullCount(
				this.gcpPubSubProperties.getSubscriber().getParallelPullCount());
	}
	if (this.gcpPubSubProperties.getSubscriber()
			.getPullEndpoint() != null) {
		factory.setPullEndpoint(
				this.gcpPubSubProperties.getSubscriber().getPullEndpoint());
	}
	return factory;
}
 
Example 5
Source File: LoggingConfiguration.java    From jhipster-registry with Apache License 2.0 5 votes vote down vote up
public LoggingConfiguration(@Value("${spring.application.name}") String appName,
                            @Value("${server.port}") String serverPort,
                            JHipsterProperties jHipsterProperties,
                            ObjectProvider<BuildProperties> buildProperties,
                            ObjectMapper mapper) throws JsonProcessingException {

    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();

    Map<String, String> map = new HashMap<>();
    map.put("app_name", appName);
    map.put("app_port", serverPort);
    buildProperties.ifAvailable(it -> map.put("version", it.getVersion()));
    String customFields = mapper.writeValueAsString(map);

    JHipsterProperties.Logging loggingProperties = jHipsterProperties.getLogging();
    JHipsterProperties.Logging.Logstash logstashProperties = loggingProperties.getLogstash();

    if (loggingProperties.isUseJsonFormat()) {
        addJsonConsoleAppender(context, customFields);
    }
    if (logstashProperties.isEnabled()) {
        addLogstashTcpSocketAppender(context, customFields, logstashProperties);
    }
    if (loggingProperties.isUseJsonFormat() || logstashProperties.isEnabled()) {
        addContextListener(context, customFields, loggingProperties);
    }
    if (jHipsterProperties.getMetrics().getLogs().isEnabled()) {
        setMetricsMarkerLogbackFilter(context, loggingProperties.isUseJsonFormat());
    }
}
 
Example 6
Source File: ZookeeperAutoConfiguration.java    From spring-cloud-zookeeper with Apache License 2.0 5 votes vote down vote up
@Bean(destroyMethod = "close")
@ConditionalOnMissingBean
public CuratorFramework curatorFramework(RetryPolicy retryPolicy,
		ZookeeperProperties properties,
		ObjectProvider<CuratorFrameworkCustomizer> optionalCuratorFrameworkCustomizerProvider,
		ObjectProvider<EnsembleProvider> optionalEnsembleProvider,
		ObjectProvider<TracerDriver> optionalTracerDriverProvider) throws Exception {
	CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();

	EnsembleProvider ensembleProvider = optionalEnsembleProvider.getIfAvailable();
	if (ensembleProvider != null) {
		builder.ensembleProvider(ensembleProvider);
	}
	else {
		builder.connectString(properties.getConnectString());
	}
	builder.sessionTimeoutMs((int) properties.getSessionTimeout().toMillis())
			.connectionTimeoutMs((int) properties.getConnectionTimeout().toMillis())
			.retryPolicy(retryPolicy);

	optionalCuratorFrameworkCustomizerProvider.orderedStream()
			.forEach(curatorFrameworkCustomizer -> curatorFrameworkCustomizer
					.customize(builder));

	CuratorFramework curator = builder.build();
	optionalTracerDriverProvider.ifAvailable(tracerDriver -> {
		if (curator.getZookeeperClient() != null) {
			curator.getZookeeperClient().setTracerDriver(tracerDriver);
		}
	});

	curator.start();
	log.trace("blocking until connected to zookeeper for "
			+ properties.getBlockUntilConnectedWait()
			+ properties.getBlockUntilConnectedUnit());
	curator.blockUntilConnected(properties.getBlockUntilConnectedWait(),
			properties.getBlockUntilConnectedUnit());
	log.trace("connected to zookeeper");
	return curator;
}
 
Example 7
Source File: LoadBalancerAutoConfiguration.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Bean
public SmartInitializingSingleton loadBalancedRestTemplateInitializerDeprecated(
		final ObjectProvider<List<RestTemplateCustomizer>> restTemplateCustomizers) {
	return () -> restTemplateCustomizers.ifAvailable(customizers -> {
		for (RestTemplate restTemplate : LoadBalancerAutoConfiguration.this.restTemplates) {
			for (RestTemplateCustomizer customizer : customizers) {
				customizer.customize(restTemplate);
			}
		}
	});
}