Added stubs for kafka connection but configuration (wsl + windows) needs to be checked.
parent
9e8a692eb4
commit
4ad8473aaa
|
@ -1,19 +1,29 @@
|
||||||
package ch.polgrabia.demos.server;
|
package ch.polgrabia.demos.server;
|
||||||
|
|
||||||
import io.vertx.core.Vertx;
|
import io.vertx.core.Vertx;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
public class WebsocketServerApp {
|
public class WebsocketServerApp {
|
||||||
private static final Logger logger = LoggerFactory.getLogger(WebsocketServerApp.class);
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
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<String, byte[]> kafkaProducer = new KafkaProducer<>(props);
|
||||||
|
**/
|
||||||
|
|
||||||
var vertx = Vertx.vertx();
|
var vertx = Vertx.vertx();
|
||||||
WebsocketServerChatVerticle websocketServerChatVerticle = new WebsocketServerChatVerticle(8080, "localhost");
|
WebsocketServerChatVerticle websocketServerChatVerticle = new WebsocketServerChatVerticle(
|
||||||
|
8080,
|
||||||
|
"localhost",
|
||||||
|
null); // kafka producer
|
||||||
vertx.deployVerticle(websocketServerChatVerticle);
|
vertx.deployVerticle(websocketServerChatVerticle);
|
||||||
websocketServerChatVerticle
|
websocketServerChatVerticle
|
||||||
.waitUntilFinished()
|
.waitUntilFinished()
|
||||||
.join();
|
.join();
|
||||||
vertx.close();
|
vertx.close();
|
||||||
|
// kafkaProducer.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -2,6 +2,7 @@ package ch.polgrabia.demos.server;
|
||||||
|
|
||||||
import io.vertx.core.AbstractVerticle;
|
import io.vertx.core.AbstractVerticle;
|
||||||
import io.vertx.core.http.HttpServer;
|
import io.vertx.core.http.HttpServer;
|
||||||
|
import org.apache.kafka.clients.producer.Producer;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@ -9,6 +10,7 @@ import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
public class WebsocketServerChatVerticle extends AbstractVerticle {
|
public class WebsocketServerChatVerticle extends AbstractVerticle {
|
||||||
private static final Logger logger = LoggerFactory.getLogger(WebsocketServerChatVerticle.class);
|
private static final Logger logger = LoggerFactory.getLogger(WebsocketServerChatVerticle.class);
|
||||||
|
private final Producer<String, byte[]> kafkaProducer;
|
||||||
private HttpServer httpServer;
|
private HttpServer httpServer;
|
||||||
private final int port;
|
private final int port;
|
||||||
private final String hostname;
|
private final String hostname;
|
||||||
|
@ -16,16 +18,17 @@ public class WebsocketServerChatVerticle extends AbstractVerticle {
|
||||||
private final Object runningMonitor = new Object();
|
private final Object runningMonitor = new Object();
|
||||||
private boolean shutdownActivation = false;
|
private boolean shutdownActivation = false;
|
||||||
|
|
||||||
public WebsocketServerChatVerticle(int port, String hostname) {
|
public WebsocketServerChatVerticle(int port, String hostname, Producer<String, byte[]> kafkaProducer) {
|
||||||
this.port = port;
|
this.port = port;
|
||||||
this.hostname = hostname;
|
this.hostname = hostname;
|
||||||
|
this.kafkaProducer = kafkaProducer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start() throws Exception {
|
public void start() throws Exception {
|
||||||
httpServer = getVertx()
|
httpServer = getVertx()
|
||||||
.createHttpServer();
|
.createHttpServer();
|
||||||
httpServer.webSocketHandler(new WebsocketServerHandler()).listen(port, hostname);
|
httpServer.webSocketHandler(new WebsocketServerHandler(kafkaProducer)).listen(port, hostname);
|
||||||
shutdownActivation = true;
|
shutdownActivation = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,8 @@ package ch.polgrabia.demos.server;
|
||||||
import io.vertx.core.Handler;
|
import io.vertx.core.Handler;
|
||||||
import io.vertx.core.buffer.Buffer;
|
import io.vertx.core.buffer.Buffer;
|
||||||
import io.vertx.core.http.ServerWebSocket;
|
import io.vertx.core.http.ServerWebSocket;
|
||||||
|
import org.apache.kafka.clients.producer.Producer;
|
||||||
|
import org.apache.kafka.clients.producer.ProducerRecord;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@ -15,11 +17,16 @@ public class WebsocketServerHandler implements Handler<ServerWebSocket> {
|
||||||
|
|
||||||
private static final ConcurrentHashMap<String, Set<ServerWebSocket>>
|
private static final ConcurrentHashMap<String, Set<ServerWebSocket>>
|
||||||
channelSocketMapping = new ConcurrentHashMap<>();
|
channelSocketMapping = new ConcurrentHashMap<>();
|
||||||
|
private final Producer<String, byte[]> kafkaProducer;
|
||||||
|
|
||||||
|
public WebsocketServerHandler(Producer<String, byte[]> kafkaProducer) {
|
||||||
|
this.kafkaProducer = kafkaProducer;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handle(ServerWebSocket serverWebSocket) {
|
public void handle(ServerWebSocket serverWebSocket) {
|
||||||
serverWebSocket.writeBinaryMessage(Buffer.buffer("Hello"));
|
serverWebSocket.writeBinaryMessage(Buffer.buffer("Hello"));
|
||||||
serverWebSocket.handler(new WebsocketMessageHandler(serverWebSocket));
|
serverWebSocket.handler(new WebsocketMessageHandler(kafkaProducer, serverWebSocket));
|
||||||
String channel = serverWebSocket.path();
|
String channel = serverWebSocket.path();
|
||||||
Set<ServerWebSocket> sockets = channelSocketMapping.computeIfAbsent(channel, (k) -> {
|
Set<ServerWebSocket> sockets = channelSocketMapping.computeIfAbsent(channel, (k) -> {
|
||||||
Set<ServerWebSocket> s = new HashSet<>();
|
Set<ServerWebSocket> s = new HashSet<>();
|
||||||
|
@ -50,8 +57,10 @@ public class WebsocketServerHandler implements Handler<ServerWebSocket> {
|
||||||
static class WebsocketMessageHandler implements Handler<Buffer> {
|
static class WebsocketMessageHandler implements Handler<Buffer> {
|
||||||
|
|
||||||
private final ServerWebSocket serverWebSocket;
|
private final ServerWebSocket serverWebSocket;
|
||||||
|
private final Producer<String, byte[]> kafkaProducer;
|
||||||
|
|
||||||
public WebsocketMessageHandler(ServerWebSocket serverWebSocket) {
|
public WebsocketMessageHandler(Producer<String, byte[]> kafkaProducer, ServerWebSocket serverWebSocket) {
|
||||||
|
this.kafkaProducer = kafkaProducer;
|
||||||
this.serverWebSocket = serverWebSocket;
|
this.serverWebSocket = serverWebSocket;
|
||||||
this.serverWebSocket.setWriteQueueMaxSize(0);
|
this.serverWebSocket.setWriteQueueMaxSize(0);
|
||||||
}
|
}
|
||||||
|
@ -62,6 +71,7 @@ public class WebsocketServerHandler implements Handler<ServerWebSocket> {
|
||||||
String channel = serverWebSocket.path();
|
String channel = serverWebSocket.path();
|
||||||
logger.info("[{}] Got message {}", channel, s);
|
logger.info("[{}] Got message {}", channel, s);
|
||||||
serverWebSocket.writeBinaryMessage(Buffer.buffer("Pong: " + s.toString() + "\n")); // TBD send to kafka
|
serverWebSocket.writeBinaryMessage(Buffer.buffer("Pong: " + s.toString() + "\n")); // TBD send to kafka
|
||||||
|
// kafkaProducer.send(new ProducerRecord<>(channel, channel, s.getBytes())); // kafka setup needs to be checked
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,13 +5,16 @@ services:
|
||||||
environment:
|
environment:
|
||||||
KAFKA_NODE_ID: 1
|
KAFKA_NODE_ID: 1
|
||||||
KAFKA_PROCESS_ROLES: broker,controller
|
KAFKA_PROCESS_ROLES: broker,controller
|
||||||
KAFKA_LISTENERS: PLAINTEXT://localhost:9092,CONTROLLER://localhost:9093
|
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092,CONTROLLER://0.0.0.0:9093
|
||||||
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
|
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://0.0.0.0:9092
|
||||||
KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER
|
KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER
|
||||||
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
|
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
|
||||||
KAFKA_CONTROLLER_QUORUM_VOTERS: 1@localhost:9093
|
KAFKA_CONTROLLER_QUORUM_VOTERS: 1@0.0.0.0:9093
|
||||||
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
|
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
|
||||||
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
|
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
|
||||||
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
|
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
|
||||||
KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
|
KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
|
||||||
KAFKA_NUM_PARTITIONS: 3
|
KAFKA_NUM_PARTITIONS: 3
|
||||||
|
ports:
|
||||||
|
- "9092:9092"
|
||||||
|
- "9093:9093"
|
Loading…
Reference in New Issue