写点什么

Magician 1.1.16 is released, the handler adopts annotation configuration

发布于: 刚刚

From the day of its birth, Magician has been positioned as a network programming package. Although it belongs to the application layer, it is biased towards the bottom layer. In the hands of users, they generally assume the role of the kernel, whose main responsibility is to be responsible for network communications.


Due to the characteristics of the bottom layer, such problems often occur during use. After we created a TCP service

Magician.createTCPServer()                    .handler("/", new DemoHandler())                    .bind(8080);
复制代码

If you need to add a handler, you have to change this part of the code to make it like this

Magician.createTCPServer()                    .handler("/", new DemoHandler())                    .handler("/test", new TestHandler())                    .bind(8080);
复制代码

If you need WebSocket, you have to change it to this

Magician.createTCPServer()                    .handler("/", new DemoHandler())                    .handler("/test", new TestHandler())                    .webSocketHandler("/websocket", new DemoSocketHandler())                    .bind(8080);
复制代码

The steps of creating a service and the steps of creating a handler are too tightly coupled. If you use this project purely, the problem is not big, but if you use this project as the core of another project, the problem will be very obvious, because in this scenario The code for creating a service is usually encapsulated separately. If the encapsulated code is changed frequently, there will be certain security risks.


So in 1.1.16 we solved this problem.

We use annotations to define a handler

When creating a service, you only need to specify a scan range

Magician.createTCPServer()                    .scan("handler所在的包名")// 指定扫描范围                    .bind(8080);
复制代码

In this case, if you need to add a handler, you don't need to change this code.

For example, add a TCPHandler

@TCPHandler(path="/")public class DemoHandler implements TCPBaseHandler<MagicianRequest> {
@Override public void request(MagicianRequest magicianRequest) { // response data magicianRequest.getResponse() .sendJson(200, "{'status':'ok'}"); }}
复制代码

Add a WebSocketHandler

@WebSocketHandler(path = "/websocket")public class DemoSocketHandler implements WebSocketBaseHandler {       @Override    public void onOpen(WebSocketSession webSocketSession) {         }       @Override    public void onClose(WebSocketSession webSocketSession) {            }
@Override public void onMessage(String message, WebSocketSession webSocketSession) {
}}
复制代码


Doing so will completely decouple. Creating a service is purely creating a service, and creating a handler is purely creating a handler. There is no strong connection between the two.

In addition to Magician, these components have also been upgraded


Magician-Web component, Martian component. The upgrade points of these two components are mainly changed to match Magician's changes. And it brings a new feature to Martian: it can easily support WebSocket.

Magician's performance test

The TFB test result reached 150,000 requests per second.


Project website: http://magician-io.com

发布于: 刚刚阅读数: 6
用户头像

还未添加个人签名 2020.12.08 加入

还未添加个人简介

评论

发布
暂无评论
Magician 1.1.16 is released, the handler adopts annotation configuration