Skip to main content

request/multiple_response

Introduction

request/multiple_response, which simultaneously requests multiple logic servers of the same type, is one of the communication models provided by the framework. This model can collect results from multiple same-type logic servers.

The framework supports starting multiple identical logic servers, such as LogicServer-B-1 and LogicServer-B-2. Sometimes we need multiple same-type logic servers for load balancing.


Scenario Example - 1

The request/multiple_response model is especially useful in certain business scenarios. For example, there are 3 chess logic servers: ChessLogicServer-1, ChessLogicServer-2, and ChessLogicServer-3. These servers can be distributed across different processes.

Because we can request same-type logic servers simultaneously, we can do things like:

  • Find the ChessLogicServer with the fewest rooms, so subsequent players can be routed to that server.
  • Count the total number of rooms.
tip

This is only an example. In real scenarios, you can apply your own ideas.

An


Scenario Example - 2

In the scenario above, we showed that request/multiple_response can retrieve the ChessLogicServer with the fewest rooms. Combined with dynamic logic server binding, subsequent players can be routed to that logic server, meaning subsequent requests are handled by the specified server.

Examples

The following code demonstrates both synchronous and asynchronous callback usage.

  • code 9: asynchronous callback request model, requesting same-type logic servers.
  • code 10~11: get and iterate the response list; because there may be multiple same-type logic servers, responses are in a list.
  • code 12: print response results from each logic server.
  • code 17: synchronous request model, requesting same-type logic servers.
@ActionController(CallCollectCmd.cmd)
public class CallCollectAction {
@ActionMethod(CallCollectCmd.callCollectInt)
private List<Integer> callInt() {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.intAction);
int data = 1;
// Asynchronous callback
communication().callCollectAsync(cmdInfo, data, responseCollect -> {
List<Response> responseList = responseCollect.getResponseList();
for (Response response : responseList) {
log.info("{}", response.getInt());
}
});

// Synchronous call
var responseCollect = communication().callCollect(cmdInfo, data);
List<Response> responseList = responseCollect.getResponseList();
return responseList.stream()
.map(Response::getInt)
.toList();
}

private Communication communication() {
return CommunicationKit.getCommunication();
}
}

The following action comes from ChessLogicServer, where multiple instances are started.

@ActionController(InternalCmd.cmd)
public class InternalAction {
AtomicInteger inc = new AtomicInteger(1);

@ActionMethod(InternalCmd.intAction)
private int intAction(int value) {
return value + inc.getAndIncrement();
}
}

More Examples

For more examples, see Examples Communication RequestMultipleResponse.

Notes

warning

The request/multiple_response model must have a return value. The called action cannot be void.

Summary

As shown, requesting same-type logic servers is very simple and can be done with a single method.

var communication = CommunicationKit.getCommunication();
communication.callCollect
communication.callCollectAsync
// or
flowContext.callCollect
flowContext.callCollectAsync

Finally, use your imagination and put this feature to work.

Enterprise Feature

warning

This is an enterprise feature