Search in sources :

Example 56 with UnitResponse

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;
}
Also used : UnitResponse(info.xiancloud.core.message.UnitResponse) NotifyHandler(info.xiancloud.core.NotifyHandler) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 57 with 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();
    }
}
Also used : AbstractAction(info.xiancloud.dao.jdbc.sql.AbstractAction) Action(info.xiancloud.dao.jdbc.sql.Action) UnitResponse(info.xiancloud.core.message.UnitResponse) ArrayList(java.util.ArrayList)

Example 58 with UnitResponse

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;
    });
}
Also used : UnitResponse(info.xiancloud.core.message.UnitResponse) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 59 with UnitResponse

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;
    });
}
Also used : UnitResponse(info.xiancloud.core.message.UnitResponse) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 60 with UnitResponse

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);
}
Also used : UnitRequest(info.xiancloud.core.message.UnitRequest) UnitResponse(info.xiancloud.core.message.UnitResponse) JSONArray(com.alibaba.fastjson.JSONArray) NotifyHandler(info.xiancloud.core.NotifyHandler)

Aggregations

UnitResponse (info.xiancloud.core.message.UnitResponse)106 JSONObject (com.alibaba.fastjson.JSONObject)74 HashMap (java.util.HashMap)22 UnitRequest (info.xiancloud.core.message.UnitRequest)17 NotifyHandler (info.xiancloud.core.NotifyHandler)16 JSONArray (com.alibaba.fastjson.JSONArray)9 Unit (info.xiancloud.core.Unit)6 UnitMeta (info.xiancloud.core.UnitMeta)6 CacheGroup (info.xiancloud.cache.service.CacheGroup)5 Group (info.xiancloud.core.Group)5 Input (info.xiancloud.core.Input)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Redis (info.xiancloud.cache.redis.Redis)4 CacheConfigBean (info.xiancloud.core.support.cache.CacheConfigBean)4 ThreadPoolManager (info.xiancloud.core.thread_pool.ThreadPoolManager)3 LOG (info.xiancloud.core.util.LOG)3 Set (java.util.Set)3 Test (org.junit.Test)3 MessageType (info.xiancloud.core.distribution.MessageType)2 UnitUndefinedException (info.xiancloud.core.distribution.exception.UnitUndefinedException)2