简体   繁体   English

JAVA EE中的并发处理

[英]Concurrent processing in JAVA EE

I'm working in a Java EE application and I want that some WebServices are executed in parallel. 我正在使用Java EE应用程序,并且希望某些WebServices可以并行执行。

I would like to know the pros and cons of 2 different approaches: 我想知道2种不同方法的利弊:

  1. Use JMS queues and MDBs , so each message I put in the queue would be executed in parallel. 使用JMS队列和MDB ,因此我放入队列中的每条消息将并行执行。 This way the application part that put the message into the queue would have a while, that waits the MDBs to response in a RS Queue. 这样,将消息放入队列的应用程序部分将有一段时间,等待MDB在RS队列中响应。
  2. Use the java concurrent API (Future / Callable) . 使用Java并发API(Future / Callable)

ADDED 添加

This is what the application needs to do: 这是应用程序需要执行的操作:

在此处输入图片说明

The application already does it via an MDB, but I was thinking about a refactoring. 该应用程序已经通过MDB完成此操作,但是我正在考虑进行重构。

TODAY'S SCENARIO: 今天的场景:

//CALLER CLASS
FOREACH INTEGRATION
    PUT MESSAGE INTO A QUEUE AND STORE AN ARRAY OF CORRELATION_IDs
END

THREAD.SLEEP(X) // SOMETIME FOR INTEGRATION TO FINISH

WHILE (true){
    GET RESPONSE FROM THE RESPONSE QUEUE FOR EACH INTEGRATION USING THE CORRELATION PREVIOUSLY STORED
}


//MDB CLASS
HAS A HUGE SWITCH CASE THAT PROCESS EACH INTEGRATION

RETURN THE RESULT INTO THE RESPONSE QUEUE;

Questions: 问题:

  1. Is it ok to use the concurrent API in java? 在Java中使用并发API可以吗? In my opinion using the concurrent API will eliminate a layer of failure ( JMS ). 我认为使用并发API将消除故障层( JMS )。
  2. My deployment environment is Websphere . 我的部署环境是Websphere Is it a good practice to create your own threads with the concurrent java API. 用并发Java API创建自己的线程是一种好习惯。

Thanks in advance 提前致谢

jms pros: you can have persistence, you can connect to existing infrastructure jms cons: seems to heavy to be used only as a dispatcher jms优点:可以保持持久性,可以连接到现有的基础结构jms缺点:看起来很沉重,只能用作调度程序

manual concurrency cons: well, it's manual. 手动并发缺点:是的,这是手动的。 and parallel programming is difficult. 并行编程很困难。 some webservers (especially clouds) may forbid to create your own threads 某些Web服务器(尤其是云服务器)可能禁止创建自己的线程

not sure what exactly you want to do but webserver by default processes requests in parallel, so maybe you don't need anything else? 不确定您到底要做什么,但是默认情况下Web服务器并行处理请求,所以也许您不需要其他任何东西了?

Whatever solution you go with, you will eventually need to cope with a burst of traffic. 无论采用哪种解决方案,最终都将需要应对大量流量。 The JMS/MDB the burst is controlled by the queue effectively. 突发的JMS / MDB由队列有效地控制。 Also a point to consider is that the queue can be made persistence, so it will survive a server restart. 还要考虑的一点是,可以使队列具有持久性,因此它将在服务器重新启动后幸免。 Also a queue can be distributed across many servers, giving you horizontal scalability. 队列也可以分布在许多服务器上,从而为您提供水平可伸缩性。

The thread approach is of course quicker to develop, test and deploy. 当然,线程方法可以更快地进行开发,测试和部署。 However, I would consider using a BlockingQueue so that your threads do not run amock. 但是,我会考虑使用BlockingQueue,以便您的线程不会运行amock。

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

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