Search in sources :

Example 1 with SaturnApi

use of com.vip.saturn.job.basic.SaturnApi in project Saturn by vipshop.

the class SaturnJavaJob method createJobBusinessInstanceIfNecessary.

private void createJobBusinessInstanceIfNecessary() {
    String jobClassStr = configService.getJobConfiguration().getJobClass();
    if (StringUtils.isBlank(jobClassStr)) {
        LogUtils.error(log, jobName, "jobClass is not set");
        throw new JobInitAlarmException("jobClass is not set");
    }
    jobClassStr = jobClassStr.trim();
    LogUtils.info(log, jobName, "start to create job business instance, jobClass is {}", jobClassStr);
    if (jobBusinessInstance == null) {
        ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
        ClassLoader jobClassLoader = saturnExecutorService.getJobClassLoader();
        Thread.currentThread().setContextClassLoader(jobClassLoader);
        try {
            Class<?> jobClass = jobClassLoader.loadClass(jobClassStr);
            jobBusinessInstance = tryToGetSaturnBusinessInstanceFromSaturnApplication(jobClassLoader, jobClass);
            if (jobBusinessInstance == null) {
                try {
                    jobBusinessInstance = jobClass.getMethod("getObject").invoke(null);
                    if (jobBusinessInstance != null) {
                        LogUtils.info(log, jobName, "get job instance from getObject");
                    }
                } catch (NoSuchMethodException e) {
                    LogUtils.info(log, jobName, "the jobClass hasn't the static getObject method, will initialize job by default no arguments constructor method");
                }
            }
            // 业务没有重写getObject方法,BaseSaturnJob会默认返回null
            if (jobBusinessInstance == null) {
                jobBusinessInstance = jobClass.newInstance();
                LogUtils.info(log, jobName, "get job instance from newInstance");
            }
            SaturnApi saturnApi = new SaturnApi(getNamespace(), executorName);
            jobClass.getMethod("setSaturnApi", Object.class).invoke(jobBusinessInstance, saturnApi);
        } catch (Throwable t) {
            throw new JobInitAlarmException(logBusinessExceptionIfNecessary(jobName, t));
        } finally {
            Thread.currentThread().setContextClassLoader(oldClassLoader);
        }
    }
    if (jobBusinessInstance == null) {
        LogUtils.error(log, jobName, "job instance is null");
        throw new JobInitAlarmException("job instance is null");
    }
}
Also used : JobInitAlarmException(com.vip.saturn.job.exception.JobInitAlarmException) SaturnApi(com.vip.saturn.job.basic.SaturnApi)

Aggregations

SaturnApi (com.vip.saturn.job.basic.SaturnApi)1 JobInitAlarmException (com.vip.saturn.job.exception.JobInitAlarmException)1