Commit e3270179 authored by 李丛阳's avatar 李丛阳

QuartzConfig

parent d386e232
......@@ -12,6 +12,7 @@ import org.rcisoft.core.result.ResultCode;
import org.rcisoft.core.service.RcRedisService;
import org.rcisoft.core.service.SerializationUtils;
import org.rcisoft.core.util.FreemarkerUtil;
import org.rcisoft.core.util.TaskUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;
......@@ -117,17 +118,8 @@ public class BCodeLxcServiceImpl implements BCodeLxcService {
.withIdentity(global.getLxcPrefix() + lxc.getUserId())
.startAt(endTime).build();
// 触发器时间设定
Scheduler sched = schedulerFactoryBean.getScheduler();
/*触发器*/
try {
sched.scheduleJob(job, trigger);
// 启动
if (!sched.isShutdown()) {
sched.start();
}
}catch (Exception e){
log.error(e.getMessage());
}
TaskUtil.startSchedule(schedulerFactoryBean.getScheduler(),trigger,job);
/*5.端口放进去 */
rcRedisServiceImpl.setList("lxcKeys",
key);
......
......@@ -70,14 +70,16 @@ public class ClearLxcJob implements Job {
if(null != shutdown && shutdown.after(new Date()))
return;
/*到点了*/
containerPath = global.getBASE_UPLOAD_SERVER_LOCATION() + File.separator +
containerPath = global.getPHYSICAL_UPLOAD_SERVER_LOCATION() + File.separator +
global.getLxcDockerfilePath() + File.separator + lxc.getUserId() + File.separator;
/*关闭容器*/
/*2.docker-compose 起容器*/
CommandResult commandResult = null;
commandResult = lxcCommand.startOrDownLxc(containerPath,false);
/*关闭失败 返回 不删除key*/
if(!commandResult.isSuccess())
return;
}
/*3.移除容器*/
rcRedisServiceImpl.remove(key);
/*4.移除端口*/
......@@ -85,5 +87,4 @@ public class ClearLxcJob implements Job {
key);
rcRedisServiceImpl.removeList("lxcPorts",lxc.getContainerPort() + "");
}
}
}
package org.rcisoft.business.listener;
package org.rcisoft.business.component;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.rcisoft.business.bcode.task.ClearLxcJob;
import org.rcisoft.core.util.TaskUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.HashMap;
/**
* Created by lcy on 18/1/19.
* Created by lcy on 18/1/20.
*
*
*
* JobDetail job = JobBuilder.newJob(ClearLxcJob.class).
withIdentity(IDENTITY_LXC_GROUP, JOB_LXC_GROUP).
usingJobData(new JobDataMap(new HashMap())).build();
Trigger trigger = TriggerBuilder.newTrigger().forJob(job)
.usingJobData(new JobDataMap(new HashMap()))
.withIdentity(IDENTITY_LXC_GROUP)
.startNow().build();
// 触发器时间设定
Scheduler sched = schedulerFactoryBean.getScheduler();
try {
sched.scheduleJob(job, trigger);
// 启动
if (!sched.isShutdown()) {
sched.start();
}
}catch (Exception e){
log.error(e.getMessage());
}
* 容器所有bean都被defined 之后触发
*/
@Slf4j
public class RcListener implements ServletContextListener {
@Component
public class RcApplicationRunner implements ApplicationRunner {
/*任务组名*/
......@@ -48,16 +34,17 @@ public class RcListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0) {
}
/**
* 执行任务
* @param arg0
*/
@Override
public void contextInitialized(ServletContextEvent arg0) {
public void run(ApplicationArguments args) throws Exception {
JobDetail job = JobBuilder.newJob(ClearLxcJob.class).
withIdentity(IDENTITY_LXC_GROUP, JOB_LXC_GROUP).
usingJobData(new JobDataMap(new HashMap())).build();
Trigger trigger = TriggerBuilder.newTrigger().forJob(job)
.usingJobData(new JobDataMap(new HashMap()))
.withIdentity(IDENTITY_LXC_GROUP)
.startNow().build();
// 触发器时间设定
/*触发器*/
TaskUtil.startSchedule(schedulerFactoryBean.getScheduler(),trigger,job);
}
}
......@@ -27,7 +27,7 @@ public class ApplicationContextHolder implements ApplicationContextAware {
}
/**
* Get bean by class
* Get component by class
*
* @param clazz
* @param <T>
......@@ -38,7 +38,7 @@ public class ApplicationContextHolder implements ApplicationContextAware {
}
/**
* Get bean by class name
* Get component by class name
*
* @param name
* @param <T>
......
......@@ -4,13 +4,11 @@ import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.rcisoft.business.listener.RcListener;
import org.rcisoft.core.exception.ServiceException;
import org.rcisoft.core.result.Result;
import org.rcisoft.core.result.ResultCode;
import org.rcisoft.core.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
......@@ -241,14 +239,5 @@ public class MvcConfig extends WebMvcConfigurerAdapter {
}
/**
* 监听器
* @return
*/
@Bean
public ServletListenerRegistrationBean servletListenerRegistrationBean(){
ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean();
servletListenerRegistrationBean.setListener(new RcListener());
return servletListenerRegistrationBean;
}
}
package org.rcisoft.core.util;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import java.io.Serializable;
/**
* Created by lcy on 18/1/20.
*/
@Slf4j
public class TaskUtil implements Serializable{
private static final long serialVersionUID = -4513365435486468892L;
/**
* 开启 schedule
* @param sched
* @param trigger
* @param job
* @return
*/
public static boolean startSchedule(Scheduler sched,Trigger trigger,JobDetail job){
try {
sched.scheduleJob(job, trigger);
// 启动
if (!sched.isShutdown()) {
sched.start();
}
}catch (Exception e){
log.error(e.getMessage());
return false;
}
return true;
}
}
spring:
profiles:
active: prod
\ No newline at end of file
active: dev
\ No newline at end of file
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