跳到主要内容

Examples Communication

介绍

本篇主要介绍以下通信模型的使用示例

  • request/response、request/void
  • Broadcast
  • EventBus
  • request/multiple_response
提示

通信模型的请求参数支持 int、long、bool、String、Object、 List<Integer>、List<Long>、List<Boolean>、List<String>、List<YourClass>

Example Source Code

see https://github.com/iohao/ionet-examples

path : ionet-cookbook-code

  • InternalAction,当前文档演示内部请求的 action
  • SendAction
  • FlowContextSendAction
  • CallAction
  • FlowContextCallAction
  • BroadcastAction
  • FlowContextBroadcastAction

Request/Response

本节我们介绍内部请求,也就是逻辑服之间交互。 这次,我们在调用端的角度对 request/response 系列的使用。 逻辑服之间的请求通过 call、send 系列方法进行交互。

  • send 对应 request/void 通信模型。
  • call 对应 request/response 通信模型。

Send

send 系列用于内部无响应的请求,对应 request/void 通信模型。 send 是异步调用的,因为明确知道请求是无响应的。

@ActionController(SendCmd.cmd)
public class SendAction {
private Communication communication() {
return CommunicationKit.getCommunication();
}

@ActionMethod(SendCmd.sendEmpty)
private boolean sendEmpty() {
var cmdInfo = InternalCmd.of(InternalCmd.sendEmptyAction);
int data = 1;

communication().send(cmdInfo, data);
return true;
}

@ActionMethod(SendCmd.sendInt)
private boolean sendInt() {
var cmdInfo = InternalCmd.of(InternalCmd.sendIntAction);
int data = 1;

communication().send(cmdInfo, data);
return true;
}

@ActionMethod(SendCmd.sendBool)
private boolean sendBool() {
var cmdInfo = InternalCmd.of(InternalCmd.sendBoolAction);
boolean data = true;

communication().send(cmdInfo, data);
return true;
}

@ActionMethod(SendCmd.sendLong)
private boolean sendLong() {
var cmdInfo = InternalCmd.of(InternalCmd.sendLongAction);
long data = 1L;

communication().send(cmdInfo, data);
return true;
}

@ActionMethod(SendCmd.sendString)
private boolean sendString() {
var cmdInfo = InternalCmd.of(InternalCmd.sendStringAction);
String data = "hello";

communication().send(cmdInfo, data);
return true;
}

@ActionMethod(SendCmd.sendObject)
private boolean sendObject() {
var cmdInfo = InternalCmd.of(InternalCmd.sendObjectAction);
var data = new AuthorMessage();
data.authorName = "Joker";

communication().send(cmdInfo, data);
return true;
}

@ActionMethod(SendCmd.sendIntList)
private boolean sendIntList() {
var cmdInfo = InternalCmd.of(InternalCmd.sendIntListAction);
List<Integer> dataList = List.of(1, 2);

communication().sendListInt(cmdInfo, dataList);
return true;
}

@ActionMethod(SendCmd.sendBoolList)
private boolean sendBoolList() {
var cmdInfo = InternalCmd.of(InternalCmd.sendBoolListAction);
List<Boolean> dataList = List.of(true, false);

communication().sendListBool(cmdInfo, dataList);
return true;
}

@ActionMethod(SendCmd.sendLongList)
private boolean sendLongList() {
var cmdInfo = InternalCmd.of(InternalCmd.sendLongListAction);
List<Long> dataList = List.of(1L, 2L);

communication().sendListLong(cmdInfo, dataList);
return true;
}

@ActionMethod(SendCmd.sendStringList)
private boolean sendStringList() {
var cmdInfo = InternalCmd.of(InternalCmd.sendStringListAction);
List<String> dataList = List.of("hello", "ionet");

communication().sendListString(cmdInfo, dataList);
return true;
}

@ActionMethod(SendCmd.sendObjectList)
private boolean sendObjectList() {
var cmdInfo = InternalCmd.of(InternalCmd.sendObjectListAction);

var author1 = new AuthorMessage();
author1.authorName = "David Myers";

var author2 = new AuthorMessage();
author2.authorName = "Gustave Le Bon";

List<AuthorMessage> dataList = List.of(author1, author2);
communication().send(cmdInfo, dataList);
return true;
}
}

Call & Callback

call 系列用于内部有响应的请求,对应 request/response 通信模型。

  • call: 同步调用。
  • callback: 异步调用。
@ActionController(CallCmd.cmd)
public class CallAction {
private Communication communication() {
return CommunicationKit.getCommunication();
}

@ActionMethod(CallCmd.callEmpty)
private int callEmpty() {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.emptyAction);

// Asynchronous callback
communication().callback(cmdInfo, response -> {
log.info("{}", response.getInt());
});

// Synchronous call
var response = communication().call(cmdInfo);
return response.getInt();
}

@ActionMethod(CallCmd.callInt)
private int 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().callback(cmdInfo, data, response -> {
log.info("{}", response.getInt());
});

// Synchronous call
var response = communication().call(cmdInfo, data);
return response.getInt();
}

@ActionMethod(CallCmd.callBool)
private boolean callBool() {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.boolAction);
boolean data = true;

// Asynchronous callback
communication().callback(cmdInfo, data, response -> {
log.info("{}", response.getBoolean());
});

// Synchronous call
var response = communication().call(cmdInfo, data);
return response.getBoolean();
}

@ActionMethod(CallCmd.callLong)
private long callLong() {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.longAction);
long data = 1L;

// Asynchronous callback
communication().callback(cmdInfo, data, response -> {
log.info("{}", response.getLong());
});

// Synchronous call
var response = communication().call(cmdInfo, data);
return response.getLong();
}

@ActionMethod(CallCmd.callString)
private String callString() {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.stringAction);
String data = "hello";

// Asynchronous callback
communication().callback(cmdInfo, data, response -> {
log.info("{}", response.getString());
});

// Synchronous call
var response = communication().call(cmdInfo, data);
return response.getString();
}

@ActionMethod(CallCmd.callObject)
private BookMessage callObject() {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.objectAction);
var data = new AuthorMessage();

// Asynchronous callback
communication().callback(cmdInfo, data, response -> {
log.info("{}", response.getValue(BookMessage.class));
});

// Synchronous call
var response = communication().call(cmdInfo, data);
return response.getValue(BookMessage.class);
}

@ActionMethod(CallCmd.callIntList)
private List<Integer> callIntList() {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.intListAction);
List<Integer> dataList = List.of(1, 2);

// Asynchronous callback
communication().callbackListInt(cmdInfo, dataList, response -> {
log.info("{}", response.listInt());
});

// Synchronous call
var response = communication().callListInt(cmdInfo, dataList);
return response.listInt();
}

@ActionMethod(CallCmd.callBoolList)
private List<Boolean> callBoolList() {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.boolListAction);
List<Boolean> dataList = List.of(true, false);

// Asynchronous callback
communication().callbackListBool(cmdInfo, dataList, response -> {
log.info("{}", response.listBoolean());
});

// Synchronous call
var response = communication().callListBool(cmdInfo, dataList);
return response.listBoolean();
}

@ActionMethod(CallCmd.callLongList)
private List<Long> callLongList() {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.longListAction);
List<Long> dataList = List.of(1L, 2L);

// Asynchronous callback
communication().callbackListLong(cmdInfo, dataList, response -> {
log.info("{}", response.listLong());
});

// Synchronous call
var response = communication().callListLong(cmdInfo, dataList);
return response.listLong();
}

@ActionMethod(CallCmd.callStringList)
private List<String> callStringList() {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.stringListAction);
List<String> dataList = List.of("hello", "ionet");

// Asynchronous callback
communication().callbackListString(cmdInfo, dataList, response -> {
log.info("{}", response.listString());
});

// Synchronous call
var response = communication().callListString(cmdInfo, dataList);
return response.listString();
}

@ActionMethod(CallCmd.callObjectList)
private List<BookMessage> callObjectList() {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.objectListAction);

var author1 = new AuthorMessage();
author1.authorName = "David Myers";

var author2 = new AuthorMessage();
author2.authorName = "Gustave Le Bon";

List<AuthorMessage> dataList = List.of(author1, author2);

// Asynchronous callback
communication().callback(cmdInfo, dataList, response -> {
log.info("{}", response.listValue(BookMessage.class));
});

// Synchronous call
var response = communication().call(cmdInfo, dataList);
return response.listValue(BookMessage.class);
}
}

Broadcast

本节介绍 Broadcast 的使用示例,广播常用于主动给一个、多个或全服的用户发送数据。比如:

  1. broadcastUser (一个): 给指定的在线用户发送一些奖励。
  2. broadcastUsers (多个): 给在同一个房间内的用户广播一些数据,如某一个用户射击子弹,把这子弹的数据广播给房间内的其他用户。
  3. broadcastMulticast (全服): 给全服的所有在线用户广播消息,如广播公告、即将停服维护等。

broadcastUser

广播给指定单个用户, api 名字以 broadcastUser 打头。

@ActionController(BroadcastCmd.cmd)
public class BroadcastAction {
AtomicInteger inc = new AtomicInteger();

@ActionMethod(triggerBroadcastUser)
private void triggerBroadcastUser(long userId) {
var communication = CommunicationKit.getCommunication();

// ---------- empty ----------
communication.broadcastUser(userId, BroadcastCmd.broadcastUserEmpty);

// ---------- int ----------
int dataInt = inc.getAndIncrement();
communication.broadcastUser(userId, BroadcastCmd.broadcastUserInt, dataInt);

// ---------- boolean ----------
boolean dataBool = inc.getAndIncrement() % 2 == 0;
communication.broadcastUser(userId, BroadcastCmd.broadcastUserBool, dataBool);

// ---------- long ----------
long dataLong = inc.getAndIncrement();
communication.broadcastUser(userId, BroadcastCmd.broadcastUserLong, dataLong);

// ---------- string ----------
String dataString = "ionet-" + inc.getAndIncrement();
communication.broadcastUser(userId, BroadcastCmd.broadcastUserString, dataString);

// ---------- object ----------
BookMessage dataObject = new BookMessage();
dataObject.authorName = "ionet";
dataObject.bookName = "book-" + inc.getAndIncrement();
communication.broadcastUser(userId, BroadcastCmd.broadcastUserObject, dataObject);

// ---------- list int ----------
List<Integer> dataListInt = List.of(inc.getAndIncrement(), inc.getAndIncrement());
communication.broadcastUserListInt(userId, BroadcastCmd.broadcastUserIntList, dataListInt);

// ---------- list boolean ----------
List<Boolean> dataListBool = List.of(
inc.getAndIncrement() % 2 == 0,
inc.getAndIncrement() % 2 == 0
);

communication.broadcastUserListBool(userId, BroadcastCmd.broadcastUserBoolList, dataListBool);

// ---------- list long ----------
List<Long> dataListLong = List.of(
(long) inc.getAndIncrement(),
(long) inc.getAndIncrement()
);

communication.broadcastUserListLong(userId, BroadcastCmd.broadcastUserLongList, dataListLong);

// ---------- list string ----------
List<String> dataListString = List.of(
"ionet-" + inc.getAndIncrement(),
"ionet-" + inc.getAndIncrement()
);

communication.broadcastUserListString(userId, BroadcastCmd.broadcastUserStringList, dataListString);

// ---------- list object ----------
BookMessage message1 = new BookMessage();
message1.authorName = "ionet";
message1.bookName = "book-" + inc.getAndIncrement();

BookMessage message2 = new BookMessage();
message2.authorName = "ionet";
message2.bookName = "book-" + inc.getAndIncrement();

List<BookMessage> dataList = List.of(message1, message2);
communication.broadcastUser(userId, BroadcastCmd.broadcastUserObjectList, dataList);
}
}

broadcastUsers

广播给指定多个用户, api 名字以 broadcastUsers 打头。

@ActionController(BroadcastCmd.cmd)
public class BroadcastAction {
AtomicInteger inc = new AtomicInteger();

@ActionMethod(triggerBroadcastUsers)
private void triggerBroadcastUsers(List<Long> userIdList) {
var communication = CommunicationKit.getCommunication();
// ---------- empty ----------
communication.broadcastUsers(userIdList, BroadcastCmd.broadcastUsersEmpty);
// ---------- int ----------
int dataInt = inc.getAndIncrement();
communication.broadcastUsers(userIdList, BroadcastCmd.broadcastUsersInt, dataInt);

// ---------- boolean ----------
boolean dataBool = inc.getAndIncrement() % 2 == 0;
communication.broadcastUsers(userIdList, BroadcastCmd.broadcastUsersBool, dataBool);

// ---------- long ----------
long dataLong = inc.getAndIncrement();
communication.broadcastUsers(userIdList, BroadcastCmd.broadcastUsersLong, dataLong);

// ---------- string ----------
String dataString = "ionet-" + inc.getAndIncrement();
communication.broadcastUsers(userIdList, BroadcastCmd.broadcastUsersString, dataString);

// ---------- object ----------
BookMessage dataObject = new BookMessage();
dataObject.authorName = "ionet";
dataObject.bookName = "book-" + inc.getAndIncrement();
communication.broadcastUsers(userIdList, BroadcastCmd.broadcastUsersObject, dataObject);

// ---------- list int ----------
List<Integer> dataListInt = List.of(inc.getAndIncrement(), inc.getAndIncrement());
communication.broadcastUsersListInt(userIdList, BroadcastCmd.broadcastUsersIntList, dataListInt);

// ---------- list boolean ----------
List<Boolean> dataListBool = List.of(
inc.getAndIncrement() % 2 == 0,
inc.getAndIncrement() % 2 == 0
);

communication.broadcastUsersListBool(userIdList, BroadcastCmd.broadcastUsersBoolList, dataListBool);

// ---------- list long ----------
List<Long> dataListLong = List.of(
(long) inc.getAndIncrement(),
(long) inc.getAndIncrement()
);

communication.broadcastUsersListLong(userIdList, BroadcastCmd.broadcastUsersLongList, dataListLong);

// ---------- list string ----------
List<String> dataListString = List.of(
"ionet-" + inc.getAndIncrement(),
"ionet-" + inc.getAndIncrement()
);

communication.broadcastUsersListString(userIdList, BroadcastCmd.broadcastUsersStringList, dataListString);

// ---------- list object ----------
BookMessage message1 = new BookMessage();
message1.authorName = "ionet";
message1.bookName = "book-" + inc.getAndIncrement();

BookMessage message2 = new BookMessage();
message2.authorName = "ionet";
message2.bookName = "book-" + inc.getAndIncrement();

List<BookMessage> dataList = List.of(message1, message2);
communication.broadcastUsers(userIdList, BroadcastCmd.broadcastUsersObjectList, dataList);
}
}

broadcastMulticast

广播给所有人, api 名字以 broadcastMulticast 打头。

@ActionController(BroadcastCmd.cmd)
public class BroadcastAction {
AtomicInteger inc = new AtomicInteger();

@ActionMethod(triggerBroadcastMulticast)
private void triggerBroadcastMulticast() {
var communication = CommunicationKit.getCommunication();
// ---------- empty ----------
communication.broadcastMulticast(BroadcastCmd.broadcastMulticastEmpty);

// ---------- int ----------
int dataInt = inc.getAndIncrement();
communication.broadcastMulticast(BroadcastCmd.broadcastMulticastInt, dataInt);

// ---------- boolean ----------
boolean dataBool = inc.getAndIncrement() % 2 == 0;
communication.broadcastMulticast(BroadcastCmd.broadcastMulticastBool, dataBool);

// ---------- long ----------
long dataLong = inc.getAndIncrement();
communication.broadcastMulticast(BroadcastCmd.broadcastMulticastLong, dataLong);

// ---------- string ----------
String dataString = "ionet-" + inc.getAndIncrement();
communication.broadcastMulticast(BroadcastCmd.broadcastMulticastString, dataString);

// ---------- object ----------
BookMessage dataObject = new BookMessage();
dataObject.authorName = "ionet";
dataObject.bookName = "book-" + inc.getAndIncrement();
communication.broadcastMulticast(BroadcastCmd.broadcastMulticastObject, dataObject);

// ---------- list int ----------
List<Integer> dataListInt = List.of(inc.getAndIncrement(), inc.getAndIncrement());
communication.broadcastMulticastListInt(BroadcastCmd.broadcastMulticastIntList, dataListInt);

// ---------- list boolean ----------
List<Boolean> dataListBool = List.of(
inc.getAndIncrement() % 2 == 0,
inc.getAndIncrement() % 2 == 0
);

communication.broadcastMulticastListBool(BroadcastCmd.broadcastMulticastBoolList, dataListBool);

// ---------- list long ----------
List<Long> dataListLong = List.of(
(long) inc.getAndIncrement(),
(long) inc.getAndIncrement()
);

communication.broadcastMulticastListLong(BroadcastCmd.broadcastMulticastLongList, dataListLong);

// ---------- list string ----------
List<String> dataListString = List.of(
"ionet-" + inc.getAndIncrement(),
"ionet-" + inc.getAndIncrement()
);

communication.broadcastMulticastListString(BroadcastCmd.broadcastMulticastStringList, dataListString);

// ---------- list object ----------
BookMessage message1 = new BookMessage();
message1.authorName = "ionet";
message1.bookName = "book-" + inc.getAndIncrement();

BookMessage message2 = new BookMessage();
message2.authorName = "ionet";
message2.bookName = "book-" + inc.getAndIncrement();

List<BookMessage> dataList = List.of(message1, message2);
communication.broadcastMulticast(BroadcastCmd.broadcastMulticastObjectList, dataList);
}
}

EventBus

注意

该功能为企业级功能

本节介绍分布式事件总线 EventBus 的使用示例。 通过分布式事件总线 EventBus,可以实现跨服务的轻量级事件通信。

@ActionController(EventBusCmd.cmd)
public class EventBusAction {
@ActionMethod(EventBusCmd.fire)
private boolean fire(FlowContext flowContext) {
long userId = flowContext.getUserId();

var message = UserLoginEventMessage.of(userId);
flowContext.fire(message);

return true;
}

@ActionMethod(EventBusCmd.fireAny)
private boolean fireAny(FlowContext flowContext) {
long userId = flowContext.getUserId();

var message = UserLoginEventMessage.of(userId);
flowContext.fireAny(message);

return true;
}

@ActionMethod(EventBusCmd.fireMe)
private boolean fireMe(FlowContext flowContext) {
long userId = flowContext.getUserId();

var message = UserLoginEventMessage.of(userId);
flowContext.fireMe(message);

return true;
}
}

RequestMultipleResponse

注意

该功能为企业级功能

本节介绍 request/multiple_response 的使用示例, 该模型可以把多个同类型逻辑服的结果收集到一起。

@ActionController(CallCollectCmd.cmd)
public class CallCollectAction {
private Communication communication() {
return CommunicationKit.getCommunication();
}

@ActionMethod(CallCollectCmd.callCollectEmpty)
private List<Integer> callCollectEmpty() {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.intAction);

// Asynchronous callback
communication().callbackCollect(cmdInfo, responseCollect -> {
List<Response> responseList = responseCollect.getResponseList();
for (Response response : responseList) {
log.info("{}", response.getInt());
}
});

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

@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().callbackCollect(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();
}

@ActionMethod(CallCollectCmd.callCollectBool)
private List<Boolean> callBool() {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.boolAction);
boolean data = true;

// Asynchronous callback
communication().callbackCollect(cmdInfo, data, responseCollect -> {
List<Response> responseList = responseCollect.getResponseList();
for (Response response : responseList) {
log.info("{}", response.getBoolean());
}
});

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

@ActionMethod(CallCollectCmd.callCollectLong)
private List<Long> callLong() {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.longAction);
long data = 1L;

// Asynchronous callback
communication().callbackCollect(cmdInfo, data, responseCollect -> {
List<Response> responseList = responseCollect.getResponseList();
for (Response response : responseList) {
log.info("{}", response.getLong());
}
});

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

@ActionMethod(CallCollectCmd.callCollectString)
private List<String> callString() {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.stringAction);
String data = "hello";

// Asynchronous callback
communication().callbackCollect(cmdInfo, data, responseCollect -> {
List<Response> responseList = responseCollect.getResponseList();
for (Response response : responseList) {
log.info("{}", response.getString());
}
});

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

@ActionMethod(CallCollectCmd.callCollectObject)
private List<BookMessage> callObject() {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.objectAction);
var data = new AuthorMessage();

// Asynchronous callback
communication().callbackCollect(cmdInfo, data, responseCollect -> {
List<Response> responseList = responseCollect.getResponseList();
for (Response response : responseList) {
log.info("{}", response.getValue(BookMessage.class));
}
});

// Synchronous call
var responseCollect = communication().callCollect(cmdInfo, data);
List<Response> responseList = responseCollect.getResponseList();
return responseList.stream()
.map(response -> response.getValue(BookMessage.class))
.toList();
}

@ActionMethod(CallCollectCmd.callCollectIntList)
private List<Integer> callIntList() {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.intListAction);
List<Integer> dataList = List.of(1, 2);

// Asynchronous callback
communication().callbackCollectListInt(cmdInfo, dataList, responseCollect -> {
List<Response> responseList = responseCollect.getResponseList();
for (Response response : responseList) {
log.info("{}", response.listInt());
}
});

var responseCollect = communication().callCollectListInt(cmdInfo, dataList);
List<Response> responseList = responseCollect.getResponseList();
return responseList.stream()
.flatMap(response -> response.listInt().stream())
.toList();
}

@ActionMethod(CallCollectCmd.callCollectBoolList)
private List<Boolean> callBoolList() {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.boolListAction);
List<Boolean> dataList = List.of(true, false);

// Asynchronous callback
communication().callbackCollectListBool(cmdInfo, dataList, responseCollect -> {
List<Response> responseList = responseCollect.getResponseList();
for (Response response : responseList) {
log.info("{}", response.listBoolean());
}
});

// Synchronous call
var responseCollect = communication().callCollectListBool(cmdInfo, dataList);
List<Response> responseList = responseCollect.getResponseList();
return responseList.stream()
.flatMap(response -> response.listBoolean().stream())
.toList();
}

@ActionMethod(CallCollectCmd.callCollectLongList)
private List<Long> callLongList() {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.longListAction);
List<Long> dataList = List.of(1L, 2L);

// Asynchronous callback
communication().callbackCollectListLong(cmdInfo, dataList, responseCollect -> {
List<Response> responseList = responseCollect.getResponseList();
for (Response response : responseList) {
log.info("{}", response.listLong());
}
});

// Synchronous call
var responseCollect = communication().callCollectListLong(cmdInfo, dataList);
List<Response> responseList = responseCollect.getResponseList();
return responseList.stream()
.flatMap(response -> response.listLong().stream())
.toList();
}

@ActionMethod(CallCollectCmd.callCollectStringList)
private List<String> callStringList() {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.stringListAction);
List<String> dataList = List.of("hello", "ionet");

// Asynchronous callback
communication().callbackCollectListString(cmdInfo, dataList, responseCollect -> {
List<Response> responseList = responseCollect.getResponseList();
for (Response response : responseList) {
log.info("{}", response.listString());
}
});

// Synchronous call
var responseCollect = communication().callCollectListString(cmdInfo, dataList);
List<Response> responseList = responseCollect.getResponseList();
return responseList.stream()
.flatMap(response -> response.listString().stream())
.toList();
}

@ActionMethod(CallCollectCmd.callCollectObjectList)
private List<BookMessage> callObjectList() {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.objectListAction);

var author1 = new AuthorMessage();
author1.authorName = "David Myers";

var author2 = new AuthorMessage();
author2.authorName = "Gustave Le Bon";

List<AuthorMessage> dataList = List.of(author1, author2);

// Asynchronous callback
communication().callbackCollect(cmdInfo, dataList, responseCollect -> {
List<Response> responseList = responseCollect.getResponseList();
for (Response response : responseList) {
log.info("{}", response.listValue(BookMessage.class));
}
});

// Synchronous call
var responseCollect = communication().callCollect(cmdInfo, dataList);
List<Response> responseList = responseCollect.getResponseList();
return responseList.stream()
.flatMap(response -> response.listValue(BookMessage.class).stream())
.toList();
}
}