Commit d17d6fe2 authored by zhangyong's avatar zhangyong

1

parent 2ce1c995
...@@ -26,6 +26,10 @@ ...@@ -26,6 +26,10 @@
<artifactId>spring-boot-devtools</artifactId> <artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency> <dependency>
<groupId>mysql</groupId> <groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId> <artifactId>mysql-connector-java</artifactId>
......
...@@ -16,11 +16,11 @@ import com.egolm.common.jdbc.JdbcTemplate; ...@@ -16,11 +16,11 @@ import com.egolm.common.jdbc.JdbcTemplate;
import com.egolm.common.jdbc.dialect.MySqlDialect; import com.egolm.common.jdbc.dialect.MySqlDialect;
@Configuration @Configuration
public class ShopDataSourceConfig { public class DataSourceConfig {
@Bean @Bean
@Primary @Primary
@Qualifier("shopDataSource") @Qualifier("dataSource")
@ConfigurationProperties(prefix = "spring.datasource.shop") @ConfigurationProperties(prefix = "spring.datasource.shop")
public DataSource getDataSource() { public DataSource getDataSource() {
return DataSourceBuilder.create().type(DruidDataSource.class).build(); return DataSourceBuilder.create().type(DruidDataSource.class).build();
...@@ -28,15 +28,15 @@ public class ShopDataSourceConfig { ...@@ -28,15 +28,15 @@ public class ShopDataSourceConfig {
@Bean @Bean
@Primary @Primary
@Qualifier("shopTxManager") @Qualifier("txManager")
public PlatformTransactionManager getTransactionManager(@Qualifier("shopDataSource")DataSource dataSource) { public PlatformTransactionManager getTransactionManager(@Qualifier("dataSource")DataSource dataSource) {
return new DataSourceTransactionManager(dataSource); return new DataSourceTransactionManager(dataSource);
} }
@Bean @Bean
@Primary @Primary
@Qualifier("shopJdbcTemplate") @Qualifier("jdbcTemplate")
public JdbcTemplate getJdbcTemplate(@Qualifier("shopDataSource")DataSource dataSource) { public JdbcTemplate getJdbcTemplate(@Qualifier("dataSource")DataSource dataSource) {
JdbcTemplate jdbcTemplate = new JdbcTemplate(); JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource); jdbcTemplate.setDataSource(dataSource);
jdbcTemplate.setDialect(new MySqlDialect()); jdbcTemplate.setDialect(new MySqlDialect());
......
package com.egolm.jobs.config.datasource;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import com.alibaba.druid.pool.DruidDataSource;
import com.egolm.common.jdbc.JdbcTemplate;
import com.egolm.common.jdbc.dialect.MySqlDialect;
@Configuration
public class ScheduleDataSourceConfig {
@Bean
@Qualifier("shopDataSource")
@ConfigurationProperties(prefix = "spring.datasource.schedule")
public DataSource getDataSource() {
return DataSourceBuilder.create().type(DruidDataSource.class).build();
}
@Bean
@Qualifier("shopTxManager")
public PlatformTransactionManager getTransactionManager(@Qualifier("shopDataSource")DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean
@Qualifier("shopJdbcTemplate")
public JdbcTemplate getJdbcTemplate(@Qualifier("shopDataSource")DataSource dataSource) {
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
jdbcTemplate.setDialect(new MySqlDialect());
jdbcTemplate.setSql_level(3);
return jdbcTemplate;
}
}
\ No newline at end of file
...@@ -4,14 +4,18 @@ import org.quartz.JobExecutionContext; ...@@ -4,14 +4,18 @@ import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException; import org.quartz.JobExecutionException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean; import org.springframework.scheduling.quartz.QuartzJobBean;
import com.egolm.common.StringUtil; import com.egolm.common.StringUtil;
import com.egolm.jobs.service.JobService;
public class ScheduleJob extends QuartzJobBean { public class ScheduleJob extends QuartzJobBean {
private static final Logger logger = LoggerFactory.getLogger(ScheduleJob.class); private static final Logger logger = LoggerFactory.getLogger(ScheduleJob.class);
@Autowired
private JobService jobService;
/** /**
* 计划任务执行入口 * 计划任务执行入口
*/ */
...@@ -24,6 +28,9 @@ public class ScheduleJob extends QuartzJobBean { ...@@ -24,6 +28,9 @@ public class ScheduleJob extends QuartzJobBean {
if(data == null) { if(data == null) {
throw new XScheduleException("任务定义实体[data]不能为空"); throw new XScheduleException("任务定义实体[data]不能为空");
} }
//调用业务逻辑
jobService.executeJob(jobCommon);
} }
} }
package com.egolm.jobs.controller;
import java.util.Date;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.egolm.common.bean.Rjx;
import com.egolm.jobs.config.schedule.JobCommon;
import com.egolm.jobs.config.schedule.ScheduleUtils;
import com.egolm.jobs.service.JobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
@Api(tags={"定时任务相关接口"})
@RestController
@RequestMapping("job")
public class JobController {
@Autowired
private JobService jobService;
@Autowired
private Scheduler scheduler;
@ApiOperation("启动/停止")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", name = "id", dataType = "String", required = true, value = "任务ID", defaultValue = "") ,
@ApiImplicitParam(paramType = "query", name = "type", dataType = "String", required = true, value = "操作", defaultValue = "start/stop") ,
@ApiImplicitParam(paramType = "query", name = "userNO", dataType = "String", required = true, value = "用户", defaultValue = "")
})
@RequestMapping(value = "/execute",method=RequestMethod.GET)
public String execute(HttpServletRequest request) {
String id= request.getParameter("id");
String type = request.getParameter("type");
Map<String,Object> params = jobService.queryById(id);
if(params != null) {
if(type.equals("start")) {
JobCommon jobCommon=new JobCommon();
jobCommon.setsJobID(id);
jobCommon.setnRunFlg(0);
jobCommon.setsCronExpression(params.get("sCron")+"");
jobCommon.setData(params.get("sClass"));
ScheduleUtils.createScheduleJob(scheduler, jobCommon);
ScheduleUtils.resumeJob(scheduler, id);
}else if(type.equals("stop")) {
ScheduleUtils.deleteScheduleJob(scheduler, id);
}else {
return Rjx.jsonErr().setMessage("操作类型错误").toJson();
}
}else {
return Rjx.jsonErr().setMessage("任务不存在").toJson();
}
return Rjx.jsonOk().toString();
}
}
package com.egolm.jobs.service;
import java.util.List;
import java.util.Map;
import com.egolm.jobs.config.schedule.JobCommon;
public interface JobService {
public Map<String, Object> queryById(String id);
public List<Map<String, Object>> queryAll();
public void modify(Map<String,Object> params);
public void executeJob(JobCommon jobCommon) ;
}
package com.egolm.jobs.service.impl;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.egolm.common.jdbc.JdbcTemplate;
import com.egolm.jobs.config.schedule.JobCommon;
import com.egolm.jobs.service.JobService;
@Service
public class JobServiceImpl implements JobService {
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private RedisTemplate redisTemplate;
public Map<String,Object> queryById(String id){
try {
String sql = "SELECT * FROM tQuartz WHERE nID = ? AND nTag&1=0";
return jdbcTemplate.queryForMap(sql, id);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public List<Map<String,Object>> queryAll(){
try {
String sql = "SELECT * FROM tQuartz WHERE nTag&1=0";
return jdbcTemplate.queryForList(sql);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public void modify(Map<String, Object> params) {
String id = params.get("id")+"";
String status = params.get("status")+"";
String userNO = params.get("userNO")+"";
String sql = "UPDATE tQuartz SET nStatus = ? ,sChangeUser = ? ,dChangeDate = getdate() ,dLastUpdateTime = getdate() WHERE nID = ?";
jdbcTemplate.update(sql,status,userNO,id);
}
public void executeJob(JobCommon jobCommon) {
String data = jobCommon.getData()+"";
if(data.equals("redisInit")) {
String sql = "SELECT * FROM tSystemCtrl WHERE sCode IN ('Client_Md5Key') AND nTag&1=0 ";
List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
if(list != null && list.size() >0) {
for(Map<String,Object> map:list) {
if(map.get("sCode").equals("Client_Md5Key")) {
redisTemplate.opsForValue().set("Client_Md5Key", map.get("sValue1"));
}
}
}
}
}
}
...@@ -11,19 +11,10 @@ spring.http.encoding.force=true ...@@ -11,19 +11,10 @@ spring.http.encoding.force=true
spring.http.encoding.enabled=true spring.http.encoding.enabled=true
spring.http.encoding.charset=utf-8 spring.http.encoding.charset=utf-8
spring.datasource.schedule.url=jdbc:mysql://10.10.0.20:3306/msgcenter?useSSL=false
spring.datasource.schedule.username=root
spring.datasource.schedule.password=egolm2018
spring.datasource.schedule.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.schedule.testWhileIdle=true
spring.datasource.schedule.validationQuery=SELECT 1
spring.datasource.schedule.timeBetweenEvictionRunsMillis=3600000
spring.datasource.shop.username=ERP spring.datasource.shop.username=ERP
spring.datasource.shop.password=qiyang@2013 spring.datasource.shop.password=qiyang@2013
spring.datasource.shop.url=jdbc:sqlserver://10.10.0.22:1433;instanceName=SQLSERVER;DatabaseName=EGOLMDEV spring.datasource.shop.url=jdbc:sqlserver://10.10.0.22:1433;instanceName=SQLSERVER;DatabaseName=EGOLMDEV;SelectMethod=cursor;
spring.datasource.shop.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver spring.datasource.shop.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.shop.testWhileIdle=true spring.datasource.shop.testWhileIdle=true
spring.datasource.shop.validationQueryTimeout=5 spring.datasource.shop.validationQueryTimeout=5
......
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