Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the functions of developing with Next

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "what is the role of using Next to develop". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Cloudopt Next is a very lightweight and modern full-stack development framework based on Kotlin, which supports both Java and Kotlin. It can handle the parsing of Url, the encapsulation of data, the output of Json, and so on, fundamentally reducing development time and improving development experience.

At the same time, Next is also recommended by Vert.x and awesome-kotlin. Let's take a look at some interesting features of Next.

For example, if you directly use Vert.x to write a route, it is written as follows:

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!")

});

So when you have to manipulate the database, it looks like a callback is embedded with a callback, which is easy to form a callback hell.

Let's look at how to implement a simple route to output Json in Next:

@ GET ("json")

Fun json () {

Var map = hashMapOf ()

Map.put ("a", 1)

Map.put ("b", 2)

RenderJson (map)

}

In the writing method, there is no need to change your habits and use the traditional writing method. You can even directly use parameter injection like spring boot (combined with parameter verification of hibernate):

Fun argsController (

@ Chinese (false)

@ Parameter ("name", defaultValue = "Peter")

Name: String

@ Min (18)

@ Parameter ()

Age: Int) {

Var map = hashMapOf ()

Map ["name"] = name

Map ["age"] = age

RenderJson (map)

}

And support a variety of asynchronous writing methods in Cloudopt Next, you can choose to write freely as you like:

For example, if we now need to run some blocked code and need to pass the results back, you can run it in the following way. Of course, you can also run blocked code through Worker.worker. Remember to pass the result through handler.complete at the end.

Fun test () {

Var id = worker ({handler->

Handler.complete (1)

}, {asyncResult->

/ / onComplete

})

}

The second approach is to take advantage of the await feature of kotlin's co-program to make it more elegant to write. However, using the kotlin protocol requires the declaration of suspend on the method. This is also the asynchronous writing method we recommend.

Suspend fun test () {

Var id = await {handler->

Handler.complete (1)

}

}

The third situation is that you use suspend syntax where you can't support it. You can run it in the following ways:

Fun test () {

Global {

Var id = await {handler->

Handler.complete (1)

}

}

}

The fourth way is that you can add @ Blocking annotation to the routing method to make it automatically become a normal route.

@ Blocking

@ GET ("blocking")

Fun blocking () {

RenderText ("This is Blocking!")

}

For example, using the await syntax, we can directly use any synchronous operation of the database orm without worrying about blocking (jooq is used here):

@ GET

Suspend fun get () {

Await {promise->

Val result = JooqManager.dsl?.selectFrom (Tables.TODO)? .fetch ()? .into (Todo::class.java)

RenderJson (result)

Promise.complete ()

}

}

Of course, if you seal it up, it looks like this:

@ GET

Suspend fun get () {

RenderJson (todoService.getTodoList ())

}

Of course, it also supports setting validators, Handler, etc., through annotations, and even simplifies Vert.x 's EventBus. You only need to declare @ AfterEvent on the route to automatically execute the xxx event after the xxx event.

@ GET ("afterEvent")

@ AfterEvent (["net.cloudopt.web.test"])

Fun afterEvent () {

This.context.data (. Put ("key", "value")

RenderText ("AfterEvent is success!")

}

You just need to declare @ AutoEvent on other classes and inherit EventListener to automatically subscribe to this event.

@ AutoEvent ("net.cloudopt.web.test")

Class TestEventListener:EventListener {

Override fun listener (message: Message) {

Print (message.body ())

}

}

Cloudopt Next allows you to retain your original programming habits to a large extent, while gaining the powerful performance of Vert.x. Not only that, Cloudopt Next provides a large number of plug-ins, such as distributed secondary cache, jooq, redis, logging, spring support, quartz, internationalization and other functions that Vert.x does not provide. It is also compatible with Vert.x module architecture. In other words, you can use not only the official plug-ins of Next, but also the plug-ins of Vert.x and a series of ecology contributed by the Vert.x community.

Give it a try. With Koltin + Next, you will get an asynchronous service with a smooth learning curve, easy to use, intuitive and extremely high performance.

This is the end of the content of "what are the functions of developing with Next". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report