-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Scheduled tasks seem to run twice [SPR-6859] #11525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Daniel Hodan commented Hello, I have still same problem with 3.0.1.RELEASE using The workaround for now is to avoid annotation and use XML-based configuration with declaration of scheduled method which works correctly - runs method only once. |
Rafis Ismagilov commented The problem still occurs in 3.0.1 for short-running tasks. No annotations, only XML.
Sometimes task starts a little earlier than a next scheduled time (at 14:31:00,000):
The task is finished earlier than the scheduled time, and it rescheduled again at the same time according to code in CronTrigger.nextExecutionTime(TriggerContext triggerContext):
It seems the source of the problem is accuracy of method System.currentTimeMillis() used in ReschedulingRunnable.schedule() and in java.util.Date default constructor. I executed the following code on my Windows Vista x64:
and received the output:
Time was updated in 5-16 milliseconds. I suggest to use in CronTrigger.nextExecutionTime(TriggerContext triggerContext) the latest of TriggerContext.lastCompletionTime() and TriggerContext.lastCompletionTime(). It helps to avoid double execution at the same time. |
Juergen Hoeller commented See #11669: Fortunately, this should be fixed in the latest 3.0.3 snapshots already. Juergen |
Rafis Ismagilov commented Cool! Thanks. |
Paul Zolnierczyk commented So I'm experiencing this issue on 3.0.5 . I have a task that is scheduled to run every ten minutes (I'm still in development). All it does is query the database for users that meet a specific criteria and then I send them an e-mail reminder. All I did to enable this was add in my XML
and then I annotated like this:
I'm also on JDK 1.6 and developing on windows XP. For some reason I wonder if it's a dual core issue? |
Paul Zolnierczyk commented Silly me, I was loading the application context twice! |
Bahman Kalali commented I am experiencing the same the same problem where the methods that are scheduled to run every 2 and 1 minutes respectively are invoked twice at specified fixed rate. Here is the xml fragment of my spring app context ( the XML app context is called edgeServerContext.xml)
The only difference with my issue and the rest of other commentators is that my spring xml context is imported into mule-config.xml as follows: <?xml version="1.0" encoding="UTF-8"?> <mule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.mulesoft.org/schema/mule/core"
</mule> I am not sure this issue is related to mule or spring. It seems that schedulers are initialized twice when deployed two a mule server. I am using mule 3.1.2, spring 3.0.6.RELEASE, jdk1.6.0_25 I appreciated it any insight on this issue... BR//Bahman |
Sidharth Kaushik commented I am using Spring 3.0.5. Trying to schedule a simple job <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"
</beans> The cron expression is read from properties file as : 3/10 * * * * ? On running the application on TOMCAT, the output in console is ################processing next########## at Tue Dec 13 13:10:43 IST 2011 SidddThe Milli time is : 1323762043001 How can I stop the job from being called twice. When I run it as a simple standalone application, it works fine. Also, my context xml is being loaded correctly and only once. Regards, |
Hoshiar Ravesh commented The issue is still reproducing in 3.1.1 Release |
Paul Boonyarungsrit commented I had this issue and found a solution for it. In my case, one of my applicationContext*.xml that I have was loaded by both root application context (contextConfigLocation in web.xml) and by web application context (via DispatcherServlet). Putting <tasks:scheduler /> in the config that is loaded by both contexts causes 2 scheduler threads to be created, one for each context. Making sure that <tasks:scheduler /> is loaded by the root application context only fixes my issue. It would be nice if Spring can detect that, but at least you can avoid this issue yourself. |
suresh m commented This issue is coming in Spring 3.1.0 RELEASE, any workaround |
Juergen Hoeller commented As of 3.2.2, we've done everything we can do to avoid a repeat of the current execution time as next scheduled time: CronTrigger uses the later of lastCompletionTime and lastScheduledExecutionTime to calculate from, and CronSequenceGenerator explicitly recalculates from the next second if it ends up computing the same time as the original timestamp. In other words, if you're still running into double execution with only one scheduler set up (please always double-check that), then we have no idea how that could happen. I'll mark this issue as resolved for that reason. If you happen to be able to reproduce this under proper circumstances against 3.2.2, please create a more specific issue as a follow-up. Juergen |
Assaf commented I have just tried this on 3.2.2 and it still does not work. My configuration is as follows: <!-- Scheduler --> <task:annotation-driven scheduler="taskScheduler"/> and the class: .....
The method is being called twice |
James Hunvanich commented I also have 3.2.2, and this is still a problem. Similar configs as users above. I tripled-check to make sure context was not being loaded more than once. Trying to use java.util.Timer also gets run twice if initiated from ... Timer timer = new Timer(); Result: |
Assaf commented I retract my issue. Apparently, spring was loading my configuration twice and hence the code being triggered twice. Thanks guys for helping! Assaf |
Kirill Kadyrko commented Confirm the issue still exists. I'm sure my application context is loaded only once. Another interesting thing is that the task is not always executed twice, still, I don't understand the pattern. I have no issues when running the app on Tomcat from within Eclipse. But it's exposed after deploying the app to GlassFish. |
Kirill Kadyrko commented Well, I'm taking my words back. Seems the new jar wasn't in the repository when I did the check. So far so good with 3.2.2 :) Thanks! |
Hasnain Javed commented Hi All, I ran into the same problem even after upgrading to Spring 3.2.2 and was able to solve the problem by removing the <Context> element inside the <Host> element in tomcat's server.xml file (I'm running tomcat 7) Snippet: <!--- <Context docBase="myWebApp" path="/test" reloadable="true" source="org.eclipse.jst.jee.server:myWebApp"/> --> I guess the IDE (STS in my case) modified the server.xml file while configuring tomcat within it. If that is not the case, manually remove or comment the <Context> tag and hopefully it should work fine. The reason was that the application was deployed twice by tomcat. Hope this helps. Kind Regards. |
Juergen Hoeller commented To the best of our knowledge, this is caused by accidental duplicate deployment of your application - in one form or the other. The comments above seem to confirm this. You might otherwise not notice that your app is being deployed twice since only one of those instances will actually serve requests... Juergen |
Salano commented The job context is loaded twice. I am not sure what's causing this. I do see app deployed once only. |
Salano commented App is deployed only once, but contexts are loaded twice. Not sure what's causing this |
Juergen Hoeller commented This issue was just about double execution when the context was only loaded once. If your context is being loaded twice, it's not really a scheduling bug; you're rather initializing the context twice in some place - a different issue. Juergen |
@spring-issuemaster : Hi, This problem still does exist job runs twice. 2020-11-16 17:00:12.150 INFO 7754 --- [ main] com.concretepage.Main : Starting Main on kalees-desktop with PID 7754 (/home/kalees/Downloads/spring-batch-demo/target/classes started by kalees in /home/kalees/Downloads/spring-batch-demo) |
Matt Young opened SPR-6859 and commented
I posted this on the forum, but with no response... I wondered if you all could look into it.
I was stubbing out some code that I'd like to run on a schedule, and just to test it, I have it running once every 60 seconds. I noticed that in my logging, the method seems to be executed twice. To make sure it wasn't a logging issue, I put a static int counter into the class and have it increment on each call. So I can confirm the method is actually being run twice.
I suspect that this is something like doing a context:component-scan twice-over and resulting in double execution, but can't seem to locate the problem. I thought I would check first if there were any pointers.
The class
Code:
/**
Date: Feb 15, 2010
Time: 10:54:36 AM
*/
@Component
public class DataHarvestingServiceImpl implements DataHarvestingService {
private static final Logger logger = Logger.getLogger(DataHarvestingServiceImpl.class);
private static int count = 0;
@Override
// Every night at 1 AM
//
@Scheduled
(cron = "* * 1 * * ?")@Scheduled
(cron = "0 * * * * ?")public void collectSocialMediaData() {
logger.info("Starting data retrieval at " + new Date(System.currentTimeMillis()));
}
}
The configuration
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:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
</beans>
The output I see:
Code:
11316 [searchScheduler-1] INFO com.vodori.cms.feature.socialMedia.service.impl.DataHarvestingServiceImpl - Starting data retrieval at Mon Feb 15 14:56:00 CST 2010
11321 [searchScheduler-1] INFO com.vodori.cms.feature.socialMedia.service.impl.DataHarvestingServiceImpl - Finished media data retrieval at Mon Feb 15 14:56:00 CST 2010
count is 0
11321 [searchScheduler-1] INFO com.vodori.cms.feature.socialMedia.service.impl.DataHarvestingServiceImpl - Starting data retrieval at Mon Feb 15 14:56:00 CST 2010
11321 [searchScheduler-1] INFO com.vodori.cms.feature.socialMedia.service.impl.DataHarvestingServiceImpl - Finished media data retrieval at Mon Feb 15 14:56:00 CST 2010
count is 1
71318 [searchScheduler-1] INFO xxx.service.impl.DataHarvestingServiceImpl - Starting data retrieval at Mon Feb 15 14:57:00 CST 2010
71318 [searchScheduler-1] INFO xxx.service.impl.DataHarvestingServiceImpl - Finished media data retrieval at Mon Feb 15 14:57:00 CST 2010
count is 2
71318 [searchScheduler-1] INFO xxx.service.impl.DataHarvestingServiceImpl - Starting data retrieval at Mon Feb 15 14:57:00 CST 2010
71318 [searchScheduler-1] INFO xxx.service.impl.DataHarvestingServiceImpl - Finished media data retrieval at Mon Feb 15 14:57:00 CST 2010
count is 3
Any insight very much appreciated.
Affects: 3.0 GA
Reference URL: http://forum.springsource.org/showthread.php?t=84747
Issue Links:
5 votes, 21 watchers
The text was updated successfully, but these errors were encountered: