简体   繁体   English

如何在Java EE应用程序中并行发送与异步服务的请求并收集响应?

[英]How to send requests parallel to asynchronous services and collect the responses in a Java EE application?

I develop an application that at some point starts to aggregate an infomation from a bunch of services. 我开发了一个应用程序,该应用程序有时会开始聚合来自一堆服务的信息。 Some of that services are called via SOAP interfaces synchronously and some of them works asynchronosly - I have to send a request to JMS queue Q1 and get an answer to Q2 at some point. 其中一些服务是通过SOAP接口同步调用的,而某些服务则是异步运行的-我必须向JMS队列Q1发送请求,并在某个时候获得对Q2的答复。

The problem is that the app sends requests in one thread and the responses a processed using MDBs (Message-Driven Bean). 问题在于该应用程序在一个线程中发送请求,并使用MDB(消息驱动的Bean)对响应进行处理。 The solution from the top of my head is to store already aggregated responses in some shared container (like ConcurrentHashMap) with some correlationId. 从头开始的解决方案是将已聚合的响应存储在带有某些correlationId的某个共享容器中(例如ConcurrentHashMap)。 So when an MDB gets a response it looks through the shared container and adds response to the corresponding record. 因此,当MDB获得响应时,它会查看共享容器,并将响应添加到相应的记录中。

The app runs on WildFly AS in domain HA mode. 该应用程序在域HA模式下在WildFly AS上运行。

  1. Are there some problems that I can run into with this approach? 使用这种方法是否会遇到一些问题? Like the container will be instantiated one for each node in cluster. 就像容器将为集群中的每个节点实例化一个一样。
  2. Or I can accidently process so many requests that I will store so many responses that I will get OutOfMemoryError? 还是我可能不小心处理了这么多请求,以至于我将存储太多响应,从而导致OutOfMemoryError?
  3. What are the best approaches for this kind of problems? 解决此类问题的最佳方法是什么?

Let me answer your questions: 让我回答您的问题:

  1. A response to a JMS service call could arrive anytime (quite soon : the destination server down, the operator take a rest, etc). 对JMS服务调用的响应可以随时到达(很快就会消失:目标服务器关闭,操作员休息等等)。 So you should store the requests in a database during the data aggregation. 因此,您应该在数据聚合期间将请求存储在数据库中。
  2. Performance issues always could happen when you serve many requests parallel. 当您并行处理多个请求时,总是会发生性能问题。 And if you have asynchronous answers you can store many-many hashes for a long time (or with SFSB activate/passivate) till the last answer arrives. 而且,如果您有异步答案,则可以长时间存储许多哈希值(或使用SFSB激活/钝化),直到最后一个答案到达为止。 The first answer (partly) solve this problem as well, because it stores most of the data in the db and takes just the current ones in the memory. 第一个答案(部分)也解决了这个问题,因为它将大多数数据存储在db中,而仅将当前数据存储在内存中。 And more robust. 而且更强大。 The persistent data live survive a server crash/shutdown. 持久数据可以在服务器崩溃/关闭后幸存下来。
  3. When you need the data, create a db entry for all and send out the requests with its PK in the header. 当您需要数据时,为所有数据库创建一个数据库条目,并在标头中发送请求及其PK。 When an answer arrives, its header contains the same PK for the identification. 答案到达时,其标题包含用于标识的相同PK。 The MDBs are best way to receive them. MDB是接收它们的最佳方法。 But use them just to receive the messages. 但是使用它们只是为了接收消息。 Process its contents by EJBs. 通过EJB处理其内容。 Delegate the message contents synchronously to the EJB(s) and acknowledge them according to the EJB answers. 将消息内容同步委派给EJB,并根据EJB答复确认它们。 At the very end of the EJB processing fetch the IDs of the unprocessed requests belong to the current aggregation. 在EJB处理的最后,未处理请求的ID属于当前聚合。 If there is no one, (remove the query entries from the db table and) call the appropriate EJB (via MDB?) to proceed the work with the fulfilled data needs. 如果没有人,(从db表中删除查询条目,然后)调用相应的EJB(通过MDB?)以处理满足数据需求的工作。

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

相关问题 如何在Java中发送异步请求并收集响应 - How to send Asynchronous Requests in Java and Collect Responses 如何发送并行GET请求并等待结果响应? - How to send parallel GET requests and wait for result responses? 如何使用Unirest并行发送多个异步请求 - How to send multiple asynchronous requests in parallel using Unirest 如何向不同的Web服务发送多个异步请求? - How to send multiple asynchronous requests to different web services? 如何在Java中发送并行HTTP POST请求 - How to send parallel HTTP POST requests in Java 如何发送并行POST请求Java - How to send parallel POST requests Java 如何在异步httpclient java 11中将多个异步get请求的响应写入单个文件? - How to write responses of multiple asynchronous get requests to a single file in asynchronous httpclient java 11? 如何将异步HTTP请求响应合并到一个? - How to combine asynchronous HTTP requests responses to one? 异步小部件JSP foreach(Java EE应用程序) - asynchronous widget JSP foreach (Java EE application) 如何通过协作通道适配器发送异步请求/响应而不关联数据 - How to send asynchronous requests/responses through collaborating Channel Adapters without correlating data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM