From de4f2cd7b325fc05618442783e4b701bccb43cbc Mon Sep 17 00:00:00 2001 From: Lumijiez Date: Thu, 21 Nov 2024 23:31:49 +0200 Subject: [PATCH] Initialization, dockerization, structure, RabbitMQ setup, docker-compose --- .gitignore | 39 ++++++++ .idea/.gitignore | 8 ++ .idea/misc.xml | 14 +++ .idea/vcs.xml | 6 ++ SymphonyManager/Dockerfile | 24 +++++ SymphonyManager/pom.xml | 42 +++++++++ .../main/java/io/github/lumijiez/Main.java | 17 ++++ SymphonyProducer/Dockerfile | 24 +++++ SymphonyProducer/dependency-reduced-pom.xml | 42 +++++++++ SymphonyProducer/pom.xml | 84 +++++++++++++++++ .../main/java/io/github/lumijiez/Main.java | 91 +++++++++++++++++++ docker-compose.yml | 40 ++++++++ pom.xml | 34 +++++++ 13 files changed, 465 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml create mode 100644 SymphonyManager/Dockerfile create mode 100644 SymphonyManager/pom.xml create mode 100644 SymphonyManager/src/main/java/io/github/lumijiez/Main.java create mode 100644 SymphonyProducer/Dockerfile create mode 100644 SymphonyProducer/dependency-reduced-pom.xml create mode 100644 SymphonyProducer/pom.xml create mode 100644 SymphonyProducer/src/main/java/io/github/lumijiez/Main.java create mode 100644 docker-compose.yml create mode 100644 pom.xml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..934c7d6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,39 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +**/.idea/ +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..fdc35ea --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/SymphonyManager/Dockerfile b/SymphonyManager/Dockerfile new file mode 100644 index 0000000..3624fdd --- /dev/null +++ b/SymphonyManager/Dockerfile @@ -0,0 +1,24 @@ +FROM ubuntu:latest +LABEL authors="lumijiez" + +FROM maven:3.9.8-eclipse-temurin-21 AS build + +WORKDIR /app + +COPY pom.xml . + +RUN mvn dependency:go-offline + +COPY src /app/src + +RUN mvn clean package -DskipTests + +FROM openjdk:21 + +WORKDIR /app + +COPY --from=build /app/target/SymphonyManager-1.0-SNAPSHOT.jar /app/SymphonyManager.jar + +EXPOSE 8081 + +ENTRYPOINT ["java", "-jar", "SymphonyManager.jar"] diff --git a/SymphonyManager/pom.xml b/SymphonyManager/pom.xml new file mode 100644 index 0000000..3a4ee54 --- /dev/null +++ b/SymphonyManager/pom.xml @@ -0,0 +1,42 @@ + + + 4.0.0 + io.github.lumijiez + SymphonyManager + 1.0-SNAPSHOT + + jar + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 21 + 21 + + + + + org.apache.maven.plugins + maven-jar-plugin + 3.1.0 + + + + io.github.lumijiez.Main + + + + + + + + diff --git a/SymphonyManager/src/main/java/io/github/lumijiez/Main.java b/SymphonyManager/src/main/java/io/github/lumijiez/Main.java new file mode 100644 index 0000000..6d2c214 --- /dev/null +++ b/SymphonyManager/src/main/java/io/github/lumijiez/Main.java @@ -0,0 +1,17 @@ +package io.github.lumijiez; + +//TIP To Run code, press or +// click the icon in the gutter. +public class Main { + public static void main(String[] args) { + //TIP Press with your caret at the highlighted text + // to see how IntelliJ IDEA suggests fixing it. + System.out.printf("Hello and welcome!"); + + for (int i = 1; i <= 5; i++) { + //TIP Press to start debugging your code. We have set one breakpoint + // for you, but you can always add more by pressing . + System.out.println("i = " + i); + } + } +} \ No newline at end of file diff --git a/SymphonyProducer/Dockerfile b/SymphonyProducer/Dockerfile new file mode 100644 index 0000000..6713868 --- /dev/null +++ b/SymphonyProducer/Dockerfile @@ -0,0 +1,24 @@ +FROM ubuntu:latest +LABEL authors="lumijiez" + +FROM maven:3.9.9-eclipse-temurin-21 AS build + +WORKDIR /app + +COPY pom.xml . + +RUN mvn dependency:go-offline + +COPY src /app/src + +RUN mvn clean package -DskipTests + +FROM openjdk:21 + +WORKDIR /app + +COPY --from=build /app/target/SymphonyProducer-1.0-SNAPSHOT.jar /app/SymphonyProducer.jar + +EXPOSE 8082 + +ENTRYPOINT ["java", "-jar", "SymphonyProducer.jar"] diff --git a/SymphonyProducer/dependency-reduced-pom.xml b/SymphonyProducer/dependency-reduced-pom.xml new file mode 100644 index 0000000..875fc00 --- /dev/null +++ b/SymphonyProducer/dependency-reduced-pom.xml @@ -0,0 +1,42 @@ + + + 4.0.0 + io.github.lumijiez + SymphonyProducer + 1.0-SNAPSHOT + + + + maven-compiler-plugin + 3.8.1 + + 21 + 21 + + + + maven-shade-plugin + 3.2.1 + + + package + + shade + + + + + + maven-jar-plugin + 3.1.0 + + + + io.github.lumijiez.Main + + + + + + + diff --git a/SymphonyProducer/pom.xml b/SymphonyProducer/pom.xml new file mode 100644 index 0000000..8fa328a --- /dev/null +++ b/SymphonyProducer/pom.xml @@ -0,0 +1,84 @@ + + + 4.0.0 + io.github.lumijiez + SymphonyProducer + 1.0-SNAPSHOT + + jar + + + + com.rabbitmq + amqp-client + 5.23.0 + + + + com.google.code.gson + gson + 2.11.0 + + + + org.slf4j + slf4j-api + 1.7.32 + + + + ch.qos.logback + logback-classic + 1.5.12 + + + + ch.qos.logback + logback-core + 1.5.12 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 21 + 21 + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.1 + + + package + + shade + + + + + + + org.apache.maven.plugins + maven-jar-plugin + 3.1.0 + + + + io.github.lumijiez.Main + + + + + + + diff --git a/SymphonyProducer/src/main/java/io/github/lumijiez/Main.java b/SymphonyProducer/src/main/java/io/github/lumijiez/Main.java new file mode 100644 index 0000000..92d3f1a --- /dev/null +++ b/SymphonyProducer/src/main/java/io/github/lumijiez/Main.java @@ -0,0 +1,91 @@ +package io.github.lumijiez; + +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; +import com.rabbitmq.client.ConnectionFactory; +import com.rabbitmq.client.Connection; +import com.rabbitmq.client.Channel; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.Random; +import java.util.Timer; +import java.util.TimerTask; +import java.util.concurrent.TimeoutException; + +public class Main { + private static final String QUEUE_NAME = "random_json_queue"; + private static final String RABBITMQ_HOST = "rabbitmq"; + private static final String RABBITMQ_USER = "symphony"; + private static final String RABBITMQ_PASSWORD = "symphony"; + + public static void main(String[] args) { + ConnectionFactory factory = new ConnectionFactory(); + factory.setHost(RABBITMQ_HOST); + factory.setUsername(RABBITMQ_USER); + factory.setPassword(RABBITMQ_PASSWORD); + + Connection connection = null; + Channel channel = null; + + while (true) { + try { + System.out.println("Attempting to connect to RabbitMQ..."); + + connection = factory.newConnection(); + channel = connection.createChannel(); + + channel.queueDeclare(QUEUE_NAME, false, false, false, null); + System.out.println("Connected to RabbitMQ and queue declared."); + + Timer timer = new Timer(true); + Channel finalChannel = channel; + timer.scheduleAtFixedRate(new TimerTask() { + @Override + public void run() { + try { + String jsonMessage = generateRandomJson(); + finalChannel.basicPublish("", QUEUE_NAME, null, jsonMessage.getBytes(StandardCharsets.UTF_8)); + System.out.println("Sent: " + jsonMessage); + } catch (IOException e) { + System.err.println("Failed to send message: " + e.getMessage()); + } + } + }, 0, 10000); + + System.out.println("Press Ctrl+C to exit."); + Thread.sleep(Long.MAX_VALUE); + break; + + } catch (IOException | TimeoutException | InterruptedException e) { + System.err.println("Failed to connect to RabbitMQ: " + e.getMessage()); + System.err.println("Retrying in 5 seconds..."); + try { + if (connection != null && connection.isOpen()) { + connection.close(); + } + if (channel != null && channel.isOpen()) { + channel.close(); + } + } catch (IOException | TimeoutException ignored) { + } + + try { + Thread.sleep(5000); + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + } + } + } + } + + private static String generateRandomJson() { + Random random = new Random(); + JsonObject jsonObject = new JsonObject(); + jsonObject.add("id", new JsonPrimitive(random.nextInt(1000))); + jsonObject.add("name", new JsonPrimitive("Item_" + random.nextInt(100))); + jsonObject.add("value", new JsonPrimitive(random.nextDouble() * 100)); + + return jsonObject.toString(); + } +} diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..dc6e1bd --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,40 @@ +services: + symphony-manager: + build: + context: ./SymphonyManager + dockerfile: Dockerfile + ports: + - "8081:8081" + networks: + - symphony-network + + symphony-producer: + build: + context: ./SymphonyProducer + dockerfile: Dockerfile + ports: + - "8082:8082" + networks: + - symphony-network + + rabbitmq: + image: "rabbitmq:management" + ports: + - "5672:5672" + - "15672:15672" + environment: + - RABBITMQ_DEFAULT_USER=symphony + - RABBITMQ_DEFAULT_PASS=symphony + - RABBITMQ_NODENAME=rabbit@rabbitmq + volumes: + - rabbitmq_data:/var/lib/rabbitmq + networks: + - symphony-network + restart: always + +networks: + symphony-network: + driver: bridge + +volumes: + rabbitmq_data: \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..42d7a6b --- /dev/null +++ b/pom.xml @@ -0,0 +1,34 @@ + + + 4.0.0 + + io.github.lumijiez + Symphony + 1.0-SNAPSHOT + + pom + + + SymphonyManager + SymphonyProducer + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 21 + 21 + + + + +