Search in sources :

Example 81 with UnitResponse

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

the class CacheListUtil method addAll.

public static boolean addAll(CacheConfigBean cacheConfigBean, String cacheKey, List list) {
    UnitResponse unitResponseObject = SyncXian.call(CacheService.CACHE_SERVICE, "cacheListAddAll", new JSONObject() {

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

Example 82 with UnitResponse

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

the class CacheListUtil method getRange.

/**
 * 指定获取范围 (不推荐使用)
 */
@Deprecated
public static List getRange(CacheConfigBean cacheConfigBean, String cacheKey, int startIndex, int endIndex) {
    UnitResponse unitResponseObject = SyncXian.call(CacheService.CACHE_SERVICE, "cacheListRange", new JSONObject() {

        {
            put("cacheConfig", cacheConfigBean);
            put("key", cacheKey);
            put("startIndex", startIndex);
            put("endIndex", endIndex);
        }
    });
    unitResponseObject.throwExceptionIfNotSuccess();
    if (unitResponseObject.getData() != null)
        return unitResponseObject.getData();
    else
        return null;
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) UnitResponse(info.xiancloud.core.message.UnitResponse)

Example 83 with UnitResponse

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

the class CacheListUtil method delete.

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

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

Example 84 with UnitResponse

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

the class CacheMapUtil method containsKey.

public static boolean containsKey(CacheConfigBean cacheConfigBean, String key, String field) {
    UnitResponse unitResponseObject = SyncXian.call(CacheService.CACHE_SERVICE, "cacheMapExists", new JSONObject() {

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

Example 85 with UnitResponse

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

the class DistributedLock method lock.

/**
 * @param cacheConfigBean    cacheConfigBean
 * @param key                key
 * @param value              vlaue
 * @param expireTimeInSecond 单位: 秒, KEY 过期时间
 * @param timeOutInSecond    单位: 秒, 获取锁超时时间
 * @return DistributedLock
 * @throws TimeOutException
 */
public static DistributedLock lock(CacheConfigBean cacheConfigBean, String key, Object value, int expireTimeInSecond, int timeOutInSecond) throws TimeOutException, RuntimeException {
    final long applyTime = System.currentTimeMillis();
    final int _expireTimeInSecond = expireTimeInSecond < 1 ? 3 : expireTimeInSecond;
    final int _timeOutInSecond = timeOutInSecond < 1 ? 3 : timeOutInSecond;
    if (expireTimeInSecond != _expireTimeInSecond)
        LOG.warn(String.format("key: %s, 原 expireTime: %s < 1, 校正为现 expireTime: %s", key, expireTimeInSecond, _expireTimeInSecond));
    if (timeOutInSecond != _timeOutInSecond)
        LOG.warn(String.format("key: %s, 原 timeOutInSecond: %s < 1, 校正为现 timeOutInSecond: %s", key, timeOutInSecond, _timeOutInSecond));
    final String lockKey = "LOCK_" + key;
    UnitResponse unitResponseObject = SyncXian.call("cache", "distributedLock", new JSONObject() {

        {
            put("cacheConfig", cacheConfigBean);
            put("key", lockKey);
            put("value", value);
            put("expireTimeInSecond", _expireTimeInSecond);
            put("timeOutInSecond", _timeOutInSecond);
        }
    });
    final long receiveTime = System.currentTimeMillis();
    if (unitResponseObject.succeeded()) {
        int autoIncrement = AUTO_INCREMENT.incrementAndGet();
        if (!EnvUtil.getEnv().equals(EnvUtil.PRODUCTION))
            LOG.info(String.format("锁编号: %s, key: %s, lockKey: %s, value: %s, 分布式加锁, 成功, 耗时: %s 毫秒", autoIncrement, key, lockKey, value, (receiveTime - applyTime)));
        return new DistributedLock(autoIncrement, key, lockKey, value);
    } else if (unitResponseObject.getCode().equals(Group.CODE_TIME_OUT))
        throw new TimeOutException(String.format("分布式加锁, 超时, key: %s, lockKey: %s, 耗时: %s 毫秒", key, lockKey, (receiveTime - applyTime)));
    else
        throw new RuntimeException(String.format("分布式加锁, 异常, key: %s, lockKey: %s, 耗时: %s 毫秒", key, lockKey, (receiveTime - applyTime)));
}
Also used : TimeOutException(info.xiancloud.core.support.cache.exception.TimeOutException) JSONObject(com.alibaba.fastjson.JSONObject) UnitResponse(info.xiancloud.core.message.UnitResponse)

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