use of ProducerDummy.Messages.JsonMessage in project amos2022ss02-audit-chain by amosproj.
the class AggregateConsumerClient method start.
public void start() throws IOException, TimeoutException {
System.out.println("Starting to receive Messages.");
Connection connection = this.factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
AggregateMessage message;
try {
message = (AggregateMessage) deserializeMessage(delivery.getBody());
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
Vector<Message> messages = message.getMessages();
for (int i = 0; i < message.getMessageSize(); i++) {
Message single_message = messages.get(i);
this.persistenceStrategy.StoreMessage(new JsonMessage(single_message.getSequence_number(), single_message.getMessage()));
System.out.println(String.format("Received event %d with the content: %s", single_message.getSequence_number(), single_message.getMessage()));
}
};
channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
});
}
use of ProducerDummy.Messages.JsonMessage in project amos2022ss02-audit-chain by amosproj.
the class Client method start.
/**
* Start Sending Messages as JSON to the RabbitMQ Server.
* TODO sending Messages is still bad and just a minimal example make it better
* @throws IOException if an I/O error occurs
* @throws TimeoutException if the timeout expires
* @throws InterruptedException if the thread is interrupted
*/
public void start() throws IOException, TimeoutException {
System.out.println("Starting to send Messages.Message to AMQP Host");
try (Connection connection = this.factory.newConnection();
Channel channel = connection.createChannel()) {
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
// Recover last Message which was stored in file and maybe was not send
Message message = this.recoverLastMessage();
if (message != null) {
channel.basicPublish("", QUEUE_NAME, null, serialize(message));
this.sequence_number += 1;
}
for (String line = this.dataGenerator.getData(); line != null; line = this.dataGenerator.getData()) {
System.out.println("The following Messages.Message will be send:\n" + line);
message = new JsonMessage(this.sequence_number, line);
this.persistenceStrategy.StoreMessage(message);
channel.basicPublish("", QUEUE_NAME, null, serialize(message));
this.sequence_number += 1;
try {
TimeUnit.SECONDS.sleep(SECOND_DELAY_BETWEEN_MESSAGES);
} catch (InterruptedException e) {
System.out.println("Thread was interrupted");
}
}
}
}
use of ProducerDummy.Messages.JsonMessage in project amos2022ss02-audit-chain by amosproj.
the class AggregateMessageFilePersistence method ReadLastMessage.
@Override
public Message ReadLastMessage() {
// TODO right now even if there are no Messages an empty Message will be returned, it should return NULL tho
AggregateMessage messages = new AggregateMessage();
File file = new File(this.filepath.toString());
try {
BufferedReader br = new BufferedReader(new FileReader(file));
for (String line = br.readLine(); line != null; line = br.readLine()) {
int number = Integer.parseInt(line);
String message = br.readLine();
messages.addMessage(new JsonMessage(number, message));
}
br.close();
} catch (IOException e) {
// there are no values in the file or the file is missing therefore we never did send a Messages.Message to the Broker
return null;
}
return messages;
}
Aggregations