From 2919af7ea3aa84553ea3c5dc08485b7d153d3517 Mon Sep 17 00:00:00 2001 From: Anca Iordache Date: Thu, 5 Mar 2020 19:19:41 +0100 Subject: [PATCH] add sparkjava-mysql application sample Signed-off-by: Anca Iordache --- samples/sparkjava-mysql/backend/Dockerfile | 10 +++ samples/sparkjava-mysql/backend/pom.xml | 59 ++++++++++++++ .../backend/src/main/java/App.java | 78 +++++++++++++++++++ samples/sparkjava-mysql/db/password.txt | 1 + samples/sparkjava-mysql/docker-compose.yaml | 24 ++++++ 5 files changed, 172 insertions(+) create mode 100755 samples/sparkjava-mysql/backend/Dockerfile create mode 100755 samples/sparkjava-mysql/backend/pom.xml create mode 100755 samples/sparkjava-mysql/backend/src/main/java/App.java create mode 100644 samples/sparkjava-mysql/db/password.txt create mode 100644 samples/sparkjava-mysql/docker-compose.yaml diff --git a/samples/sparkjava-mysql/backend/Dockerfile b/samples/sparkjava-mysql/backend/Dockerfile new file mode 100755 index 0000000..4df8594 --- /dev/null +++ b/samples/sparkjava-mysql/backend/Dockerfile @@ -0,0 +1,10 @@ +FROM maven:3.5-jdk-8-alpine AS build +COPY pom.xml . +RUN mvn --batch-mode dependency:resolve +COPY src/ src +RUN mvn --batch-mode clean compile assembly:single + +FROM openjdk:8-jre-alpine3.7 +EXPOSE 8080 +COPY --from=build target/app.jar /app.jar +CMD java -jar /app.jar diff --git a/samples/sparkjava-mysql/backend/pom.xml b/samples/sparkjava-mysql/backend/pom.xml new file mode 100755 index 0000000..aebd30c --- /dev/null +++ b/samples/sparkjava-mysql/backend/pom.xml @@ -0,0 +1,59 @@ + + + 4.0.0 + + com.company + project + 1.0-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + maven-assembly-plugin + + + + App + + + + jar-with-dependencies + + app + false + + + + + + + + com.sparkjava + spark-core + 2.5 + + + + mysql + mysql-connector-java + 8.0.11 + + + com.google.code.gson + gson + 2.8.4 + + + + + \ No newline at end of file diff --git a/samples/sparkjava-mysql/backend/src/main/java/App.java b/samples/sparkjava-mysql/backend/src/main/java/App.java new file mode 100755 index 0000000..83cef38 --- /dev/null +++ b/samples/sparkjava-mysql/backend/src/main/java/App.java @@ -0,0 +1,78 @@ +import com.google.gson.Gson; +import com.mysql.cj.jdbc.exceptions.CommunicationsException; + +import static spark.Spark.*; + +import java.nio.file.Files; +import java.nio.file.Paths; +import java.sql.*; +import java.util.ArrayList; +import java.util.List; + +public class App { + + public static void main(String[] args) throws Exception { + Class.forName("com.mysql.cj.jdbc.Driver"); + + prepare(); + + port(8080); + + get("/", (req, res) -> new Gson().toJson(titles())); + } + + private static List titles() { + try (Connection conn = connect()) { + List titles = new ArrayList<>(); + try (Statement stmt = conn.createStatement()) { + try (ResultSet rs = stmt.executeQuery("SELECT title FROM blog")) { + while (rs.next()) { + titles.add(rs.getString("title")); + } + } + } + return titles; + } catch (Exception ex) { + return new ArrayList<>(); + } + } + + public static void prepare() throws Exception { + try (Connection conn = connect()) { + recreateTable(conn); + insertData(conn); + } + } + + private static void insertData(Connection conn) throws SQLException { + for (int i = 0; i < 5; i++) { + try (PreparedStatement stmt = conn.prepareStatement("INSERT INTO blog (title) VALUES (?);")) { + stmt.setString(1, "Blog post #" + i); + stmt.execute(); + } + } + } + + private static void recreateTable(Connection conn) throws SQLException { + try (Statement stmt = conn.createStatement()) { + stmt.execute("DROP TABLE IF EXISTS blog"); + stmt.execute("CREATE TABLE IF NOT EXISTS blog (id int NOT NULL AUTO_INCREMENT, title varchar(255), PRIMARY KEY (id))"); + } + } + + + private static Connection connect() throws Exception { + for (int i = 0; i < 60; i++) { + try { + return DriverManager.getConnection("jdbc:mysql://db/example?useSSL=false", "root", Files.lines(Paths.get("/run/secrets/db-password")).findFirst().get()); + } catch (CommunicationsException ex) { + Thread.sleep(1000L); + continue; + } catch (Exception ex) { + throw ex; + } + } + throw new RuntimeException("Unable to connect to MySQL"); + } +} + diff --git a/samples/sparkjava-mysql/db/password.txt b/samples/sparkjava-mysql/db/password.txt new file mode 100644 index 0000000..447c7b8 --- /dev/null +++ b/samples/sparkjava-mysql/db/password.txt @@ -0,0 +1 @@ +db-vp2lf \ No newline at end of file diff --git a/samples/sparkjava-mysql/docker-compose.yaml b/samples/sparkjava-mysql/docker-compose.yaml new file mode 100644 index 0000000..647c1f3 --- /dev/null +++ b/samples/sparkjava-mysql/docker-compose.yaml @@ -0,0 +1,24 @@ +version: "3.7" +services: + backend: + build: backend + image: docker.io/docker/back + ports: + - 80:8080 + secrets: + - db-password + db: + environment: + MYSQL_DATABASE: example + MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db-password + image: mysql:5.7 + restart: always + secrets: + - db-password + volumes: + - db-data:/var/lib/mysql +volumes: + db-data: {} +secrets: + db-password: + file: db/password.txt