java - Load email using javax.mail -
i using java.mail jar file version 1.4.7
i created window's scheduler load emails email server. scheduler run every 7 minutes. @ end of code, emails loaded, system delete them email server.
this supposes work if previous instance takes more 7 minutes load emails, new instance load same email because first instance have not run code delete email.
i think concurrency issue.
i have tried few solutions:
- add
synchronized
keyword method. - add checkbox named
isrun
. before instance run method, need verify whetherisrun == false
. if false, system run method; if true, bypass of code. after instance finishes running method, updateisrun
checkbox false again. - increase length of time of window's scheduler. run method every 15 minutes instead of 7 minutes.
- change method non-static static.
these methods not workable if size of emails large. (possible have lot of attachments.)
do have idea on how solve this?
i presume windows scheduler event fires off new instance of java application. in case need use kind of external locking. perhaps creating lock file. process checks if locking file exists , if not creates 1 , continues. second process detect file , have wait or abort. once process finished removes lock file. need careful of how file locking implemented processes. have implement locking though multiple processes don't check file (at same time), see it's not there , both try create it. shouldn't problem processes won't starting @ same time should considered. examples shown here:
how can lock file using java (if possible)
another solution have java application running , every 7 minutes run update. way synchronization can performed internally. main part of application sit in loop , wait 7 minutes pass , run update routine (method). required kind of daemon thread running. apache has takes bit of work , running.
http://commons.apache.org/proper/commons-daemon/
there option in scheduler not run in parallel. easiest option. in settings tab of task properties @ bottom (win 8). can tell not start or wait if there instance running.