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 是异步调用的,因为明确知道请求是无响应的。
- By CommunicationKit
- By FlowContext
@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;
}
}
@ActionController(FlowContextSendCmd.cmd)
public class FlowContextSendAction {
@ActionMethod(FlowContextSendCmd.sendEmpty)
private boolean sendEmpty(FlowContext flowContext) {
var cmdInfo = InternalCmd.of(InternalCmd.sendEmptyAction);
int data = 1;
flowContext.send(cmdInfo, data);
return true;
}
@ActionMethod(FlowContextSendCmd.sendInt)
private boolean sendInt(FlowContext flowContext) {
var cmdInfo = InternalCmd.of(InternalCmd.sendIntAction);
int data = 1;
flowContext.send(cmdInfo, data);
return true;
}
@ActionMethod(FlowContextSendCmd.sendBool)
private boolean sendBool(FlowContext flowContext) {
var cmdInfo = InternalCmd.of(InternalCmd.sendBoolAction);
boolean data = true;
flowContext.send(cmdInfo, data);
return true;
}
@ActionMethod(FlowContextSendCmd.sendLong)
private boolean sendLong(FlowContext flowContext) {
var cmdInfo = InternalCmd.of(InternalCmd.sendLongAction);
long data = 1L;
flowContext.send(cmdInfo, data);
return true;
}
@ActionMethod(FlowContextSendCmd.sendString)
private boolean sendString(FlowContext flowContext) {
var cmdInfo = InternalCmd.of(InternalCmd.sendStringAction);
String data = "hello";
flowContext.send(cmdInfo, data);
return true;
}
@ActionMethod(FlowContextSendCmd.sendObject)
private boolean sendObject(FlowContext flowContext) {
var cmdInfo = InternalCmd.of(InternalCmd.sendObjectAction);
var data = new AuthorMessage();
data.authorName = "Joker";
flowContext.send(cmdInfo, data);
return true;
}
@ActionMethod(FlowContextSendCmd.sendIntList)
private boolean sendIntList(FlowContext flowContext) {
var cmdInfo = InternalCmd.of(InternalCmd.sendIntListAction);
List<Integer> dataList = List.of(1, 2);
flowContext.sendListInt(cmdInfo, dataList);
return true;
}
@ActionMethod(FlowContextSendCmd.sendBoolList)
private boolean sendBoolList(FlowContext flowContext) {
var cmdInfo = InternalCmd.of(InternalCmd.sendBoolListAction);
List<Boolean> dataList = List.of(true, false);
flowContext.sendListBool(cmdInfo, dataList);
return true;
}
@ActionMethod(FlowContextSendCmd.sendLongList)
private boolean sendLongList(FlowContext flowContext) {
var cmdInfo = InternalCmd.of(InternalCmd.sendLongListAction);
List<Long> dataList = List.of(1L, 2L);
flowContext.sendListLong(cmdInfo, dataList);
return true;
}
@ActionMethod(FlowContextSendCmd.sendStringList)
private boolean sendStringList(FlowContext flowContext) {
var cmdInfo = InternalCmd.of(InternalCmd.sendStringListAction);
List<String> dataList = List.of("hello", "ionet");
flowContext.sendListString(cmdInfo, dataList);
return true;
}
@ActionMethod(FlowContextSendCmd.sendObjectList)
private boolean sendObjectList(FlowContext flowContext) {
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);
flowContext.send(cmdInfo, dataList);
return true;
}
}
Call & Callback
call 系列用于内部有响应的请求,对应 request/response 通信模型。
- call: 同步调用。
- callback: 异步调用。
- By CommunicationKit
- By FlowContext
@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);
}
}
@ActionController(FlowContextCallCmd.cmd)
public class FlowContextCallAction {
@ActionMethod(FlowContextCallCmd.callEmpty)
private int callEmpty(FlowContext flowContext) {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.emptyAction);
// Asynchronous callback
flowContext.callback(cmdInfo, response -> {
log.info("{}", response.getInt());
});
// Synchronous call
var response = flowContext.call(cmdInfo);
return response.getInt();
}
@ActionMethod(FlowContextCallCmd.callInt)
private int callInt(FlowContext flowContext) {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.intAction);
int data = 1;
// Asynchronous callback
flowContext.callback(cmdInfo, data, response -> {
log.info("{}", response.getInt());
});
// Synchronous call
var response = flowContext.call(cmdInfo, data);
return response.getInt();
}
@ActionMethod(FlowContextCallCmd.callBool)
private boolean callBool(FlowContext flowContext) {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.boolAction);
boolean data = true;
// Asynchronous callback
flowContext.callback(cmdInfo, data, response -> {
log.info("{}", response.getBoolean());
});
// Synchronous call
var response = flowContext.call(cmdInfo, data);
return response.getBoolean();
}
@ActionMethod(FlowContextCallCmd.callLong)
private long callLong(FlowContext flowContext) {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.longAction);
long data = 1L;
// Asynchronous callback
flowContext.callback(cmdInfo, data, response -> {
log.info("{}", response.getLong());
});
// Synchronous call
var response = flowContext.call(cmdInfo, data);
return response.getLong();
}
@ActionMethod(FlowContextCallCmd.callString)
private String callString(FlowContext flowContext) {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.stringAction);
String data = "hello";
// Asynchronous callback
flowContext.callback(cmdInfo, data, response -> {
log.info("{}", response.getString());
});
// Synchronous call
var response = flowContext.call(cmdInfo, data);
return response.getString();
}
@ActionMethod(FlowContextCallCmd.callObject)
private BookMessage callObject(FlowContext flowContext) {
// 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
flowContext.callback(cmdInfo, data, response -> {
log.info("{}", response.getValue(BookMessage.class));
});
// Synchronous call
var response = flowContext.call(cmdInfo, data);
return response.getValue(BookMessage.class);
}
@ActionMethod(FlowContextCallCmd.callIntList)
private List<Integer> callIntList(FlowContext flowContext) {
// 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
flowContext.callbackListInt(cmdInfo, dataList, response -> {
log.info("{}", response.listInt());
});
// Synchronous call
var response = flowContext.callListInt(cmdInfo, dataList);
return response.listInt();
}
@ActionMethod(FlowContextCallCmd.callBoolList)
private List<Boolean> callBoolList(FlowContext flowContext) {
// 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
flowContext.callbackListBool(cmdInfo, dataList, response -> {
log.info("{}", response.listBoolean());
});
// Synchronous call
var response = flowContext.callListBool(cmdInfo, dataList);
return response.listBoolean();
}
@ActionMethod(FlowContextCallCmd.callLongList)
private List<Long> callLongList(FlowContext flowContext) {
// 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
flowContext.callbackListLong(cmdInfo, dataList, response -> {
log.info("{}", response.listLong());
});
// Synchronous call
var response = flowContext.callListLong(cmdInfo, dataList);
return response.listLong();
}
@ActionMethod(FlowContextCallCmd.callStringList)
private List<String> callStringList(FlowContext flowContext) {
// 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
flowContext.callbackListString(cmdInfo, dataList, response -> {
log.info("{}", response.listString());
});
// Synchronous call
var response = flowContext.callListString(cmdInfo, dataList);
return response.listString();
}
@ActionMethod(FlowContextCallCmd.callObjectList)
private List<BookMessage> callObjectList(FlowContext flowContext) {
// 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
flowContext.callback(cmdInfo, dataList, response -> {
log.info("{}", response.listValue(BookMessage.class));
});
// Synchronous call
var response = flowContext.call(cmdInfo, dataList);
return response.listValue(BookMessage.class);
}
}
Broadcast
本节介绍 Broadcast 的使用示例,广播常用于主动给一个、多个或全服的用户发送数据。比如:
- broadcastUser (一个): 给指定的在线用户发送一些奖励。
- broadcastUsers (多个): 给在同一个房间内的用户广播一些数据,如某一个用户射击子弹,把这子弹的数据广播给房间内的其他用户。
- broadcastMulticast (全服): 给全服的所有在线用户广播消息,如广播公告、即将停服维护等。
broadcastUser
广播给指定单个用户, api 名字以 broadcastUser 打头。
- By CommunicationKit
- By FlowContext
@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);
}
}
@ActionController(FlowContextBroadcastCmd.cmd)
public class FlowContextBroadcastAction {
AtomicInteger inc = new AtomicInteger();
@ActionMethod(triggerBroadcastUser)
private void triggerBroadcastUser(long userId, FlowContext flowContext) {
// ---------- empty ----------
flowContext.broadcastUser(userId, BroadcastCmd.broadcastUserEmpty);
// ---------- int ----------
int dataInt = inc.getAndIncrement();
flowContext.broadcastUser(userId, BroadcastCmd.broadcastUserInt, dataInt);
// ---------- boolean ----------
boolean dataBool = inc.getAndIncrement() % 2 == 0;
flowContext.broadcastUser(userId, BroadcastCmd.broadcastUserBool, dataBool);
// ---------- long ----------
long dataLong = inc.getAndIncrement();
flowContext.broadcastUser(userId, BroadcastCmd.broadcastUserLong, dataLong);
// ---------- string ----------
String dataString = "ionet-" + inc.getAndIncrement();
flowContext.broadcastUser(userId, BroadcastCmd.broadcastUserString, dataString);
// ---------- object ----------
BookMessage dataObject = new BookMessage();
dataObject.authorName = "ionet";
dataObject.bookName = "book-" + inc.getAndIncrement();
flowContext.broadcastUser(userId, BroadcastCmd.broadcastUserObject, dataObject);
// ---------- list int ----------
List<Integer> dataListInt = List.of(inc.getAndIncrement(), inc.getAndIncrement());
flowContext.broadcastUserListInt(userId, BroadcastCmd.broadcastUserIntList, dataListInt);
// ---------- list boolean ----------
List<Boolean> dataListBool = List.of(
inc.getAndIncrement() % 2 == 0,
inc.getAndIncrement() % 2 == 0
);
flowContext.broadcastUserListBool(userId, BroadcastCmd.broadcastUserBoolList, dataListBool);
// ---------- list long ----------
List<Long> dataListLong = List.of(
(long) inc.getAndIncrement(),
(long) inc.getAndIncrement()
);
flowContext.broadcastUserListLong(userId, BroadcastCmd.broadcastUserLongList, dataListLong);
// ---------- list string ----------
List<String> dataListString = List.of(
"ionet-" + inc.getAndIncrement(),
"ionet-" + inc.getAndIncrement()
);
flowContext.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);
flowContext.broadcastUser(userId, BroadcastCmd.broadcastUserObjectList, dataList);
}
broadcastUsers
广播给指定多个用户, api 名字以 broadcastUsers 打头。
- By CommunicationKit
- By FlowContext
@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);
}
}
@ActionController(FlowContextBroadcastCmd.cmd)
public class FlowContextBroadcastAction {
AtomicInteger inc = new AtomicInteger();
@ActionMethod(triggerBroadcastUsers)
private void triggerBroadcastUsers(List<Long> userIdList, FlowContext flowContext) {
// ---------- empty ----------
flowContext.broadcastUsers(userIdList, BroadcastCmd.broadcastUsersEmpty);
// ---------- int ----------
int dataInt = inc.getAndIncrement();
flowContext.broadcastUsers(userIdList, BroadcastCmd.broadcastUsersInt, dataInt);
// ---------- boolean ----------
boolean dataBool = inc.getAndIncrement() % 2 == 0;
flowContext.broadcastUsers(userIdList, BroadcastCmd.broadcastUsersBool, dataBool);
// ---------- long ----------
long dataLong = inc.getAndIncrement();
flowContext.broadcastUsers(userIdList, BroadcastCmd.broadcastUsersLong, dataLong);
// ---------- string ----------
String dataString = "ionet-" + inc.getAndIncrement();
flowContext.broadcastUsers(userIdList, BroadcastCmd.broadcastUsersString, dataString);
// ---------- object ----------
BookMessage dataObject = new BookMessage();
dataObject.authorName = "ionet";
dataObject.bookName = "book-" + inc.getAndIncrement();
flowContext.broadcastUsers(userIdList, BroadcastCmd.broadcastUsersObject, dataObject);
// ---------- list int ----------
List<Integer> dataListInt = List.of(inc.getAndIncrement(), inc.getAndIncrement());
flowContext.broadcastUsersListInt(userIdList, BroadcastCmd.broadcastUsersIntList, dataListInt);
// ---------- list boolean ----------
List<Boolean> dataListBool = List.of(
inc.getAndIncrement() % 2 == 0,
inc.getAndIncrement() % 2 == 0
);
flowContext.broadcastUsersListBool(userIdList, BroadcastCmd.broadcastUsersBoolList, dataListBool);
// ---------- list long ----------
List<Long> dataListLong = List.of(
(long) inc.getAndIncrement(),
(long) inc.getAndIncrement()
);
flowContext.broadcastUsersListLong(userIdList, BroadcastCmd.broadcastUsersLongList, dataListLong);
// ---------- list string ----------
List<String> dataListString = List.of(
"ionet-" + inc.getAndIncrement(),
"ionet-" + inc.getAndIncrement()
);
flowContext.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);
flowContext.broadcastUsers(userIdList, BroadcastCmd.broadcastUsersObjectList, dataList);
}
}
broadcastMulticast
广播给所有人, api 名字以 broadcastMulticast 打头。
- By CommunicationKit
- By FlowContext
@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);
}
}
@ActionController(FlowContextBroadcastCmd.cmd)
public class FlowContextBroadcastAction {
AtomicInteger inc = new AtomicInteger();
@ActionMethod(triggerBroadcastMulticast)
private void triggerBroadcastMulticast(FlowContext flowContext) {
// ---------- empty ----------
flowContext.broadcastMulticast(BroadcastCmd.broadcastMulticastEmpty);
// ---------- int ----------
int dataInt = inc.getAndIncrement();
flowContext.broadcastMulticast(BroadcastCmd.broadcastMulticastInt, dataInt);
// ---------- boolean ----------
boolean dataBool = inc.getAndIncrement() % 2 == 0;
flowContext.broadcastMulticast(BroadcastCmd.broadcastMulticastBool, dataBool);
// ---------- long ----------
long dataLong = inc.getAndIncrement();
flowContext.broadcastMulticast(BroadcastCmd.broadcastMulticastLong, dataLong);
// ---------- string ----------
String dataString = "ionet-" + inc.getAndIncrement();
flowContext.broadcastMulticast(BroadcastCmd.broadcastMulticastString, dataString);
// ---------- object ----------
BookMessage dataObject = new BookMessage();
dataObject.authorName = "ionet";
dataObject.bookName = "book-" + inc.getAndIncrement();
flowContext.broadcastMulticast(BroadcastCmd.broadcastMulticastObject, dataObject);
// ---------- list int ----------
List<Integer> dataListInt = List.of(inc.getAndIncrement(), inc.getAndIncrement());
flowContext.broadcastMulticastListInt(BroadcastCmd.broadcastMulticastIntList, dataListInt);
// ---------- list boolean ----------
List<Boolean> dataListBool = List.of(
inc.getAndIncrement() % 2 == 0,
inc.getAndIncrement() % 2 == 0
);
flowContext.broadcastMulticastListBool(BroadcastCmd.broadcastMulticastBoolList, dataListBool);
// ---------- list long ----------
List<Long> dataListLong = List.of(
(long) inc.getAndIncrement(),
(long) inc.getAndIncrement()
);
flowContext.broadcastMulticastListLong(BroadcastCmd.broadcastMulticastLongList, dataListLong);
// ---------- list string ----------
List<String> dataListString = List.of(
"ionet-" + inc.getAndIncrement(),
"ionet-" + inc.getAndIncrement()
);
flowContext.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);
flowContext.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 的使用示例, 该模型可以把多个同类型逻辑服的结果收集到一起。
- By CommunicationKit
- By FlowContext
@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();
}
}
@ActionController(FlowContextCallCollectCmd.cmd)
public class FlowContextCallCollectAction {
@ActionMethod(FlowContextCallCollectCmd.callCollectEmpty)
private List<Integer> callCollectEmpty(FlowContext flowContext) {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.intAction);
// Asynchronous callback
flowContext.callbackCollect(cmdInfo, responseCollect -> {
List<Response> responseList = responseCollect.getResponseList();
for (Response response : responseList) {
log.info("{}", response.getInt());
}
});
// Synchronous call
var responseCollect = flowContext.callCollect(cmdInfo);
List<Response> responseList = responseCollect.getResponseList();
return responseList.stream()
.map(Response::getInt)
.toList();
}
@ActionMethod(FlowContextCallCollectCmd.callCollectInt)
private List<Integer> callInt(FlowContext flowContext) {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.intAction);
int data = 1;
// Asynchronous callback
flowContext.callbackCollect(cmdInfo, data, responseCollect -> {
List<Response> responseList = responseCollect.getResponseList();
for (Response response : responseList) {
log.info("{}", response.getInt());
}
});
// Synchronous call
var responseCollect = flowContext.callCollect(cmdInfo, data);
List<Response> responseList = responseCollect.getResponseList();
return responseList.stream()
.map(Response::getInt)
.toList();
}
@ActionMethod(FlowContextCallCollectCmd.callCollectBool)
private List<Boolean> callBool(FlowContext flowContext) {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.boolAction);
boolean data = true;
// Asynchronous callback
flowContext.callbackCollect(cmdInfo, data, responseCollect -> {
List<Response> responseList = responseCollect.getResponseList();
for (Response response : responseList) {
log.info("{}", response.getBoolean());
}
});
// Synchronous call
var responseCollect = flowContext.callCollect(cmdInfo, data);
List<Response> responseList = responseCollect.getResponseList();
return responseList.stream()
.map(Response::getBoolean)
.toList();
}
@ActionMethod(FlowContextCallCollectCmd.callCollectLong)
private List<Long> callLong(FlowContext flowContext) {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.longAction);
long data = 1L;
// Asynchronous callback
flowContext.callbackCollect(cmdInfo, data, responseCollect -> {
List<Response> responseList = responseCollect.getResponseList();
for (Response response : responseList) {
log.info("{}", response.getLong());
}
});
// Synchronous call
var responseCollect = flowContext.callCollect(cmdInfo, data);
List<Response> responseList = responseCollect.getResponseList();
return responseList.stream()
.map(Response::getLong)
.toList();
}
@ActionMethod(FlowContextCallCollectCmd.callCollectString)
private List<String> callString(FlowContext flowContext) {
// The two below are equivalent, one is synchronous and the other is asynchronous.
var cmdInfo = InternalCmd.of(InternalCmd.stringAction);
String data = "hello";
// Asynchronous callback
flowContext.callbackCollect(cmdInfo, data, responseCollect -> {
List<Response> responseList = responseCollect.getResponseList();
for (Response response : responseList) {
log.info("{}", response.getString());
}
});
// Synchronous call
var responseCollect = flowContext.callCollect(cmdInfo, data);
List<Response> responseList = responseCollect.getResponseList();
return responseList.stream()
.map(Response::getString)
.toList();
}
@ActionMethod(FlowContextCallCollectCmd.callCollectObject)
private List<BookMessage> callObject(FlowContext flowContext) {
// 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
flowContext.callbackCollect(cmdInfo, data, responseCollect -> {
List<Response> responseList = responseCollect.getResponseList();
for (Response response : responseList) {
log.info("{}", response.getValue(BookMessage.class));
}
});
// Synchronous call
var responseCollect = flowContext.callCollect(cmdInfo, data);
List<Response> responseList = responseCollect.getResponseList();
return responseList.stream()
.map(response -> response.getValue(BookMessage.class))
.toList();
}
@ActionMethod(FlowContextCallCollectCmd.callCollectIntList)
private List<Integer> callIntList(FlowContext flowContext) {
// 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
flowContext.callbackCollectListInt(cmdInfo, dataList, responseCollect -> {
List<Response> responseList = responseCollect.getResponseList();
for (Response response : responseList) {
log.info("{}", response.listInt());
}
});
var responseCollect = flowContext.callCollectListInt(cmdInfo, dataList);
List<Response> responseList = responseCollect.getResponseList();
return responseList.stream()
.flatMap(response -> response.listInt().stream())
.toList();
}
@ActionMethod(FlowContextCallCollectCmd.callCollectBoolList)
private List<Boolean> callBoolList(FlowContext flowContext) {
// 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
flowContext.callbackCollectListBool(cmdInfo, dataList, responseCollect -> {
List<Response> responseList = responseCollect.getResponseList();
for (Response response : responseList) {
log.info("{}", response.listBoolean());
}
});
// Synchronous call
var responseCollect = flowContext.callCollectListBool(cmdInfo, dataList);
List<Response> responseList = responseCollect.getResponseList();
return responseList.stream()
.flatMap(response -> response.listBoolean().stream())
.toList();
}
@ActionMethod(FlowContextCallCollectCmd.callCollectLongList)
private List<Long> callLongList(FlowContext flowContext) {
// 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
flowContext.callbackCollectListLong(cmdInfo, dataList, responseCollect -> {
List<Response> responseList = responseCollect.getResponseList();
for (Response response : responseList) {
log.info("{}", response.listLong());
}
});
// Synchronous call
var responseCollect = flowContext.callCollectListLong(cmdInfo, dataList);
List<Response> responseList = responseCollect.getResponseList();
return responseList.stream()
.flatMap(response -> response.listLong().stream())
.toList();
}
@ActionMethod(FlowContextCallCollectCmd.callCollectStringList)
private List<String> callStringList(FlowContext flowContext) {
// 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
flowContext.callbackCollectListString(cmdInfo, dataList, responseCollect -> {
List<Response> responseList = responseCollect.getResponseList();
for (Response response : responseList) {
log.info("{}", response.listString());
}
});
// Synchronous call
var responseCollect = flowContext.callCollectListString(cmdInfo, dataList);
List<Response> responseList = responseCollect.getResponseList();
return responseList.stream()
.flatMap(response -> response.listString().stream())
.toList();
}
@ActionMethod(FlowContextCallCollectCmd.callCollectObjectList)
private List<BookMessage> callObjectList(FlowContext flowContext) {
// 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
flowContext.callbackCollect(cmdInfo, dataList, responseCollect -> {
List<Response> responseList = responseCollect.getResponseList();
for (Response response : responseList) {
log.info("{}", response.listValue(BookMessage.class));
}
});
// Synchronous call
var responseCollect = flowContext.callCollect(cmdInfo, dataList);
List<Response> responseList = responseCollect.getResponseList();
return responseList.stream()
.flatMap(response -> response.listValue(BookMessage.class).stream())
.toList();
}
}