Announcement Announcement Module
Collapse
No announcement yet.
quartz passing parameter BlockingQueue Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • quartz passing parameter BlockingQueue

    Hi,

    I'm using Quartz CronTrigger and I would like to pass an argument to one of my job.

    Here is my contextApplication.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    	<bean id="job" class="org.springframework.scheduling.quartz.JobDetailBean">
    		<property name="name" value="Job1"/>
    		<property name="group" value="Group1"/>
    		<property name="jobClass" value="QuartzSample.MyProducer" />
    		<property name="jobDataAsMap">
    			<map>
    				<entry key="message" value="Original Message" />
    			</map>
    		</property>
    	</bean>
    
    
    	<bean id="trigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    		<property name="jobDetail" ref="job" />
    		<property name="cronExpression" value="0/5 * * * * ?"/>
    	</bean>
    
    	<bean id="schedulerFactory"
    		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    		<property name="triggers">
    			<list>
    				<ref local="trigger" />
    			</list>
    		</property>
    	</bean>
    </beans>
    Here is my main class where I would like to pass "MyQueue" to Producer class.
    But I don't know what's the best way to do this.

    Code:
    public class MyMain
    {
    	
    	private static BlockingQueue<String> MyQueue= new ArrayBlockingQueue<String>(5);
    	
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args)throws Exception
    	{
    		ApplicationContext ctx = new FileSystemXmlApplicationContext("contextApplication.xml");
    
    		//get the scheduler
    		Scheduler scheduler = (Scheduler) ctx.getBean("schedulerFactory");
    
    		JobDetail job = scheduler.getJobDetail("Job1", "Group1");// = scheduler.getJobDetail("otherJob", Scheduler.DEFAULT_GROUP);
    
    		[COLOR="Blue"]
                    /** I'm stuck here, I don't know what is the best way to do this */
                   job.getJobDetailMap().put("queue",MyQueue);
    		scheduler.start();
    	}
    }
    MyProducer class
    Code:
    public class MyProducer implements StatefulJob
    {
            private BlockingQueue<String> queue;
    
    @Override
    	public void execute(JobExecutionContext context) throws JobExecutionException
    	{
                this.queue = (BlockingQueue<String>)context.getJobDetail().getJobDataMap().get("queue");
    	    System.out.println("Queue size: " + this.queue.size());
    		
    	}
    }
    But I don't get anything from my queue in MyProducer class. Can someone please help me


    Thanks,
    Last edited by geek.shrek; May 16th, 2010, 10:12 PM. Reason: fixing syntax error

  • #2
    You could define your producer and queue classes as spring beans and inject the queue object as a dependency to your producer bean.

    Comment

    Working...
    X