提交 b9c7210d 编写于 作者: M MaxKey

Jobs

上级 ecc9247b
......@@ -46,8 +46,8 @@ public class AbstractOnlineTicketService implements OnlineTicketService{
private static final String HISTORY_LOGOUT_UPDATE_STATEMENT =
"update mxk_history_login set logouttime = ? ,sessionstatus = 7 where sessionid = ?";
public List<HistoryLogin> query() {
@Override
public List<HistoryLogin> queryOnlineTicket() {
List<HistoryLogin> listOnlineTicket = jdbcTemplate.query(
DEFAULT_DEFAULT_SELECT_STATEMENT,
new OnlineTicketRowMapper());
......
......@@ -18,6 +18,9 @@
package org.maxkey.authn.online;
import java.time.LocalTime;
import java.util.List;
import org.maxkey.entity.HistoryLogin;
public interface OnlineTicketService {
......@@ -33,5 +36,7 @@ public interface OnlineTicketService {
public void setValiditySeconds(int validitySeconds);
public List<HistoryLogin> queryOnlineTicket();
public void terminate(String onlineTicket,String userId,String username);
}
......@@ -17,13 +17,12 @@
package org.maxkey;
import org.maxkey.authn.online.OnlineTicketService;
import org.maxkey.jobs.AccountsStrategyJob;
import org.maxkey.jobs.DynamicGroupsJob;
import org.maxkey.persistence.repository.LoginHistoryRepository;
import org.maxkey.persistence.repository.LoginRepository;
import org.maxkey.jobs.OnlineTicketListenerJob;
import org.maxkey.persistence.service.AccountsService;
import org.maxkey.persistence.service.GroupsService;
import org.opensaml.xml.ConfigurationException;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Job;
......@@ -45,58 +44,78 @@ import org.springframework.scheduling.quartz.SchedulerFactoryBean;
public class MaxKeyMgtJobs implements InitializingBean {
private static final Logger _logger = LoggerFactory.getLogger(MaxKeyMgtJobs.class);
/**
* schedulerJobsInit.
* @return schedulerJobsInit
* @throws ConfigurationException
* @throws SchedulerException
*/
@Bean(name = "schedulerJobs")
public String schedulerJobs(
@Bean(name = "schedulerTicketListenerJobs")
public String ticketListenerJob(
SchedulerFactoryBean schedulerFactoryBean,
OnlineTicketService onlineTicketService) throws SchedulerException {
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("service", onlineTicketService);
addJobScheduler(
OnlineTicketListenerJob.class,
schedulerFactoryBean,
jobDataMap,
"* 10 * * * ? ",//10 minutes
"TicketListener"
);
return "schedulerTicketListenerJobs";
}
@Bean(name = "schedulerDynamicGroupsJobs")
public String dynamicGroupsJobs(
SchedulerFactoryBean schedulerFactoryBean,
GroupsService groupsService,
AccountsService accountsService,
@Value("${maxkey.job.cron.schedule}") String cronSchedule
) throws SchedulerException {
_logger.debug("cron schedule : " + cronSchedule);
Scheduler scheduler = schedulerFactoryBean.getScheduler();
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("service", groupsService);
addDynamicGroupsJob(
DynamicGroupsJob.class,
scheduler,
groupsService,
cronSchedule,
"DynamicGroups"
);
addJobScheduler(
DynamicGroupsJob.class,
schedulerFactoryBean,
jobDataMap,
cronSchedule,
"DynamicGroups"
);
addAccountsStrategyJob(
AccountsStrategyJob.class,
scheduler,
accountsService,
cronSchedule,
"AccountsStrategy"
);
return "schedulerDynamicGroupsJobs";
}
@Bean(name = "schedulerAccountsStrategyJobs")
public String accountsStrategyJobs(
SchedulerFactoryBean schedulerFactoryBean,
AccountsService accountsService,
@Value("${maxkey.job.cron.schedule}") String cronSchedule
) throws SchedulerException {
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("service", accountsService);
addJobScheduler(
AccountsStrategyJob.class,
schedulerFactoryBean,
jobDataMap,
cronSchedule,
"AccountsStrategy"
);
return "schedulerJobs";
return "schedulerAccountsStrategyJobs";
}
private void addDynamicGroupsJob( Class <? extends Job> jobClass,
Scheduler scheduler ,
GroupsService groupsService,
private void addJobScheduler( Class <? extends Job> jobClass,
SchedulerFactoryBean schedulerFactoryBean ,
JobDataMap jobDataMap,
String cronSchedule,
String identity
) throws SchedulerException {
_logger.debug("Cron {} , Job schedule {} ", cronSchedule , identity );
Scheduler scheduler = schedulerFactoryBean.getScheduler();
_logger.debug("add DynamicGroups Job");
JobDetail jobDetail =
JobBuilder.newJob(jobClass)
.withIdentity(identity + "Job", identity + "Group")
.build();
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("service", groupsService);
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronSchedule);
CronTrigger cronTrigger =
......@@ -108,41 +127,6 @@ public class MaxKeyMgtJobs implements InitializingBean {
scheduler.scheduleJob(jobDetail,cronTrigger);
}
private void addAccountsStrategyJob(Class <? extends Job> jobClass,
Scheduler scheduler ,
AccountsService accountsService,
String cronSchedule,
String identity
) throws SchedulerException {
_logger.debug("add Accounts Strategy Job");
JobDetail jobDetail =
JobBuilder.newJob(jobClass)
.withIdentity(identity + "Job", identity + "Group")
.build();
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("service", accountsService);
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronSchedule);
CronTrigger cronTrigger =
TriggerBuilder.newTrigger()
.withIdentity("trigger" + identity, identity + "TriggerGroup")
.usingJobData(jobDataMap)
.withSchedule(scheduleBuilder)
.build();
scheduler.scheduleJob(jobDetail,cronTrigger);
}
public String SessionListenerJob(
SchedulerFactoryBean schedulerFactoryBean,
LoginRepository loginRepository,
LoginHistoryRepository loginHistoryRepository) {
return "sessionListenerJob";
}
@Override
public void afterPropertiesSet() throws Exception {
......
......@@ -36,7 +36,6 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
@Configuration
......@@ -59,9 +58,6 @@ public class MaxKeyMgtMvcConfig implements WebMvcConfigurer {
@Autowired
HistoryLogsAdapter historyLogsAdapter;
@Autowired
LocaleChangeInterceptor localeChangeInterceptor;
@Autowired
RestApiPermissionAdapter restApiPermissionAdapter;
......@@ -142,10 +138,6 @@ public class MaxKeyMgtMvcConfig implements WebMvcConfigurer {
;
_logger.debug("add HistoryLogsAdapter");
registry.addInterceptor(localeChangeInterceptor);
_logger.debug("add LocaleChangeInterceptor");
/*
* api
* idm
......
package org.maxkey.jobs;
import org.quartz.JobExecutionContext;
public class AbstractScheduleJob {
public final static class JOBSTATUS{
public static int STOP = 0;
public static int RUNNING = 1;
public static int ERROR = 2;
public static int FINISHED = 3;
}
protected int jobStatus = JOBSTATUS.STOP;
void init(JobExecutionContext context){};
}
......@@ -24,7 +24,7 @@ import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AccountsStrategyJob implements Job , Serializable {
public class AccountsStrategyJob extends AbstractScheduleJob implements Job , Serializable {
/**
*
......@@ -35,40 +35,33 @@ public class AccountsStrategyJob implements Job , Serializable {
private static AccountsService accountsService = null;
public static class JOBSTATUS{
public static int STOP = 0;
public static int RUNNING = 1;
public static int FINISHED = 2;
}
private static int jobStatus = JOBSTATUS.STOP;
@Override
public void execute(JobExecutionContext context){
if(jobStatus == JOBSTATUS.RUNNING) {
_logger.info("Accounts Strategy job is in running . " );
return;
}
if(jobStatus == JOBSTATUS.RUNNING) {return;}
_logger.debug("Accounts Strategy job is running ... " );
init(context);
_logger.debug("Accounts Strategy Job running ... " );
jobStatus = JOBSTATUS.RUNNING;
try {
if(accountsService == null) {
accountsService =
(AccountsService) context.getMergedJobDataMap().get("service");
}else {
if(accountsService != null) {
accountsService.refreshAllByStrategy();
//10 minutes
Thread.sleep(10 * 1000);
Thread.sleep(10 * 1000);//10 minutes
}
_logger.debug("Accounts Strategy job is success " );
_logger.debug("Accounts Strategy Job finished " );
jobStatus = JOBSTATUS.FINISHED;
}catch(Exception e) {
jobStatus = JOBSTATUS.ERROR;
_logger.error("Exception " ,e);
jobStatus = JOBSTATUS.STOP;
}
jobStatus = JOBSTATUS.FINISHED;
_logger.debug("Accounts Strategy job is finished . " );
}
@Override
void init(JobExecutionContext context){
if(accountsService == null) {
accountsService =
(AccountsService) context.getMergedJobDataMap().get("service");
}
}
}
......@@ -24,7 +24,7 @@ import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DynamicGroupsJob implements Job , Serializable {
public class DynamicGroupsJob extends AbstractScheduleJob implements Job , Serializable {
/**
*
*/
......@@ -33,40 +33,34 @@ public class DynamicGroupsJob implements Job , Serializable {
final static Logger _logger = LoggerFactory.getLogger(DynamicGroupsJob.class);
private static GroupsService groupsService = null;
public static class JOBSTATUS{
public static int STOP = 0;
public static int RUNNING = 1;
public static int FINISHED = 2;
}
private static int jobStatus = JOBSTATUS.STOP;
@Override
public void execute(JobExecutionContext context){
if(jobStatus == JOBSTATUS.RUNNING) {
_logger.info("DynamicGroupsJob is in running . " );
return;
}
if(jobStatus == JOBSTATUS.RUNNING) {return;}
_logger.debug("DynamicGroupsJob is running ... " );
init(context);
_logger.debug("DynamicGroups Job running ... " );
jobStatus = JOBSTATUS.RUNNING;
try {
if(groupsService == null) {
groupsService =
(GroupsService) context.getMergedJobDataMap().get("service");
}else {
if(groupsService != null) {
groupsService.refreshAllDynamicGroups();
Thread.sleep(10 *1000);
Thread.sleep(10 * 1000);//10 minutes
}
_logger.debug("DynamicGroupsJob is success " );
_logger.debug("DynamicGroups Job finished " );
jobStatus = JOBSTATUS.FINISHED;
}catch(Exception e) {
jobStatus = JOBSTATUS.ERROR;
_logger.error("Exception " ,e);
jobStatus = JOBSTATUS.STOP;
}
jobStatus = JOBSTATUS.FINISHED;
_logger.debug("DynamicGroupsJob is finished . " );
}
@Override
void init(JobExecutionContext context){
if(groupsService == null) {
groupsService =
(GroupsService) context.getMergedJobDataMap().get("service");
}
}
}
......@@ -17,6 +17,6 @@
package org.maxkey.jobs;
public class DynamicRolesJob {
public class DynamicRolesJob extends AbstractScheduleJob {
}
......@@ -17,21 +17,53 @@ package org.maxkey.jobs;
import java.io.Serializable;
import org.maxkey.authn.online.OnlineTicketService;
import org.maxkey.entity.HistoryLogin;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class OnlineTicketListenerJob implements Job , Serializable {
public class OnlineTicketListenerJob extends AbstractScheduleJob implements Job , Serializable {
final static Logger _logger = LoggerFactory.getLogger(OnlineTicketListenerJob.class);
private static final long serialVersionUID = 4782358765969474833L;
OnlineTicketService onlineTicketService;
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
// TODO Auto-generated method stub
if(jobStatus == JOBSTATUS.RUNNING) {return;}
init(context);
_logger.debug("OnlineTicketListener Job is running ... " );
jobStatus = JOBSTATUS.RUNNING;
try {
if(onlineTicketService != null) {
for (HistoryLogin onlineTicket : onlineTicketService.queryOnlineTicket()) {
if(onlineTicketService.get(onlineTicket.getSessionId()) == null) {
onlineTicketService.terminate(
onlineTicket.getSessionId(),
onlineTicket.getUserId(),
onlineTicket.getUsername());
}
}
}
_logger.debug("OnlineTicketListener Job finished " );
jobStatus = JOBSTATUS.FINISHED;
}catch(Exception e) {
jobStatus = JOBSTATUS.ERROR;
_logger.error("Exception " ,e);
}
}
@Override
void init(JobExecutionContext context){
if(onlineTicketService == null) {
onlineTicketService =
(OnlineTicketService) context.getMergedJobDataMap().get("service");
}
}
}
......@@ -25,8 +25,6 @@ import org.maxkey.authn.online.OnlineTicketService;
import org.maxkey.entity.HistoryLogin;
import org.maxkey.entity.Message;
import org.maxkey.entity.UserInfo;
import org.maxkey.persistence.repository.LoginHistoryRepository;
import org.maxkey.persistence.repository.LoginRepository;
import org.maxkey.persistence.service.HistoryLoginService;
import org.maxkey.util.DateUtils;
import org.maxkey.util.StringUtils;
......@@ -57,11 +55,6 @@ public class LoginSessionController {
@Autowired
HistoryLoginService historyLoginService;
@Autowired
LoginRepository loginRepository;
@Autowired
LoginHistoryRepository loginHistoryRepository;
@Autowired
OnlineTicketService onlineTicketService;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册