简体   繁体   English

使用javax.mail加载电子邮件

[英]Load email using javax.mail

I am using java.mail jar file version 1.4.7 我正在使用java.mail jar文件版本1.4.7

I created a Window's scheduler to load emails from email server. 我创建了一个Window的调度程序,以从电子邮件服务器加载电子邮件。 This scheduler is run every 7 minutes. 该调度程序每7分钟运行一次。 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. 这可以正常工作,但是如果前一个实例花费了7分钟以上的时间来加载电子邮件,则另一个新实例可能会加载相同的电子邮件,因为第一个实例尚未运行代码来删除电子邮件。

I think this is a concurrency issue. 我认为这是一个并发问题。

I have tried a few solutions: 我尝试了一些解决方案:

  1. Add the synchronized keyword to the method. synchronized关键字添加到方法中。
  2. Add a checkbox named isrun . 添加一个名为isrun的复选框。 Before any instance run the method, it need to verify whether isrun == false . 在任何实例运行该方法之前,它需要验证isrun == false If false, then the system will run the method; 如果为false,则系统将运行该方法;否则为false。 if true, then it will bypass all of the code. 如果为true,则它将绕过所有代码。 After the instance finishes running the method, it will update the isrun checkbox to false again. 实例完成方法运行后,它将再次将isrun复选框更新为false。
  3. Increase the length of the time of window's scheduler. 增加窗口调度程序的时间长度。 Run this method every 15 minutes instead of 7 minutes. 每15分钟而不是7分钟运行一次此方法。
  4. 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? 您对如何解决这个问题有任何想法吗?

I presume that the Windows Scheduler event fires off a new instance of the Java application. 我认为Windows Scheduler事件会触发Java应用程序的新实例。 In that case you need to use some kind of external locking. 在这种情况下,您需要使用某种外部锁定。 Perhaps creating a lock file. 也许创建一个锁定文件。 The process checks if a locking file exists and if not creates one and then continues. 该过程将检查锁定文件是否存在,如果不存在,则继续操作。 A second process would detect this file and have to wait or abort. 第二个过程将检测到该文件,并且必须等待或中止。 Once the process is finished it removes the lock file. 该过程完成后,将删除锁定文件。 You need to be careful of how the file locking is implemented so that processes. 您需要注意如何实现文件锁定以便进行处理。 You have to implement the locking carefully though so that multiple processes don't just check for a file (at the same time), see it's not there and then both try to create it. 但是,您必须仔细实现锁定,以便多个进程不只是同时检查一个文件,查看它是否不存在,然后都尝试创建该文件。 This shouldn't be a problem as your processes won't be starting at the same time but should be considered. 这不应该是一个问题,因为您的流程不会同时启动,但应予以考虑。 Examples shown here: 此处显示的示例:

How can I lock a file using java (if possible) 如何使用Java锁定文件(如果可能)

Another solution would be to have the Java application running constantly and every seven minutes run the update. 另一个解决方案是让Java应用程序持续运行,并每隔七分钟运行一次更新。 This way the synchronization can be performed internally. 这样,可以在内部执行同步。 The main part of the application would sit in a loop and wait for seven minutes to pass and then run your update routine (method). 该应用程序的主要部分将处于循环状态,等待七分钟,然后运行更新例程(方法)。 This would required some kind of daemon thread running. 这将需要某种守护程序线程运行。 Apache has this but it takes a bit of work to get it up and running. Apache拥有此功能,但需要花费一些工作才能使其启动并运行。

http://commons.apache.org/proper/commons-daemon/ http://commons.apache.org/proper/commons-daemon/

There is also an option in the scheduler to not run in parallel. 调度程序中还有一个选项可以不并行运行。 This is probably the easiest option. 这可能是最简单的选择。 Look in the Settings tab of the task properties at the bottom (Win 8). 在底部的任务属性的“设置”选项卡中查找(Win 8)。 You can tell it not to start or to wait if there is an instance already running. 您可以告诉它不要启动,或者等待实例正在运行。

You can have a look at Quartz scheduler. 您可以看一下Quartz调度程序。 It's an open source job scheduling framework, that let you scheduler a task with great flexibility. 这是一个开放源代码的工作计划框架,使您可以灵活地计划任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM