Announcement Announcement Module
Collapse
No announcement yet.
TimerTask bean is not singleton Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    Kick, and can anybody move this topic to the Core Container forum?

    Comment


    • #3
      Kick, can anybody help me?

      Comment


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

        Comment


        • #5
          I still haven't found a solution.

          Comment


          • #6
            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?

            Comment


            • #7
              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.

              Comment


              • #8
                Are you sure the job fires?

                Comment


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

                  Comment


                  • #10
                    Solved?

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

                    Comment

                    Working...
                    X