diff --git a/2024/08/chat_demo1/chat_demo1_web/build.gradle.kts b/2024/08/chat_demo1/chat_demo1_web/build.gradle.kts index 8226bc2..209280f 100644 --- a/2024/08/chat_demo1/chat_demo1_web/build.gradle.kts +++ b/2024/08/chat_demo1/chat_demo1_web/build.gradle.kts @@ -10,6 +10,7 @@ repositories { } dependencies { + implementation("commons-cli:commons-cli:1.9.0") implementation("io.vertx:vertx-core:4.5.9") implementation("ch.qos.logback:logback-classic:1.5.6") implementation("org.apache.kafka:kafka-clients:3.9.0") diff --git a/2024/08/chat_demo1/chat_demo1_web/src/main/java/ch/polgrabia/demos/server/WebsocketServerApp.java b/2024/08/chat_demo1/chat_demo1_web/src/main/java/ch/polgrabia/demos/server/WebsocketServerApp.java index d118f23..18febac 100644 --- a/2024/08/chat_demo1/chat_demo1_web/src/main/java/ch/polgrabia/demos/server/WebsocketServerApp.java +++ b/2024/08/chat_demo1/chat_demo1_web/src/main/java/ch/polgrabia/demos/server/WebsocketServerApp.java @@ -1,32 +1,69 @@ package ch.polgrabia.demos.server; import io.vertx.core.Vertx; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.DefaultParser; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; import org.apache.kafka.clients.producer.KafkaProducer; import org.apache.kafka.clients.producer.Producer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.Properties; public class WebsocketServerApp { + private static final Logger logger = LoggerFactory.getLogger(WebsocketServerApp.class); + public static void main(String[] args) { - Properties props = new Properties(); - props.put("bootstrap.servers", "localhost:9092"); - props.put("linger.ms", 1); - props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); - props.put("value.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer"); - - Producer kafkaProducer = new KafkaProducer<>(props); - - var vertx = Vertx.vertx(); - WebsocketServerChatVerticle websocketServerChatVerticle = new WebsocketServerChatVerticle( - 8080, - "localhost", - null); // kafka producer - vertx.deployVerticle(websocketServerChatVerticle); - websocketServerChatVerticle - .waitUntilFinished() - .join(); - vertx.close(); - kafkaProducer.close(); + Producer kafkaProducer = null; + try { + CommandLineParser parser = new DefaultParser(); + var options = new Options() + .addOption("h", "host", true, "Host") + .addOption("p", "port", true, "Port") + .addOption("ke", "kafka-enabled", true, "Enable Kafka server") + .addOption("bs", "bootstrap-servers", true, "Bootstrap server address"); + CommandLine result = parser.parse(options, args); + logger.info("Got options: {}", result); + boolean isKafkaEnabled = "true".equals(result.getOptionValue("kafka-enabled")); + String bootstrapServers = result.getOptionValue("bootstrap-servers"); + logger.info("Got options kafka.enabled: {}", isKafkaEnabled); + if (isKafkaEnabled) { + logger.info("Bootstrap servers {}", bootstrapServers); + Properties props = new Properties(); + props.put("bootstrap.servers", bootstrapServers); + props.put("linger.ms", 1); + props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); + props.put("value.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer"); + + kafkaProducer = new KafkaProducer<>(props); + } + + var config = new WebsocketServerConfig( + result.getOptionValue("host"), + result.getOptionValue("port"), + isKafkaEnabled, + bootstrapServers, + kafkaProducer); + + logger.info("Configuration {}", config); + + WebsocketServerChatVerticle websocketServerChatVerticle = new WebsocketServerChatVerticle(config); // kafka producer + vertx.deployVerticle(websocketServerChatVerticle); + websocketServerChatVerticle + .waitUntilFinished() + .join(); + } catch (ParseException e) { + logger.error("Invalid use of configuration", e); + } finally { + vertx.close(); + if (kafkaProducer != null) { + kafkaProducer.close(); + } + } + } } \ No newline at end of file diff --git a/2024/08/chat_demo1/chat_demo1_web/src/main/java/ch/polgrabia/demos/server/WebsocketServerChatVerticle.java b/2024/08/chat_demo1/chat_demo1_web/src/main/java/ch/polgrabia/demos/server/WebsocketServerChatVerticle.java index 452c2ab..46d973a 100644 --- a/2024/08/chat_demo1/chat_demo1_web/src/main/java/ch/polgrabia/demos/server/WebsocketServerChatVerticle.java +++ b/2024/08/chat_demo1/chat_demo1_web/src/main/java/ch/polgrabia/demos/server/WebsocketServerChatVerticle.java @@ -18,10 +18,11 @@ public class WebsocketServerChatVerticle extends AbstractVerticle { private final Object runningMonitor = new Object(); private boolean shutdownActivation = false; - public WebsocketServerChatVerticle(int port, String hostname, Producer kafkaProducer) { - this.port = port; - this.hostname = hostname; - this.kafkaProducer = kafkaProducer; + public WebsocketServerChatVerticle(WebsocketServerConfig websocketServerConfig) { + this.port = websocketServerConfig.port() == null ? 8080 : Integer.parseInt(websocketServerConfig.port()); + this.hostname = websocketServerConfig.host() == null ? "localhost" : websocketServerConfig.host(); + this.kafkaProducer = websocketServerConfig.kafkaProducer(); + logger.info("Host: {}, port {}", hostname, port); } @Override diff --git a/2024/08/chat_demo1/chat_demo1_web/src/main/java/ch/polgrabia/demos/server/WebsocketServerConfig.java b/2024/08/chat_demo1/chat_demo1_web/src/main/java/ch/polgrabia/demos/server/WebsocketServerConfig.java new file mode 100644 index 0000000..ade6569 --- /dev/null +++ b/2024/08/chat_demo1/chat_demo1_web/src/main/java/ch/polgrabia/demos/server/WebsocketServerConfig.java @@ -0,0 +1,9 @@ +package ch.polgrabia.demos.server; + +public record WebsocketServerConfig( + String host, + String port, + boolean isKafkaEnabled, + String bootstrapServers, + org.apache.kafka.clients.producer.Producer kafkaProducer) { +}