From 2999fb294c32f334943c127fd764201361468260 Mon Sep 17 00:00:00 2001 From: reynisson Date: Sun, 14 Nov 2021 15:16:42 +0100 Subject: [PATCH] Implemented the auction started event over mqtt --- .../ch/unisg/common/ConfigProperties.java | 5 ++- .../tapas/TapasAuctionHouseApplication.java | 4 +-- .../common/clients/TapasMqttClient.java | 5 ++- .../mqtt/AuctionEventsMqttDispatcher.java | 2 +- ...PublishAuctionStartedEventMqttAdapter.java | 36 +++++++++++++++++++ ...blishAuctionStartedEventWebSubAdapter.java | 1 - .../unisg/tapas/common/ConfigProperties.java | 10 ++++++ .../src/main/resources/application.properties | 3 ++ 8 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/out/messaging/websub/PublishAuctionStartedEventMqttAdapter.java diff --git a/executor-pool/src/main/java/ch/unisg/common/ConfigProperties.java b/executor-pool/src/main/java/ch/unisg/common/ConfigProperties.java index b46bf63..253922c 100644 --- a/executor-pool/src/main/java/ch/unisg/common/ConfigProperties.java +++ b/executor-pool/src/main/java/ch/unisg/common/ConfigProperties.java @@ -12,10 +12,9 @@ public class ConfigProperties { private Environment environment; /** - * Retrieves the URI of the WebSub hub. In this project, we use a single WebSub hub, but we could - * use multiple. + * Retrieves the URI of the MQTT broker. * - * @return the URI of the WebSub hub + * @return the URI of the MQTT broker */ public URI getMqttBrokerUri() { return URI.create(environment.getProperty("mqtt.broker.uri")); diff --git a/tapas-auction-house/src/main/java/ch/unisg/tapas/TapasAuctionHouseApplication.java b/tapas-auction-house/src/main/java/ch/unisg/tapas/TapasAuctionHouseApplication.java index 8fc22d0..db57cc7 100644 --- a/tapas-auction-house/src/main/java/ch/unisg/tapas/TapasAuctionHouseApplication.java +++ b/tapas-auction-house/src/main/java/ch/unisg/tapas/TapasAuctionHouseApplication.java @@ -21,14 +21,14 @@ public class TapasAuctionHouseApplication { private static final Logger LOGGER = LogManager.getLogger(TapasAuctionHouseApplication.class); public static String RESOURCE_DIRECTORY = "https://api.interactions.ics.unisg.ch/auction-houses/"; - public static String MQTT_BROKER = "tcp://broker.hivemq.com:1883"; + public static String MQTT_BROKER = "tcp://localhost:1883"; public static void main(String[] args) { SpringApplication tapasAuctioneerApp = new SpringApplication(TapasAuctionHouseApplication.class); // We will use these bootstrap methods in Week 6: // bootstrapMarketplaceWithWebSub(); - // bootstrapMarketplaceWithMqtt(); + bootstrapMarketplaceWithMqtt(); tapasAuctioneerApp.run(args); } diff --git a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/common/clients/TapasMqttClient.java b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/common/clients/TapasMqttClient.java index 708d512..db5903c 100644 --- a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/common/clients/TapasMqttClient.java +++ b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/common/clients/TapasMqttClient.java @@ -68,7 +68,10 @@ public class TapasMqttClient { mqttClient.subscribe(topic); } - private void publishMessage(String topic, String payload) throws MqttException { + public void publishMessage(String topic, String payload) throws MqttException { + mqttClient = new org.eclipse.paho.client.mqttv3.MqttClient(brokerAddress, mqttClientId, new MemoryPersistence()); + mqttClient.connect(); + MqttMessage message = new MqttMessage(payload.getBytes(StandardCharsets.UTF_8)); mqttClient.publish(topic, message); } diff --git a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/in/messaging/mqtt/AuctionEventsMqttDispatcher.java b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/in/messaging/mqtt/AuctionEventsMqttDispatcher.java index e5eaf12..3e55d5e 100644 --- a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/in/messaging/mqtt/AuctionEventsMqttDispatcher.java +++ b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/in/messaging/mqtt/AuctionEventsMqttDispatcher.java @@ -26,7 +26,7 @@ public class AuctionEventsMqttDispatcher { // TODO: Register here your topics and event listener adapters private void initRouter() { - router.put("ch/unisg/tapas-group-tutors/executors", new ExecutorAddedEventListenerMqttAdapter()); + router.put("ch/unisg/tapas/executors/added", new ExecutorAddedEventListenerMqttAdapter()); } /** diff --git a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/out/messaging/websub/PublishAuctionStartedEventMqttAdapter.java b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/out/messaging/websub/PublishAuctionStartedEventMqttAdapter.java new file mode 100644 index 0000000..d5bb0fc --- /dev/null +++ b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/out/messaging/websub/PublishAuctionStartedEventMqttAdapter.java @@ -0,0 +1,36 @@ +package ch.unisg.tapas.auctionhouse.adapter.out.messaging.websub; + +import ch.unisg.tapas.auctionhouse.adapter.common.clients.TapasMqttClient; +import ch.unisg.tapas.auctionhouse.adapter.common.formats.AuctionJsonRepresentation; +import ch.unisg.tapas.auctionhouse.adapter.in.messaging.mqtt.AuctionEventsMqttDispatcher; +import ch.unisg.tapas.auctionhouse.application.port.out.AuctionStartedEventPort; +import ch.unisg.tapas.auctionhouse.domain.AuctionStartedEvent; +import ch.unisg.tapas.common.ConfigProperties; +import com.fasterxml.jackson.core.JsonProcessingException; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.eclipse.paho.client.mqttv3.MqttException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Primary; +import org.springframework.stereotype.Component; + +@Component +@Primary +public class PublishAuctionStartedEventMqttAdapter implements AuctionStartedEventPort { + + private static final Logger LOGGER = LogManager.getLogger(PublishAuctionStartedEventMqttAdapter.class); + + @Autowired + private ConfigProperties config; + + @Override + public void publishAuctionStartedEvent(AuctionStartedEvent event) { + try{ + var mqttClient = TapasMqttClient.getInstance(config.getMqttBrokerUri().toString(), new AuctionEventsMqttDispatcher()); + mqttClient.publishMessage("ch/unisg/tapas/auctions", AuctionJsonRepresentation.serialize(event.getAuction())); + } + catch (MqttException | JsonProcessingException e){ + LOGGER.error(e.getMessage(), e); + } + } +} diff --git a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/out/messaging/websub/PublishAuctionStartedEventWebSubAdapter.java b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/out/messaging/websub/PublishAuctionStartedEventWebSubAdapter.java index 9e6ec67..73451e4 100644 --- a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/out/messaging/websub/PublishAuctionStartedEventWebSubAdapter.java +++ b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/out/messaging/websub/PublishAuctionStartedEventWebSubAdapter.java @@ -23,7 +23,6 @@ import java.util.stream.Collectors; * This class is a template for publishing auction started events via WebSub. */ @Component -@Primary public class PublishAuctionStartedEventWebSubAdapter implements AuctionStartedEventPort { // You can use this object to retrieve properties from application.properties, e.g. the // WebSub hub publish endpoint, etc. diff --git a/tapas-auction-house/src/main/java/ch/unisg/tapas/common/ConfigProperties.java b/tapas-auction-house/src/main/java/ch/unisg/tapas/common/ConfigProperties.java index 748afda..2933465 100644 --- a/tapas-auction-house/src/main/java/ch/unisg/tapas/common/ConfigProperties.java +++ b/tapas-auction-house/src/main/java/ch/unisg/tapas/common/ConfigProperties.java @@ -61,4 +61,14 @@ public class ConfigProperties { public URI getTaskListUri() { return URI.create(environment.getProperty("tasks.list.uri")); } + + + /** + * Retrieves the URI of the MQTT broker. + * + * @return the URI of the MQTT broker + */ + public URI getMqttBrokerUri() { + return URI.create(environment.getProperty("mqtt.broker.uri")); + } } diff --git a/tapas-auction-house/src/main/resources/application.properties b/tapas-auction-house/src/main/resources/application.properties index e9c609f..1ededee 100644 --- a/tapas-auction-house/src/main/resources/application.properties +++ b/tapas-auction-house/src/main/resources/application.properties @@ -6,3 +6,6 @@ websub.hub.publish=https://websub.appspot.com/ group=tapas-group-tutors auction.house.uri=https://tapas-auction-house.86-119-34-23.nip.io/ tasks.list.uri=https://tapas-tasks.86-119-34-23.nip.io/ + + +mqtt.broker.uri=tcp://localhost:1883