synchronizers separate

This commit is contained in:
MaxKey
2022-01-23 08:12:25 +08:00
parent 2444351ef7
commit 757c3861d9
43 changed files with 92 additions and 69 deletions

View File

@@ -0,0 +1,13 @@
description = "maxkey-synchronizer"
apply plugin: 'java'
dependencies {
//local jars
implementation fileTree(dir: '../maxkey-lib/*/', include: '*.jar')
implementation project(":maxkey-common")
implementation project(":maxkey-core")
implementation project(":maxkey-persistence")
}

View File

@@ -0,0 +1,146 @@
/*
* Copyright [2021] [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.synchronizer;
import java.util.HashMap;
import java.util.List;
import org.maxkey.entity.Organizations;
import org.maxkey.entity.Synchronizers;
import org.maxkey.persistence.service.HistorySynchronizerService;
import org.maxkey.persistence.service.OrganizationsService;
import org.maxkey.persistence.service.UserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
public abstract class AbstractSynchronizerService {
private static final Logger _logger =
LoggerFactory.getLogger(AbstractSynchronizerService.class);
@Autowired
protected OrganizationsService organizationsService;
@Autowired
protected UserInfoService userInfoService;
@Autowired
protected HistorySynchronizerService historySynchronizerService;
protected Synchronizers synchronizer;
protected HashMap<String,Organizations> orgsNamePathMap;
protected Organizations rootOrganization = null;
public void loadOrgsById(String orgId) {
List<Organizations> orgsList = organizationsService.query(null);
if(orgId== null || orgId.equals("")) {
orgId="1";
}
for(Organizations org : orgsList) {
if(org.getId().equals(orgId) && orgId.equals("1")) {
rootOrganization = org;
rootOrganization.setNamePath("/"+rootOrganization.getName());
rootOrganization.setCodePath("/1");
rootOrganization.setParentId("-1");
rootOrganization.setParentName("");
}else if(org.getId().equals(orgId)){
rootOrganization = org;
}
}
orgsNamePathMap =new HashMap<String,Organizations>();
orgsNamePathMap.put(rootOrganization.getNamePath(), rootOrganization);
push(orgsNamePathMap,orgsList,rootOrganization);
_logger.trace("orgsNamePathMap " + orgsNamePathMap);
}
public void push(HashMap<String,Organizations> orgsNamePathMap,
List<Organizations> orgsList,
Organizations parentOrg) {
for(Organizations org : orgsList) {
if(org.getParentId().equals(parentOrg.getId())) {
if(org.getNamePath() == null
|| !org.getNamePath().equals(parentOrg.getNamePath()+"/"+org.getName())) {
org.setParentName(parentOrg.getName());
org.setNamePath(parentOrg.getNamePath()+"/"+org.getName());
org.setCodePath(parentOrg.getCodePath()+"/"+org.getId());
organizationsService.update(org);
}
orgsNamePathMap.put(org.getNamePath(), org);
push(orgsNamePathMap,orgsList,org);
}
}
return;
}
public OrganizationsService getOrganizationsService() {
return organizationsService;
}
public void setOrganizationsService(OrganizationsService organizationsService) {
this.organizationsService = organizationsService;
}
public UserInfoService getUserInfoService() {
return userInfoService;
}
public void setUserInfoService(UserInfoService userInfoService) {
this.userInfoService = userInfoService;
}
public HashMap<String, Organizations> getOrgsNamePathMap() {
return orgsNamePathMap;
}
public void setOrgsNamePathMap(HashMap<String, Organizations> orgsNamePathMap) {
this.orgsNamePathMap = orgsNamePathMap;
}
public Organizations getRootOrganization() {
return rootOrganization;
}
public void setRootOrganization(Organizations rootOrganization) {
this.rootOrganization = rootOrganization;
}
public Synchronizers getSynchronizer() {
return synchronizer;
}
public void setSynchronizer(Synchronizers synchronizer) {
this.synchronizer = synchronizer;
}
public HistorySynchronizerService getHistorySynchronizerService() {
return historySynchronizerService;
}
public void setHistorySynchronizerService(HistorySynchronizerService historySynchronizerService) {
this.historySynchronizerService = historySynchronizerService;
}
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright [2021] [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.synchronizer;
import org.maxkey.entity.Synchronizers;
public interface ISynchronizerService {
public void sync() throws Exception ;
public void setSynchronizer(Synchronizers synchronizer);
}

View File

@@ -0,0 +1,86 @@
/*
* Copyright [2020] [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.synchronizer;
import java.util.HashMap;
import org.apache.mybatis.jpa.util.WebContext;
import org.maxkey.entity.Synchronizers;
import org.maxkey.persistence.service.SynchronizersService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SynchronizerJob implements Job {
final static Logger _logger = LoggerFactory.getLogger(SynchronizerJob.class);
SynchronizersService synchronizersService;
public static class JOBSTATUS{
public static int STOP = 0;
public static int RUNNING = 1;
public static int FINISHED = 2;
}
private static HashMap<String,Integer> jobStatus = new HashMap<String,Integer>();
@Override
public void execute(JobExecutionContext context){
Synchronizers synchronizer = readSynchronizer(context);
if(jobStatus.get(synchronizer.getId()) ==null ) {
//init
jobStatus.put(synchronizer.getId(), JOBSTATUS.STOP) ;
}else if(jobStatus.get(synchronizer.getId())== JOBSTATUS.RUNNING) {
_logger.info("SynchronizerJob is in running . " );
return;
}
_logger.debug("SynchronizerJob is running ... " );
jobStatus.put(synchronizer.getId(), JOBSTATUS.RUNNING) ;
try {
_logger.debug("synchronizer : " + synchronizer.getName()+"("+synchronizer.getId()+"_"+synchronizer.getSourceType()+")");
_logger.debug("synchronizer service : " + synchronizer.getService());
_logger.debug("synchronizer Scheduler : " + synchronizer.getScheduler());
ISynchronizerService service = (ISynchronizerService)WebContext.getBean(synchronizer.getService());
service.setSynchronizer(synchronizer);
service.sync();
jobStatus.put(synchronizer.getId(), JOBSTATUS.FINISHED);
_logger.debug("SynchronizerJob is success " );
}catch(Exception e) {
_logger.error("Exception " ,e);
jobStatus.put(synchronizer.getId(), JOBSTATUS.STOP);
}
_logger.debug("SynchronizerJob is finished . " );
}
public Synchronizers readSynchronizer(JobExecutionContext context) {
Synchronizers jobSynchronizer = (Synchronizers)context.getMergedJobDataMap().get("synchronizer");
if(synchronizersService == null) {
synchronizersService = (SynchronizersService)WebContext.getBean("synchronizersService");
}
//read synchronizer by id from database
Synchronizers synchronizer = synchronizersService.get(jobSynchronizer.getId());
_logger.trace("synchronizer " + synchronizer);
return synchronizer;
}
}

View File

@@ -0,0 +1,131 @@
/*
* Copyright [2021] [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.synchronizer.autoconfigure;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.maxkey.entity.Synchronizers;
import org.maxkey.synchronizer.SynchronizerJob;
import org.quartz.CronExpression;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
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.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
@Configuration
public class SynchronizerAutoConfiguration implements InitializingBean {
private static final Logger _logger =
LoggerFactory.getLogger(SynchronizerAutoConfiguration.class);
public static final String SYNCHRONIZERS_SELECT_STATEMENT = "select * from mxk_synchronizers where status ='1'";
@Bean(name = "schedulerSynchronizerJobs")
public String schedulerSynchronizerJobs(
JdbcTemplate jdbcTemplate,
SchedulerFactoryBean schedulerFactoryBean,
@Value("${maxkey.job.cron.enable}") boolean jobCronEnable
) throws SchedulerException {
Scheduler scheduler = schedulerFactoryBean.getScheduler();
if(jobCronEnable) {
List<Synchronizers> synchronizerList = querySynchronizers(jdbcTemplate);
for(Synchronizers synchronizer : synchronizerList) {
if(synchronizer.getScheduler()!=null
&& !synchronizer.getScheduler().equals("")
&& CronExpression.isValidExpression(synchronizer.getScheduler())) {
_logger.debug("synchronizer details : " + synchronizer);
buildJob(scheduler,synchronizer);
}
}
}
return "schedulerSynchronizerJobs";
}
private void buildJob(Scheduler scheduler ,
Synchronizers synchronizer) throws SchedulerException {
JobDetail jobDetail =
JobBuilder.newJob(SynchronizerJob.class)
.withIdentity(synchronizer.getService()+"Job", "SynchronizerGroups")
.build();
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("synchronizer", synchronizer);
_logger.debug("synchronizer : " + synchronizer.getName()+"("+synchronizer.getId()+"_"+synchronizer.getSourceType()+")");
_logger.debug("synchronizer service : " + synchronizer.getService());
_logger.debug("synchronizer Scheduler : " + synchronizer.getScheduler());
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(synchronizer.getScheduler());
CronTrigger cronTrigger =
TriggerBuilder.newTrigger()
.withIdentity("trigger"+synchronizer.getService(), "SynchronizerGroups")
.usingJobData(jobDataMap)
.withSchedule(scheduleBuilder)
.build();
scheduler.scheduleJob(jobDetail,cronTrigger);
}
public List<Synchronizers> querySynchronizers(JdbcTemplate jdbcTemplate) {
List<Synchronizers> synchronizerList = jdbcTemplate.query(SYNCHRONIZERS_SELECT_STATEMENT, new RowMapper<Synchronizers>() {
public Synchronizers mapRow(ResultSet rs, int rowNum) throws SQLException {
Synchronizers synchronizer = new Synchronizers();
synchronizer.setId( rs.getString("id"));
synchronizer.setName( rs.getString("name"));
synchronizer.setScheduler( rs.getString("scheduler"));
synchronizer.setSourceType( rs.getString("sourcetype"));
synchronizer.setProviderUrl(rs.getString("providerurl"));
synchronizer.setDriverClass(rs.getString("driverclass"));
synchronizer.setPrincipal( rs.getString("principal"));
synchronizer.setCredentials(rs.getString("credentials"));
synchronizer.setResumeTime( rs.getString("resumetime"));
synchronizer.setSuspendTime(rs.getString("suspendtime"));
synchronizer.setFilters( rs.getString("filters"));
synchronizer.setBasedn( rs.getString("basedn"));
synchronizer.setMsadDomain( rs.getString("msaddomain"));
synchronizer.setSslSwitch( rs.getString("sslswitch"));
synchronizer.setTrustStore( rs.getString("truststore"));
synchronizer.setStatus( rs.getString("status"));
synchronizer.setDescription(rs.getString("description"));
synchronizer.setSyncStartTime(rs.getInt("syncstarttime"));
synchronizer.setService(rs.getString("service"));
return synchronizer;
}
});
return synchronizerList;
}
@Override
public void afterPropertiesSet() throws Exception {
}
}

View File

@@ -0,0 +1,78 @@
/*
* Copyright [2021] [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.synchronizer.entity;
public class AccessToken {
int errcode;
String errmsg;
String access_token;
String expires_in;
public AccessToken() {
super();
}
public int getErrcode() {
return errcode;
}
public void setErrcode(int errcode) {
this.errcode = errcode;
}
public String getErrmsg() {
return errmsg;
}
public void setErrmsg(String errmsg) {
this.errmsg = errmsg;
}
public String getAccess_token() {
return access_token;
}
public void setAccess_token(String access_token) {
this.access_token = access_token;
}
public String getExpires_in() {
return expires_in;
}
public void setExpires_in(String expires_in) {
this.expires_in = expires_in;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("AccessToken [errcode=");
builder.append(errcode);
builder.append(", errmsg=");
builder.append(errmsg);
builder.append(", access_token=");
builder.append(access_token);
builder.append(", expires_in=");
builder.append(expires_in);
builder.append("]");
return builder.toString();
}
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright [2021] [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.synchronizer.entity;
public class ResponseData {
protected long errcode;
protected String errmsg;
public long getErrcode() {
return errcode;
}
public void setErrcode(long errcode) {
this.errcode = errcode;
}
public String getErrmsg() {
return errmsg;
}
public void setErrmsg(String errmsg) {
this.errmsg = errmsg;
}
public ResponseData() {
super();
}
}