Implemented the auction started event over mqtt #58

Merged
reynisson merged 1 commits from post_new_auctions_to_mqtt into dev 2021-11-14 14:29:48 +00:00
8 changed files with 58 additions and 8 deletions

View File

@ -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"));

View File

@ -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";
reynisson commented 2021-11-14 14:18:58 +00:00 (Migrated from github.com)
Review

Will get from config in new PR where I connect the auction house properly to the executor added event

Will get from config in new PR where I connect the auction house properly to the executor added event
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);
}

View File

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

View File

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

View File

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

View File

@ -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.

View File

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

View File

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