All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.es.plus.adapter.lock.EsLock Maven / Gradle / Ivy

There is a newer version: 0.3.941
Show newest version
package com.es.plus.adapter.lock;

import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptType;

import java.util.HashMap;
import java.util.Map;

import static com.es.plus.constant.EsConstant.*;

public class EsLock extends ELock {
    public EsLock(ELockClient esPlusLockClient, String key) {
        super(esPlusLockClient, key);
    }

    @Override
    public String lockIndexName() {
        return GLOBAL_LOCK;
    }

    @Override
    public boolean tryLock0() {
        Map data = new HashMap<>();
        data.put(GLOBAL_LOCK_EXPIRETIME, System.currentTimeMillis() + GLOBAL_LOCK_TIMEOUT * 1000);
        //自增
        Map params = new HashMap<>();
        //构建scipt语句
        params.put("currentTime", System.currentTimeMillis());
        params.put("expireTime", (System.currentTimeMillis() + GLOBAL_LOCK_TIMEOUT * 1000));
        String script = "if(ctx._source." + GLOBAL_LOCK_EXPIRETIME + " data = new HashMap<>();
        data.put(GLOBAL_LOCK_EXPIRETIME, System.currentTimeMillis() + GLOBAL_LOCK_TIMEOUT * 1000);
        data.put("value", value);
        //自增
        Map params = new HashMap<>();
        //构建scipt语句
        params.put("currentTime", System.currentTimeMillis());
        params.put("expireTime", (System.currentTimeMillis() + GLOBAL_LOCK_TIMEOUT * 1000));
        String script = "if(ctx._source." + GLOBAL_LOCK_EXPIRETIME + " params = new HashMap<>();
        //构建scipt语句
        params.put("param", "delete");
        //构建scipt语句
        String script = "ctx.op = params.param;";
        Script painless = new Script(ScriptType.INLINE, PAINLESS, script, params);
        UpdateResponse update = esPlusLockClient.updateByScript(lockIndexName(), key, painless);
    }
    
  
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy