动态Component组件代理实现类/类方法invoke实现(暂时记录下)

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));
		}
	}
}

猜你喜欢

转载自blog.csdn.net/bj_chengrong/article/details/103053606