Results 1 to 3 of 3

Thread: how to dynamically pass parameters of method

  1. #1
    Join Date
    Jan 2008
    Posts
    17

    Question how to dynamically pass parameters of method

    I am using Spring quartz to implement some schedule tasks. My initial configuration works fine, it looks like:
    Code:
        <bean id="quartzJobDetailSample" class="..." />
    
        <bean id="jobDetailId" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <property name="targetObject" ref="quartzJobDetailSample" />
            <property name="targetMethod" value="methodName"/>
            <property name="arguments">
                <list>
                    <value>initialParam1</value>
                    <value>initialParam2</value>
                </list>
            </property>
        </bean>
    
        <bean id="jobTriggerId" class="org.springframework.scheduling.quartz.CronTriggerBean">
            <property name="jobDetail" ref="jobDetailId" />
            <property name="cronExpression" value="0/15 * * * * ?" />
        </bean>
    
        <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
                    <ref bean="jobTriggerId" />
                </list>
            </property>
        </bean>
    But I need to manage all tasks through UI startup of web application, I wonder if there is some way to pass new parameters to the method. I tried:

    MethodInvokingJobDetailFactoryBean bean = (MethodInvokingJobDetailFactoryBean)getApplication Context().getBean("jobDetailId");
    bean.setArguments(new String[]{"1","2"});
    It turns out the MethodInvokingJobDetailFactoryBean generated a JobDetail instance for the bean with id "jobDetailId", but there's no way to pass arguments to method in JobDetail instance. I know I probably can use DataMap to achieve this, but I don't want to disturb my service layer (here is quartzJobDetailSample class) with any Quartz code if possible.

    If anyone has any idea, please share it. Thanks in advance

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    It turns out the MethodInvokingJobDetailFactoryBean generated a JobDetail instance for the bean with id "jobDetailId"
    Because that is what a FactoryBean should do. If you really want to access your FactoryBean use a special bean name. You have to use the '&[beanname]' as the name/id of the bean to retrieve. So in your case '&jobDetailId'.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jan 2008
    Posts
    17

    Default

    Marten, thanks a lot! I managed to pass new arguments to my scheduled business method with your helpful information.

    MethodInvokingJobDetailFactoryBean bean = (MethodInvokingJobDetailFactoryBean)this.getApplic ationContext().getBean("&jobDetailId");
    bean.setArguments(new String[]{"new argument 1","new argument 2"});
    scheduler.addJob((JobDetail)bean.getObject(), true);
    I initially doubt if it works. Because the isSingleton method of "bean" return true, which indicates getObject() returns the same instance all the time. I just had a try in my test, when I saw the new arguments appearing on console, I was released

Posting Permissions

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