|
|
@@ -6,14 +6,19 @@ import com.zswl.dataservice.domain.mqtt.OperationMessage;
|
|
|
import com.zswl.dataservice.helper.DBHelper;
|
|
|
import com.zswl.dataservice.model.artemis.OperationMessageSearchParam;
|
|
|
import com.zswl.dataservice.utils.CommonUtil;
|
|
|
+import com.zswl.dataservice.utils.TokenUtil;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
import org.springframework.data.domain.Sort;
|
|
|
+import org.springframework.data.mongodb.core.FindAndModifyOptions;
|
|
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
|
|
import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
import org.springframework.data.mongodb.core.query.Query;
|
|
|
+import org.springframework.data.mongodb.core.query.Update;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
@@ -27,6 +32,7 @@ import java.util.regex.Pattern;
|
|
|
*/
|
|
|
public class OperationMessageDaoImpl extends BaseImpl implements OperationMessageDaoExtend {
|
|
|
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(OperationMessageDaoImpl.class);
|
|
|
@Autowired
|
|
|
private MongoTemplate mongoTemplate;
|
|
|
|
|
|
@@ -75,4 +81,59 @@ public class OperationMessageDaoImpl extends BaseImpl implements OperationMessag
|
|
|
return dbHelper.pages(query, pageable, OperationMessage.class);
|
|
|
}
|
|
|
|
|
|
+ public OperationMessage init(String dataId, String token) {
|
|
|
+ OperationMessage doc = null;
|
|
|
+ try {
|
|
|
+ Query query = Query.query(Criteria.where("dataId").is(dataId).and("token").isNull());
|
|
|
+ Update update = new Update()
|
|
|
+ .set("token", token)
|
|
|
+ ;
|
|
|
+ FindAndModifyOptions options = new FindAndModifyOptions().upsert(true)
|
|
|
+ .returnNew(true);
|
|
|
+ doc = mongoTemplate.findAndModify(query, update, options,
|
|
|
+ OperationMessage.class);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("init {}", e.getMessage());
|
|
|
+ }
|
|
|
+ return doc;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String acquire(String dataId, Long expiration) {
|
|
|
+ Query query = Query.query(Criteria.where("dataId").is(dataId).and("token").isNull());
|
|
|
+ String token = TokenUtil.create();
|
|
|
+ Update update = new Update()
|
|
|
+ .set("expireAt", System.currentTimeMillis() + expiration)
|
|
|
+ .set("token", token);
|
|
|
+
|
|
|
+ FindAndModifyOptions options = new FindAndModifyOptions().upsert(false)
|
|
|
+ .returnNew(true);
|
|
|
+
|
|
|
+ OperationMessage doc = mongoTemplate.findAndModify(query, update, options,
|
|
|
+ OperationMessage.class);
|
|
|
+
|
|
|
+ if (doc == null) {
|
|
|
+ OperationMessage lockObj = mongoTemplate.findOne(Query.query(Criteria.where("dataId").is(dataId)), OperationMessage.class);
|
|
|
+ doc = lockObj;
|
|
|
+ }
|
|
|
+ boolean locked = doc != null && doc.getToken() != null && doc.getToken().equals(token);
|
|
|
+ // 如果已过期
|
|
|
+ if (!locked && doc != null && doc.getExpireAt() < System.currentTimeMillis()) {
|
|
|
+ release(dataId);
|
|
|
+ // 成功释放锁, 再次尝试获取锁
|
|
|
+ return this.acquire(dataId, expiration);
|
|
|
+ }
|
|
|
+ return locked ? token : null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean release(String dataId) {
|
|
|
+ Query releaseQuery = Query.query(Criteria.where("dataId").is(dataId));
|
|
|
+ Update releaseUpdate = new Update().set("expireAt", null).set("token", null);
|
|
|
+
|
|
|
+ FindAndModifyOptions releaseOptions = new FindAndModifyOptions().upsert(true)
|
|
|
+ .returnNew(true);
|
|
|
+ OperationMessage flowDisposition = mongoTemplate.findAndModify(releaseQuery, releaseUpdate, releaseOptions,
|
|
|
+ OperationMessage.class);
|
|
|
+ return StringUtils.isEmpty(flowDisposition.getToken());
|
|
|
+ }
|
|
|
+
|
|
|
}
|