Skip to content
This repository was archived by the owner on Mar 14, 2023. It is now read-only.

Commit c1cd2c6

Browse files
author
clouless
committed
initial code
1 parent 32fafb3 commit c1cd2c6

File tree

7 files changed

+336
-1
lines changed

7 files changed

+336
-1
lines changed

.editorconfig

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# EditorConfig is awesome: http://EditorConfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
# Unix-style newlines with a newline ending every file
7+
[*]
8+
end_of_line = lf
9+
insert_final_newline = true
10+
11+
# Matches multiple files with brace expansion notation
12+
# Set default charset
13+
[*.{js,py,xml,java}]
14+
charset = utf-8
15+
16+
# 4 space indentation
17+
[*.xml]
18+
indent_style = space
19+
indent_size = 2
20+
21+
[*.java]
22+
indent_style = space
23+
indent_size = 4
24+
25+
# Tab indentation (no size specified)
26+
[Makefile]
27+
indent_style = tab
28+
29+
# Indentation override for all JS under lib directory
30+
[lib/**.js]
31+
indent_style = space
32+
indent_size = 2
33+
34+
# Matches the exact files either package.json or .travis.yml
35+
[{package.json,.travis.yml}]
36+
indent_style = space
37+
indent_size = 2

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.iml
2+
.idea
3+
target
4+
output.xml
5+
.classpath
6+
.project
7+
.settings
8+
.vscode

DEVELOPMENT.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Development
2+
3+
### Build & Test
4+
5+
```
6+
mvn package -Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true
7+
java -jar target/is-database-up.jar \
8+
-j "jdbc:postgresql://localhost:5441/test" \
9+
-u postgres \
10+
-p s3cr3t \
11+
-s true \
12+
-w 10
13+
```
14+
15+
Now in another terminal start dockerized postgres
16+
```
17+
docker run -i -t --rm -p 5441:5432 \
18+
-e POSTGRES_PASSWORD=s3cr3t \
19+
-e POSTGRES_USER=postgres \
20+
-e POSTGRES_DB=test \
21+
postgres:9.5 \
22+
postgres -c 'log_statement=all'
23+
```
24+
25+
### Testcoverage
26+
27+
[Run OpenClover](http://openclover.org/) with maven:
28+
29+
```bash
30+
mvn clean clover:setup test clover:aggregate clover:clover
31+
```
32+
33+
Now look into `target/site/clover/`

README.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,44 @@
11
# java-is-database-up
2-
Is database ready for connections
2+
3+
4+
5+
----
6+
 
7+
8+
### Is this for me?
9+
10+
You are starting maybe dockerized MySQL or PostgreSQL databases and they need some time
11+
until they are ready and accept connections?
12+
You want to busy wait for the database to accept connections?
13+
Then this is for you.
14+
15+
----
16+
 
17+
18+
### Usage
19+
20+
Simply download the jar file and use it to wait for a database to accept connections like so and wait max 10 minutes.
21+
Currently supported database are PostgreSQL and MySQL.
22+
23+
```
24+
# INSTALL
25+
curl -L -o is-database-up.jar \
26+
https://github.com/codeclou/java-is-database-up/releases/download/1.0.0/is-database-up.jar
27+
28+
# RUN
29+
java -jar is-database-up.jar \
30+
-j "jdbc:postgresql://myhostname/mydb" \
31+
-u admin \
32+
-p myS3cr3t \
33+
-s true \
34+
-w 10
35+
```
36+
37+
You can specify `-v` to see more information printed out.
38+
39+
----
40+
 
41+
42+
### License
43+
44+
[MIT](https://github.com/codeclou/java-is-database-up/blob/master/LICENSE) © [Bernhard Grünewaldt](https://github.com/clouless)

pom.xml

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>io.codeclou</groupId>
6+
<artifactId>java-is-database-up</artifactId>
7+
<version>1.0.0</version>
8+
<organization>
9+
<name>codeclou.io</name>
10+
<url>https://codeclou.io/</url>
11+
</organization>
12+
<licenses>
13+
<license>
14+
<name>MIT</name>
15+
<url>https://github.com/codeclou/java-is-database-up/blob/master/LICENSE</url>
16+
</license>
17+
</licenses>
18+
<name>java-is-database-up</name>
19+
<description>java-is-database-up</description>
20+
<packaging>jar</packaging>
21+
<dependencies>
22+
<dependency>
23+
<groupId>commons-cli</groupId>
24+
<artifactId>commons-cli</artifactId>
25+
<version>1.4</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>mysql</groupId>
29+
<artifactId>mysql-connector-java</artifactId>
30+
<version>8.0.15</version>
31+
<!-- needs to be added to src/assembly/src.xml -->
32+
</dependency>
33+
<dependency>
34+
<groupId>org.postgresql</groupId>
35+
<artifactId>postgresql</artifactId>
36+
<version>9.4-1200-jdbc41</version>
37+
<!-- needs to be added to src/assembly/src.xml -->
38+
</dependency>
39+
<dependency>
40+
<groupId>junit</groupId>
41+
<artifactId>junit</artifactId>
42+
<version>4.12</version>
43+
<scope>test</scope>
44+
</dependency>
45+
<dependency>
46+
<groupId>com.openpojo</groupId>
47+
<artifactId>openpojo</artifactId>
48+
<version>0.8.6</version>
49+
<scope>test</scope>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.powermock</groupId>
53+
<artifactId>powermock-module-junit4</artifactId>
54+
<version>${powermock.version}</version>
55+
<scope>test</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.powermock</groupId>
59+
<artifactId>powermock-api-mockito</artifactId>
60+
<version>${powermock.version}</version>
61+
<scope>test</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>org.mockito</groupId>
65+
<artifactId>mockito-all</artifactId>
66+
<version>1.10.19</version>
67+
<scope>test</scope>
68+
</dependency>
69+
<dependency>
70+
<groupId>org.powermock</groupId>
71+
<artifactId>powermock-api-mockito-common</artifactId>
72+
<version>1.7.0</version>
73+
<scope>test</scope>
74+
</dependency>
75+
</dependencies>
76+
<build>
77+
<plugins>
78+
<plugin>
79+
<groupId>org.openclover</groupId>
80+
<artifactId>clover-maven-plugin</artifactId>
81+
<version>4.2.0</version>
82+
</plugin>
83+
<plugin>
84+
<artifactId>maven-assembly-plugin</artifactId>
85+
<version>3.1.0</version>
86+
<configuration>
87+
<finalName>is-database-up</finalName>
88+
<archive>
89+
<manifest>
90+
<mainClass>io.codeclou.java.is.database.up.Readiness</mainClass>
91+
</manifest>
92+
</archive>
93+
<descriptorRefs>
94+
<descriptorRef>jar-with-dependencies</descriptorRef>
95+
</descriptorRefs>
96+
<appendAssemblyId>false</appendAssemblyId>
97+
</configuration>
98+
<executions>
99+
<execution>
100+
<id>make-assembly</id>
101+
<phase>package</phase>
102+
<goals>
103+
<goal>single</goal>
104+
</goals>
105+
</execution>
106+
</executions>
107+
</plugin>
108+
</plugins>
109+
</build>
110+
<properties>
111+
<powermock.version>1.7.0</powermock.version>
112+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
113+
<maven.compiler.source>1.8</maven.compiler.source>
114+
<maven.compiler.target>1.8</maven.compiler.target>
115+
</properties>
116+
</project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.codeclou.java.is.database.up;
2+
3+
public class Readiness {
4+
public static void main(String[] args) throws Exception {
5+
ReadinessChecker checker = new ReadinessChecker();
6+
checker.run(args);
7+
}
8+
9+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package io.codeclou.java.is.database.up;
2+
3+
import org.apache.commons.cli.*;
4+
5+
import java.sql.Connection;
6+
import java.sql.DriverManager;
7+
import java.util.Properties;
8+
9+
public class ReadinessChecker {
10+
11+
private CommandLineParser parser = new DefaultParser();
12+
private Options options = new Options();
13+
private Boolean hasCmdLineParameterErrors = false;
14+
15+
protected void run(String[] args) throws Exception {
16+
//
17+
// Pull in drivers
18+
//
19+
Class.forName("org.postgresql.Driver");
20+
Class.forName("com.mysql.cj.jdbc.Driver");
21+
//
22+
// Options
23+
//
24+
options.addOption("j", "jdbc", true, "the jdbc url e.g.: jdbc:postgresql://localhost/test");
25+
options.addOption("u", "user", true, "the database username");
26+
options.addOption("p", "password", true, "the database password");
27+
options.addOption("s", "ssl", true, "use ssl true/false. default true.");
28+
options.addOption("w", "maxwait", true, "how long should we wait until we exit with error in minutes.");
29+
options.addOption("v", "verbose", false, "if specified a lot of info is printed.");
30+
CommandLine cmd = this.parser.parse(options, args);
31+
System.out.println("\033[35m+-------------------------+\033[0m");
32+
System.out.println("\033[35m| Java Is Database Up |\033[0m");
33+
System.out.println("\033[35m+-------------------------+\033[0m");
34+
35+
if (!cmd.hasOption("jdbc")) {
36+
System.out.println("\033[31mError >> Please specify connection with -j\033[0m");
37+
hasCmdLineParameterErrors = true;
38+
}
39+
if (!cmd.hasOption("user")) {
40+
System.out.println("\033[31mError >> Please specify database user with -u\033[0m");
41+
hasCmdLineParameterErrors = true;
42+
}
43+
if (!cmd.hasOption("password")) {
44+
System.out.println("\033[31mError >> Please specify database password with -p\033[0m");
45+
hasCmdLineParameterErrors = true;
46+
}
47+
if (!cmd.hasOption("ssl")) {
48+
System.out.println("\033[31mError >> Please specify database connection should use ssl with -s\033[0m");
49+
hasCmdLineParameterErrors = true;
50+
}
51+
if (!cmd.hasOption("maxwait")) {
52+
System.out.println("\033[31mError >> Please specify maxwait in minutes with -w\033[0m");
53+
hasCmdLineParameterErrors = true;
54+
}
55+
if (!hasCmdLineParameterErrors) {
56+
//
57+
// CONNECTION PROPS
58+
//
59+
String url = cmd.getOptionValue("jdbc");
60+
Properties props = new Properties();
61+
props.setProperty("user", cmd.getOptionValue("user"));
62+
props.setProperty("password", cmd.getOptionValue("password"));
63+
props.setProperty("ssl", cmd.getOptionValue("ssl"));
64+
Integer maxWaitMinutes = Integer.parseInt(cmd.getOptionValue("maxwait"));
65+
System.out.println("\033[36mTrying >> to connect to db " + url + " and waiting for max " + maxWaitMinutes + "min. \033[0m");
66+
for (int i=0; i < maxWaitMinutes * 2; i++) {
67+
//
68+
// TRY CONNECTION
69+
//
70+
try {
71+
Connection conn = DriverManager.getConnection(url, props);
72+
System.out.println("\033[32mSuccess >> database is up\033[0m");
73+
System.exit(0);
74+
} catch (Exception e) {
75+
if (cmd.hasOption("verbose")) {
76+
System.out.println(" . " + e.getMessage());
77+
} else {
78+
System.out.println(" .");
79+
}
80+
Thread.sleep(30000); // sleep 30s
81+
}
82+
}
83+
//
84+
// FINALLY (after maxwait reached)
85+
//
86+
System.out.println("\033[31mError >> Could not connect to db. Max wait reached. Exit.\033[0m");
87+
System.exit(1);
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)