Search in sources :

Example 1 with ExecutorEngine

use of io.shardingjdbc.core.executor.ExecutorEngine in project sharding-jdbc by shardingjdbc.

the class ShardingDataSourceTest method assertRenewWithoutChangeExecutorPoolEngine.

@Test
public void assertRenewWithoutChangeExecutorPoolEngine() throws SQLException, NoSuchFieldException, IllegalAccessException {
    DataSource originalDataSource = mockDataSource("H2");
    Map<String, DataSource> originalDataSourceMap = new HashMap<>(1, 1);
    originalDataSourceMap.put("ds", originalDataSource);
    ShardingDataSource shardingDataSource = createShardingDataSource(originalDataSourceMap);
    ExecutorEngine originExecutorEngine = getExecutorEngine(shardingDataSource);
    DataSource newDataSource = mockDataSource("H2");
    Map<String, DataSource> newDataSourceMap = new HashMap<>(1, 1);
    newDataSourceMap.put("ds", newDataSource);
    shardingDataSource.renew(newDataSourceMap, new ShardingRule(createShardingRuleConfig(newDataSourceMap), newDataSourceMap.keySet()), new Properties());
    assertThat(originExecutorEngine, is(getExecutorEngine(shardingDataSource)));
}
Also used : HashMap(java.util.HashMap) ExecutorEngine(io.shardingjdbc.core.executor.ExecutorEngine) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) Properties(java.util.Properties) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 2 with ExecutorEngine

use of io.shardingjdbc.core.executor.ExecutorEngine in project sharding-jdbc by shardingjdbc.

the class ShardingDataSourceTest method assertRenewWithChangeExecutorEnginePoolSize.

@Test
public void assertRenewWithChangeExecutorEnginePoolSize() throws SQLException, NoSuchFieldException, IllegalAccessException {
    DataSource originalDataSource = mockDataSource("H2");
    Map<String, DataSource> originalDataSourceMap = new HashMap<>(1, 1);
    originalDataSourceMap.put("ds", originalDataSource);
    ShardingDataSource shardingDataSource = createShardingDataSource(originalDataSourceMap);
    final ExecutorEngine originExecutorEngine = getExecutorEngine(shardingDataSource);
    DataSource newDataSource = mockDataSource("H2");
    Map<String, DataSource> newDataSourceMap = new HashMap<>(1, 1);
    newDataSourceMap.put("ds", newDataSource);
    Properties props = new Properties();
    props.setProperty(ShardingPropertiesConstant.EXECUTOR_SIZE.getKey(), "100");
    shardingDataSource.renew(newDataSourceMap, new ShardingRule(createShardingRuleConfig(newDataSourceMap), newDataSourceMap.keySet()), props);
    assertThat(originExecutorEngine, not(getExecutorEngine(shardingDataSource)));
}
Also used : HashMap(java.util.HashMap) ExecutorEngine(io.shardingjdbc.core.executor.ExecutorEngine) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) Properties(java.util.Properties) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 3 with ExecutorEngine

use of io.shardingjdbc.core.executor.ExecutorEngine in project sharding-jdbc by shardingjdbc.

the class ShardingDataSourceTest method getExecutorEngine.

private ExecutorEngine getExecutorEngine(final ShardingDataSource shardingDataSource) throws NoSuchFieldException, IllegalAccessException {
    Field field = ShardingDataSource.class.getDeclaredField("executorEngine");
    field.setAccessible(true);
    return (ExecutorEngine) field.get(shardingDataSource);
}
Also used : Field(java.lang.reflect.Field) ExecutorEngine(io.shardingjdbc.core.executor.ExecutorEngine)

Example 4 with ExecutorEngine

use of io.shardingjdbc.core.executor.ExecutorEngine in project sharding-jdbc by shardingjdbc.

the class ShardingDataSource method renew.

/**
 * Renew sharding data source.
 *
 * @param newDataSourceMap new data source map
 * @param newShardingRule new sharding rule
 * @param newProps new sharding properties
 */
public void renew(final Map<String, DataSource> newDataSourceMap, final ShardingRule newShardingRule, final Properties newProps) {
    ShardingProperties newShardingProperties = new ShardingProperties(null == newProps ? new Properties() : newProps);
    int originalExecutorSize = shardingProperties.getValue(ShardingPropertiesConstant.EXECUTOR_SIZE);
    int newExecutorSize = newShardingProperties.getValue(ShardingPropertiesConstant.EXECUTOR_SIZE);
    if (originalExecutorSize != newExecutorSize) {
        ExecutorEngine originalExecutorEngine = executorEngine;
        executorEngine = new ExecutorEngine(newExecutorSize);
        originalExecutorEngine.close();
    }
    boolean newShowSQL = newShardingProperties.getValue(ShardingPropertiesConstant.SQL_SHOW);
    shardingProperties = newShardingProperties;
    shardingContext = new ShardingContext(newDataSourceMap, newShardingRule, getDatabaseType(), executorEngine, newShowSQL);
}
Also used : ShardingProperties(io.shardingjdbc.core.constant.ShardingProperties) ExecutorEngine(io.shardingjdbc.core.executor.ExecutorEngine) Properties(java.util.Properties) ShardingProperties(io.shardingjdbc.core.constant.ShardingProperties) ShardingContext(io.shardingjdbc.core.jdbc.core.ShardingContext)

Example 5 with ExecutorEngine

use of io.shardingjdbc.core.executor.ExecutorEngine in project sharding-jdbc by shardingjdbc.

the class AbstractBaseExecutorTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    ExecutorExceptionHandler.setExceptionThrown(false);
    executorEngine = new ExecutorEngine(Runtime.getRuntime().availableProcessors());
    overallExecutionEventListener = new TestOverallExecutionEventListener(eventCaller);
    dqlExecutionEventListener = new TestDQLExecutionEventListener(eventCaller);
    dmlExecutionEventListener = new TestDMLExecutionEventListener(eventCaller);
    EventBusInstance.getInstance().register(overallExecutionEventListener);
    EventBusInstance.getInstance().register(dqlExecutionEventListener);
    EventBusInstance.getInstance().register(dmlExecutionEventListener);
}
Also used : TestOverallExecutionEventListener(io.shardingjdbc.core.executor.fixture.TestOverallExecutionEventListener) TestDMLExecutionEventListener(io.shardingjdbc.core.executor.fixture.TestDMLExecutionEventListener) ExecutorEngine(io.shardingjdbc.core.executor.ExecutorEngine) TestDQLExecutionEventListener(io.shardingjdbc.core.executor.fixture.TestDQLExecutionEventListener) Before(org.junit.Before)

Aggregations

ExecutorEngine (io.shardingjdbc.core.executor.ExecutorEngine)5 Properties (java.util.Properties)3 ShardingRule (io.shardingjdbc.core.rule.ShardingRule)2 HashMap (java.util.HashMap)2 DataSource (javax.sql.DataSource)2 Test (org.junit.Test)2 ShardingProperties (io.shardingjdbc.core.constant.ShardingProperties)1 TestDMLExecutionEventListener (io.shardingjdbc.core.executor.fixture.TestDMLExecutionEventListener)1 TestDQLExecutionEventListener (io.shardingjdbc.core.executor.fixture.TestDQLExecutionEventListener)1 TestOverallExecutionEventListener (io.shardingjdbc.core.executor.fixture.TestOverallExecutionEventListener)1 ShardingContext (io.shardingjdbc.core.jdbc.core.ShardingContext)1 Field (java.lang.reflect.Field)1 Before (org.junit.Before)1