Search in sources :

Example 11 with NotifyHandler

use of info.xiancloud.core.NotifyHandler 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)

Example 12 with NotifyHandler

use of info.xiancloud.core.NotifyHandler in project xian by happyyangyuan.

the class RpcClientUnitHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, JSONObject response) throws Exception {
    try {
        MsgIdHolder.set(response.getString("$msgId"));
        logRpcFly(response, ctx);
        String ssid = response.remove("$ssid").toString();
        NotifyHandler handler = LocalNodeManager.handleMap.getIfPresent(ssid);
        LocalNodeManager.handleMap.invalidate(ssid);
        if (handler == null) {
            LOG.error(String.format("ssid=%s的消息没有找到对应的notifyHandler!整个消息内容=%s,", ssid, response), new Throwable());
            return;
        }
        UnitResponse responseUnitResponse = UnitResponse.create(response);
        ThreadPoolManager.execute(() -> handler.callback(responseUnitResponse));
    /*try {
                不再需要单独检测线程池被销毁而使用独立线程了,threadPoolManager已支持
            } catch (RejectedExecutionException threadPoolAlreadyShutdown) {
                LOG.info("线程池已关闭,这里使用临时线程执行任务,针对停服务时线程池已关闭的情况。");
                new Thread(() -> handler.callback(responseUnitResponse)).start();
            }*/
    } catch (Throwable e) {
        LOG.error(e);
    } finally {
        MsgIdHolder.clear();
    }
}
Also used : UnitResponse(info.xiancloud.core.message.UnitResponse) NotifyHandler(info.xiancloud.core.NotifyHandler)

Example 13 with NotifyHandler

use of info.xiancloud.core.NotifyHandler in project xian by happyyangyuan.

the class CloudFile method save.

/**
 * @param path  文件相对路径
 * @param data  文件内容
 * @param async 是否异步执行,如果不清楚,请使用{@linkplain CloudFile#save(String, String)}
 */
public static void save(String path, String data, boolean async) {
    Map map = new HashMap<String, Object>() {

        {
            put("path", path);
            put("data", data);
        }
    };
    if (async) {
        Xian.call("cosService", "cosWrite", map, new NotifyHandler() {

            @Override
            protected void handle(UnitResponse unitResponse) {
                LOG.info(unitResponse);
            }
        });
    } else {
        SyncXian.call("cosService", "cosWrite", map);
    }
}
Also used : HashMap(java.util.HashMap) UnitResponse(info.xiancloud.core.message.UnitResponse) NotifyHandler(info.xiancloud.core.NotifyHandler) Map(java.util.Map) HashMap(java.util.HashMap)

Example 14 with NotifyHandler

use of info.xiancloud.core.NotifyHandler in project xian by happyyangyuan.

the class TransactionalCache method rollbackDistributedTrans.

public static void rollbackDistributedTrans() {
    JSONArray clientIds = JSON.parseObject(transMetas.get(MsgIdHolder.get())).getJSONArray("clientIds");
    if (clientIds != null && !clientIds.isEmpty()) {
        String clientId = clientIds.getString(0);
        UnitRequest request = UnitRequest.create(Constant.SYSTEM_DAO_GROUP_NAME, "rollbackAndCloseTogether");
        request.getContext().setDestinationNodeId(clientId);
        LocalNodeManager.send(request, new NotifyHandler() {

            protected void handle(UnitResponse unitResponse) {
                LOG.info(clientId + "的事务已经回滚");
            }
        });
    } else {
        LOG.warn("回滚事务时,缓存中的事务信息竟然是空的!", new RuntimeException());
    }
}
Also used : UnitRequest(info.xiancloud.core.message.UnitRequest) UnitResponse(info.xiancloud.core.message.UnitResponse) JSONArray(com.alibaba.fastjson.JSONArray) NotifyHandler(info.xiancloud.core.NotifyHandler)

Example 15 with NotifyHandler

use of info.xiancloud.core.NotifyHandler in project xian by happyyangyuan.

the class TransactionalCache method commitDistributedTrans.

public static void commitDistributedTrans() {
    decreaseCount();
    JSONArray clientIds = JSON.parseObject(transMetas.get(MsgIdHolder.get())).getJSONArray("clientIds");
    if (clientIds != null && !clientIds.isEmpty()) {
        String clientId = clientIds.getString(0);
        UnitRequest request = UnitRequest.create(Constant.SYSTEM_DAO_GROUP_NAME, "commitAndCloseTogether");
        request.getContext().setDestinationNodeId(clientId);
        LocalNodeManager.send(request, new NotifyHandler() {

            protected void handle(UnitResponse unitResponse) {
                LOG.info(clientId + "的事务已经提交");
            }
        });
    } else {
        LOG.warn("提交事务时,缓存中的事务信息竟然是空的!", new RuntimeException());
    }
}
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

NotifyHandler (info.xiancloud.core.NotifyHandler)16 UnitResponse (info.xiancloud.core.message.UnitResponse)16 UnitRequest (info.xiancloud.core.message.UnitRequest)8 JSONArray (com.alibaba.fastjson.JSONArray)4 HashMap (java.util.HashMap)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 JSONObject (com.alibaba.fastjson.JSONObject)2 MessageType (info.xiancloud.core.distribution.MessageType)2 UnitInstance (info.xiancloud.core.distribution.service_discovery.UnitInstance)2 DefaultLocalAsyncSender (info.xiancloud.core.message.sender.local.DefaultLocalAsyncSender)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 Constant (info.xiancloud.core.Constant)1 UnitMeta (info.xiancloud.core.UnitMeta)1 LocalNodeManager (info.xiancloud.core.distribution.LocalNodeManager)1 UnitOfflineException (info.xiancloud.core.distribution.exception.UnitOfflineException)1 UnitUndefinedException (info.xiancloud.core.distribution.exception.UnitUndefinedException)1 IdManager (info.xiancloud.core.message.IdManager)1 RoutedLocalAsyncSender (info.xiancloud.core.message.sender.local.RoutedLocalAsyncSender)1 ISequencer (info.xiancloud.core.sequence.ISequencer)1 Stream (info.xiancloud.core.stream.Stream)1