Skip to content

Commit

Permalink
Do not create tracingDatasource when elasticjob.tracing.type is not R…
Browse files Browse the repository at this point in the history
…DB (#2254)

* Do not create tracingDatasource when elasticjob.tracing.type is not RDB

* modify unit test of TracingConfigurationTest

* modify unit test of TracingConfigurationTest

---------

Co-authored-by: Gabry <gabry@kupotech.com>
Co-authored-by: gaopeng <95430950@qq.com>
  • Loading branch information
3 people authored Sep 12, 2023
1 parent 26ef7ea commit 6c946cd
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

<dependency>
Expand All @@ -74,14 +75,17 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -25,49 +25,55 @@
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;

import javax.sql.DataSource;

/**
* ElasticJob tracing auto configuration.
*/
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(TracingProperties.class)
public class ElasticJobTracingConfiguration {

/**
* Create a bean of tracing DataSource.
*
* @param tracingProperties tracing Properties
* @return tracing DataSource
*/
@Bean("tracingDataSource")
public DataSource tracingDataSource(final TracingProperties tracingProperties) {
DataSourceProperties dataSource = tracingProperties.getDataSource();
if (dataSource == null) {
return null;
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(name = "elasticjob.tracing.type", havingValue = "RDB")
static class RDBTracingConfiguration {
/**
* Create a bean of tracing DataSource.
*
* @param tracingProperties tracing Properties
* @return tracing DataSource
*/
@Bean("tracingDataSource")
public DataSource tracingDataSource(final TracingProperties tracingProperties) {
DataSourceProperties dataSource = tracingProperties.getDataSource();
if (dataSource == null) {
return null;
}
HikariDataSource tracingDataSource = new HikariDataSource();
tracingDataSource.setJdbcUrl(dataSource.getUrl());
BeanUtils.copyProperties(dataSource, tracingDataSource);
return tracingDataSource;
}
HikariDataSource tracingDataSource = new HikariDataSource();
tracingDataSource.setJdbcUrl(dataSource.getUrl());
BeanUtils.copyProperties(dataSource, tracingDataSource);
return tracingDataSource;
}

/**
* Create a bean of tracing configuration.
*
* @param dataSource required by constructor
* @param tracingDataSource tracing ataSource
* @return a bean of tracing configuration
*/
@Bean
@ConditionalOnBean(DataSource.class)
@ConditionalOnProperty(name = "elasticjob.tracing.type", havingValue = "RDB")
public TracingConfiguration<DataSource> tracingConfiguration(final DataSource dataSource, @Nullable final DataSource tracingDataSource) {
DataSource ds = tracingDataSource;
if (ds == null) {
ds = dataSource;
/**
* Create a bean of tracing configuration.
*
* @param dataSource required by constructor
* @param tracingDataSource tracing ataSource
* @return a bean of tracing configuration
*/
@Bean
@ConditionalOnBean(DataSource.class)
public TracingConfiguration<DataSource> tracingConfiguration(final DataSource dataSource,
@Nullable final DataSource tracingDataSource) {
DataSource ds = tracingDataSource;
if (ds == null) {
ds = dataSource;
}
return new TracingConfiguration<>("RDB", ds);
}
return new TracingConfiguration<>("RDB", ds);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.elasticjob.lite.spring.boot.tracing;

import org.apache.shardingsphere.elasticjob.lite.spring.boot.job.fixture.EmbedTestingServer;
import org.apache.shardingsphere.elasticjob.tracing.api.TracingConfiguration;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.ResolvableType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

import javax.sql.DataSource;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;

@SpringBootTest
@SpringBootApplication
@ActiveProfiles("tracing")
public class TracingConfigurationTest extends AbstractJUnit4SpringContextTests {

@BeforeClass
public static void init() {
EmbedTestingServer.start();
}

@Test
public void assertNotRDBConfiguration() {
assertNotNull(applicationContext);
assertFalse(applicationContext.containsBean("tracingDataSource"));
ObjectProvider<Object> provider = applicationContext.getBeanProvider(ResolvableType.forClassWithGenerics(TracingConfiguration.class, DataSource.class));
assertNull(provider.getIfAvailable());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

elasticjob:
regCenter:
serverLists: localhost:18181
namespace: elasticjob-lite-spring-boot-starter

0 comments on commit 6c946cd

Please sign in to comment.