Initialization, dockerization, structure, RabbitMQ setup, docker-compose

This commit is contained in:
2024-11-21 23:31:49 +02:00
commit de4f2cd7b3
13 changed files with 465 additions and 0 deletions

View File

@@ -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"]

View File

@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.lumijiez</groupId>
<artifactId>SymphonyProducer</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>21</source>
<target>21</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifestEntries>
<Main-Class>io.github.lumijiez.Main</Main-Class>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>

84
SymphonyProducer/pom.xml Normal file
View File

@@ -0,0 +1,84 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.lumijiez</groupId>
<artifactId>SymphonyProducer</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.23.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.12</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.5.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>21</source>
<target>21</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifestEntries>
<Main-Class>io.github.lumijiez.Main</Main-Class>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -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();
}
}