Commit b126d2db by huangfusuper

线程创建帮助器修改

parent e445a98e
...@@ -2,9 +2,6 @@ package com.byit.factory; ...@@ -2,9 +2,6 @@ package com.byit.factory;
import com.byit.thread.BaseThreadRunHelper; import com.byit.thread.BaseThreadRunHelper;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.sql.Connection; import java.sql.Connection;
...@@ -29,11 +26,15 @@ public class DaemonScanThreadRunHelper { ...@@ -29,11 +26,15 @@ public class DaemonScanThreadRunHelper {
/** /**
* 初始睡眠时间 * 初始睡眠时间
*/ */
private static final Long INIT_SLEEP_DATE = 5000L; private static final Long INIT_SLEEP_DATE = 10000L;
/** /**
* 关闭等待时间 * 关闭等待时间
*/ */
private static final Long CLOSE_WAIT_TIME = 1000L; private static final Long CLOSE_WAIT_TIME = 1000L;
/**
* 循环间隔
*/
private static final Long CYCLE_INTERVAL = 1000L;
/** /**
* 线程运行必须原料 * 线程运行必须原料
...@@ -96,10 +97,9 @@ public class DaemonScanThreadRunHelper { ...@@ -96,10 +97,9 @@ public class DaemonScanThreadRunHelper {
* 线程构建 * 线程构建
* @param lockName 行锁名称 * @param lockName 行锁名称
* @param examplesValue 线程运行资源 * @param examplesValue 线程运行资源
* @return
*/ */
private static void buildThread(String lockName, BaseThreadRunHelper examplesValue){ private static void buildThread(String lockName, BaseThreadRunHelper examplesValue){
log.info("-----------开始构建扫描线程----------------"); log.info("-----------开始构建扫描线程,线程锁为{}----------------",lockName);
DataSource dataSource = examplesValue.getDataSource(); DataSource dataSource = examplesValue.getDataSource();
if(dataSource != null){ if(dataSource != null){
String threadClassName = examplesValue.getClass().getSimpleName(); String threadClassName = examplesValue.getClass().getSimpleName();
...@@ -109,8 +109,9 @@ public class DaemonScanThreadRunHelper { ...@@ -109,8 +109,9 @@ public class DaemonScanThreadRunHelper {
dateAligned(INIT_SLEEP_DATE,threadName); dateAligned(INIT_SLEEP_DATE,threadName);
log.info("---------------{}线程启动成功----------------",threadName); log.info("---------------{}线程启动成功----------------",threadName);
while (!THREAD_GROUP_STOP){ while (!THREAD_GROUP_STOP){
dateAligned(CYCLE_INTERVAL,threadName);
//定义睡眠变量 //定义睡眠变量
boolean isSleep = false; Long sleepTime = 0L;
Connection conn = null; Connection conn = null;
Boolean connAutoCommit = null; Boolean connAutoCommit = null;
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
...@@ -124,7 +125,7 @@ public class DaemonScanThreadRunHelper { ...@@ -124,7 +125,7 @@ public class DaemonScanThreadRunHelper {
preparedStatement = conn.prepareStatement("SELECT * FROM JOB_LOCK WHERE LOCK_NAME = '"+lockName+"' FOR UPDATE "); preparedStatement = conn.prepareStatement("SELECT * FROM JOB_LOCK WHERE LOCK_NAME = '"+lockName+"' FOR UPDATE ");
preparedStatement.execute(); preparedStatement.execute();
//调用业务操作 //调用业务操作
isSleep = examplesValue.start(); sleepTime = examplesValue.start();
}catch (Exception e){ }catch (Exception e){
if(!THREAD_GROUP_STOP){ if(!THREAD_GROUP_STOP){
e.printStackTrace(); e.printStackTrace();
...@@ -133,49 +134,47 @@ public class DaemonScanThreadRunHelper { ...@@ -133,49 +134,47 @@ public class DaemonScanThreadRunHelper {
}finally { }finally {
//提交行锁 //提交行锁
if (conn != null) { if (conn != null) {
try{ try {
conn.commit(); conn.commit();
}catch (Exception e){ } catch (Exception e) {
if(!THREAD_GROUP_STOP){ if (!THREAD_GROUP_STOP) {
log.error("--------------------【提交行锁出错】---------------------"); log.error("--------------------【提交行锁出错】---------------------");
} }
} }
} }
//恢复自动提交 //恢复自动提交
if (conn != null) { if (conn != null) {
try{ try {
conn.setAutoCommit(connAutoCommit); conn.setAutoCommit(connAutoCommit);
}catch (Exception e){ } catch (Exception e) {
if(!THREAD_GROUP_STOP){ if (!THREAD_GROUP_STOP) {
log.error("--------------------【恢复自动提交出错】---------------------"); log.error("--------------------【恢复自动提交出错】---------------------");
} }
} }
} }
//关闭数据库执行器 //关闭数据库执行器
if(preparedStatement != null){ if (preparedStatement != null) {
try { try {
preparedStatement.close(); preparedStatement.close();
} catch (SQLException e) { } catch (SQLException e) {
if(!THREAD_GROUP_STOP){ if (!THREAD_GROUP_STOP) {
log.error("--------------------【关闭执行器出错】---------------------"); log.error("--------------------【关闭执行器出错】---------------------");
} }
} }
} }
//关闭数据库连接 //关闭数据库连接
if(conn != null){ if (conn != null) {
try { try {
conn.close(); conn.close();
} catch (SQLException e) { } catch (SQLException e) {
if(!THREAD_GROUP_STOP){ if (!THREAD_GROUP_STOP) {
log.error("--------------------【关闭执行器出错】---------------------"); log.error("--------------------【关闭执行器出错】---------------------");
} }
} }
} }
} }
sleepTime = sleepTime==null?examplesValue.UNIVERSAL_WAIT_TIME:sleepTime;
if(isSleep){ dateAligned(sleepTime,threadName);
dateAligned(examplesValue.getSleepTime(),threadName);
}
} }
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment