在JobDataMap中存储EJB之后,Quartz触发器处于ERROR状态

时间:2019-04-10 09:34:26

标签: java java-ee quartz-scheduler

我有一个启动单例EJB,而不是初始化调度程序并将EJB存储在JobDataMap中。当我存储EJB时,触发器进入ERROR状态。

@Singleton
@Startup
@Lock(LockType.READ)
public class NotificationScheduler {

    private Scheduler scheduler;

    @Inject
    private QuartzProperties quartzProperties;

    @Inject
    private NotificationMailProcess notificationMailProcess;

    @PostConstruct
    public void init() {

        try {
            Properties properties = new Properties();
            properties.put(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, "NotificationScheduler");
            properties.put(StdSchedulerFactory.PROP_SCHED_INSTANCE_ID, StdSchedulerFactory.AUTO_GENERATE_INSTANCE_ID);
            properties.put(StdSchedulerFactory.PROP_THREAD_POOL_CLASS, quartzProperties.getQuartzThreadPoolClass());
            properties.put(StdSchedulerFactory.PROP_THREAD_POOL_PREFIX + ".threadCount", quartzProperties.getQuartzThreadPoolCount());
            properties.put(StdSchedulerFactory.PROP_JOB_STORE_CLASS, quartzProperties.getQuartzJobStoreClass());
            properties.put(StdSchedulerFactory.PROP_JOB_STORE_PREFIX + ".driverDelegateClass",
                    quartzProperties.getQuartzJobStoreDriverDelegateClass());
            properties.put(StdSchedulerFactory.PROP_JOB_STORE_PREFIX + ".dataSource", quartzProperties.getQuartzJobStoreDataSource());
            properties.put("org.quartz.dataSource." + quartzProperties.getQuartzJobStoreDataSource() + ".jndiURL",
                    quartzProperties.getQuartzJobStoreDataSource());
            properties.put(StdSchedulerFactory.PROP_JOB_STORE_PREFIX + ".nonManagedTXDataSource",
                    quartzProperties.getQuartzJobStoreNonManagedTXDataSource());
            properties.put("org.quartz.dataSource." + quartzProperties.getQuartzJobStoreNonManagedTXDataSource() + ".jndiURL",
                    quartzProperties.getQuartzJobStoreNonManagedTXDataSource());
            properties.put(StdSchedulerFactory.PROP_JOB_STORE_PREFIX + ".isClustered", quartzProperties.getQuartzJobStoreIsClustered());
            properties.put(StdSchedulerFactory.PROP_JOB_STORE_PREFIX + ".clusterCheckinInterval",
                    quartzProperties.getQuartzJobStoreClusterCheckinInterval());

            StdSchedulerFactory schedulerFactory = new StdSchedulerFactory();
            schedulerFactory.initialize(properties);

            scheduler = schedulerFactory.getScheduler();
            scheduler.start();

            JobDataMap jobDataMap = new JobDataMap();
            jobDataMap.put("process", notificationMailProcess);


            JobDetail notificationMailJobDetail = newJob(NotificationMailJob.class)                         .withIdentity(Constants.JOB_NAME,Constants.JOB_GROUP).usingJobData(jobDataMap).build();

            String cronExpression =                         Constants.CONFIGURATION_DEFAULT_JOB_CRON);

            Trigger trigger = newTrigger().withIdentity(Constants.JOB_TRIGGER, Constants.JOB_GROUP)
                        .withSchedule(cronSchedule(cronExpression)).build();

            scheduler.scheduleJob(notificationMailJobDetail, new HashSet<>(Arrays.asList(trigger)), true);

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

NotificationMailProcess是一个虚拟EJB类,它实现带有@Stateless注释的Serializable接口。

1 个答案:

答案 0 :(得分:0)

最后发现,在持久注入的EJB期间,这是一个序列化问题。解决方案是使用JNDI查找来更改实现并在Job中获得EJB。