作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我认为我的 QuartzJobObject
不能将任何 DAO 或其他 Spring 管理的对象注入(inject)其中是否正确?
希望我能做这样的事情(orderService
是我想要注入(inject)的):
<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>
...我知道因为它的类型而没有意义。但是,我可以通过某种方式注入(inject)一些 DAO、服务等。不过我想不通。我该怎么做?
最佳答案
这就是我最终得到的结果,它完美地工作(希望对其他人有用)
了解 Spring 上下文的作业工厂
/// <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 是一个注入(inject)的 spring 管理对象,它本身有几个 DAO)。 HowManyMenuItemsIsOK 是通过作业数据映射传入的静态属性。
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 配置
<!-- 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>
关于c# - 将属性注入(inject) QuartzJobObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7548481/
我认为我的 QuartzJobObject 不能将任何 DAO 或其他 Spring 管理的对象注入(inject)其中是否正确? 希望我能做这样的事情(orderService 是我想要注入(inj
我是一名优秀的程序员,十分优秀!