-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcoreExamples.kt
158 lines (131 loc) · 4.48 KB
/
coreExamples.kt
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package examples
import io.vertx.core.AbstractVerticle
import io.vertx.core.DeploymentOptions
import io.vertx.core.Vertx
import io.vertx.core.http.HttpServer
import io.vertx.core.http.HttpServerRequest
import io.vertx.kotlin.lang.*
import io.vertx.kotlin.lang.json.JsonObject
// Kotlin port of https://github.com/eclipse/vert.x/blob/master/src/main/java/examples/CoreExamples.java
class CoreExamples {
fun example1() {
DefaultVertx {
}
}
fun example2() {
val options = VertxOptions {
setWorkerPoolSize(40)
}
DefaultVertx(options) {
}
}
fun example3(request: HttpServerRequest) {
request.response()
.contentType("text/plain")
.writeBuffer {
appendString("some text")
}
.end()
}
fun example4(request: HttpServerRequest) {
request.response {
contentType("text/plain")
replyWithBuffer {
appendString("some text")
}
}
}
fun example5(vertx: Vertx) {
vertx.setPeriodic(1000) {
// This handler will get called every second
println("Timer fired")
}
}
fun example6(server: HttpServer) {
server.requestHandler { request ->
request.replyWithBuffer {
appendString("hello world!")
}
}
}
object BlockingAPI {
fun someBlockingMethod(s: String) = s
}
fun example7(vertx: Vertx) {
vertx.executeBlocking<String>({ future ->
future.complete(BlockingAPI.someBlockingMethod("hello"))
}, {
when (it) {
is AsyncSuccessResult -> println("Success result is ${it.result}")
is AsyncErrorResult -> println("Failed")
}
})
}
fun example7_1(vertx: Vertx) {
vertx.deployVerticle("com.mycompany.MyOrderProcessorVerticle", DeploymentOptions().setWorker(true))
}
class MyVerticle : AbstractVerticle() {
override fun start() {
super.start()
}
}
fun example8(vertx: Vertx) {
val vertifcle = MyVerticle()
vertx.deployVerticle(vertifcle)
}
fun example9(vertx: Vertx) {
// Deploy a Java verticle - the name is the fully qualified class name of the verticle class
vertx.deployVerticle("com.mycompany.MyOrderProcessorVerticle")
// Deploy a JavaScript verticle
vertx.deployVerticle("verticles/myverticle.js")
// Deploy a Ruby verticle verticle
vertx.deployVerticle("verticles/my_verticle.rb")
}
public fun example10(vertx: Vertx) {
vertx.deployVerticle("com.mycompany.MyOrderProcessorVerticle") {
when (it) {
is AsyncSuccessResult -> println("Deployment id is: ${it.result}");
is AsyncErrorResult -> println("Deployment failed")
}
}
}
fun example11(vertx: Vertx, deploymentID: String) {
vertx.undeploy(deploymentID) {
when (it) {
is AsyncSuccessResult -> println("Undeployed ok");
is AsyncErrorResult -> println("Undeploy failed")
}
}
}
public fun example12(vertx: Vertx) {
val options = DeploymentOptions().setInstances(16)
vertx.deployVerticle("com.mycompany.MyOrderProcessorVerticle", options)
}
public fun example13(vertx: Vertx) {
val options = DeploymentOptions().setConfig(JsonObject(
"name" to "tim",
"directory" to "blah"
))
vertx.deployVerticle("com.mycompany.MyOrderProcessorVerticle", options)
}
public fun example14(vertx: Vertx) {
val options = DeploymentOptions().setIsolationGroup("mygroup")
options.setExtraClasspath(listOf("lib/jars/some-library.jar"))
vertx.deployVerticle("com.mycompany.MyIsolatedVerticle", options)
}
fun example15(vertx: Vertx) {
val timerID = vertx.setTimer(1000) { id ->
println("And one second later this is printed");
}
println("First this is printed as timer with ID $timerID scheduled")
}
fun example16(vertx: Vertx) {
val timerID = vertx.setPeriodic(1000) { id ->
println("And every second this is printed");
}
println("First this is printed as timer with ID $timerID scheduled")
}
public fun example17(vertx: Vertx, timerID: Long) {
vertx.cancelTimer(timerID)
}
}