Search in sources :

Example 1 with NotifyHandler

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

the class App method main.

public static void main(String[] args) {
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("subDec", " * some third access \r\n\r\n * appId=e8249b116f5efa7c67ab1e7c79d8ba02cbe2451a,appSecret=另行邮件通知 \r\n\r\n  除获取token接口外,其他接口必须传入header:xian-accessToken=<yourToken>\r\n");
    params.put("docName", "接口文档");
    // params.put("path", "test");
    // "[{'apiBuildService':['apiDocUnit']}]"
    params.put("unitFilter", "apiBuildService.apiDocUnit");
    Xian.call("apiBuildService", "apiDocUnit", params, new NotifyHandler() {

        @Override
        protected void handle(UnitResponse unitResponse) {
            System.out.println(unitResponse.toString());
        }
    });
}
Also used : HashMap(java.util.HashMap) UnitResponse(info.xiancloud.core.message.UnitResponse) NotifyHandler(info.xiancloud.core.NotifyHandler)

Example 2 with NotifyHandler

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

the class AppTransaction method rollbackAndCloseTogether.

public void rollbackAndCloseTogether() 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(RollbackAndCloseTogetherUnit.class);
                request.getContext().setDestinationNodeId(clientId);
                LocalNodeManager.send(request, new NotifyHandler() {

                    protected void handle(UnitResponse unitResponse) {
                        LOG.info(clientId + "的事务已经回滚");
                    }
                });
            }
        }
    }
    // 回滚和关闭自己的数据库事务
    connection.rollback();
    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 3 with NotifyHandler

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

the class Recorder method handleRecordItem.

private void handleRecordItem(Unit unit, UnitRequest request, UnitResponse response, Object beforeReturn) {
    String msgId = MsgIdHolder.get();
    Map<String, Object> map = new HashMap<>();
    map.put("msgId", String.valueOf(msgId.hashCode()));
    map.put("group", unit.getGroup().getName());
    map.put("unit", unit.getName());
    map.put("requestMap", request.argJson().toJSONString());
    map.put("responseCode", response.getCode());
    map.put("responseData", response.getData() != null ? response.getData().toString() : "");
    map.put("requestTime", beforeReturn.toString());
    map.put("responseTime", String.valueOf(System.currentTimeMillis()));
    map.put("cost", String.valueOf(System.currentTimeMillis() - Long.parseLong(beforeReturn.toString())));
    Xian.call("recorder", "actionItemRecord", map, new NotifyHandler() {

        protected void handle(UnitResponse unitResponse) {
            LOG.info(unitResponse.toJSONString());
        }
    });
}
Also used : HashMap(java.util.HashMap) UnitResponse(info.xiancloud.core.message.UnitResponse) NotifyHandler(info.xiancloud.core.NotifyHandler)

Example 4 with NotifyHandler

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

the class NonblockingMqttCallBack method messageArrived.

@Override
public void messageArrived(String topicIgnored, MqttMessage message) throws Exception {
    try {
        /*第一个参数如果包含"."会被eclipse paho转为"/",所以,不要依赖这个参数!*/
        String msgFullStr = message.toString();
        JSONObject json = JSONObject.parseObject(msgFullStr);
        IdManager.makeSureMsgId(json);
        MessageType messageType = MessageType.getMessageType(json);
        switch(messageType) {
            case offline:
                LOG.info(String.format("离线广播: %s", json));
                break;
            case request:
                UnitRequest request = Reflection.toType(json, UnitRequest.class);
                logMqttFly(request, msgFullStr);
                String group = request.getContext().getGroup(), unit = request.getContext().getUnit();
                request.getContext().setFromRemote(true);
                ISequencer.build(group, unit, json).sequence(// run this runnable if succeeded.
                () -> new DefaultLocalAsyncSender(request, new NotifyHandler() {

                    protected void handle(UnitResponse unitResponse) {
                        LocalNodeManager.sendBack(unitResponse);
                    }
                }).send(), /*failed directly, call this handler*/
                new NotifyHandler() {

                    protected void handle(UnitResponse unitResponse) {
                        LocalNodeManager.sendBack(unitResponse);
                    }
                });
                break;
            case response:
                LOG.debug("ssid只在远程请求和响应时才会有值");
                UnitResponse response = Reflection.toType(json, UnitResponse.class);
                logMqttFly(response, msgFullStr);
                String ssid = response.getContext().getSsid();
                NotifyHandler handler = LocalNodeManager.handleMap.getIfPresent(ssid);
                LocalNodeManager.handleMap.invalidate(ssid);
                UnitResponse responseUnitResponse = UnitResponse.create(json);
                if (handler == null) {
                    LOG.error(String.format("ssid=%s的消息没有找到对应的notifyHandler!整个消息内容=%s,", ssid, json), new Throwable());
                    break;
                }
                try {
                    ThreadPoolManager.execute(() -> {
                        handler.callback(responseUnitResponse);
                    });
                } catch (RejectedExecutionException threadPoolAlreadyShutdown) {
                    LOG.info("线程池已关闭,这里使用临时线程执行任务,针对停服务时线程池已关闭的情况。");
                    new Thread(() -> handler.callback(responseUnitResponse)).start();
                }
                break;
            default:
                LOG.error("未知的mqtt消息类型:" + messageType, new RuntimeException());
        }
    } catch (Throwable e) {
        LOG.error(e);
    } finally {
        MsgIdHolder.clear();
    }
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) UnitRequest(info.xiancloud.core.message.UnitRequest) UnitResponse(info.xiancloud.core.message.UnitResponse) NotifyHandler(info.xiancloud.core.NotifyHandler) DefaultLocalAsyncSender(info.xiancloud.core.message.sender.local.DefaultLocalAsyncSender) MessageType(info.xiancloud.core.distribution.MessageType) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 5 with NotifyHandler

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

the class StreamRpcClientHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, JSONObject msg) throws Exception {
    LOG.warn("已关闭此功能");
    if (MessageType.isStream(msg)) {
        try {
            StreamFragmentBean streamFragmentBean = msg.toJavaObject(StreamFragmentBean.class);
            MsgIdHolder.set(streamFragmentBean.getHeader().getMsgId());
            String ssid = streamFragmentBean.getHeader().getId();
            NotifyHandler handler = LocalNodeManager.handleMap.getIfPresent(ssid);
            LocalNodeManager.handleMap.invalidate(ssid);
            // 以下写出不会阻塞
            Stream stream = StreamManager.singleton.add(streamFragmentBean);
            if (streamFragmentBean.getHeader().isFirst()) {
                UnitResponse responseUnitResponse = UnitResponse.success(stream);
                try {
                    ThreadPoolManager.execute(() -> handler.callback(responseUnitResponse));
                } catch (RejectedExecutionException threadPoolAlreadyShutdown) {
                    LOG.info("线程池已关闭,这里使用临时线程执行任务,针对停服务时线程池已关闭的情况。");
                    new Thread(() -> handler.callback(responseUnitResponse)).start();
                }
            }
        } catch (Throwable e) {
            LOG.error(e);
        } finally {
            MsgIdHolder.clear();
        }
    } else
        ctx.fireChannelRead(msg);
}
Also used : StreamFragmentBean(info.xiancloud.core.stream.StreamFragmentBean) UnitResponse(info.xiancloud.core.message.UnitResponse) NotifyHandler(info.xiancloud.core.NotifyHandler) Stream(info.xiancloud.core.stream.Stream) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

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