Dev #65

Merged
reynisson merged 67 commits from dev into main 2021-11-16 18:14:00 +00:00
8 changed files with 58 additions and 8 deletions
Showing only changes of commit 491b529105 - Show all commits

View File

@ -12,10 +12,9 @@ public class ConfigProperties {
private Environment environment; private Environment environment;
/** /**
* Retrieves the URI of the WebSub hub. In this project, we use a single WebSub hub, but we could * Retrieves the URI of the MQTT broker.
* use multiple.
* *
* @return the URI of the WebSub hub * @return the URI of the MQTT broker
*/ */
public URI getMqttBrokerUri() { public URI getMqttBrokerUri() {
return URI.create(environment.getProperty("mqtt.broker.uri")); 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); 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 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) { public static void main(String[] args) {
SpringApplication tapasAuctioneerApp = new SpringApplication(TapasAuctionHouseApplication.class); SpringApplication tapasAuctioneerApp = new SpringApplication(TapasAuctionHouseApplication.class);
// We will use these bootstrap methods in Week 6: // We will use these bootstrap methods in Week 6:
// bootstrapMarketplaceWithWebSub(); // bootstrapMarketplaceWithWebSub();
// bootstrapMarketplaceWithMqtt(); bootstrapMarketplaceWithMqtt();
tapasAuctioneerApp.run(args); tapasAuctioneerApp.run(args);
} }

View File

@ -68,7 +68,10 @@ public class TapasMqttClient {
mqttClient.subscribe(topic); 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)); MqttMessage message = new MqttMessage(payload.getBytes(StandardCharsets.UTF_8));
mqttClient.publish(topic, message); mqttClient.publish(topic, message);
} }

View File

@ -26,7 +26,7 @@ public class AuctionEventsMqttDispatcher {
// TODO: Register here your topics and event listener adapters // TODO: Register here your topics and event listener adapters
private void initRouter() { 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. * This class is a template for publishing auction started events via WebSub.
*/ */
@Component @Component
@Primary
public class PublishAuctionStartedEventWebSubAdapter implements AuctionStartedEventPort { public class PublishAuctionStartedEventWebSubAdapter implements AuctionStartedEventPort {
// You can use this object to retrieve properties from application.properties, e.g. the // You can use this object to retrieve properties from application.properties, e.g. the
// WebSub hub publish endpoint, etc. // WebSub hub publish endpoint, etc.

View File

@ -61,4 +61,14 @@ public class ConfigProperties {
public URI getTaskListUri() { public URI getTaskListUri() {
return URI.create(environment.getProperty("tasks.list.uri")); 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 group=tapas-group-tutors
auction.house.uri=https://tapas-auction-house.86-119-34-23.nip.io/ 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/ tasks.list.uri=https://tapas-tasks.86-119-34-23.nip.io/
mqtt.broker.uri=tcp://localhost:1883