request/response
Introduction
This section introduces communication for single-request handling. There are two types:
- request/response
- request/void
This communication model is powerful. It can be used for both client request/response and internal logic-server request/response.
request/response
request/response is a common communication model in server development: after the client sends a request, the server returns a response.
Usage scenarios
Equipment upgrades, character upgrades, player movement, lottery draws, and fetching scene configuration from the server when entering a game scene, etc. When writing actions, methods with return values represent request/response.
Example
For coding style, action provides an MVC-like style for developers (non-intrusive Java Bean), which effectively avoids class explosion. Return values and parameters support custom protocol types such as int, long, bool, String, and List, making business development more convenient.
The learning cost is very low, almost zero. Even without network programming experience, developers can get started quickly. Developers only need basic Java method or webMVC-related knowledge to build business logic with the framework.
@ActionController(RequestCmd.cmd)
public class RequestAction {
AtomicInteger inc = new AtomicInteger(1);
@ActionMethod(RequestCmd.intAction)
private int intAction(int value) {
return value + inc.getAndIncrement();
}
@ActionMethod(RequestCmd.boolAction)
private boolean boolAction(boolean value) {
return inc.getAndIncrement() % 2 == 0;
}
@ActionMethod(RequestCmd.longAction)
private long longAction(long value) {
return value + inc.getAndIncrement();
}
@ActionMethod(RequestCmd.stringAction)
private String stringAction(String value) {
return value + inc.getAndIncrement();
}
@ActionMethod(RequestCmd.objectAction)
private BookMessage objectAction(AuthorMessage author) {
var book = new BookMessage();
book.bookName = "Social Psychology";
book.authorName = author.authorName;
return book;
}
@ActionMethod(RequestCmd.intListAction)
private List<Integer> intListAction(List<Integer> valueList) {
List<Integer> list = new ArrayList<>();
for (var value : valueList) {
list.add(value + inc.getAndIncrement());
}
return list;
}
@ActionMethod(RequestCmd.boolListAction)
private List<Boolean> boolListAction(List<Boolean> valueList) {
List<Boolean> list = new ArrayList<>();
for (var value : valueList) {
list.add(!value);
}
return list;
}
@ActionMethod(RequestCmd.longListAction)
private List<Long> longListAction(List<Long> valueList) {
List<Long> list = new ArrayList<>();
for (var value : valueList) {
list.add(value + inc.getAndIncrement());
}
return list;
}
@ActionMethod(RequestCmd.stringListAction)
private List<String> stringListAction(List<String> valueList) {
List<String> list = new ArrayList<>();
for (var value : valueList) {
list.add(value + inc.getAndIncrement());
}
return list;
}
@ActionMethod(RequestCmd.objectListAction)
private List<BookMessage> objectListAction(List<AuthorMessage> authorList) {
var book1 = new BookMessage();
book1.bookName = "Social Psychology";
book1.authorName = "David Myers";
var book2 = new BookMessage();
book2.bookName = "SThe Crowd: A Study of the Popular Mind";
book2.authorName = "Gustave Le Bon";
return List.of(book1, book2);
}
}
@ProtobufClass
public class AuthorMessage {
public String authorName;
}
request/void
For request/void, after the client sends a request, the server returns no response data to the requester.
When writing actions, declaring the method return type as void means request/void.
Usage scenarios
Data collection, metrics sampling, log transfer, metrics reporting, etc.
Example
When writing actions, declare the method return type as
void
@ActionController(RequestCmd.cmd)
public class RequestAction {
@ActionMethod(RequestCmd.emptyAction)
private void emptyAction() {
// your code
}
}
How to Use
About handling client requests
You can initiate requests through a simulated client.
About internal request handling
Summary
action combines both request/response and request/void communication models, and can be used for handling both client requests and internal requests.