首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将属性注入QuartzJobObject

将属性注入QuartzJobObject
EN

Stack Overflow用户
提问于 2011-09-26 04:45:09
回答 2查看 1.5K关注 0票数 2

我认为我的QuartzJobObject不能注入任何DAO或其他Spring托管对象,这样想对吗?

我希望我能做这样的事情(orderService是我想要注入的东西):

代码语言:javascript
复制
<object name="checkPendingOrdersJob" type="Spring.Scheduling.Quartz.JobDetailObject, Spring.Scheduling.Quartz">
  <property name="JobType" value="Munch.ScheduledTasks.CheckPendingOrdersJob" />
  <!-- We can inject values through JobDataMap -->
  <property name="JobDataAsMap">
    <dictionary>
      <!--entry key="UserName" value="Alexandre" /-->
    </dictionary>      
  </property>
  <property name="orderService" ref="orderService"/>
</object>

我知道...which没有任何意义,因为它的类型。但是,我可以通过某种方式注入一些DAO、服务等。不过我还是想不通。我该怎么做呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-09-26 16:53:57

您可以通过覆盖AdaptableJobFactoryCreateJobInstance来将属性/构造函数注入到您的工作中,并注册新的JobFactory而不是默认的。

传入的TriggerFiredBundle为您提供了足够的信息来询问匹配作业的上下文(基于约定)。bundle.JobDetail.JobType.Namebundle.JobDetail.JobType满足了我的需求,所以回到2008年,我最终得到了一些东西。就像这样(这个类是从AdaptableJobFactory派生出来的,它实现了IApplicationContextAware来注入上下文):

代码语言:javascript
复制
public class ContextAwareJobFactory : AdaptableJobFactory, IApplicationContextAware
{
  private IApplicationContext m_Context;

  public IApplicationContext ApplicationContext
  {
    set
    {
      m_Context = value;
    }
  }

  protected override object CreateJobInstance( TriggerFiredBundle bundle )
  {
    return m_Context.GetObject( bundle.JobDetail.JobType.Name, bundle.JobDetail.JobType );
  }
}

您需要使用以下配置注册ContextAwareJobFactory:

代码语言:javascript
复制
<objects xmlns="http://www.springframework.net">
  <!-- Some simple dependency -->
  <object name="SomeDependency" type="Namespace.SomeDependency, Assembly" />

  <!-- The scheduled job, gets the dependency. -->
  <object name="ExampleJob" type="Namespace.ExampleJob, Assembly" singleton="false">
    <constructor-arg name="dependency" ref="SomeDependency"/>
  </object>

  <!-- The JobDetail is configured as usual. -->
  <object name="ExampleJobDetail" type="Spring.Scheduling.Quartz.JobDetailObject, Spring.Scheduling.Quartz">
    <property name="JobType" value="Namespace.ExampleJob, Assembly"/>        
  </object>

  <!-- The new JobFactory. -->
  <object name="ContextAwareJobFactory" type="Namespace.ContextAwareJobFactory, Assembly" />

  <!-- Set the new JobFactory onto the scheduler factory. -->
  <object id="quartzSchedulerFactory" type="Spring.Scheduling.Quartz.SchedulerFactoryObject, Spring.Scheduling.Quartz">
    <property name="JobFactory" ref="ContextAwareJobFactory"/>
  </object>      
</objects>

我不知道有没有什么东西。ootb,因为这是在2008年开发的,我没有关注为quartz.net所做的集成进展。

票数 1
EN

Stack Overflow用户

发布于 2011-09-28 05:38:39

这就是我最终得到的结果,它工作得很完美(希望对其他人有用)

感知上下文的Spring工厂

代码语言:javascript
复制
/// <summary>
/// A custom job factory that is aware of the spring context
/// </summary>
public class ContextAwareJobFactory : AdaptableJobFactory, IApplicationContextAware
{
    /// <summary>
    /// The spring app context
    /// </summary>
    private IApplicationContext m_Context;

    /// <summary>
    /// Set the context
    /// </summary>
    public IApplicationContext ApplicationContext
    {
        set
        {
            m_Context = value;
        }
    }

    /// <summary>
    /// Overrides the default version and sets the context
    /// </summary>
    /// <param name="bundle"></param>
    /// <returns></returns>
    protected override object CreateJobInstance(TriggerFiredBundle bundle)
    {
        return m_Context.GetObject(bundle.JobDetail.JobType.Name, bundle.JobDetail.JobType);
    }
}

作业本身的(检查数据库中的记录,如果记录至少有HomeManyMenuItemsIsOK,则一切正常)。注意: menuService是一个注入的spring托管对象,它本身有几个DAO)。HowManyMenuItemsIsOK是通过作业数据映射传入的静态属性。

代码语言:javascript
复制
public class CheckMenuIsHealthyJob : QuartzJobObject 
{
    private static readonly ILog log = LogManager.GetLogger(typeof(CheckMenuIsHealthyJob));

    public IMenuService menuService { get; set; }
    public int HowManyMenuItemsIsOK { get; set; }

    /// <summary>
    /// Check how healthy the menu is by seeing how many menu items are stored in the database. If there
    /// are more than 'HowManyMenuItemsIsOK' then we're ok.
    /// </summary>
    /// <param name="context"></param>
    protected override void ExecuteInternal(JobExecutionContext context)
    {
        IList<MenuItem> items = menuService.GetAllMenuItems();
        if (items != null && items.Count >= HowManyMenuItemsIsOK)
        {
            log.Debug("There are " + items.Count + " menu items. Menu is healthy!");
        }
        else
        {
            log.Warn("Menu needs some menu items adding!");
        }

    }
}

,最后是Spring配置

代码语言:javascript
复制
<!-- Scheduled Services using Quartz -->
  <!-- This section contains Quartz config that can be reused by all our Scheduled Tasks ---->  
  <!-- The Quartz scheduler factory -->
  <object id="quartzSchedulerFactory" type="Spring.Scheduling.Quartz.SchedulerFactoryObject, Spring.Scheduling.Quartz">
    <!-- Tell Quartz to use our custom (context-aware) job factory -->
    <property name="JobFactory" ref="contextAwareJobFactory"/>

    <!-- Register the triggers -->
    <property name="triggers">
      <list>
        <ref object="frequentTrigger" />
      </list>
    </property>
  </object>  

  <!-- Funky new context-aware job factory -->
  <object name="contextAwareJobFactory" type="Munch.Service.ScheduledTasks.ContextAwareJobFactory" />

  <!-- A trigger that fires every 10 seconds (can be reused by any jobs that want to fire every 10 seconds) -->
  <object id="frequentTrigger" type="Spring.Scheduling.Quartz.CronTriggerObject, Spring.Scheduling.Quartz" lazy-init="true">
    <property name="jobDetail" ref="checkMenuIsHealthyJobDetail" />
    <property name="cronExpressionString" value="0/10 * * * * ?" />
  </object>  

  <!-- Now the job-specific stuff (two object definitions per job; 1) the job and 2) the job detail) --> 
  <!-- Configuration for the 'check menu is healthy job' -->
  <!-- 1) The job -->
  <object name="checkMenuIsHealthyJob" type="Munch.Service.ScheduledTasks.CheckMenuIsHealthyJob" singleton="false">
    <property name="menuService" ref="menuService"/>
  </object>

  <!-- 2) The job detail -->
  <object name="checkMenuIsHealthyJobDetail" type="Spring.Scheduling.Quartz.JobDetailObject, Spring.Scheduling.Quartz">
    <property name="JobType" value="Munch.Service.ScheduledTasks.CheckMenuIsHealthyJob"/>
    <property name="JobDataAsMap">
      <dictionary>
        <entry key="HowManyMenuItemsIsOK" value="20" />
      </dictionary>
    </property>    
  </object>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7548481

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档