parent
3f6a051a0a
commit
51792ffe45
@ -0,0 +1,18 @@
|
||||
package com.xxl.job.admin.mapper;
|
||||
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* job lock
|
||||
*
|
||||
* @author xuxueli 2016-1-12 18:03:45
|
||||
*/
|
||||
@Mapper
|
||||
public interface XxlJobLockMapper {
|
||||
|
||||
/**
|
||||
* get schedule lock
|
||||
*/
|
||||
String scheduleLock();
|
||||
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.xxl.job.admin.mapper.XxlJobLockMapper">
|
||||
|
||||
<select id="scheduleLock" resultType="java.lang.String" >
|
||||
SELECT * FROM xxl_job_lock
|
||||
WHERE lock_name = 'schedule_lock'
|
||||
FOR UPDATE
|
||||
</select>
|
||||
|
||||
|
||||
</mapper>
|
||||
@ -0,0 +1,49 @@
|
||||
package com.xxl.job.admin.schedule;
|
||||
|
||||
import com.xxl.job.admin.scheduler.config.XxlJobAdminBootstrap;
|
||||
import com.xxl.tool.core.DateTool;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.DefaultTransactionDefinition;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@SpringBootTest
|
||||
public class JobScheduleTest {
|
||||
private static Logger logger = LoggerFactory.getLogger(JobScheduleTest.class);
|
||||
|
||||
@Test
|
||||
public void test() throws InterruptedException {
|
||||
|
||||
// thread
|
||||
for (int i = 0; i < 10; i++) {
|
||||
int finalI = i;
|
||||
new Thread(() -> {
|
||||
lockTest("threadName-" + finalI);
|
||||
}).start();
|
||||
}
|
||||
|
||||
TimeUnit.MINUTES.sleep(10);
|
||||
}
|
||||
|
||||
private void lockTest(String threadName) {
|
||||
|
||||
TransactionStatus transactionStatus = XxlJobAdminBootstrap.getInstance().getTransactionManager().getTransaction(new DefaultTransactionDefinition());
|
||||
try {
|
||||
String lockedRecord = XxlJobAdminBootstrap.getInstance().getXxlJobLockMapper().scheduleLock(); // for update
|
||||
|
||||
logger.info(threadName + " : start at " + DateTool.format(new Date(), "yyyy-MM-dd HH:mm:ss SSS") );
|
||||
TimeUnit.MILLISECONDS.sleep(500);
|
||||
logger.info(threadName + " : end at " + DateTool.format(new Date(), "yyyy-MM-dd HH:mm:ss SSS") );
|
||||
} catch (Throwable e) {
|
||||
logger.error("error: ", e);
|
||||
} finally {
|
||||
logger.info(threadName + " : commit at " + DateTool.format(new Date(), "yyyy-MM-dd HH:mm:ss SSS") );
|
||||
XxlJobAdminBootstrap.getInstance().getTransactionManager().commit(transactionStatus);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue