Announcement Announcement Module
Collapse
No announcement yet.
Runtime Quartz jobs and ApplicationContext Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Runtime Quartz jobs and ApplicationContext

    Hi.

    Am I missing something obvious here, but I dunno how to access ApplicationContext inside Quartz job who is created runtime? Or even better, I don't need ApplicationContext, I need some spring bean injected in it.

    It seems to me that whole Spring support is concentrated around static jobs, that can be defined in applicationContext.xml beforehand, but my jobs are created in runtime, in other words, I fetch Scheduler and add newly created jobs through this interface. Parameters are stored inside job's JobDataMap, so I can fecth them during execution, but I dunno how to fecth spring bean from ApplicationContext?

    Regards,
    Vjeran

  • #2
    1. Set the applicationCOntextSchedulerContextKey property.

    Code:
    <bean name="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
      <!-- name used to publish applicationContext to Quartz Job instances, so they can call business services -->
      <property name="applicationContextSchedulerContextKey"><value>applicationContext</value></property>
    </bean>
    2. From your Job's execute method.

    Code:
    SchedulerContext schedulerContext = jobExecutionContext.getScheduler&#40;&#41;.getContext&#40;&#41;;
    ApplicationContext applicationContext = &#40;ApplicationContext&#41; schedulerContext.get&#40;"applicationContext"&#41;;

    Comment


    • #3
      Thanx a bunch.

      And now I'm greedy - is there some way for Spring to already inject required beans, rather than provide me with ApplicationContext where I would pull them from?

      Comment


      • #4
        um...no.

        but if you find out, let me know.

        Comment


        • #5
          Thanks for the info about getting the context, that helped me also. It looks to me like what prevents Spring from wiring up the job implementations is that Quartz takes a class object, not an instance of the jobs. It would be nice if that changed, though I can see why it is that way, so they aren't keeping objects alive forever I assume. Perhaps there is a way to do it through code generation / AOP.

          Comment


          • #6
            Have you seen the MethodInvokingJobDetailFactoryBean ? This is just a proxy that uses reflection to call a method on some bean. I use it all the time, it works great. Plus, using this, I don't have to create a special Job bean. I just use quartz to call a method in my service layer.

            Code:
            	<bean id="receivedBusinessUpdaterJob" 
            			class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            		<property name="targetObject"><ref local="receivedBusinessUpdater"/></property>
            		<property name="targetMethod"><value>updateAllBusinesses</value></property>
            		<property name="concurrent"><value>false</value></property>
            	</bean>

            Comment


            • #7
              Helpful thread

              Just want to post a thanks as, once I could figure out which keywords I needed to search, this info solved my learning curve issue.

              Regarding MethodInvokingJobDetailFactoryBean: I understand the concept, but I'm missing the details on how any parameter values are passed to the (say) business method being invoked? If this thread is not dead any clues would be appreciated.

              Thx.

              Comment


              • #8
                JDBCJobStore

                I am currently using JDBCJobStore and when the job executes I want to access applicationContect and get a handle to RMI service bean. Application Context access Method mentioned in this thread also works also in this case?
                -- Deha Peker



                Originally posted by gmatthews View Post
                1. Set the applicationCOntextSchedulerContextKey property.

                Code:
                <bean name="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
                  <!-- name used to publish applicationContext to Quartz Job instances, so they can call business services -->
                  <property name="applicationContextSchedulerContextKey"><value>applicationContext</value></property>
                </bean>
                2. From your Job's execute method.

                Code:
                SchedulerContext schedulerContext = jobExecutionContext.getScheduler().getContext();
                ApplicationContext applicationContext = (ApplicationContext) schedulerContext.get("applicationContext");

                Comment

                Working...
                X