The Cloudopt backend is based on Kotlin + vert.x. We developed a backend framework Cloudopt Next based on Kotlin + vert. x for better use of vert. x. It is also recommended by Vertx and awesome- Kotlin.
Cloudopt Next is a very lightweight, modern, kotlin-based, full-stack development framework that supports both Java and Kotlin. You can handle Url parsing, data encapsulation,Json output, and more, radically reducing development time and improving development experience.
Vert.x: Vert.x: vert. x: vert. x: vert. x
router.route().handler(ctx -> {
// This handler will be called for every request
HttpServerResponse response = ctx.response();
response.putHeader("content-type"."text/plain");
// Write to the response and end it
response.end("Hello World from Vert.x-Web!");
});
Copy the code
So when you have to manipulate the database, it looks like a callback with a callback inside it, and it’s easy to create callback hell.
Let’s see how to implement a simple route to output Json in Next:
@GET("json")
fun json(a) {
var map = hashMapOf<String, Any>()
map.put("a".1)
map.put("b".2)
renderJson(map)
}
Copy the code
You don’t have to change your habits at all to write it in the traditional way. You can even use spring Boot-like parameter injection (in conjunction with Hibernate parameter verification) :
fun argsController(
@Chinese(false)
@Parameter("name", defaultValue = "Peter")
name: String.@Min(18)
@Parameter()
age: Int
) {
var map = hashMapOf<String, Any>()
map["name"] = name
map["age"] = age
renderJson(map)
}
Copy the code
Cloudopt Next supports a variety of asynchronous writing methods, you can choose your own writing method freely:
For example, if you need to run some blocked code and pass the result back, you can run it as follows. Of course you can also run blocked code through worker. Worker. Remember to pass the result through handler.plete at the end.
fun test(a) {
var id = worker<Int>({ handler ->
handler.complete(1)
}, { asyncResult ->
//onComplete})}Copy the code
The second option is to use the await property of Kotlin’s coroutines, which is more elegant to write. However, using a Kotlin coroutine requires you to declare suspend on the method. This is also the recommended asynchronous writing.
suspend fun test(a) {
var id = await<Int> { handler ->
handler.complete(1)}}Copy the code
The third case is when you use await syntax where the suspend syntax is not supported. You can run it as follows:
fun test(a) {
global {
var id = await<Int> { handler ->
handler.complete(1)}}}Copy the code
Fourth, you can add an @blocking annotation to the route method to make it a normal route.
@Blocking
@GET("blocking")
fun blocking(a) {
renderText("This is Blocking!")}Copy the code
For example, with await syntax we can directly use any synchronous operation database ORM without worrying about blocking (in this case jOOq) :
@GET
suspend fun get(a) {
await { promise ->
valresult = JooqManager.dsl? .selectFrom(Tables.TODO)? .fetch()? .into(Todo::class.java)
renderJson(result )
promise.complete()
}
}
Copy the code
Of course, when you wrap it up, it looks like this:
@GET
suspend fun get(a) {
renderJson(todoService.getTodoList())
}
Copy the code
It also supports annotations to set validators, handlers, etc., and even simplifies vert. x EventBus by declaring @AfterEvent on the route to automatically execute XXX events after XXX events.
@GET("afterEvent")
@AfterEvent(["net.cloudopt.web.test"])
fun afterEvent(a) {
this.context.data().put("key"."value")
renderText("AfterEvent is success!")}Copy the code
You simply declare @AutoEvent on another class and inherit EventListener to automatically subscribe to the event.
@AutoEvent("net.cloudopt.web.test")
class TestEventListener:EventListener {
override fun listener(message: Message<Any>) {
print(message.body())
}
}
Copy the code
Cloudopt Next allows you to retain much of your programming habits while getting the power of vert. x. Not only that, Cloudopt Next provides a number of plug-ins, such as distributed level 2 caching, JOOq, Redis, logging, Spring support, Quartz, internationalization, and more that vert.x does not provide. Also compatible with vert. x module system. This means that you can use not only the official Next plugins, but also vert. x plugins and a host of ecology contributed by the vert. x community.
Give it a try, and with Koltin + Next you will have a smooth learning curve, easy to use, intuitive, and high performance asynchronous service.
Official website: next.cloudopt.net GitHub: github.com/cloudoptlab…