官术网_书友最值得收藏!

Reactive server initialization

Before jumping in to the endpoint, we will explore the structure of our files, including the initializer, handler, and repository.

The Server class for initializing our port 8081 is as follows:

public class Server { 
public static final String HOST = "localhost";
public static final int PORT = 8081;
public static void main(String[] args) throws InterruptedException, IOException{
Server server = new Server();
server.startReactorServer();
System.out.println("Press ENTER to exit.");
System.in.read();
}
public void startReactorServer() throws InterruptedException {
RouterFunction<ServerResponse> route = routingFunction();
HttpHandler httpHandler = toHttpHandler(route);
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);
HttpServer server = HttpServer.create(HOST, PORT);
server.newHandler(adapter).block();
}
public RouterFunction<ServerResponse> routingFunction() {
// our Endpoints will be coming here
}
}

In the preceding method, we created a main class. Inside the main method, we will initialize the server and start the server with the following code:

Server server = new Server(); 
server.startReactorServer();

The preceding method will start the Reactor server. The Reactor server implementation is as follows:

RouterFunction<ServerResponse> route = routingFunction();
HttpHandler httpHandler = toHttpHandler(route);
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);
HttpServer server = HttpServer.create(HOST, PORT);
server.newHandler(adapter).block();

Let's go through this code later, as the concept is Reactive-based. Let's assume that this code works fine and we will move on, focusing on the endpoints.

The following is the method for mapping all REST endpoints for our CRUD operations:

public RouterFunction<ServerResponse> routingFunction() {
// our Endpoints will be coming here
}

You might get errors on UserRepository and UserHandler. Let's fill these up now:

package com.packtpub.reactive;
public interface UserRepository {
// repository functions will be coming here
}

In the preceding code, we have just added the UserRepository interface in our existing package com.packtpub.reactive. Later, we will introduce abstract methods for our business requirements.

Now, we can add a UserHandler class, and add the necessary things:

package com.packtpub.reactive;
// import statements
public class UserHandler {
private final UserRepository userRepository;
public UserHandler(UserRepository userRepository){
this.userRepository = userRepository;
}
}

In the preceding code, the UserHandler initializes the UserRepository instance in its constructor. If someone gets an instance of UserHandler, they will have to pass the UserRepository type to the UserHandler constructor. By doing this, UserRepository will always be forwarded to UserHandler to fulfill the business requirements.

主站蜘蛛池模板: 闵行区| 余庆县| 贵溪市| 双江| 阿巴嘎旗| 卓资县| 长寿区| 新蔡县| 建平县| 南城县| 积石山| 东阿县| 惠来县| 上蔡县| 金堂县| 任丘市| 栾川县| 水城县| 丰顺县| 山阳县| 沙洋县| 寿宁县| 武安市| 和林格尔县| 马山县| 曲靖市| 苗栗市| 香格里拉县| 云和县| 抚松县| 赣州市| 五华县| 嘉黎县| 曲麻莱县| 贺兰县| 兴和县| 罗平县| 泰州市| 崇明县| 富民县| 宜城市|