use of io.vertx.ext.web.handler.sockjs.PermittedOptions in project vertx-examples by vert-x3.
the class DashboardVerticle method start.
@Override
public void start() throws Exception {
Router router = Router.router(vertx);
// The event bus bridge handler
BridgeOptions options = new BridgeOptions();
options.setOutboundPermitted(Collections.singletonList(new PermittedOptions().setAddress("dashboard")));
router.route("/eventbus/*").handler(SockJSHandler.create(vertx).bridge(options));
// The web server handler
router.route().handler(StaticHandler.create().setCachingEnabled(false));
// Start http server
HttpServer httpServer = vertx.createHttpServer();
httpServer.requestHandler(router::accept).listen(8080, ar -> {
if (ar.succeeded()) {
System.out.println("Http server started");
} else {
ar.cause().printStackTrace();
}
});
// Our dashboard that aggregates metrics from various kafka topics
JsonObject dashboard = new JsonObject();
// Publish the dashboard to the browser over the bus
vertx.setPeriodic(1000, timerID -> {
vertx.eventBus().publish("dashboard", dashboard);
});
// Get the Kafka consumer config
JsonObject config = config();
// Create the consumer
KafkaReadStream<String, JsonObject> consumer = KafkaReadStream.create(vertx, config.getMap(), String.class, JsonObject.class);
// Aggregates metrics in the dashboard
consumer.handler(record -> {
JsonObject obj = record.value();
dashboard.mergeIn(obj);
});
// Subscribe to Kafka
consumer.subscribe(Collections.singleton("the_topic"));
}
use of io.vertx.ext.web.handler.sockjs.PermittedOptions in project vertx-examples by vert-x3.
the class Server method start.
@Override
public void start() throws Exception {
Router router = Router.router(vertx);
// Allow events for the designated addresses in/out of the event bus bridge
BridgeOptions opts = new BridgeOptions().addInboundPermitted(new PermittedOptions().setAddress("chat.message")).addOutboundPermitted(new PermittedOptions().setAddress("chat.message"));
// Create the event bus bridge and add it to the router.
SockJSHandler ebHandler = SockJSHandler.create(vertx).bridge(opts);
router.route("/eventbus/*").handler(ebHandler);
// Create a router endpoint for the static content.
router.route().handler(StaticHandler.create());
// Start the web server and tell it to use the router to handle requests.
vertx.createHttpServer().requestHandler(router::accept).listen(8080);
}
use of io.vertx.ext.web.handler.sockjs.PermittedOptions in project vertx-examples by vert-x3.
the class Server method start.
@Override
public void start() throws Exception {
Router router = Router.router(vertx);
// Allow events for the designated addresses in/out of the event bus bridge
BridgeOptions opts = new BridgeOptions().addInboundPermitted(new PermittedOptions().setAddress("chat.to.server")).addOutboundPermitted(new PermittedOptions().setAddress("chat.to.client"));
// Create the event bus bridge and add it to the router.
SockJSHandler ebHandler = SockJSHandler.create(vertx).bridge(opts);
router.route("/eventbus/*").handler(ebHandler);
// Create a router endpoint for the static content.
router.route().handler(StaticHandler.create());
// Start the web server and tell it to use the router to handle requests.
vertx.createHttpServer().requestHandler(router::accept).listen(8080);
EventBus eb = vertx.eventBus();
// Register to listen for messages coming IN to the server
eb.consumer("chat.to.server").handler(message -> {
// Create a timestamp string
String timestamp = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(Date.from(Instant.now()));
// Send the message back out to all clients with the timestamp prepended.
eb.publish("chat.to.client", timestamp + ": " + message.body());
});
}
use of io.vertx.ext.web.handler.sockjs.PermittedOptions in project vertx-examples by vert-x3.
the class Server method start.
@Override
public void start() throws Exception {
Router router = Router.router(vertx);
// Allow events for the designated addresses in/out of the event bus bridge
BridgeOptions opts = new BridgeOptions().addOutboundPermitted(new PermittedOptions().setAddress("feed"));
// Create the event bus bridge and add it to the router.
SockJSHandler ebHandler = SockJSHandler.create(vertx).bridge(opts);
router.route("/eventbus/*").handler(ebHandler);
// Start the web server and tell it to use the router to handle requests.
vertx.createHttpServer().requestHandler(router::accept).listen(8080);
EventBus eb = vertx.eventBus();
vertx.setPeriodic(1000l, t -> {
// Create a timestamp string
String timestamp = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(Date.from(Instant.now()));
eb.send("feed", new JsonObject().put("now", timestamp));
});
}
use of io.vertx.ext.web.handler.sockjs.PermittedOptions in project vertx-examples by vert-x3.
the class Dashboard method start.
@Override
public void start() {
MetricsService service = MetricsService.create(vertx);
Router router = Router.router(vertx);
// Allow outbound traffic to the news-feed address
BridgeOptions options = new BridgeOptions().addOutboundPermitted(new PermittedOptions().setAddress("metrics"));
router.route("/eventbus/*").handler(SockJSHandler.create(vertx).bridge(options));
// Serve the static resources
router.route().handler(StaticHandler.create());
HttpServer httpServer = vertx.createHttpServer();
httpServer.requestHandler(router::accept).listen(8080);
// Send a metrics events every second
vertx.setPeriodic(1000, t -> {
JsonObject metrics = service.getMetricsSnapshot(vertx.eventBus());
vertx.eventBus().publish("metrics", metrics);
});
// Send some messages
Random random = new Random();
vertx.eventBus().consumer("whatever", msg -> {
vertx.setTimer(10 + random.nextInt(50), id -> {
vertx.eventBus().send("whatever", "hello");
});
});
vertx.eventBus().send("whatever", "hello");
}
Aggregations