generated from vertx-howtos/howto-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ServerVerticle.java
58 lines (46 loc) · 1.64 KB
/
ServerVerticle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package io.vertx.howtos.web.sessions;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.Session;
import io.vertx.ext.web.handler.SessionHandler;
import io.vertx.ext.web.sstore.SessionStore;
import io.vertx.ext.web.sstore.infinispan.InfinispanSessionStore;
import java.util.Date;
public class ServerVerticle extends AbstractVerticle {
public static final String TEMPLATE = ""
+ "Session [%s] created on %s%n"
+ "%n"
+ "Page generated on %s%n";
@Override
public void start() {
Router router = Router.router(vertx);
JsonObject options = new JsonObject()
.put("servers", new JsonArray()
.add(new JsonObject()
.put("host", "localhost")
.put("port", 11222)
.put("username", "admin")
.put("password", "bar"))
);
SessionStore store = InfinispanSessionStore.create(vertx, options); // <1>
router.route().handler(SessionHandler.create(store)); // <2>
router.get("/").handler(ctx -> {
Session session = ctx.session();
session.computeIfAbsent("createdOn", s -> System.currentTimeMillis()); // <3>
String sessionId = session.id();
Date createdOn = new Date(session.<Long>get("createdOn"));
Date now = new Date();
ctx.end(String.format(TEMPLATE, sessionId, createdOn, now)); // <4>
});
vertx.createHttpServer()
.requestHandler(router)
.listen(8080);
}
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
vertx.deployVerticle(new ServerVerticle());
}
}