Listener optimize

This commit is contained in:
MaxKey
2022-04-27 10:03:35 +08:00
parent 773334ad47
commit 470d93d7cb
13 changed files with 233 additions and 194 deletions

View File

@@ -62,9 +62,8 @@ public class MaxKeyMgtApplication extends SpringBootServletInitializer {
_logger.info("Start MaxKeyMgtApplication ...");
ConfigurableApplicationContext applicationContext =
SpringApplication.run(MaxKeyMgtApplication.class, args);
InitializeContext initWebContext =
new InitializeContext(applicationContext);
SpringApplication.run(MaxKeyMgtApplication.class, args);
InitializeContext initWebContext = new InitializeContext(applicationContext);
try {
initWebContext.init(null);

View File

@@ -1,136 +0,0 @@
/*
* Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.maxkey;
import org.maxkey.authn.session.SessionManager;
import org.maxkey.jobs.AccountsStrategyJob;
import org.maxkey.jobs.DynamicGroupsJob;
import org.maxkey.jobs.SessionListenerAdapter;
import org.maxkey.persistence.service.AccountsService;
import org.maxkey.persistence.service.GroupsService;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
@Configuration
public class MaxKeyMgtJobs implements InitializingBean {
private static final Logger _logger = LoggerFactory.getLogger(MaxKeyMgtJobs.class);
@Bean(name = "schedulerSessionListenerAdapter")
public String sessionListenerAdapter(
SchedulerFactoryBean schedulerFactoryBean,
SessionManager sessionManager) throws SchedulerException {
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("service", sessionManager);
addJobScheduler(
SessionListenerAdapter.class,
schedulerFactoryBean,
jobDataMap,
"0 0/10 * * * ?",//10 minutes
"SessionListenerAdapter"
);
return "schedulerSessionListenerAdapter";
}
@Bean(name = "schedulerDynamicGroupsJobs")
public String dynamicGroupsJobs(
SchedulerFactoryBean schedulerFactoryBean,
GroupsService groupsService,
@Value("${maxkey.job.cron.schedule}") String cronSchedule
) throws SchedulerException {
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("service", groupsService);
addJobScheduler(
DynamicGroupsJob.class,
schedulerFactoryBean,
jobDataMap,
cronSchedule,
"DynamicGroups"
);
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 "schedulerAccountsStrategyJobs";
}
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();
JobDetail jobDetail =
JobBuilder.newJob(jobClass)
.withIdentity(identity + "Job", identity + "Group")
.build();
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronSchedule);
CronTrigger cronTrigger =
TriggerBuilder.newTrigger()
.withIdentity("trigger" + identity, identity + "TriggerGroup")
.usingJobData(jobDataMap)
.withSchedule(scheduleBuilder)
.build();
scheduler.scheduleJob(jobDetail,cronTrigger);
}
@Override
public void afterPropertiesSet() throws Exception {
}
}

View File

@@ -0,0 +1,96 @@
/*
* Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.maxkey;
import org.maxkey.authn.session.SessionManager;
import org.maxkey.listener.AccountsStrategyListenerAdapter;
import org.maxkey.listener.DynamicGroupsListenerAdapter;
import org.maxkey.listener.ListenerAdapter;
import org.maxkey.listener.ListenerParameter;
import org.maxkey.listener.SessionListenerAdapter;
import org.maxkey.persistence.service.AccountsService;
import org.maxkey.persistence.service.GroupsService;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MaxKeyMgtListenerConfig implements InitializingBean {
private static final Logger _logger = LoggerFactory.getLogger(MaxKeyMgtListenerConfig.class);
@Bean
public String sessionListenerAdapter(
Scheduler scheduler,
SessionManager sessionManager) throws SchedulerException {
ListenerAdapter.addListener(
SessionListenerAdapter.class,
scheduler,
new ListenerParameter().add("sessionManager",sessionManager).build(),
"0 0/10 * * * ?",//10 minutes
SessionListenerAdapter.class.getSimpleName()
);
_logger.debug("Session ListenerAdapter inited .");
return "sessionListenerAdapter";
}
@Bean
public String dynamicGroupsListenerAdapter(
Scheduler scheduler,
GroupsService groupsService,
@Value("${maxkey.job.cron.schedule}") String cronSchedule
) throws SchedulerException {
ListenerAdapter.addListener(
DynamicGroupsListenerAdapter.class,
scheduler,
new ListenerParameter().add("groupsService",groupsService).build(),
cronSchedule,
DynamicGroupsListenerAdapter.class.getSimpleName()
);
_logger.debug("DynamicGroups ListenerAdapter inited .");
return "dynamicGroupsListenerAdapter";
}
@Bean
public String accountsStrategyListenerAdapter(
Scheduler scheduler,
AccountsService accountsService,
@Value("${maxkey.job.cron.schedule}") String cronSchedule
) throws SchedulerException {
ListenerAdapter.addListener(
AccountsStrategyListenerAdapter.class,
scheduler,
new ListenerParameter().add("accountsService",accountsService).build(),
cronSchedule,
AccountsStrategyListenerAdapter.class.getSimpleName()
);
_logger.debug("AccountsStrategy ListenerAdapter inited .");
return "accountsStrategyListenerAdapter";
}
@Override
public void afterPropertiesSet() throws Exception {
}
}

View File

@@ -1,19 +0,0 @@
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){};
}

View File

@@ -15,23 +15,24 @@
*/
package org.maxkey.jobs;
package org.maxkey.listener;
import java.io.Serializable;
import org.maxkey.persistence.service.AccountsService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AccountsStrategyJob extends AbstractScheduleJob implements Job , Serializable {
public class AccountsStrategyListenerAdapter extends ListenerAdapter implements Job , Serializable {
/**
*
*/
private static final long serialVersionUID = 167999890940939820L;
final static Logger _logger = LoggerFactory.getLogger(AccountsStrategyJob.class);
final static Logger _logger = LoggerFactory.getLogger(AccountsStrategyListenerAdapter.class);
private static AccountsService accountsService = null;
@@ -41,14 +42,14 @@ public class AccountsStrategyJob extends AbstractScheduleJob implements Job , S
init(context);
_logger.debug("Accounts Strategy Job running ... " );
_logger.debug("running ... " );
jobStatus = JOBSTATUS.RUNNING;
try {
if(accountsService != null) {
accountsService.refreshAllByStrategy();
Thread.sleep(10 * 1000);//10 minutes
}
_logger.debug("Accounts Strategy Job finished " );
_logger.debug("finished " );
jobStatus = JOBSTATUS.FINISHED;
}catch(Exception e) {
jobStatus = JOBSTATUS.ERROR;
@@ -58,9 +59,9 @@ public class AccountsStrategyJob extends AbstractScheduleJob implements Job , S
@Override
void init(JobExecutionContext context){
super.init(context);
if(accountsService == null) {
accountsService =
(AccountsService) context.getMergedJobDataMap().get("service");
accountsService = getParameter("accountsService",AccountsService.class);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
* Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,23 +15,21 @@
*/
package org.maxkey.jobs;
package org.maxkey.listener;
import java.io.Serializable;
import org.maxkey.persistence.service.GroupsService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DynamicGroupsJob extends AbstractScheduleJob implements Job , Serializable {
/**
*
*/
public class DynamicGroupsListenerAdapter extends ListenerAdapter implements Job , Serializable {
final static Logger _logger = LoggerFactory.getLogger(DynamicGroupsListenerAdapter.class);
private static final long serialVersionUID = 8831626240807856084L;
final static Logger _logger = LoggerFactory.getLogger(DynamicGroupsJob.class);
private static GroupsService groupsService = null;
@Override
@@ -40,14 +38,14 @@ public class DynamicGroupsJob extends AbstractScheduleJob implements Job , Seri
init(context);
_logger.debug("DynamicGroups Job running ... " );
_logger.debug("running ... " );
jobStatus = JOBSTATUS.RUNNING;
try {
if(groupsService != null) {
groupsService.refreshAllDynamicGroups();
Thread.sleep(10 * 1000);//10 minutes
}
_logger.debug("DynamicGroups Job finished " );
_logger.debug("finished " );
jobStatus = JOBSTATUS.FINISHED;
}catch(Exception e) {
jobStatus = JOBSTATUS.ERROR;
@@ -57,9 +55,9 @@ public class DynamicGroupsJob extends AbstractScheduleJob implements Job , Seri
@Override
void init(JobExecutionContext context){
super.init(context);
if(groupsService == null) {
groupsService =
(GroupsService) context.getMergedJobDataMap().get("service");
groupsService = getParameter("groupsService",GroupsService.class);
}
}

View File

@@ -15,8 +15,8 @@
*/
package org.maxkey.jobs;
package org.maxkey.listener;
public class DynamicRolesJob extends AbstractScheduleJob {
public class DynamicRolesListenerAdapter extends ListenerAdapter {
}

View File

@@ -0,0 +1,80 @@
/*
* Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.maxkey.listener;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ListenerAdapter {
private static final Logger _logger = LoggerFactory.getLogger(ListenerAdapter.class);
JobExecutionContext context;
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){
this.context = context;
};
@SuppressWarnings("unchecked")
public <T> T getParameter(String name, Class<T> requiredType) {
return (T) context.getMergedJobDataMap().get(name);
};
public static void addListener(
Class <? extends Job> jobClass,
Scheduler scheduler ,
JobDataMap jobDataMap,
String cronSchedule,
String identity
) throws SchedulerException {
_logger.debug("Cron {} , Job schedule {} ", cronSchedule , identity );
JobDetail jobDetail =
JobBuilder.newJob(jobClass)
.withIdentity(identity, identity + "Group")
.build();
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronSchedule);
CronTrigger cronTrigger =
TriggerBuilder.newTrigger()
.withIdentity("trigger" + identity, identity + "TriggerGroup")
.usingJobData(jobDataMap)
.withSchedule(scheduleBuilder)
.build();
scheduler.scheduleJob(jobDetail,cronTrigger);
}
}

View File

@@ -0,0 +1,20 @@
package org.maxkey.listener;
import org.quartz.JobDataMap;
public class ListenerParameter {
JobDataMap parameters ;
public ListenerParameter() {
parameters = new JobDataMap();
}
public ListenerParameter add(String key , Object value) {
parameters.put(key, value);
return this;
}
public JobDataMap build() {
return this.parameters;
}
}

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.maxkey.jobs;
package org.maxkey.listener;
import java.io.Serializable;
@@ -25,7 +25,7 @@ import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SessionListenerAdapter extends AbstractScheduleJob implements Job , Serializable {
public class SessionListenerAdapter extends ListenerAdapter implements Job , Serializable {
final static Logger _logger = LoggerFactory.getLogger(SessionListenerAdapter.class);
private static final long serialVersionUID = 4782358765969474833L;
@@ -37,7 +37,7 @@ public class SessionListenerAdapter extends AbstractScheduleJob implements Job
if(jobStatus == JOBSTATUS.RUNNING) {return;}
init(context);
_logger.debug("SessionListener Job is running ... " );
_logger.debug("running ... " );
jobStatus = JOBSTATUS.RUNNING;
try {
if(sessionManager != null) {
@@ -50,7 +50,7 @@ public class SessionListenerAdapter extends AbstractScheduleJob implements Job
}
}
}
_logger.debug("SessionListener Job finished " );
_logger.debug("finished " );
jobStatus = JOBSTATUS.FINISHED;
}catch(Exception e) {
jobStatus = JOBSTATUS.ERROR;
@@ -61,9 +61,9 @@ public class SessionListenerAdapter extends AbstractScheduleJob implements Job
@Override
void init(JobExecutionContext context){
super.init(context);
if(sessionManager == null) {
sessionManager =
(SessionManager) context.getMergedJobDataMap().get("service");
sessionManager = getParameter("sessionManager",SessionManager.class);
}
}
}

View File

@@ -10,5 +10,5 @@ org.maxkey.synchronizer.autoconfigure.SynchronizerAutoConfiguration,\
org.maxkey.autoconfigure.SwaggerConfig,\
org.maxkey.Oauth20ClientAutoConfiguration,\
org.maxkey.MaxKeyMgtConfig,\
org.maxkey.MaxKeyMgtJobs,\
org.maxkey.MaxKeyMgtMvcConfig
org.maxkey.MaxKeyMgtMvcConfig,\
org.maxkey.MaxKeyMgtListenerConfig