提交 6c877971 编写于 作者: P Pramy 提交者: Liang Zhang

Implement MySQL time service (#3343)

Change-Id: I791c1d3ff4c4cbf0f90464c7bb8bb9490dca4019
上级 ebdba014
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-core</artifactId>
<version>4.0.0-RC3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>database-time-service</artifactId>
<name>${project.artifactId}</name>
<dependencies>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-core-route</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP-java7</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
/*
* 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.route.time.exception;
/**
* TimeService init exception.
*
* @author chenchuangliu
*/
public class TimeServiceInitException extends RuntimeException {
private static final long serialVersionUID = -834638295454826244L;
public TimeServiceInitException(final String message, final Throwable cause) {
super(message, cause);
}
}
/*
* 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.route.time.mysql;
import org.apache.shardingsphere.core.route.spi.TimeService;
import org.apache.shardingsphere.route.time.exception.TimeServiceInitException;
import javax.sql.DataSource;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.Properties;
/**
* MySQL time service.
* Need to create a mysql-time-service.properties file under the classpath.
*
* @author chenchuangliu
*/
public final class MySQLTimeService implements TimeService {
private static DataSource dataSource;
static {
init();
}
private static void init() {
try {
Properties properties = new Properties();
properties.load(MySQLTimeService.class.getResourceAsStream("/mysql-time-service.properties"));
if (properties.isEmpty()) {
return;
}
String dataSourceType = (String) properties.remove("dataSourceType");
Class dataSourceClass = Class.forName(dataSourceType);
DataSource dataSource = (DataSource) dataSourceClass.newInstance();
for (String each : properties.stringPropertyNames()) {
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(each, dataSourceClass);
Method writeMethod = propertyDescriptor.getWriteMethod();
writeMethod.invoke(dataSource, properties.getProperty(each));
}
MySQLTimeService.dataSource = dataSource;
} catch (final NullPointerException | IOException | ClassNotFoundException | InstantiationException | IllegalAccessException | InvocationTargetException | IntrospectionException e) {
throw new TimeServiceInitException("please check your mysql-time-service.properties", e);
}
}
@Override
public Date getTime() {
if (null != dataSource) {
try (Connection connection = dataSource.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("select now()");
ResultSet resultSet = preparedStatement.executeQuery()) {
resultSet.next();
return (Date) resultSet.getObject(1);
} catch (final SQLException ignore) {
}
}
return new Date();
}
}
#
# 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.
#
org.apache.shardingsphere.route.time.mysql.MySQLTimeService
/*
* 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.route.time.mysql;
import lombok.SneakyThrows;
import org.junit.Assert;
import org.junit.Test;
import javax.sql.DataSource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Properties;
public final class MySQLTimeServiceTest {
private final File file = new File(MySQLTimeServiceTest.class.getResource("/").getPath() + "mysql-time-service.properties");
@Test
public void assertInitDataSource() throws IOException {
FileOutputStream stream = new FileOutputStream(file);
Properties properties = new Properties();
properties.put("dataSourceType", "com.zaxxer.hikari.HikariDataSource");
properties.put("jdbcUrl", "jdbc:mysql://localhost:3306/test");
properties.put("username", "root");
properties.put("password", "root");
properties.put("driverClassName", "com.mysql.jdbc.Driver");
properties.store(stream, null);
stream.close();
MySQLTimeService service = new MySQLTimeService();
Assert.assertNotNull(getDataSource(service));
Assert.assertTrue(file.delete());
}
@SneakyThrows
private DataSource getDataSource(final MySQLTimeService service) {
Field field = service.getClass().getDeclaredField("dataSource");
field.setAccessible(true);
return (DataSource) field.get(null);
}
}
......@@ -33,6 +33,7 @@
<module>sharding-core-parse</module>
<module>sharding-core-preprocessor</module>
<module>sharding-core-route</module>
<module>database-time-service</module>
<module>sharding-core-rewrite</module>
<module>sharding-core-execute</module>
<module>sharding-core-merge</module>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册