queues, and posting to db works

This commit is contained in:
Daniel
2024-12-13 00:03:02 +02:00
parent 8b5f755152
commit 20b7465d6b
9 changed files with 171 additions and 76 deletions

View File

@@ -1,24 +1,22 @@
package io.github.lumijiez;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import static io.github.lumijiez.Main.logger;
public class AddressHolder {
private String host;
private int port;
private static AddressHolder INSTANCE;
private AddressHolder() {
// Timer timer = new Timer(true);
// timer.scheduleAtFixedRate(new TimerTask() {
// @Override
// public void run() {
// Main.logger.info("Host: {}, Port: {}", host, port);
// }
// }, 0, 1000);
}
public static AddressHolder getInstance() {
if (INSTANCE == null) {
INSTANCE = new AddressHolder();

View File

@@ -7,13 +7,18 @@ import com.rabbitmq.client.DeliverCallback;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import static io.github.lumijiez.Main.logger;
public class BrokerConnector {
private static final String QUEUE_NAME = "random_json_queue";
private static final String QUEUE_NAME = "random_sha";
private static final String RABBITMQ_HOST = "rabbitmq";
private static final String RABBITMQ_USER = "symphony";
private static final String RABBITMQ_PASSWORD = "symphony";
@@ -49,11 +54,38 @@ public class BrokerConnector {
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
logger.info("Connected to RabbitMQ and queue declared");
DeliverCallback deliverCallback = (consumerTag, delivery) -> channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
System.out.println("Received message: " + message);
try {
sendPostRequest(AddressHolder.getInstance().getHost(), AddressHolder.getInstance().getPort(), message);
} catch (Exception e) {
throw new RuntimeException(e);
}
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
};
channel.basicConsume(QUEUE_NAME, false, deliverCallback, consumerTag -> {});
shutdownLatch.await();
}
}
private static void sendPostRequest(String host, int port, String data) throws Exception {
URL url = new URL("http://" + host + ":" + port + "/push");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "text/plain");
connection.setDoOutput(true);
byte[] postData = data.getBytes(StandardCharsets.UTF_8);
connection.getOutputStream().write(postData);
int responseCode = connection.getResponseCode();
logger.info("POST to {}:{}/push with data: {}, Response Code: {}", host, port, data, responseCode);
connection.getInputStream().close();
}
}