use of info.xiancloud.core.message.UnitResponse in project xian by happyyangyuan.
the class Node method send.
public UnitResponse send(UnitRequest request) {
String ssid = IdManager.nextSsid();
request.getContext().setSsid(ssid);
fillRequestContext(request.getContext());
UnitResponse unitResponse = UnitResponse.create(true);
final CountDownLatch latch = new CountDownLatch(1);
NotifyHandler handler = new NotifyHandler() {
public void handle(UnitResponse output) {
UnitResponse.copy(output, output);
latch.countDown();
}
};
handleMap.put(ssid, handler);
String payload = JSON.toJSONStringWithDateFormat(request, Constant.DATE_SERIALIZE_FORMAT);
publisher.p2pPublish(request.getContext().getDestinationNodeId(), payload);
try {
if (!latch.await(Constant.UNIT_DEFAULT_TIME_OUT_IN_MILLI, TimeUnit.MILLISECONDS)) {
handler.setTimeout(true);
return UnitResponse.error(Group.CODE_TIME_OUT, null, "Response time out!").setContext(UnitResponse.Context.create().setSsid(ssid));
}
} catch (InterruptedException e) {
return UnitResponse.exception(e);
}
return unitResponse;
}
use of info.xiancloud.core.message.UnitResponse in project xian by happyyangyuan.
the class DaoUnit method execute.
@Override
public final UnitResponse execute(UnitRequest msg) {
try {
init(msg);
transaction.get().begin();
List<UnitResponse> unitResponseObjects = new ArrayList<>();
for (Action action : getProxiedActions()) {
UnitResponse unitResponseObject = action.execute(this, msg.getArgMap(), transaction.get().getConnection());
if (Group.CODE_SUCCESS.equals(unitResponseObject.getCode())) {
unitResponseObjects.add(unitResponseObject);
} else {
// db插件内如果返回的code不是SUCCESS,目前的实现是一律回滚事务
transaction.get().rollback();
return unitResponseObject;
}
}
transaction.get().commit();
return unitResponseObjects.get(unitResponseObjects.size() - 1);
} catch (Throwable t) {
LOG.error(t);
transaction.get().rollback();
return UnitResponse.error(DaoGroup.CODE_DB_ERROR, t, null);
} finally {
destroy();
}
}
use of info.xiancloud.core.message.UnitResponse in project xian by happyyangyuan.
the class StreamUnitResponseObjectTest method printResponseStreamPartByPart.
@Test
public void printResponseStreamPartByPart() throws FileNotFoundException {
UnitResponse unitResponseObject = UnitResponse.success(new FileInputStream("/Users/happyyangyuan/Downloads/zz.txt"));
unitResponseObject.processStreamPartByPart("[{]", part -> {
System.out.println(part);
return null;
});
}
use of info.xiancloud.core.message.UnitResponse in project xian by happyyangyuan.
the class StreamUnitResponseObjectTest method printResponseStreamLineByLine.
@Test
public void printResponseStreamLineByLine() throws FileNotFoundException {
UnitResponse unitResponseObject = UnitResponse.success(new FileInputStream("/Users/happyyangyuan/Downloads/zz.txt"));
unitResponseObject.processStreamLineByLine(line -> {
System.out.println(line);
return null;
});
}
use of info.xiancloud.core.message.UnitResponse in project xian by happyyangyuan.
the class AppTransaction method commitAndCloseTogether.
@Override
public void commitAndCloseTogether() throws SQLException {
// 发送远程请求,将其他数据库事务提交和关闭
JSONArray clientIds = TransactionalCache.clear();
if (clientIds != null) {
for (int i = 0; i < clientIds.size(); i++) {
final String clientId = clientIds.getString(i);
if (!LocalNodeManager.LOCAL_NODE_ID.equals(clientId)) {
UnitRequest request = UnitRequest.create(CommitAndCloseTogetherUnit.class);
request.getContext().setDestinationNodeId(clientId);
LocalNodeManager.send(request, new NotifyHandler() {
protected void handle(UnitResponse unitResponse) {
LOG.info(clientId + "的事务已经提交");
}
});
}
}
}
// 提交和关闭自己的数据库事务
connection.commit();
connection.close();
// 清除本地事务
localTransMap.remove(transactionId);
}
Aggregations