use of info.xiancloud.core.message.sender.local.RoutedLocalAsyncSender in project xian by happyyangyuan.
the class XhashSender method asyncSend.
@Override
protected void asyncSend() {
String group = unitRequest.getContext().getGroup(), unit = unitRequest.getContext().getUnit();
try {
List<UnitInstance> unitInstances = UnitRouter.singleton.allInstances(Unit.fullName(group, unit));
List<String> clientIds = new ArrayList<>();
for (UnitInstance clientInfo : unitInstances) {
clientIds.add(clientInfo.getNodeId());
}
String[] xhashNames = unitInstances.get(0).getPayload().getInput().getXhashNames();
// sorting is for constant-hash requirement.
Collections.sort(clientIds);
String clientId = new Shard<>(clientIds).getShardInfo(xhashString(xhashNames));
if (clientId.equals(LocalNodeManager.LOCAL_NODE_ID)) {
new RoutedLocalAsyncSender(unitRequest, callback).send();
} else {
unitRequest.getContext().setDestinationNodeId(clientId);
LocalNodeManager.send(unitRequest, callback);
}
} catch (UnitOfflineException | UnitUndefinedException e) {
LOG.error("代码写错了吧? 进入xhashSender的前提就是unit在线!", e);
callback.callback(UnitResponse.exception(e));
}
}
use of info.xiancloud.core.message.sender.local.RoutedLocalAsyncSender in project xian by happyyangyuan.
the class BroadcastSender method asyncSend.
@Override
protected void asyncSend() throws UnitOfflineException, UnitUndefinedException {
List<UnitInstance> list = UnitRouter.singleton.allInstances(Unit.fullName(unitRequest.getContext().getGroup(), unitRequest.getContext().getUnit()));
UnitMeta.Broadcast broadcast = list.get(0).getPayload().getMeta().getBroadcast();
final CountDownLatch latch = new CountDownLatch(list.size());
Collection<Object> piledUpOutput = new ConcurrentLinkedQueue<>();
final NotifyHandler tmpHandler = new NotifyHandler() {
protected void handle(UnitResponse output) {
if (!broadcast.isSuccessDataOnly()) {
piledUpOutput.add(output);
} else if (output.succeeded()) {
piledUpOutput.add(output.getData());
}
latch.countDown();
}
};
for (UnitInstance unitInstance : list) {
if (unitInstance.getNodeId().equals(LocalNodeManager.LOCAL_NODE_ID)) {
// we must not share the same unitRequest object while sending request concurrently.
UnitRequest clonedUnitRequest = CloneUtil.cloneBean(unitRequest, UnitRequest.class);
/*clonedUnitRequest.getContext().setDestinationNodeId(unitInstance.getNodeId()); no need */
new RoutedLocalAsyncSender(clonedUnitRequest, tmpHandler).send();
} else {
LOG.debug("In order not to share the same unit request object,we clone a new request");
UnitRequest clonedRequest = CloneUtil.cloneBean(unitRequest, UnitRequest.class);
clonedRequest.getContext().setDestinationNodeId(unitInstance.getNodeId());
LocalNodeManager.send(clonedRequest, tmpHandler);
}
}
if (broadcast.isAsync()) {
callback.callback(UnitResponse.success());
} else {
try {
if (!latch.await(broadcast.getTimeoutInMilli(), TimeUnit.MILLISECONDS)) {
LOG.error(new TimeoutException());
callback.callback(UnitResponse.error(Group.CODE_TIME_OUT, piledUpOutput, "Time out while waiting for all the units to response, the data is only part of the result. "));
} else {
callback.callback(UnitResponse.success(piledUpOutput));
}
} catch (InterruptedException e) {
callback.callback(UnitResponse.exception(e));
}
}
}
Aggregations