Implemented the auction started event over mqtt
This commit is contained in:
parent
55c094fc56
commit
2999fb294c
|
@ -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"));
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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.
|
||||
|
|
|
@ -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"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user