Search in sources :

Example 51 with UnitResponse

use of info.xiancloud.core.message.UnitResponse in project xian by happyyangyuan.

the class CacheObjectUtil method get.

public static <T> T get(CacheConfigBean cacheConfigBean, String cacheKey, Class<T> clazz) {
    UnitResponse unitResponseObject = SyncXian.call(CacheService.CACHE_SERVICE, "cacheObjectGet", new JSONObject() {

        {
            put("cacheConfig", cacheConfigBean);
            put("key", cacheKey);
        }
    });
    unitResponseObject.throwExceptionIfNotSuccess();
    return Reflection.toType(unitResponseObject.getData(), clazz);
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) UnitResponse(info.xiancloud.core.message.UnitResponse)

Example 52 with UnitResponse

use of info.xiancloud.core.message.UnitResponse in project xian by happyyangyuan.

the class CacheObjectUtil method type.

public static String type(CacheConfigBean cacheConfigBean, String cacheKey) {
    UnitResponse unitResponseObject = SyncXian.call(CacheService.CACHE_SERVICE, "cacheType", new JSONObject() {

        {
            put("cacheConfig", cacheConfigBean);
            put("key", cacheKey);
        }
    });
    unitResponseObject.throwExceptionIfNotSuccess();
    return (String) unitResponseObject.getData();
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) UnitResponse(info.xiancloud.core.message.UnitResponse)

Example 53 with UnitResponse

use of info.xiancloud.core.message.UnitResponse in project xian by happyyangyuan.

the class CacheSetUtil method adds.

public static long adds(CacheConfigBean cacheConfigBean, String key, Set members) {
    UnitResponse unitResponseObject = SyncXian.call(CacheService.CACHE_SERVICE, "cacheSetAdd", new JSONObject() {

        {
            put("cacheConfig", cacheConfigBean);
            put("key", key);
            put("members", members);
        }
    });
    unitResponseObject.throwExceptionIfNotSuccess();
    if (unitResponseObject.getData() == null)
        return 0L;
    return (long) unitResponseObject.getData();
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) UnitResponse(info.xiancloud.core.message.UnitResponse)

Example 54 with UnitResponse

use of info.xiancloud.core.message.UnitResponse in project xian by happyyangyuan.

the class DistributedLockTest method QPS.

@Test
public void QPS() {
    final CountDownLatch startCountDownLatch = new CountDownLatch(1);
    final int number = 1000;
    final CountDownLatch finishCountDownLatch = new CountDownLatch(number);
    final List<Long> consumeTimes = new CopyOnWriteArrayList<>();
    for (int i = 0; i < number; i++) {
        int _i = i;
        ThreadPoolManager.execute(() -> {
            try {
                startCountDownLatch.await();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            long startTime = System.nanoTime();
            // Redis Lock
            DistributedLockSynchronizer.call("QPS_" + _i, 3, () -> {
                return null;
            }, 3);
            // ZK Lock
            // try
            // {
            // Synchronizer.call("QPS_" + _i, () -> {
            // return null;
            // }, 3L);
            // }
            // catch (Exception e)
            // {
            // LOG.error(e);
            // }
            finishCountDownLatch.countDown();
            long endTime = System.nanoTime();
            consumeTimes.add(endTime - startTime);
        });
    }
    try {
        startCountDownLatch.countDown();
        finishCountDownLatch.await();
        long totalConsumeTime = 0;
        for (long consumeTime : consumeTimes) totalConsumeTime += consumeTime;
        long avgConsumeTime = totalConsumeTime / number;
        LongSummaryStatistics intSummaryStatistics = consumeTimes.stream().collect(Collectors.summarizingLong(value -> value));
        LOG.info(String.format("QPS, 加锁解锁, 任务数量: %s, 累计耗时: %s, %s, 平均耗时:%s, %s, %s", number, totalConsumeTime, totalConsumeTime / 1000000, avgConsumeTime, avgConsumeTime / 1000000, intSummaryStatistics));
    } catch (Exception e) {
        LOG.error(e);
    }
    UnitResponse unitResponseObject = Xian.call(CacheService.CACHE_SERVICE, "cacheKeys", new JSONObject() {

        {
            put("pattern", "LOCK_QPS_*");
        }
    });
    if (unitResponseObject.succeeded() && unitResponseObject.getData() != null) {
        Set<String> keys = unitResponseObject.getData();
        LOG.info(String.format("分布锁剩余数量: %s", keys.size()));
    }
    Xian.call("diyMonitor", "jedisLockMonitor", new JSONObject());
}
Also used : Xian(info.xiancloud.core.message.Xian) Set(java.util.Set) UnitResponse(info.xiancloud.core.message.UnitResponse) Test(org.junit.Test) Collectors(java.util.stream.Collectors) EnvUtil(info.xiancloud.core.util.EnvUtil) CacheService(info.xiancloud.core.support.cache.CacheService) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ThreadPoolManager(info.xiancloud.core.thread_pool.ThreadPoolManager) DistributedLockSynchronizer(info.xiancloud.core.support.cache.lock.DistributedLockSynchronizer) After(org.junit.After) JSONObject(com.alibaba.fastjson.JSONObject) XianConfig(info.xiancloud.core.conf.XianConfig) LOG(info.xiancloud.core.util.LOG) LongSummaryStatistics(java.util.LongSummaryStatistics) Before(org.junit.Before) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) LongSummaryStatistics(java.util.LongSummaryStatistics) JSONObject(com.alibaba.fastjson.JSONObject) UnitResponse(info.xiancloud.core.message.UnitResponse) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Example 55 with UnitResponse

use of info.xiancloud.core.message.UnitResponse in project xian by happyyangyuan.

the class IUnitAop method intercept.

/**
 * intercept
 */
default void intercept() {
    Collection<Unit> units = getUnitCollection();
    if (units == null || units.isEmpty()) {
        LOG.info("nothing to intercept at all.");
        return;
    }
    final IUnitAop thiz = this;
    for (final Unit originUnit : units) {
        LOG.debug(originUnit.getName() + "    代理对象:" + Proxy.isProxyClass(originUnit.getClass()));
        final Unit nakedUnit;
        if (Proxy.isProxyClass(originUnit.getClass())) {
            nakedUnit = (Unit) ProxyBuilder.getProxyBuilder(originUnit.hashCode()).getMostOriginalObject();
        } else {
            nakedUnit = originUnit;
        }
        Unit proxy = new ProxyBuilder<Unit>(originUnit, true) {

            @Override
            public Object before(Method method, Object[] args) throws UnitResponseReplacement {
                if ("execute".equals(method.getName())) {
                    if (!asyncBefore()) {
                        return thiz.before(nakedUnit, (UnitRequest) args[0]);
                    } else {
                        Runnable runnableForBefore = () -> {
                            try {
                                Object beforeReturnIgnored = thiz.before(nakedUnit, (UnitRequest) args[0]);
                                LOG.info("异步AOP的前置拦截方法before返回的内容被忽略:" + beforeReturnIgnored);
                            } catch (UnitResponseReplacement outputReplacement) {
                                throw new RuntimeException("异步aop不允许打断被拦截的方法!", outputReplacement);
                            }
                        };
                        if (trackMsgIdIfAsync()) {
                            ThreadPoolManager.execute(runnableForBefore, MsgIdHolder.get());
                        } else {
                            ThreadPoolManager.executeWithoutTrackingMsgId(runnableForBefore);
                        }
                        return "";
                    }
                }
                return "";
            }

            @Override
            public void after(Method method, Object[] args, Object unitReturn, Object beforeReturn) throws UnitResponseReplacement {
                if ("execute".equals(method.getName())) /*&& !beforeReturn.toString().equals(AOP_FILTER_PLAG)*/
                {
                    final UnitResponse finalMethodReturn;
                    if (unitReturn instanceof Throwable) {
                        // 有异常抛出,则需要回滚
                        finalMethodReturn = UnitResponse.exception((Throwable) unitReturn);
                    } else {
                        finalMethodReturn = (UnitResponse) unitReturn;
                    }
                    if (!asyncAfter()) {
                        // 同步
                        thiz.after(originUnit, (UnitRequest) args[0], finalMethodReturn, beforeReturn);
                    } else {
                        Runnable runnableAfter = () -> {
                            try {
                                thiz.after(originUnit, (UnitRequest) args[0], finalMethodReturn, beforeReturn);
                            } catch (UnitResponseReplacement outputReplacement) {
                                throw new RuntimeException("异步aop不允许执行此操作!", outputReplacement);
                            }
                        };
                        if (trackMsgIdIfAsync()) {
                            ThreadPoolManager.execute(runnableAfter, MsgIdHolder.get());
                        } else {
                            ThreadPoolManager.executeWithoutTrackingMsgId(runnableAfter);
                        }
                    }
                }
            }
        }.getProxy();
        LocalUnitsManager.replaceUnit(proxy);
    }
}
Also used : Method(java.lang.reflect.Method) Unit(info.xiancloud.core.Unit) UnitRequest(info.xiancloud.core.message.UnitRequest) UnitResponse(info.xiancloud.core.message.UnitResponse) JSONObject(com.alibaba.fastjson.JSONObject)

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