Java电子邮件发送队列 - 发送尽可能多的邮件的固定线程数 [英] Java email sending queue - fixed number of threads sending as many messages as are available

查看:142
本文介绍了Java电子邮件发送队列 - 发送尽可能多的邮件的固定线程数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个消息处理应用程序(电子邮件),我想有一个传出队列。我设计的方式是有一个单例队列类,ThreadedQueueSender,由Executor服务和BlockingQueue支持。此外,javax.mail.Transport对象的线程池用于获取和释放与外发SMTP服务器的连接。



此类暴露了一个方法 add(MimeMessage),将消息添加到工作队列( BlockingQueue )。



在类的实例化时, ExecutorService 被初始化为具有固定线程数的 ThreadPoolExecutor 每个线程的 run()方法在无限循环中,只有当它检测到中断时(当 ExecutorService.shutdownNow()被调用)。



此方法使用 BlockingQueue.poll()队列直到没有阻塞可用,然后从连接池请求一个传输对象,打开连接,发送它检索的所有消息,关闭连接并返回运输对象。



这样工作,但我觉得我没有充分利用ExecutorService的固定数的为应用程序的生命运行的线程。此外,我自己管理工作队列,而不是让并发框架处理它。其他人如何实现此功能?是否最好在Runnable中包装每个传入的消息,然后执行发送逻辑?



谢谢,任何评论都感谢。



Ryan

解决方案

您应该为执行者服务应该完成的每一项工作创建任务。



例如,您可以创建一个可调用的MailSendingTask来保存MimeMessage并封装邮件发送。通过将这些MailSendingTasks提交给执行程序来将它们排队。现在你的Executor决定创建多少个线程(通过设置下层和上层线程池边界来配置)



你只需要创建2或3个类/ p>


  • 一个提供简单发送(MimeMessage msg)方法的MailService接口

  • 一个MailServiceImplementation类,并保存对配置的执行程序的引用

  • 一个类MailSenderTask实现可调用接口,该接口保存对MimeMessage对象的引用并执行邮件发送。



您甚至可以创建一个额外的服务来管理MailSenderTask可以使用的邮件套接字连接。



如果你想添加cancellation,你应该看看Future和FutureTask类


I'm writing a message processing application (email) that I want to have an outgoing queue. The way I've designed this is having a singleton queue class, ThreadedQueueSender, backed by an Executor Service and a BlockingQueue. Additionally, a thread pool of javax.mail.Transport objects is used to obtain and release connections to the outgoing SMTP server.

This class exposes a method, add(MimeMessage), that adds messages to the work queue (BlockingQueue).

At instantiation of the class, the ExecutorService is initialized to a ThreadPoolExecutor with a fixed number of threads, lets say 5. Each thread's run() method is in infinite loop that only exits when it detects interrupt (when ExecutorService.shutdownNow() is called).

This run method uses BlockingQueue.poll() to take messsages from the work queue until no more are available without blocking, then requests a Transport object from the connection pool, opens the connection, sends all the messages its retrieved, closes the connection and returns the Transport object.

This works, but I feel I am not taking full advantage of the ExecutorService by having a fixed number of threads that run for the life of the application. Also, I am managing the work queue myself instead of letting the concurrency frameworks handle it. How would others implement this functionality? Is it better to wrap each incoming message in a Runnable, then execute the sending logic?

Thank you, any comments are appreciated.

Ryan

解决方案

You should create tasks for every piece of work that should be done by your executor service.

For example you could create a callable "MailSendingTask" that holds the MimeMessage and wraps the mail sending. Queue these MailSendingTasks by submitting them to your executor. Now your Executor decides how many threads will be created (Config it by setting lower and upper thread pool bounds)

You only need to create 2 or 3 classes/interfaces

  • one MailService Interface that provides a simple send(MimeMessage msg) method
  • one MailServiceImplementation Class that implements MailService and holds a reference to a configured executor
  • one class MailSenderTask implementing the callable interface that holds a reference to the MimeMessage object and which does the mail sending.

You could even go futher by creating an extra service that manages the mail socket connections which could be used by the MailSenderTask.

If you want to add "cancelation" you should look at the classes Future and FutureTask

这篇关于Java电子邮件发送队列 - 发送尽可能多的邮件的固定线程数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆