如果您想了解如何在quartz作业中使用@Autowired?的知识,那么本篇文章将是您的不二之选。同时我们将深入剖析@Autowired,required,@Qualifier作用、c#–Topsh
如果您想了解如何在quartz作业中使用@Autowired?的知识,那么本篇文章将是您的不二之选。同时我们将深入剖析@Autowired,required,@Qualifier作用、c# – Topshelf需要使用Autofac的Quartz作业工厂、java – 在Seam中使用JobStoreTX代替JobStoreCMT for Quartz作业是否可以?、JavaQuartz作业持久性的各个方面,并给出实际的案例分析,希望能帮助到您!
本文目录一览:- 如何在quartz作业中使用@Autowired?
- @Autowired,required,@Qualifier作用
- c# – Topshelf需要使用Autofac的Quartz作业工厂
- java – 在Seam中使用JobStoreTX代替JobStoreCMT for Quartz作业是否可以?
- JavaQuartz作业持久性
如何在quartz作业中使用@Autowired?
我在spring使用quartz,我想注入/使用工作班级中的另一个班级,但我不知道该怎么做
xml:
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- Scheduler task --> <bean name="schedulerTask"/> <!-- Scheduler job --> <bean name="schedulerJob"> <property name="jobClass" value="com.mkyong.quartz.SchedulerJob" /> <property name="jobDataAsMap"> <map> <entry key="schedulerTask" value-ref="schedulerTask" /> </map> </property> </bean> <!-- Cron Trigger --> <bean id="cronTrigger"> <property name="jobDetail" ref="schedulerJob" /> <property name="cronExpression" value="0/10 * * * * ?" /> </bean> <!-- Scheduler --> <bean> <property name="jobDetails"> <list> <ref bean="schedulerJob" /> </list> </property> <property name="triggers"> <list> <ref bean="cronTrigger" /> </list> </property> </bean></beans>
quartz作业:
package com.mkyong.quartz;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import org.springframework.scheduling.quartz.QuartzJobBean;public class SchedulerJob extends QuartzJobBean{ private SchedulerTask schedulerTask; public void setSchedulerTask(SchedulerTask schedulerTask) { this.schedulerTask = schedulerTask; } protected void executeInternal(JobExecutionContext context) throws JobExecutionException { schedulerTask.printSchedulerMessage(); }}
要执行的任务:
package com.mkyong.quartz;public class SchedulerTask { public void printSchedulerMessage() { System.out.println("Struts 2 + Spring + Quartz ......"); }}
我想在任务类中注入另一个处理数据库的DTO类,以在任务中完成一些数据库工作,该怎么做?
答案1
小编典典不确定这是否是您想要的,但是您可以将一些配置值传递给Quartz作业。我相信在您的情况下,您可以利用jobDataAsMap
已经设置的属性,例如:
<property name="jobDataAsMap"> <map> <entry key="schedulerTask" value-ref="schedulerTask" /> <entry key="param1" value="com.custom.package.ClassName"/> </map> </property>
然后,您应该可以通过实际的Java代码以手动方式访问它:
protected void executeInternal(JobExecutionContext context) throws JobExecutionException { schedulerTask.printSchedulerMessage(); System.out.println(context.getJobDetail().getJobDataMap().getString("param1"));}
或使用神奇的Spring方法- param1
使用getter / setter定义属性。您可以尝试使用java.lang.Class
type
定义它,然后自动完成(Spring会为您完成):
private Class<?> param1; // getter & setter protected void executeInternal(JobExecutionContext context) throws JobExecutionException { schedulerTask.printSchedulerMessage(); System.out.println("Class injected" + getParam1().getName()); }
我还没有测试过。
@Autowired,required,@Qualifier作用
@Autowired(required = false)
当不能确定 Spring 容器中一定拥有某个类的 Bean 时,可以在需要自动注入该类 Bean 的地方可以使用 @Autowired(required = false),这等于告诉 Spring:在找不到匹配 Bean 时也不报错。
@Autowired
@Qualifier("office")
private Office office; //xml配置文件中存在office和office2两个Bean
在 Spring 容器中配置了两个类型为 Office 类型的 Bean,当对 Boss 的 office 成员变量进行自动注入时,Spring 容器将无法确定到底要用哪一个 Bean,因此异常发生了。
Spring 允许我们通过 @Qualifier 注释指定注入 Bean 的名称,这样歧义就消除了。
@Qualifier("office") 中的 office 是 Bean 的名称,所以 @Autowired 和 @Qualifier 结合使用时,自动注入的策略就从 byType 转变成 byName 了。
@Autowired 可以对成员变量、方法以及构造函数进行注释,而 @Qualifier 的标注对象是成员变量、方法入参、构造函数入参。
正是由于注释对象的不同,所以 Spring 不将 @Autowired 和 @Qualifier 统一成一个注释类。
@Autowired是根据类型注入,@Qualifier是根据名称注入
以上说的Bean 的名称是指bean中的id
参考:http://darkmasky.iteye.com/blog/1129828
c# – Topshelf需要使用Autofac的Quartz作业工厂
cfg.UsingQuartzJobFactory(() => container.Resolve<IJobFactory>());
似乎是错误的做事方式.有没有更好的方法告诉Topshelf使用自定义autofac jobfactory? jobfactory的生命范围是什么?我担心这行代码会在将来的某个时候引起一些麻烦.如何在不再需要时释放jobfactory?这条线是否正常?
class Poller : IJob { private readonly ILogger _log; public Poller(ILogger log) { _log = log; _log.Info("Instantiating..."); } public void Execute(IJobExecutionContext context) { _log.Info("Executing..."); } } class Program { static Autofac.IContainer BuildContainer() { var builder = new ContainerBuilder(); builder.RegisterModule<NLogModule>(); builder.RegisterModule<QuartzAutofacFactoryModule>(); builder.RegisterModule(new QuartzAutofacJobsModule(typeof(Poller).Assembly)); var container = builder.Build(); return container; } static void Main(string[] args) { var container = BuildContainer(); HostFactory.Run(cfg => { cfg.UseNLog(); cfg.UseAutofacContainer(container); cfg.SetDescription("DESCRIPTION"); cfg.SetdisplayName("disPLAY"); cfg.SetServiceName("NAME"); cfg.UsingQuartzJobFactory(() => container.Resolve<IJobFactory>()); cfg.ScheduleQuartzJobAsService(q => { q.WithJob(() => JobBuilder.Create<Poller>().Build()); q.AddTrigger(() => TriggerBuilder.Create().WithSimpleSchedule(b => b.WithIntervalInSeconds(20).RepeatForever()).Build()); }); cfg.StartAutomatically(); cfg.RunAsLocalSystem(); }); } }
供参考:TopShelf.Quartz.ScheduleHobHostConfiguratorExtensions
另请参阅:Autofac.Extras.Quartz.QuartzAutofacFactoryModule
解决方法
try { var container = new UnityContainer(); schedulerFactory = CreateSchedulerFactory(); quartzscheduler = GetScheduler(); SyncPost.Initialize.RepositoryConfig(container); SyncPost.Initialize.AddToSchedulerContextCustomVars(quartzscheduler,container); quartzscheduler.JobFactory = new JobFactoryInjection(container); } catch (Exception e) { logger.Error("Server initialization Failed:" + e.Message,e); throw; }
JobFactoryInjection在哪里实现IJobFactory:
public class JobFactoryInjection : IJobFactory { private readonly UnityContainer container = new UnityContainer(); public JobFactoryInjection(UnityContainer container) { if (container == null) throw new ArgumentNullException("container","Container is null"); this.container = container; } public IJob NewJob(TriggerFiredBundle bundle,IScheduler scheduler) { // Return job registrated in container bundle.JobDetail.JobDataMap.Put(SyncUtils.ContextKeyCenterCode,scheduler.Context.Get(SyncUtils.ContextKeyCenterCode)); return (IJob)container.Resolve(bundle.JobDetail.JobType); } public void ReturnJob(IJob job) { } }
关于JobFactory的生命周期,不用担心.来自Quartz文档:“JobFactory只是激活作业类的新实例.您可能想要创建自己的JobFactory实现来完成诸如让应用程序的IoC或DI容器生成/初始化作业实例之类的事情”
java – 在Seam中使用JobStoreTX代替JobStoreCMT for Quartz作业是否可以?
我们的项目需要保留作业,以便我们可以进行集群,但是我无法让JobStoreCMT执行作业. JobStoreCMT和JobStoreTX特别针对Seam有什么区别?
是否可以优先使用JobStoreTX,只是因为它接缝更容易,或者我错过了一些关键的东西?
解决方法
JobStoreCMT is meant to be used in an application-server environment that provides container-managed-transactions. No commit / rollback will be1 handled by this class.
If you need commit / rollback,use JobStoreTX instead.
和JobStoreTX:
JobStoreTX is meant to be used in a standalone environment. Both commit and rollback will be handled by this class.
If you need a JobStore class to use within an application-server environment,use JobStoreCMT instead.
因此,使用哪个问题的答案归结为您是否希望创建触发器/等等,以使其成为应用程序中较大事务的一部分,或者它是否应该是独立的.
JavaQuartz作业持久性
我对Java Quartz不太熟悉,我们只是使用了每天安排的测试工作。对于我们的Struts2
Web应用程序,我们希望运行一些计划在一天中不同时间的日常工作。作业应处于持久状态,以便即使由于服务器关闭/应用程序失败而导致作业失败,也应在服务器启动后稍后重新执行。我还可以将作业的状态/结果存储在DB中,以便监视作业。任何的意见都将会有帮助。
- 谢谢
关于如何在quartz作业中使用@Autowired?的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于@Autowired,required,@Qualifier作用、c# – Topshelf需要使用Autofac的Quartz作业工厂、java – 在Seam中使用JobStoreTX代替JobStoreCMT for Quartz作业是否可以?、JavaQuartz作业持久性的相关信息,请在本站寻找。
本文标签: