I am using java.mail jar file version 1.4.7
I created a Window's scheduler to load emails from email server. This scheduler is run every 7 minutes. At the end of the code, for those emails that were loaded, system will delete them from the email server.
This supposes to work but if the previous instance takes more than 7 minutes to load the emails, another new instance could load the same email because the first instance have not run the code to delete the email.
I think this is a concurrency issue.
I have tried a few solutions:
- Add the
synchronized
keyword to the method. - Add a checkbox named
isrun
. Before any instance run the method, it need to verify whetherisrun == false
. If false, then the system will run the method; if true, then it will bypass all of the code. After the instance finishes running the method, it will update theisrun
checkbox to false again. - Increase the length of the time of window's scheduler. Run this method every 15 minutes instead of 7 minutes.
- Change the method from non-static to static.
These methods do help but they are not workable if the size of the emails are too large. (Possible they have a lot of attachments.)
Do you have any idea on how to solve this?