Pre Merge pull request !44 from 菜呱/master
commit
84a50dacc6
After Width: | Height: | Size: 38 KiB |
After Width: | Height: | Size: 184 KiB |
After Width: | Height: | Size: 103 KiB |
@ -0,0 +1,45 @@
|
||||
package com.xxl.job.core.handler;
|
||||
|
||||
import com.xxl.job.core.context.XxlJobHelper;
|
||||
import com.xxl.job.core.handler.IJobHandler;
|
||||
|
||||
/**
|
||||
* glue job handler
|
||||
*
|
||||
* @author caigua 2022-11-11 16:29:45
|
||||
*/
|
||||
public abstract class SQLJobHandler extends IJobHandler {
|
||||
|
||||
|
||||
private long glueUpdatetime;
|
||||
|
||||
// 具体sql实现
|
||||
private SQLJobHandler jobHandler;
|
||||
private String[] sqlString;
|
||||
|
||||
public SQLJobHandler(){
|
||||
}
|
||||
|
||||
public SQLJobHandler(SQLJobHandler jobHandler, long glueUpdatetime, String sqlSourcesString) {
|
||||
this.jobHandler = jobHandler;
|
||||
this.glueUpdatetime = glueUpdatetime;
|
||||
this.sqlString = sqlSourcesString.split("\n");
|
||||
|
||||
}
|
||||
public long getGlueUpdatetime() {
|
||||
return glueUpdatetime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() throws Exception {
|
||||
XxlJobHelper.log("----------- glue.version:"+ glueUpdatetime +" -----------");
|
||||
execSql(sqlString);
|
||||
}
|
||||
|
||||
/**
|
||||
* sql执行
|
||||
* @param sql
|
||||
*/
|
||||
public abstract void execSql(String sql[]);
|
||||
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.xxl.job.executor.core.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* glue job handler
|
||||
*
|
||||
* @author caigua 2022-11-11 16:29:45
|
||||
*/
|
||||
|
||||
@Configuration
|
||||
public class JdbcConfig {
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
|
||||
@Bean({"jdbcTemplate"})
|
||||
public JdbcTemplate jdbcTemplate() {
|
||||
return new JdbcTemplate(this.dataSource);
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
package com.xxl.job.executor.service.jobhandler;
|
||||
|
||||
import com.xxl.job.core.context.XxlJobHelper;
|
||||
import com.xxl.job.core.handler.SQLJobHandler;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* glue job handler
|
||||
*
|
||||
* @author caigua 2022-11-11 16:29:45
|
||||
*/
|
||||
|
||||
@Component
|
||||
public class MYSQLJobHandler extends SQLJobHandler {
|
||||
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
MYSQLJobHandler() {
|
||||
super();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void execSql(String sql[]) {
|
||||
|
||||
boolean contains_select = false;
|
||||
|
||||
for (String s : sql) {
|
||||
if (s.trim().toLowerCase().startsWith("select")) {
|
||||
contains_select = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (contains_select) {
|
||||
// 存在查询sql 一条一条执行
|
||||
for (String s : sql) {
|
||||
if (s.trim().toLowerCase().startsWith("select")) {
|
||||
|
||||
XxlJobHelper.log("exec sql : {} \n result: {}", s, jdbcTemplate.queryForList(s));
|
||||
|
||||
|
||||
} else {
|
||||
XxlJobHelper.log("exec sql : {} \n rows:{}", s, jdbcTemplate.update(s));
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
int rows[] = jdbcTemplate.batchUpdate(sql);
|
||||
XxlJobHelper.log("exec rows:{}", rows);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in new issue