简体   繁体   English

Java EE容器内的并发编程

[英]Concurrent programming inside a Java EE container

I have a few Quartz scheduled jobs that are quite IO intensive and are easily breakables into multiple tasks. 我有一些Quartz计划的工作,这些工作非常耗费IO,并且很容易分解为多个任务。

The jobs are instantiated and scheduled inside the tomcat web container thanks to Spring. 借助Spring,作业可以在tomcat Web容器内实例化并计划。

Is it all right if I use the java.util.concurrent API within the Job class and inside the Java EE container ? 如果我在Job类内和Java EE容器内使用java.util.concurrent API,可以吗?

Can I share the logical processors with Tomcat by using and sizing a FixedThreadPool and giving away a few cores (like two in this example) ? 是否可以通过使用和调整FixedThreadPool的大小并放弃几个内核(例如本示例中的两个内核)来与Tomcat共享逻辑处理器?

int numberOfCores = Runtime.getRuntime().availableProcessors();
final int poolSize = numberOfCores - 2 // Give away Two slots for TOMCAT
final ExecutorService executorPool = Executors.newFixedThreadPool(poolSize);

Yeah you can do that. 是的,你可以做到。 But Before putting your job inside tomcat. 但是在把你的工作放入tomcat之前。 Note the following. 请注意以下几点。

Are you having a web app in tomcat? 您在tomcat中有一个Web应用程序吗? if yes ,Is your web app a highly active high load portal ? 如果是,您的Web应用程序是一个活跃的高负载门户吗?
If yes, your thread jobs will take away valuable processing from the tomcat server 如果是的话,您的线程作业将剥夺来自tomcat服务器的宝贵处理

Is the Job done by thread jobs tightly coupled or require tomcat ? 作业是由线程作业紧密结合还是需要tomcat?
If your jobs are really in depended . 如果您的工作真的很靠谱。 you should better create a separate batch server and use it. 您最好创建一个单独的批处理服务器并使用它。 You can look to spring-batch for an implementation. 您可以期待spring-batch实现。

Maybe, carefully. 也许,小心点。

Be aware that no thread (other than the original one - let's call that the web-app thread) will be able to reliably interact with the container. 请注意,没有任何线程(原始线程除外-我们将其称为web-app线程)将能够可靠地与容器进行交互。 In general, using threads in a Java EE runtime is discouraged. 通常,不建议在Java EE运行时中使用线程。 It's not unheard of though. 这不是闻所未闻的。

Tomcat Options: Tomcat选项:

  1. Java EE has a WorkManager API . Java EE具有WorkManager API There is at least one implementation of it for tomcat . tomcat至少有一种实现 I can't speak to how well it works. 我无法说说效果如何。 This article talks more about it . 本文将对此进行更多讨论

  2. Start your own threads. 启动您自己的线程。 A model for how to manage things might be the Swing Event Dispatch Loop with SwingUtilities.invokeLater(Runnable) ; 一个用于管理事物的模型可能是带有SwingUtilities.invokeLater(Runnable)的Swing事件调度循环。 in your case have worker threads submit work-to-be-done-in-the-container back to the container-safe web-app thread. 在您的情况下,请工作线程将要在容器中完成的工作提交回容器安全的Web应用线程。 That thread would be running aa handle-work loop while waiting for worker-threads to complete. 该线程将在等待工作线程完成时运行处理工作循环。

  3. Launch worker requests back into the tomcat server: your web-app now acting as a client (web service?). 启动工作程序请求返回到tomcat服务器:您的Web应用程序现在充当客户端(Web服务?)。 Such a model would scale nicely to off-load the work, as AkhilDev suggested, to other servers. 这样的模型可以很好地扩展,以按照AkhilDev的建议将工作分担给其他服务器。

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

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