spring boot做定时任务管理模块。

spring boot做定时任务管理模块。
我做的定时器管理界面是下面这样的。
新增页面。

列表页面。

下面来说说做定时任务管理模块的步骤。
1.在pom.xml中配maven库。

org.quartz-scheduler quartz 2.2.1 1 2 3 4 5 6 2.在spring boot中的启动文件(XXApplication )中注入Environment(在使用spring boot 的时候,我们只需要注入Environment类,即可获取到所有的配置资源。)。 @SpringBootApplication(scanBasePackages="com.tky") @EnableTransactionManagement // 启注解事务管理 @Controller @RequestMapping(value="") public class WbybApplication {
@RequestMapping(value="/",method={RequestMethod.GET})
 public String index(){
  return "/login";
 }

 public static void main(String[] args) {
  SpringApplication springApplication = new SpringApplication(WbybApplication.class);
  springApplication.run(args);
	
 }

@Autowired
private Environment env;

//连接池
@Bean
public DataSource dataSource() {
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl(env.getProperty("spring.datasource.url"));
    dataSource.setUsername(env.getProperty("spring.datasource.username"));//用户名
    dataSource.setPassword(env.getProperty("spring.datasource.password"));//密码
    //生产环境
    //dataSource.setInitialSize(50);
    //dataSource.setMaxActive(240);
    
    //测试环境
    dataSource.setInitialSize(2);
    dataSource.setMaxActive(5);
    
    
    dataSource.setMinIdle(0);
    dataSource.setMaxWait(60000);
    dataSource.setValidationQuery("SELECT 1 FROM DUAL");
    dataSource.setTestOnBorrow(false);
    dataSource.setTestWhileIdle(true);
    dataSource.setPoolPreparedStatements(false);
    return dataSource;
}

@Bean
public StartupRunner startupRunner(){
    return new StartupRunner();
}	

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
3.在application.yml中加入 定时任务系统标志(最后的一行 dsrwbz: 1 就是加入的定时任务系统标志)。
#路内测试库
spring.datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://10.1.0.207:3306/sidian?useUnicode=true&characterEncoding=UTF-8&useSSL=true
username: root
password: 123456

#配置mongodb参数
spring:
data:
mongodb:
host: 10.1.0.207
port: 27017
database: sidian

spring.http.multipart.maxFileSize: 1000Mb
spring.http.multipart.maxRequestSize: 10000Mb

#定时任务系统标志
dsrwbz: 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
4.在ServletInitializer类的onApplicationEvent方法做一些处理,能服务器启动的时候就能执行 初始化定时任务的方法(this.scheduleJobService.initJobs();这个方法在后面会帖出来)。
/**
*

  • @author lwj
  • 发布在tomcat时需配置

*/
public class ServletInitializer extends SpringBootServletInitializer implements ApplicationListener {

@Autowired
private ScheduleJobService scheduleJobService;
@Autowired
private Environment env;

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
	return application.sources(WbybApplication.class);
}

@Override
public void onApplicationEvent(ApplicationEvent event) {
	if (event instanceof ContextClosedEvent || event instanceof ContextStoppedEvent) {

// System.out.println(event.getClass().getSimpleName() + " 事件已发生!");
} else if (event instanceof ContextRefreshedEvent) {
ContextRefreshedEvent evt = (ContextRefreshedEvent) event;
if(evt.getApplicationContext().getParent() == null){
//存一下上下文,反射执行方法要用
//SpringContextUtil.setApplicationContext(evt.getApplicationContext());
//SpringUtil.setApplicationContext(evt.getApplicationContext());
//需要执行的逻辑代码,当spring容器初始化完成后就会执行该方法。
//初始化定时任务,读取定时任务标志参数,1表示定时任务系统,0业务系统,业务系统不加载定时任务
if (“1”.equals(env.getProperty(“dsrwbz”))) {
this.scheduleJobService.initJobs();
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
5.声明定时任务工厂bean。
/**
* @ClassName: JavaConfig
* @Description: Java配置
* @author CMB
* @date 2017年5月25日 下午5:23:35
*
/
@Configuration
public class JavaConfig {
/
*
*
* @Title: schedulerFactoryBean
* @Description: 声明定时任务工厂bean
* @param @return 参数
* @return SchedulerFactoryBean 返回类型
* @throws
* @author CMB
* @date 2017年10月17日 下午5:10:48
/
@Bean
public SchedulerFactoryBean schedulerFactoryBean(){
return new SchedulerFactoryBean();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
6.写一个SpringUtils工具类获取spring中的bean实体。
/
*

  • 通类可以通过调用SpringUtils工具类获取spring中的bean实体
  • @author zhangtianlun

*/
@Component
public class SpringUtil implements ApplicationContextAware {

private static ApplicationContext applicationContext;

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    if(SpringUtil.applicationContext == null) {
        SpringUtil.applicationContext = applicationContext;
    }
    
    System.out.println("---------------------------------------------------------------------");

    System.out.println("---------------com.tky.common.util.spring.SpringUtil------------------------------------------------------");

    System.out.println("========ApplicationContext配置成功,在普通类可以通过调用SpringUtils.getAppContext()获取applicationContext对象,"
    		+ "applicationContext="+SpringUtil.applicationContext+"========");

    System.out.println("---------------------------------------------------------------------");
}

//获取applicationContext
public static ApplicationContext getApplicationContext() {
    return applicationContext;
}

//通过name获取 Bean.
public static Object getBean(String name){
    return getApplicationContext().getBean(name);
}

//通过class获取Bean.
public static <T> T getBean(Class<T> clazz){
    return getApplicationContext().getBean(clazz);
}

//通过name,以及Clazz返回指定的Bean
public static <T> T getBean(String name,Class<T> clazz){
    return getApplicationContext().getBean(name, clazz);
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
以上都做定时任务模块 的前期配置。
下面是后台方法(controller,service,dao)。
controller方法。
/**
* @Title: ScheduleJobController.java
* @Package com.tky.sgzz.controller.base
* @Description: 定时任务管理模块控制器
* @author MUJL
* @date 2018年11月25日 上午10:56:38
* @version V1.0
*/

package com.tky.xxjd.controller.scheduler;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tky.common.data.ResultBean;
import com.tky.common.data.ResultCode;
import com.tky.common.data.RowsWrapper;
import com.tky.common.util.JsonUtil;
import com.tky.core.dto.UserInfo;
import com.tky.xxjd.entity.pingmiantu.ImportXmlRecordData;
import com.tky.xxjd.entity.scheduler.ScheduleJob;
import com.tky.xxjd.service.scheduler.ScheduleJobService;

import io.swagger.annotations.ApiOperation;

/**
* @ClassName: ScheduleJobController
* @Description: 定时任务管理控制器
* @author MUJL
* @date 2018年11月25日 上午10:56:38
*
/
@Controller
@RequestMapping("/scheduleJobControl")
public class ScheduleJobController {
/
*
* 页面文件路径
/
private static final String SCHEDULE_VIEW_PATH = “/static/xxjd/schedule/”;
@Autowired
private ScheduleJobService scheduleJobService;
/
*
*
* @Title: toView
* @Description: 打开定时任务管理页面
* @param @param model
* @param @param request
* @param @param session
* @param @param id
* @param @return 参数
* @return String 返回类型
* @throws
* @author MUJL
* @date 2018年11月25日 下午2:14:15
*/
@RequestMapping("/toView")
public String toView (Model model,ServletRequest request, HttpSession session , Long id ) {
// 查询用户具有的操作权限
return SCHEDULE_VIEW_PATH + “schedulelist”;
}

/**
 * 
    * @Title: getScheduleJobList
    * @Description: 分页查询定时任务
    * @param @param model
    * @param @param request
    * @param @param session
    * @param @param pageNumber
    * @param @param pageSize
    * @param @param jobname
    * @param @param description
    * @param @param state
    * @param @return    参数
    * @return String    返回类型
    * @throws
	* @author MUJL
    * @date 2018年11月25日 下午2:41:53
 */
@RequestMapping("/getScheduleJobList")
@ResponseBody
public String getScheduleJobList (Model model,ServletRequest request, HttpSession session,
		@RequestParam(value = "page", defaultValue = "1") int pageNumber,
		@RequestParam(value = "rows", defaultValue = "10") int pageSize,
		@RequestParam(value = "jobname", defaultValue = "") String jobname,
		@RequestParam(value = "description", defaultValue = "") String description,
		@RequestParam(value = "state", defaultValue = "") String state) {
	
	ResultBean<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值