Skip to content

Commit 4620cf9

Browse files
author
belbix
committed
init
0 parents  commit 4620cf9

File tree

6 files changed

+396
-0
lines changed

6 files changed

+396
-0
lines changed

.gitignore

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
.sts4-cache
12+
13+
### IntelliJ IDEA ###
14+
.idea
15+
*.iws
16+
*.iml
17+
*.ipr
18+
19+
### NetBeans ###
20+
/nbproject/private/
21+
/nbbuild/
22+
/dist/
23+
/nbdist/
24+
/.nb-gradle/
25+
/build/
26+
/logs/
27+
/histories/
28+
/keys.txt

pom.xml

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>pro.belbix</groupId>
7+
<artifactId>dbchecker</artifactId>
8+
<version>1.0.0</version>
9+
10+
<properties>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
<logback.version>1.2.3</logback.version>
13+
<javax.mail.version>1.6.2</javax.mail.version>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>mysql</groupId>
19+
<artifactId>mysql-connector-java</artifactId>
20+
<version>8.0.17</version>
21+
</dependency>
22+
23+
<dependency>
24+
<groupId>ch.qos.logback</groupId>
25+
<artifactId>logback-classic</artifactId>
26+
<version>${logback.version}</version>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>com.sun.mail</groupId>
31+
<artifactId>javax.mail</artifactId>
32+
<version>${javax.mail.version}</version>
33+
</dependency>
34+
35+
</dependencies>
36+
37+
<build>
38+
<finalName>tim</finalName>
39+
<plugins>
40+
<plugin>
41+
<groupId>org.apache.maven.plugins</groupId>
42+
<artifactId>maven-compiler-plugin</artifactId>
43+
<version>3.8.0</version>
44+
<configuration>
45+
<source>11</source>
46+
<target>11</target>
47+
</configuration>
48+
</plugin>
49+
50+
<plugin>
51+
<groupId>org.apache.maven.plugins</groupId>
52+
<artifactId>maven-help-plugin</artifactId>
53+
<version>3.1.0</version>
54+
<executions>
55+
<execution>
56+
<id>show-profiles</id>
57+
<phase>compile</phase>
58+
<goals>
59+
<goal>active-profiles</goal>
60+
</goals>
61+
</execution>
62+
</executions>
63+
</plugin>
64+
65+
<plugin>
66+
<groupId>org.apache.maven.plugins</groupId>
67+
<artifactId>maven-jar-plugin</artifactId>
68+
<version>3.0.0</version>
69+
<configuration>
70+
<archive>
71+
<manifest>
72+
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
73+
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
74+
<addClasspath>true</addClasspath>
75+
<classpathPrefix>lib/</classpathPrefix>
76+
<classpathLayoutType>custom</classpathLayoutType>
77+
<customClasspathLayout>${artifact.artifactId}-${artifact.baseVersion}${dashClassifier?}.${artifact.extension}</customClasspathLayout>
78+
<mainClass>pro.belbix.dbchecker.Main</mainClass>
79+
</manifest>
80+
</archive>
81+
<outputDirectory>./dist</outputDirectory>
82+
</configuration>
83+
</plugin>
84+
85+
<plugin>
86+
<groupId>org.apache.maven.plugins</groupId>
87+
<artifactId>maven-dependency-plugin</artifactId>
88+
<version>3.0.0</version>
89+
<executions>
90+
<execution>
91+
<id>copy-dependencies</id>
92+
<phase>package</phase>
93+
<goals>
94+
<goal>copy-dependencies</goal>
95+
</goals>
96+
<configuration>
97+
<outputDirectory>./dist/lib</outputDirectory>
98+
<overWriteReleases>false</overWriteReleases>
99+
<overWriteSnapshots>false</overWriteSnapshots>
100+
<overWriteIfNewer>true</overWriteIfNewer>
101+
</configuration>
102+
</execution>
103+
</executions>
104+
</plugin>
105+
106+
</plugins>
107+
</build>
108+
109+
110+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package pro.belbix.dbchecker;
2+
3+
public class Config {
4+
private static String path;
5+
private static Config instance;
6+
7+
private Config() {
8+
init();
9+
}
10+
11+
public static Config getInstance() {
12+
if (instance != null) return instance;
13+
synchronized (Config.class) {
14+
if (instance != null) return instance;
15+
instance = new Config();
16+
}
17+
return instance;
18+
}
19+
20+
private void init() {
21+
if(path == null) return;
22+
}
23+
24+
public static void setPath(String path){
25+
Config.path = path;
26+
}
27+
28+
private String dbClassName = "com.mysql.cj.jdbc.Driver";
29+
private String dbUrl = "jdbc:mysql://localhost:3306/tim?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true";
30+
private String dbUser = "belbix";
31+
private String dbPassword = "111111";
32+
private int loopTime = 10;
33+
private String dateSelect = "select t.date from ticks as t order by t.date desc limit 1";
34+
private int maxDiff = 100;
35+
36+
public String getDbClassName() {
37+
return dbClassName;
38+
}
39+
40+
public String getDbUrl() {
41+
return dbUrl;
42+
}
43+
44+
public String getDbUser() {
45+
return dbUser;
46+
}
47+
48+
public String getDbPassword() {
49+
return dbPassword;
50+
}
51+
52+
public int getLoopTime() {
53+
return loopTime;
54+
}
55+
56+
public String getDateSelect() {
57+
return dateSelect;
58+
}
59+
60+
public int getMaxDiff() {
61+
return maxDiff;
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package pro.belbix.dbchecker;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import java.sql.*;
7+
import java.time.Instant;
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
12+
public class DBService {
13+
private final static Logger log = LoggerFactory.getLogger(DBService.class);
14+
private final Config config;
15+
private static DBService instance;
16+
private Connection conn;
17+
private Map<String, PreparedStatement> statements = new HashMap<>();
18+
19+
private DBService() {
20+
this.config = Config.getInstance();
21+
init();
22+
}
23+
24+
public static DBService getInstance() {
25+
if (instance != null) return instance;
26+
synchronized (DBService.class) {
27+
if (instance != null) return instance;
28+
instance = new DBService();
29+
}
30+
return instance;
31+
}
32+
33+
private void init() {
34+
try {
35+
Class.forName(config.getDbClassName());
36+
} catch (ClassNotFoundException e) {
37+
log.error(e.getMessage(), e);
38+
}
39+
40+
try {
41+
conn = DriverManager.getConnection(config.getDbUrl(), config.getDbUser(), config.getDbPassword());
42+
} catch (SQLException e) {
43+
log.error(e.getMessage(), e);
44+
}
45+
}
46+
47+
private PreparedStatement findStatement(String sql) {
48+
PreparedStatement statement;
49+
statement = statements.get(sql);
50+
if (statement != null) return statement;
51+
try {
52+
statement = conn.prepareStatement(sql);
53+
} catch (SQLException e) {
54+
log.error(e.getMessage(), e);
55+
}
56+
statements.put(sql, statement);
57+
return statement;
58+
}
59+
60+
public Instant selectLastDate(String sql) {
61+
PreparedStatement statement = findStatement(sql);
62+
Instant result = null;
63+
try {
64+
ResultSet rs = statement.executeQuery();
65+
while (rs.next()) {
66+
result = rs.getTimestamp(1).toInstant();
67+
}
68+
} catch (SQLException e) {
69+
log.error(e.getMessage(), e);
70+
}
71+
return result;
72+
}
73+
74+
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package pro.belbix.dbchecker;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import java.time.Duration;
7+
import java.time.Instant;
8+
9+
public class Main {
10+
private final static Logger log = LoggerFactory.getLogger(Main.class);
11+
private static Config config;
12+
private static DBService dbService;
13+
private static boolean run = true;
14+
15+
public static void main(String[] args) throws InterruptedException {
16+
log.info("################ Start DBChecker ##################");
17+
if (args.length != 0) {
18+
Config.setPath(args[0]);
19+
}
20+
config = Config.getInstance();
21+
dbService = DBService.getInstance();
22+
23+
Main main = new Main();
24+
while (run) {
25+
try {
26+
main.check();
27+
} catch (Exception e) {
28+
log.error("Error main loop", e);
29+
} finally {
30+
Thread.sleep(config.getLoopTime() * 1000);
31+
}
32+
}
33+
}
34+
35+
private void check() {
36+
Instant now = Instant.now();
37+
Instant date = dbService.selectLastDate(config.getDateSelect());
38+
long sec = Duration.between(date, Instant.now()).getSeconds();
39+
if (sec > config.getMaxDiff()) {
40+
log.error("TOO OLD VALUE! " + sec);
41+
sendError(sec);
42+
try {
43+
Thread.sleep(config.getLoopTime() * 1000 * 5);
44+
} catch (InterruptedException ignored) {
45+
}
46+
}
47+
48+
log.info("Check for " + Duration.between(now, Instant.now()).getSeconds());
49+
}
50+
51+
private void sendError(long sec) {
52+
53+
}
54+
55+
}

0 commit comments

Comments
 (0)