Results 1 to 7 of 7

Thread: Scheduler not injecting my custom beans

  1. #1
    Join Date
    Sep 2009
    Posts
    12

    Default Scheduler not injecting my custom beans

    Hi all,

    I have configured a scheduler in my app xml:

    <import resource="classpath:services-context.xml" />

    <bean id="drJob" class="org.springframework.scheduling.quartz.JobDe tailBean">
    <property name="jobClass">
    <value>com.web.DailyReportJob</value>
    </property>
    </bean>

    <bean id="dailyReportJobTrigger" class="org.springframework.scheduling.quartz.CronT riggerBean">
    <property name="jobDetail" ref="drJob"/>
    <property name="cronExpression" value="0 0/30 * * * ?"/>
    </bean>

    <bean id="sch" class="org.springframework.scheduling.quartz.Sched ulerFactoryBean">
    <property name="triggers">
    <list>
    <ref bean="dailyReportJobTrigger" />
    </list>
    </property>
    </bean>

    Now inside my DailyReportJob i am using a bean(servicebean) that was inside the imported file in the first line of the above xml.

    Questions:
    1. Now my problem is that the service is not getting injected, i have got the getters and setters for that ..how can i get it injected Automatically ?
    As a Temp fix currently i was doing like this :
    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext .xml");
    servicebean= (servicebean) ctx.getBean("servicebean");
    how would i get it injected automatically ?

    2. Down the code i need to get the path at which this application is depolyed for my business logic to copy some thing in the images folder..i could recall in servlet there is a way to get the realpath is there a way in my scheduler job code to get the real path of the application any way so that ican avoid the hard-coding in the properties.

    looking forward for your suggestions and advice..

    regards,
    vijay

  2. #2
    Join Date
    Sep 2009
    Posts
    12

    Default Any suggestions on this ?

    Any suggestions on this would be greatly appreciated..

    thanks
    vijay

  3. #3

    Smile

    Hi,

    If am not wrong .you want to inject ServiceBean automaitically into com.web.DailyReportJob Class.

    You can just do it in the below manner
    1)Define a bean for this class com.web.DailyReportJob.
    <bean id-"dailyReportID" class="com.web.DailyReportJob.">
    <property name="serviceBean" ref="serviceBeanId"/>
    </bean>

    DailyReportBeanId will search for serviceBeanId in your services-context.xml" or applicationContext.xml.

    and then you can inject it in your JobDetailBean as shown below.

    <bean id="drJob" class="org.springframework.scheduling.quartz.JobDe tailBean">
    <property name="jobClass" ref="dailyReportID"/>
    </bean>

    Hope it solves your problem.

  4. #4

    Default

    Another option is the user the schedulerContextAsMap property:

    userService is a spring bean

    <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.Sched ulerFactoryBean">
    <property name="transactionManager" ref="transactionManager" />
    <property name="schedulerContextAsMap">
    <map>
    <entry key="userService">
    <ref bean="userService"/>
    </entry>
    </map>
    ....

    then use this in your job bean

    SchedulerContext skedCtx = jobExecutionContext.getScheduler().getContext();
    this.userService = (UserService) skedCtx.get("userService");

  5. #5
    Join Date
    Sep 2009
    Posts
    12

    Default

    Quote Originally Posted by kravicha View Post
    Hi,

    If am not wrong .you want to inject ServiceBean automaitically into com.web.DailyReportJob Class.

    You can just do it in the below manner
    1)Define a bean for this class com.web.DailyReportJob.
    <bean id-"dailyReportID" class="com.web.DailyReportJob.">
    <property name="serviceBean" ref="serviceBeanId"/>
    </bean>

    DailyReportBeanId will search for serviceBeanId in your services-context.xml" or applicationContext.xml.

    and then you can inject it in your JobDetailBean as shown below.

    <bean id="drJob" class="org.springframework.scheduling.quartz.JobDe tailBean">
    <property name="jobClass" ref="dailyReportID"/>
    </bean>

    Hope it solves your problem.

    the property jobClass expects a String not of type class.so it doesn't work

    vijay

  6. #6
    Join Date
    Sep 2009
    Posts
    12

    Default

    Quote Originally Posted by robincusters View Post
    Another option is the user the schedulerContextAsMap property:

    userService is a spring bean

    <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.Sched ulerFactoryBean">
    <property name="transactionManager" ref="transactionManager" />
    <property name="schedulerContextAsMap">
    <map>
    <entry key="userService">
    <ref bean="userService"/>
    </entry>
    </map>
    ....

    then use this in your job bean

    SchedulerContext skedCtx = jobExecutionContext.getScheduler().getContext();
    this.userService = (UserService) skedCtx.get("userService");

    This solution works..thanks a lot..
    Also can you place share your thoughts on my 2nd doubt in the post ie::

    2. Down the code i need to get the path at which this application is depolyed for my business logic to copy some thing in the images folder..i could recall in servlet there is a way to get the realpath is there a way in my scheduler job code to get the real path of the application any way so that ican avoid the hard-coding in the properties.

    regards,
    vijay

  7. #7
    Join Date
    Dec 2009
    Posts
    4

    Default having same problem

    Hi koganti,

    >2. Down the code i need to get the path at which this application is >depolyed for my business logic to copy some thing in the images folder..i could >recall in servlet there is a way to get the realpath is there a way in my >scheduler job code to get the real path of the application any way so that >ican avoid the hard-coding in the properties.

    Did you find any solution? I have same problem.

    Thanks

    -Shyam

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •