package com.basis.datasource.service.impl;
import com.basis.datasource.UserDataSource;
import com.basis.datasource.service.WisRemoteJobService;
import com.basis.datasource.vo.RemoteJobVO;
import com.core.context.WebContextHolder;
import com.core.utils.scheduler.SchedulerHelper;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@Service
public class WisRemoteJobServiceImpl implements WisRemoteJobService {
protected static final Log logger = LogFactory.getLog(SchedulerHelper.class);
/**
* 調用遠程方法
*
*/
@Override
public void callJob(RemoteJobVO vo) {
try {
String customKey = vo.getCustomKey();
if (StringUtils.isNotBlank(customKey)) {
//切換數據源
UserDataSource.getSingleton().toggleDataSource(customKey);
Object bean = WebContextHolder.getWebAppContext().getBean(vo.getObjectType());
Method method = bean.getClass().getMethod(vo.getMethodName());
method.invoke(bean);
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
logger.error("遠程定時任務調度失敗:" + e.fillInStackTrace());
}
}
}
/* */ package com.core.context;
/* */
/* */ import com.core.authentication.UserDetailsImpl;
/* */ import com.core.framework.entity.Role;
/* */ import com.core.framework.model.IUser;
/* */ import java.util.Locale;
/* */ import org.springframework.beans.BeansException;
/* */ import org.springframework.context.ApplicationContext;
/* */ import org.springframework.context.ApplicationContextAware;
/* */ import org.springframework.context.annotation.Lazy;
/* */ import org.springframework.security.core.Authentication;
/* */ import org.springframework.security.core.context.SecurityContext;
/* */ import org.springframework.security.core.context.SecurityContextHolder;
/* */ import org.springframework.security.core.userdetails.UserDetails;
/* */ import org.springframework.stereotype.Component;
/* */
/* */
/* */
/* */ @Component
/* */ @Lazy(false)
/* */ public class WebContextHolder
/* */ implements WebContext, ApplicationContextAware
/* */ {
/* */ private static final long serialVersionUID = -264165980167726126L;
/* 25 */ private static ApplicationContext context = null;
/* */
/* */
/* 28 */ public static IUser getCurrentUser() { return ((UserDetailsImpl)getUserDetails()).getUser(); }
/* */
/* */
/* */
/* 32 */ public static UserDetailsImpl getUserDetailsImpl() { return (UserDetailsImpl)getAuthentication().getPrincipal(); }
/* */
/* */
/* */
/* 36 */ public static UserDetails getUserDetails() { return (UserDetails)getAuthentication().getPrincipal(); }
/* */
/* */
/* */
/* 40 */ public static Authentication getAuthentication() { return getContext().getAuthentication(); }
/* */
/* */
/* */
/* 44 */ public static SecurityContext getContext() { return SecurityContextHolder.getContext(); }
/* */
/* */
/* */
/* 48 */ public static ApplicationContext getWebAppContext() { return context; }
/* */
/* */
/* */ public static boolean hasRole(String roleCode) {
/* 52 */ IUser user = getCurrentUser();
/* 53 */ for (Role role : user.getRoles()) {
/* 54 */ if (role.getCode().equals(roleCode)) {
/* 55 */ return true;
/* */ }
/* */ }
/* */
/* 59 */ return false;
/* */ }
/* */
/* */ public static boolean hasRole(IUser user, String roleCode) {
/* 63 */ if (user == null || roleCode == null)
/* 64 */ return false;
/* 65 */ for (Role role : user.getRoles()) {
/* 66 */ if (role.getCode().equals(roleCode)) {
/* 67 */ return true;
/* */ }
/* */ }
/* 70 */ return false;
/* */ }
/* */
/* */ public static boolean hasRoles(String... roleCodes) {
/* 74 */ if (roleCodes == null)
/* 75 */ return false;
/* 76 */ for (String code : roleCodes) {
/* 77 */ if (!hasRole(code))
/* 78 */ return false;
/* */ }
/* 80 */ return true;
/* */ }
/* */
/* */ public static boolean containRole(String... roleCodes) {
/* 84 */ if (roleCodes == null)
/* 85 */ return false;
/* 86 */ for (String code : roleCodes) {
/* 87 */ if (hasRole(code))
/* 88 */ return true;
/* */ }
/* 90 */ return false;
/* */ }
/* */
/* */
/* */
/* */
/* */
/* */ @Deprecated
/* 98 */ public static String getMessage(Locale locale, String key, Object... args) { return getWebAppContext().getMessage(key, args, locale); }
/* */
/* */
/* */
/* */
/* 103 */ public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { context = applicationContext; }
/* */ }
/* Location: C:\jar\wis-core-4.0.0-20190902.072545-31.jar!\com\wis\core\context\WebContextHolder.class
* Java compiler version: 7 (51.0)
* JD-Core Version: 1.0.7
*/
package com.bgy.core.quartz.dynamic;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.bgy.core.cxf.context.ApplicationContextProvider;
import com.bgy.core.quartz.utils.QuartzStringUtils;
/**
* @Date 2019/05/26
* @Version 1.0
* @Last Modified By : CHENEY
* @Last Modified Time : 2019/05/26
* @Description : [:@DisallowConcurrentExecution : 此標記用在實現Job的類上面,意思是不允許併發執行.
* :注意org.quartz.threadPool.threadCount線程池中線程的數量至少要多個,否則@DisallowConcurrentExecution不生效
* :假如Job的設置時間間隔爲3秒,但Job執行時間是5秒,設置@DisallowConcurrentExecution以後程序會等任務執行完畢以後再去執行,否則會在3秒時再啓用新的線程執行]
* @Type Core JOB/Quartz核心調度類
* @Copyright All rights reserved.2019 WIS Software Co.*
*/
@DisallowConcurrentExecution
@Component
public class DynamicJob implements Job {
private Logger logger = LoggerFactory.getLogger(DynamicJob.class);
/**
* 核心方法,Quartz Job真正的執行邏輯.
* @param executorContext executorContext JobExecutionContext中封裝有Quartz運行所需要的所有信息
* @throws JobExecutionException execute()方法只允許拋出JobExecutionException異常
*/
@Override
public void execute(JobExecutionContext executorContext) throws JobExecutionException {
//注意:JobDetail中的JobDataMap是共用的,從getMergedJobDataMap獲取的JobDataMap是全新的對象
JobDataMap map = executorContext.getMergedJobDataMap();
String JOB_CLASS = map.getString("JOB_CLASS");
String JOB_COMPONENT = map.getString("JOB_COMPONENT");
String JOB_METHOD = map.getString("JOB_METHOD");
String JOB_NAME = map.getString("JOB_NAME");
String JOB_PARAMETER = map.getString("JOB_PARAMETER");
logger.info("Running Job name : {} ", JOB_NAME);
logger.info("Running Job description : " + map.getString("JOB_DESCRIPTION"));
logger.info("Running Job group: {} ", map.getString("JOB_TYPE"));
logger.info("Running Job cron : " + map.getString("CRON_EXPRESSION"));
logger.info("Running Job class : {} ", JOB_CLASS);
logger.info("Running Job component : {} ", JOB_COMPONENT);
logger.info("Running Job method : {} ", JOB_METHOD);
long startTime = System.currentTimeMillis();
try {
if (!QuartzStringUtils.getStringUtil.isEmpty(JOB_CLASS)) {
Object bean = ApplicationContextProvider.getBean(JOB_COMPONENT);
//無參方法構造
//Method method = bean.getClass().getMethod(JOB_METHOD);
//有參方法構造
Method method = bean.getClass().getDeclaredMethod(JOB_METHOD,String.class);
method.invoke(bean,JOB_PARAMETER);
}
long endTime = System.currentTimeMillis();
logger.info(">>>>>>>>>>>>> Running Job has been completed , cost time : " + (endTime - startTime) + "ms\n");
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
logger.error("定時任務調度失敗(請檢查任務編號["+JOB_NAME+"]的參數信息是否正確):" + ExceptionUtils.getStackTrace(e));
}
}
}