envoyerDate

This commit is contained in:
trochas
2025-03-06 15:05:51 +01:00
parent eeb7ce4933
commit ce935de1d7

View File

@@ -1,5 +1,40 @@
package fr.istic.date;
public class EnvoyerDate {
import java.util.Date;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class EnvoyerDate {
private static final String EXCHANGE_NAME = "logs";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setUri("amqps://cyvthtfj:O8LmaXkX5mVB0oFZN9TobaK8rX9wEhol@whale.rmq.cloudamqp.com/cyvthtfj");
try (Connection connection = factory.newConnection();
Channel channel = connection.createChannel()) {
while(true){
channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
String message = argv.length < 1 ? getDate()
: String.join(" ", argv);
channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes("UTF-8"));
System.out.println(" [x] Sent '" + message + "'");
try {
Thread.sleep(1000); // Attend 1 seconde (1000 millisecondes)
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static String getDate(){
return (new Date()).toString();
}
}