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

QuartzConfig

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