Results 1 to 10 of 10

Thread: TimerTask bean is not singleton

  1. #1

    Default TimerTask bean is not singleton

    Hello everybody,

    When i start my application i start a timertask which check if all jobs are still running:
    Code:
    <bean id="checkifJobIsRunningTask"
    		class="com.inprove.servicefarm.grid.CheckIfJobIsRunning">
    		<property name="jobManager" ref="jobManager" />
    		<property name="webFarmRepository" ref="webFarmRepository" />
    		<property name="transactionManager" ref="transactionManager" />
    	</bean>
    	<bean id="checkifJobIsRunningSchedule"
    		class="org.springframework.scheduling.timer.ScheduledTimerTask">
    		<!-- wait 10 seconds before starting repeated execution -->
    		<property name="delay" value="10000" />
    		<!-- run every 10 minutes -->
    		<property name="period" value="300000" />
    		<property name="timerTask" ref="checkifJobIsRunningTask" />
    	</bean>
    On the fly jobs are added. But when checkifJobIsRunningSchedule runs it can't find jobs. But when i use the jobManager method from a webpage it works.

    Can anybody help me to make my bean a singleton in a timertask?

  2. #2

    Default

    Kick, and can anybody move this topic to the Core Container forum?

  3. #3

    Default

    Kick, can anybody help me?

  4. #4

    Default

    So I still got my singleton bean problem. Can any body help me?

  5. #5

    Default

    I still haven't found a solution.

  6. #6
    Join Date
    Feb 2005
    Location
    Boston, MA
    Posts
    1,142

    Default

    What have you tried? Have you tried adding additional logging or remote debugging? Are you sure the job fires? Can you post the code to CheckIfJobIsRunning?
    Bill

  7. #7

    Default

    I only tried manually debugging.
    the code of checkIfJobIsRunning:
    Code:
    package com.inprove.servicefarm.grid;
    
    import java.util.Calendar;
    import java.util.Iterator;
    import java.util.List;
    import java.util.TimerTask;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.transaction.PlatformTransactionManager;
    import org.springframework.transaction.TransactionStatus;
    import org.springframework.transaction.support.TransactionCallbackWithoutResult;
    import org.springframework.transaction.support.TransactionTemplate;
    
    import com.inprove.servicefarm.job.Job;
    import com.inprove.servicefarm.job.JobManager;
    import com.inprove.servicefarm.job.JobWebservice;
    import com.inprove.servicefarm.webfarm.WebFarmRepository;
    public class CheckIfJobIsRunning extends TimerTask
    {
    	JobManager jobManager;
    	WebFarmRepository webFarmRepository;
    	private TransactionTemplate transactionTemplate;
    
    	public void run()
    	{
    		
    		transactionTemplate.execute(new TransactionCallbackWithoutResult()
    		{
    			protected void doInTransactionWithoutResult(TransactionStatus status)
    			{
    		
    				List<Job> jobs = jobManager.getRunningJobs();
    				Iterator<Job> it = jobs.iterator();
    				while (it.hasNext())
    				{
    					Job job = it.next();
    					Calendar calendar = Calendar.getInstance();
    					long currentTime = calendar.getTimeInMillis();
    					long jobStatusTime = job.getCurrentStatusTime()
    							.getTimeInMillis();
    					long different = currentTime - jobStatusTime;
    					long diffMinutes = different / (60 * 1000);
    					if (diffMinutes > 2)
    					{
    						
    						jobManager.stopJob(job.getId());
    						webFarmRepository.addNotification(
    								job.getOrganization(), job.getId(), "job",
    								"Job is stopped because there wasn't contact");
    					}
    				}
    			}
    		});
    	}
    
    	public void setTransactionManager(
    			PlatformTransactionManager transactionManager)
    	{
    		this.transactionTemplate = new TransactionTemplate(transactionManager);
    	}
    
    	public void setJobManager(JobManager jobManager)
    	{
    		this.jobManager = jobManager;
    	}
    
    	public void setWebFarmRepository(WebFarmRepository webFarmRepository)
    	{
    		this.webFarmRepository = webFarmRepository;
    	}
    
    }
    But the problem is when i but a bean in the timertask the bean isn't a singleton anymore.

  8. #8
    Join Date
    Feb 2005
    Location
    Boston, MA
    Posts
    1,142

    Default

    Are you sure the job fires?
    Bill

  9. #9

    Default

    Yes i am sure. Because I can stop it with the same bean. But not when i acces the bean from CheckIfJobIsRunning

  10. #10
    Join Date
    Jul 2008
    Posts
    1

    Default Solved?

    Is this problem solved?
    I have the same problem with TimerTask.

Posting Permissions

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