pull/991/merge
WuLang 2 years ago committed by GitHub
commit 4d26404b15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -113,4 +113,6 @@ public class Constants {
public static final int HTTP_EXECUTE_TIMEOUT = 5000;
public static final String CLIENT_VERSION = "Client-Version";
public static final String HIPPO4J_LOCK = "hippo4j_lock";
}

@ -230,6 +230,11 @@ CREATE TABLE `his_config_verify` (
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET=utf8mb4 COMMENT = '参数变更审核记录表';
DROP TABLE IF EXISTS `hippo4j_lock`;
CREATE TABLE `hippo4j_lock` (
`lock_name` varchar(50) NOT NULL COMMENT '锁名称',
PRIMARY KEY (`lock_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/* Init SQL */
INSERT IGNORE INTO `tenant` (`id`, `tenant_id`, `tenant_name`, `tenant_desc`, `owner`, `gmt_create`, `gmt_modified`, `del_flag`) VALUES ('1', 'prescription', '处方组', '负责维护处方服务, 包括不限于电子处方等业务', '谢良辰', '2021-10-24 13:42:11', '2021-10-24 13:42:11', '0');
@ -241,3 +246,5 @@ INSERT IGNORE INTO `config` (`id`, `tenant_id`, `item_id`, `tp_id`, `tp_name`, `
INSERT IGNORE INTO `user` (`id`, `user_name`, `password`, `role`, `gmt_create`, `gmt_modified`, `del_flag`) VALUES ('1', 'admin', '$2a$10$2KCqRbra0Yn2TwvkZxtfLuWuUP5KyCWsljO/ci5pLD27pqR3TV1vy', 'ROLE_ADMIN', '2021-11-04 21:35:17', '2021-11-15 23:04:59', '0');
INSERT IGNORE INTO `notify` (`id`, `tenant_id`, `item_id`, `tp_id`, `platform`, `type`, `secret_key`, `interval`, `receives`, `enable`, `gmt_create`, `gmt_modified`, `del_flag`) VALUES ('1', 'prescription', 'dynamic-threadpool-example', 'message-produce', 'DING', 'CONFIG', '4a582a588a161d6e3a1bd1de7eea9ee9f562cdfcbe56b6e72029e7fd512b2eae', NULL, '15601166691', '0', '2021-11-18 22:49:50', '2021-11-18 22:49:50', '0'), ('2', 'prescription', 'dynamic-threadpool-example', 'message-produce', 'DING', 'ALARM', '4a582a588a161d6e3a1bd1de7eea9ee9f562cdfcbe56b6e72029e7fd512b2eae', '30', '15601166691', '0', '2021-11-18 22:50:06', '2021-11-18 22:50:06', '0');
INSERT IGNORE INTO `hippo4j_lock` VALUES ('hippo4j_lock');

@ -21,6 +21,10 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;
import javax.sql.DataSource;
/**
* Database configuration.
@ -41,4 +45,15 @@ public class DataBaseConfiguration {
dataSourceProperties.setInitEnable(initEnable);
return dataSourceProperties;
}
@Bean
@ConditionalOnMissingBean(value = TransactionTemplate.class)
public TransactionTemplate transactionTemplate(DataSource dataSource) {
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(dataSource);
TransactionTemplate transactionTemplate = new TransactionTemplate();
transactionTemplate.setTransactionManager(dataSourceTransactionManager);
transactionTemplate.setPropagationBehaviorName("PROPAGATION_REQUIRED");
return transactionTemplate;
}
}

@ -0,0 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.hippo4j.config.mapper;
import cn.hippo4j.config.model.LockInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface LockMapper extends BaseMapper<LockInfo> {
@Select("select * from hippo4j_lock where lock_name = #{lockName} for update")
String tryLock(@Param("lockName") String lockName);
}

@ -0,0 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.hippo4j.config.model;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("hippo4j_lock")
public class LockInfo {
private String lockName;
}

@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.hippo4j.config.service.biz;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* Lock Service
*/
public interface LockService {
<T> void tryLocK(String lockName, T t, Consumer<T> consumer);
<T, R> R tryLocK(String lockName, T t, Function<T, R> function);
<T, R> R tryLocK(String lockName, T t, Function<T, R> function, Supplier<R> errorResult);
}

@ -0,0 +1,84 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.hippo4j.config.service.biz.impl;
import cn.hippo4j.config.mapper.LockMapper;
import cn.hippo4j.config.service.biz.LockService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* db lock
*/
@Service
@AllArgsConstructor
public class DbLockServiceImpl implements LockService {
private TransactionTemplate transactionTemplate;
private final LockMapper lockMapper;
@Override
public <T> void tryLocK(String lockName, T t, Consumer<T> consumer) {
transactionTemplate.execute(status -> {
try {
lockMapper.tryLock(lockName);
consumer.accept(t);
} catch (Exception e) {
status.setRollbackOnly();
throw new RuntimeException(e);
}
return null;
});
}
@Override
public <T, R> R tryLocK(String lockName, T t, Function<T, R> function) {
return transactionTemplate.execute(status -> {
R r = null;
try {
lockMapper.tryLock(lockName);
r = function.apply(t);
} catch (Exception e) {
status.setRollbackOnly();
throw new RuntimeException(e);
}
return r;
});
}
@Override
public <T, R> R tryLocK(String lockName, T t, Function<T, R> function, Supplier<R> errorResult) {
return transactionTemplate.execute(status -> {
R r;
try {
lockMapper.tryLock(lockName);
r = function.apply(t);
} catch (Exception e) {
status.setRollbackOnly();
return errorResult.get();
}
return r;
});
}
}
Loading…
Cancel
Save