org.zodiac.autoconfigure.pulsar.PulsarAutoConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.pulsar;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.zodiac.commons.util.lang.Strings;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
@SpringBootConfiguration
//@ComponentScan
//@EnableConfigurationProperties(value = {PulsarProperties.class})
@ConditionalOnClass(value = {org.apache.pulsar.client.api.PulsarClient.class, org.zodiac.pulsar.model.PulsarMessage.class})
@ConditionalOnProperty(value = org.zodiac.pulsar.constants.PulsarSystemPropertiesConstants.SPRING_PULSAR_ENABLED, havingValue = "true")
public class PulsarAutoConfiguration {
public PulsarAutoConfiguration() {
}
@Bean
@ConditionalOnMissingBean
@ConfigurationProperties(prefix = org.zodiac.pulsar.constants.PulsarSystemPropertiesConstants.SPRING_PULSAR_PREFIX)
protected PulsarProperties pulsarProperties() {
return new PulsarProperties();
}
@Bean
@ConditionalOnMissingBean
protected org.apache.pulsar.client.api.PulsarClient pulsarClient(PulsarProperties pulsarProperties) throws org.apache.pulsar.client.api.PulsarClientException, org.zodiac.pulsar.exception.ClientInitException, MalformedURLException {
if (Strings.isNotEmpty(pulsarProperties.getTlsAuthCertFilePath())
&& Strings.isNotEmpty(pulsarProperties.getTlsAuthKeyFilePath())
&& Strings.isNotEmpty(pulsarProperties.getTokenAuthValue()))
throw new org.zodiac.pulsar.exception.ClientInitException("You cannot use multiple auth options.");
final org.apache.pulsar.client.api.ClientBuilder pulsarClientBuilder = org.apache.pulsar.client.api.PulsarClient.builder()
.serviceUrl(pulsarProperties.getServiceUrl())
.ioThreads(pulsarProperties.getIoThreads()).listenerThreads(pulsarProperties.getListenerThreads())
.enableTcpNoDelay(pulsarProperties.isEnableTcpNoDelay())
.keepAliveInterval(pulsarProperties.getKeepAliveIntervalSec(), TimeUnit.SECONDS)
.connectionTimeout(pulsarProperties.getConnectionTimeoutSec(), TimeUnit.SECONDS)
.operationTimeout(pulsarProperties.getOperationTimeoutSec(), TimeUnit.SECONDS)
.startingBackoffInterval(pulsarProperties.getStartingBackoffIntervalMs(), TimeUnit.MILLISECONDS)
.maxBackoffInterval(pulsarProperties.getMaxBackoffIntervalSec(), TimeUnit.SECONDS)
.useKeyStoreTls(pulsarProperties.isUseKeyStoreTls())
.tlsTrustCertsFilePath(pulsarProperties.getTlsTrustCertsFilePath())
.tlsCiphers(pulsarProperties.getTlsCiphers()).tlsProtocols(pulsarProperties.getTlsProtocols())
.tlsTrustStorePassword(pulsarProperties.getTlsTrustStorePassword())
.tlsTrustStorePath(pulsarProperties.getTlsTrustStorePath())
.tlsTrustStoreType(pulsarProperties.getTlsTrustStoreType())
.allowTlsInsecureConnection(pulsarProperties.isAllowTlsInsecureConnection())
.enableTlsHostnameVerification(pulsarProperties.isEnableTlsHostnameVerification());
if (Strings.isNotEmpty(pulsarProperties.getTlsAuthCertFilePath())
&& Strings.isNotEmpty(pulsarProperties.getTlsAuthKeyFilePath())) {
pulsarClientBuilder.authentication(org.apache.pulsar.client.api.AuthenticationFactory.TLS(pulsarProperties.getTlsAuthCertFilePath(),
pulsarProperties.getTlsAuthKeyFilePath()));
}
if (Strings.isNotEmpty(pulsarProperties.getTokenAuthValue())) {
pulsarClientBuilder.authentication(org.apache.pulsar.client.api.AuthenticationFactory.token(pulsarProperties.getTokenAuthValue()));
}
if (Strings.isNotEmpty(pulsarProperties.getOauth2Audience())
&& Strings.isNotEmpty(pulsarProperties.getOauth2IssuerUrl())
&& Strings.isNotEmpty(pulsarProperties.getOauth2CredentialsUrl())) {
final URL issuerUrl = new URL(pulsarProperties.getOauth2IssuerUrl());
final URL credentialsUrl = new URL(pulsarProperties.getOauth2CredentialsUrl());
pulsarClientBuilder.authentication(org.apache.pulsar.client.impl.auth.oauth2.AuthenticationFactoryOAuth2.clientCredentials(issuerUrl, credentialsUrl,
pulsarProperties.getOauth2Audience()));
}
return pulsarClientBuilder.build();
}
@Bean
@ConditionalOnMissingBean
protected org.zodiac.pulsar.utils.UrlBuildService urlBuildService(PulsarProperties pulsarProperties) {
return org.zodiac.pulsar.utils.UrlBuildService.createUrlBuildService(pulsarProperties);
}
@Bean
@ConditionalOnMissingBean
protected org.zodiac.pulsar.collector.ConsumerCollector consumerCollector(org.zodiac.pulsar.utils.UrlBuildService urlBuildService) {
return new org.zodiac.pulsar.collector.ConsumerCollector(urlBuildService);
}
@Bean
@ConditionalOnMissingBean
protected org.zodiac.pulsar.consumer.ConsumerAggregator consumerAggregator(org.zodiac.pulsar.collector.ConsumerCollector consumerCollector,
org.apache.pulsar.client.api.PulsarClient pulsarClient,
PulsarProperties pulsarProperties, org.zodiac.pulsar.utils.UrlBuildService urlBuildService) {
return new org.zodiac.pulsar.consumer.ConsumerAggregator(consumerCollector, pulsarClient,
pulsarProperties.getConsumers().getDefault(), urlBuildService);
}
@Bean
@ConditionalOnMissingBean
protected org.zodiac.pulsar.producer.ProducerCollector producerCollector(org.apache.pulsar.client.api.PulsarClient pulsarClient,
org.zodiac.pulsar.utils.UrlBuildService urlBuildService) {
return new org.zodiac.pulsar.producer.ProducerCollector(pulsarClient, urlBuildService);
}
@Bean
@ConditionalOnMissingBean
protected org.zodiac.pulsar.producer.PulsarOperations pulsarOperations(org.zodiac.pulsar.producer.ProducerCollector producerCollector) {
return new org.zodiac.pulsar.producer.PulsarTemplate(producerCollector);
}
}