Skip to content

Commit 5128584

Browse files
author
Mano Marks
authored
Merge pull request dockersamples#50 from ManoMarks/aanand-restore-java-worker
Java and .NET Workers can co-exist
2 parents 1e6eb6a + 3dd2b2d commit 5128584

File tree

5 files changed

+260
-3
lines changed

5 files changed

+260
-3
lines changed

docker-compose-javaworker.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
version: "2"
2+
3+
services:
4+
vote:
5+
build: ./vote
6+
command: python app.py
7+
volumes:
8+
- ./vote:/app
9+
ports:
10+
- "5000:80"
11+
networks:
12+
- front-tier
13+
- back-tier
14+
15+
result:
16+
build: ./result
17+
command: nodemon --debug server.js
18+
volumes:
19+
- ./result:/app
20+
ports:
21+
- "5001:80"
22+
- "5858:5858"
23+
networks:
24+
- front-tier
25+
- back-tier
26+
27+
worker:
28+
build: ./worker/Dockerfile.j
29+
networks:
30+
- back-tier
31+
32+
redis:
33+
image: redis:alpine
34+
container_name: redis
35+
ports: ["6379"]
36+
networks:
37+
- back-tier
38+
39+
db:
40+
image: postgres:9.4
41+
container_name: db
42+
volumes:
43+
- "db-data:/var/lib/postgresql/data"
44+
networks:
45+
- back-tier
46+
47+
volumes:
48+
db-data:
49+
50+
networks:
51+
front-tier:
52+
back-tier:

worker/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
FROM microsoft/dotnet:1.0.0-preview2-sdk
22

3-
WORKDIR /app
3+
WORKDIR /code
44

5-
ADD src/ /app/src/
5+
ADD src/Worker /code/src/Worker
66

77
RUN dotnet restore -v minimal src/ \
88
&& dotnet publish -c Release -o ./ src/Worker/ \
99
&& rm -rf src/ $HOME/.nuget/
1010

11-
CMD dotnet Worker.dll
11+
CMD dotnet Worker.dll

worker/Dockerfile.j

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM java:openjdk-8-jdk-alpine
2+
3+
RUN MAVEN_VERSION=3.3.3 \
4+
&& cd /usr/share \
5+
&& wget http://archive.apache.org/dist/maven/maven-3/$MAVEN_VERSION/binaries/apache-maven-$MAVEN_VERSION-bin.tar.gz -O - | tar xzf - \
6+
&& mv /usr/share/apache-maven-$MAVEN_VERSION /usr/share/maven \
7+
&& ln -s /usr/share/maven/bin/mvn /usr/bin/mvn
8+
9+
WORKDIR /code
10+
11+
ADD pom.xml /code/pom.xml
12+
RUN ["mvn", "dependency:resolve"]
13+
RUN ["mvn", "verify"]
14+
15+
# Adding source, compile and package into a fat jar
16+
ADD src/main /code/src/main
17+
RUN ["mvn", "package"]
18+
19+
CMD ["java", "-jar", "target/worker-jar-with-dependencies.jar"]

worker/pom.xml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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>worker</groupId>
7+
<artifactId>worker</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>org.json</groupId>
14+
<artifactId>json</artifactId>
15+
<version>20140107</version>
16+
</dependency>
17+
18+
<dependency>
19+
<groupId>redis.clients</groupId>
20+
<artifactId>jedis</artifactId>
21+
<version>2.7.2</version>
22+
<type>jar</type>
23+
<scope>compile</scope>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>org.postgresql</groupId>
28+
<artifactId>postgresql</artifactId>
29+
<version>9.4-1200-jdbc41</version>
30+
</dependency>
31+
</dependencies>
32+
33+
<build>
34+
<plugins>
35+
<plugin>
36+
<groupId>org.apache.maven.plugins</groupId>
37+
<artifactId>maven-jar-plugin</artifactId>
38+
<version>2.4</version>
39+
<configuration>
40+
<finalName>worker</finalName>
41+
<archive>
42+
<manifest>
43+
<addClasspath>true</addClasspath>
44+
<mainClass>worker.Worker</mainClass>
45+
<classpathPrefix>dependency-jars/</classpathPrefix>
46+
</manifest>
47+
</archive>
48+
</configuration>
49+
</plugin>
50+
<plugin>
51+
<groupId>org.apache.maven.plugins</groupId>
52+
<artifactId>maven-compiler-plugin</artifactId>
53+
<version>3.1</version>
54+
<configuration>
55+
<source>1.7</source>
56+
<target>1.7</target>
57+
</configuration>
58+
</plugin>
59+
<plugin>
60+
<groupId>org.apache.maven.plugins</groupId>
61+
<artifactId>maven-assembly-plugin</artifactId>
62+
<executions>
63+
<execution>
64+
<goals>
65+
<goal>attached</goal>
66+
</goals>
67+
<phase>package</phase>
68+
<configuration>
69+
<finalName>worker</finalName>
70+
<descriptorRefs>
71+
<descriptorRef>jar-with-dependencies</descriptorRef>
72+
</descriptorRefs>
73+
<archive>
74+
<manifest>
75+
<mainClass>worker.Worker</mainClass>
76+
</manifest>
77+
</archive>
78+
</configuration>
79+
</execution>
80+
</executions>
81+
</plugin>
82+
</plugins>
83+
</build>
84+
</project>
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package worker;
2+
3+
import redis.clients.jedis.Jedis;
4+
import redis.clients.jedis.exceptions.JedisConnectionException;
5+
import java.sql.*;
6+
import org.json.JSONObject;
7+
8+
class Worker {
9+
public static void main(String[] args) {
10+
try {
11+
Jedis redis = connectToRedis("redis");
12+
Connection dbConn = connectToDB("db");
13+
14+
System.err.println("Watching vote queue");
15+
16+
while (true) {
17+
String voteJSON = redis.blpop(0, "votes").get(1);
18+
JSONObject voteData = new JSONObject(voteJSON);
19+
String voterID = voteData.getString("voter_id");
20+
String vote = voteData.getString("vote");
21+
22+
System.err.printf("Processing vote for '%s' by '%s'\n", vote, voterID);
23+
updateVote(dbConn, voterID, vote);
24+
}
25+
} catch (SQLException e) {
26+
e.printStackTrace();
27+
System.exit(1);
28+
}
29+
}
30+
31+
static void updateVote(Connection dbConn, String voterID, String vote) throws SQLException {
32+
PreparedStatement insert = dbConn.prepareStatement(
33+
"INSERT INTO votes (id, vote) VALUES (?, ?)");
34+
insert.setString(1, voterID);
35+
insert.setString(2, vote);
36+
37+
try {
38+
insert.executeUpdate();
39+
} catch (SQLException e) {
40+
PreparedStatement update = dbConn.prepareStatement(
41+
"UPDATE votes SET vote = ? WHERE id = ?");
42+
update.setString(1, vote);
43+
update.setString(2, voterID);
44+
update.executeUpdate();
45+
}
46+
}
47+
48+
static Jedis connectToRedis(String host) {
49+
Jedis conn = new Jedis(host);
50+
51+
while (true) {
52+
try {
53+
conn.keys("*");
54+
break;
55+
} catch (JedisConnectionException e) {
56+
System.err.println("Waiting for redis");
57+
sleep(1000);
58+
}
59+
}
60+
61+
System.err.println("Connected to redis");
62+
return conn;
63+
}
64+
65+
static Connection connectToDB(String host) throws SQLException {
66+
Connection conn = null;
67+
68+
try {
69+
70+
Class.forName("org.postgresql.Driver");
71+
String url = "jdbc:postgresql://" + host + "/postgres";
72+
73+
while (conn == null) {
74+
try {
75+
conn = DriverManager.getConnection(url, "postgres", "");
76+
} catch (SQLException e) {
77+
System.err.println("Waiting for db");
78+
sleep(1000);
79+
}
80+
}
81+
82+
PreparedStatement st = conn.prepareStatement(
83+
"CREATE TABLE IF NOT EXISTS votes (id VARCHAR(255) NOT NULL UNIQUE, vote VARCHAR(255) NOT NULL)");
84+
st.executeUpdate();
85+
86+
} catch (ClassNotFoundException e) {
87+
e.printStackTrace();
88+
System.exit(1);
89+
}
90+
91+
System.err.println("Connected to db");
92+
return conn;
93+
}
94+
95+
static void sleep(long duration) {
96+
try {
97+
Thread.sleep(duration);
98+
} catch (InterruptedException e) {
99+
System.exit(1);
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)