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

com.es.plus.adapter.lock.EsWriteLock 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.*;

/**
 * @Author: hzh
 * @Date: 2022/9/13 10:01
 */
public class EsWriteLock extends ELock {
    protected EsWriteLock(ELockClient esPlusLockClient, String key) {
        super(esPlusLockClient, key);
    }

    @Override
    public boolean tryLock0() {
        Map data = new HashMap<>();
        data.put("lock_type", "x");
        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 lockScript = "if(ctx._source." + GLOBAL_LOCK_EXPIRETIME + " params = new HashMap<>();
        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);
    }

    @Override
    public String lockIndexName() {
        return "ep_read_write_lock";
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy