Testing out getting Environment variables from main

This commit is contained in:
reynisson 2021-11-17 18:25:49 +01:00
parent 084270bbb8
commit 01b5056671
3 changed files with 22 additions and 9 deletions

View File

@ -12,6 +12,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.ConfigurableEnvironment;
import java.net.URI; import java.net.URI;
import java.util.List; import java.util.List;
@ -23,20 +24,19 @@ import java.util.List;
public class TapasAuctionHouseApplication { public class TapasAuctionHouseApplication {
private static final Logger LOGGER = LogManager.getLogger(TapasAuctionHouseApplication.class); private static final Logger LOGGER = LogManager.getLogger(TapasAuctionHouseApplication.class);
@Autowired
private ConfigProperties config;
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 DEFAULT_MQTT_BROKER = "tcp://broker.hivemq.com:1883";
private static ConfigurableEnvironment ENVIRONMENT;
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication tapasAuctioneerApp = new SpringApplication(TapasAuctionHouseApplication.class); SpringApplication tapasAuctioneerApp = new SpringApplication(TapasAuctionHouseApplication.class);
ENVIRONMENT = tapasAuctioneerApp.run(args).getEnvironment();
// TODO Set start up of message services with config
// 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);
} }
/** /**
* Discovers auction houses and subscribes to WebSub notifications * Discovers auction houses and subscribes to WebSub notifications
@ -57,8 +57,16 @@ public class TapasAuctionHouseApplication {
*/ */
private static void bootstrapMarketplaceWithMqtt() { private static void bootstrapMarketplaceWithMqtt() {
try { try {
String broker = ENVIRONMENT.getProperty("mqtt.broker.uri");
if (broker == null) {
broker = DEFAULT_MQTT_BROKER;
LOGGER.info("No MQTT broker was set in application.propreties, going with default: "
+ DEFAULT_MQTT_BROKER);
}
AuctionEventsMqttDispatcher dispatcher = new AuctionEventsMqttDispatcher(); AuctionEventsMqttDispatcher dispatcher = new AuctionEventsMqttDispatcher();
TapasMqttClient client = TapasMqttClient.getInstance(MQTT_BROKER, dispatcher); TapasMqttClient client = TapasMqttClient.getInstance(broker, dispatcher);
client.startReceivingMessages(); client.startReceivingMessages();
} catch (MqttException e) { } catch (MqttException e) {
LOGGER.error(e.getMessage(), e); LOGGER.error(e.getMessage(), e);

View File

@ -38,6 +38,7 @@ public class ExternalAuctionStartedEventListenerMqttAdapter extends AuctionEvent
// representation that makes sense in the context of your application. // representation that makes sense in the context of your application.
JsonNode data = new ObjectMapper().readTree(payload); JsonNode data = new ObjectMapper().readTree(payload);
// TODO Sanitize URIs
String auctionId = data.get("auctionId").asText(); String auctionId = data.get("auctionId").asText();
String auctionHouseUri = data.get("auctionHouseUri").asText(); String auctionHouseUri = data.get("auctionHouseUri").asText();
String taskUri = data.get("taskUri").asText(); String taskUri = data.get("taskUri").asText();
@ -72,8 +73,12 @@ public class ExternalAuctionStartedEventListenerMqttAdapter extends AuctionEvent
LOGGER.error(e.getMessage(), e); LOGGER.error(e.getMessage(), e);
return false; return false;
} catch (IOException e) { } catch (IOException e) {
LOGGER.error(e.getMessage(), e);
e.printStackTrace(); e.printStackTrace();
} catch (InterruptedException e) { } catch (InterruptedException e) {
LOGGER.error(e.getMessage(), e);
e.printStackTrace(); e.printStackTrace();
} catch (Exception e){ } catch (Exception e){
LOGGER.error(e.getMessage(), e); LOGGER.error(e.getMessage(), e);

View File

@ -10,4 +10,4 @@ tasks.list.uri=http://localhost:8081
application.environment=development application.environment=development
auctionhouse.uri=http://localhost:8086 auctionhouse.uri=http://localhost:8086
websub.hub.uri=http://localhost:3000 websub.hub.uri=http://localhost:3000
mqtt.broker.uri=tcp://localhost:1883 mqtt.broker.uri=tcp://broker.hivemq.com:1883