0

如何在石英触发器中访问我的 OSGi 服务?

下面,当定时器被触发时,我的服务 companyDao 为空。我是否需要将捆绑上下文提供给调度程序或作业?如果是这样,如何?

@Service
@Component(immediate = true, specVersion = "1.1", inherit = true)
public class TechnicalStageTimer implements Job {

    @Reference(cardinality = MANDATORY_UNARY, policy = DYNAMIC)
    protected CompanyDao companyDao;

    private static final Logger LOG = LoggerFactory.getLogger(TechnicalStageTimer.class.getCanonicalName());

    Scheduler scheduler;

    @Activate
    public void start(BundleContext context) throws Exception {

        LOG.warn("Starting Timer TechnicalStageTimer");
        SchedulerFactory sf = new StdSchedulerFactory();
        scheduler = sf.getScheduler();

        JobDetail job = JobBuilder.newJob(TechnicalStageTimer.class).build();
        Trigger trigger = TriggerBuilder.newTrigger()
                .withSchedule(CronScheduleBuilder.dailyAtHourAndMinute(11, 00)) // every day at 11:00
                .build();
        scheduler.scheduleJob(job, trigger);
        scheduler.start();

    }

    @Deactivate
    public void stop(BundleContext context) throws Exception {
        scheduler.shutdown(true);
    }

    public void execute(JobExecutionContext context)
            throws JobExecutionException {

        LOG.warn("Timer Triggered");
        List<Company> companies = companyDao.getPool();
    }

}
4

1 回答 1

0

你必须记住一件事——Quartz 为每次执行实例化新的作业实例。如果您想访问外部组件(从新工作的角度来看),您必须以某种方式带来它。在这种情况下,您有两种选择:

  • 作业执行上下文
  • 静态字段引用

第一个更可靠,第二个相当hack。

于 2014-11-02T13:53:39.293 回答