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);
}
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();
}
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();
}
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());
}
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);
}
}
Aggregations