简体   繁体   English

在Java Web应用程序中循环调用Web服务

[英]Calling a web-service in a loop in Java web application

We have an external SOAP based web-service which provides information regarding customer's gift card balance when presented with an Id. 我们有一个基于SOAP的外部Web服务,当提供ID时,该服务提供有关客户的礼品卡余额的信息。 This Id is stored in our database. 此ID存储在我们的数据库中。

The requirement is to find out the balance for all such customers who has this Id flagged and then send them an email. 要求是找出所有带有此ID标记的此类客户的余额,然后向他们发送电子邮件。 This logic is supposed to be run as a scheduled job once every alternate day. 该逻辑应该每隔一天作为计划作业运行一次。

When we queried the DB, we found out that there are more than 5000 such customers who have this Id flagged. 当查询数据库时,我们发现有超过5000个带有此ID标记的此类客户。 Unfortunately, the web-service will NOT accept a list of Ids, and can only give information about a single customer in one network call. 不幸的是,该Web服务将不接受ID列表,并且只能在一个网络呼叫中提供有关单个客户的信息。

Now, our doubt is whether it will be a good idea to loop through 5000 Ids and call the web-service in this loop as many times. 现在,我们的疑问是,遍历5000个Id并在此循环中多次调用Web服务是否是一个好主意。 As a test run, when we called the web-service for 500 Ids, it completed in 3.7 minutes and 1000 Ids 7.25 minutes. 作为测试运行,当我们调用500 Ids的Web服务时,它在3.7分钟内完成,而1000 Ids在7.25分钟内完成。 By this measure, we can guesstimate that for 5000 Ids, it should roughly take 40 minutes. 通过这种方法,我们可以估算出,对于5000个ID,大约需要40分钟。

Our web-application is JavaEE 6 stack and DB is Oracle. 我们的Web应用程序是JavaEE 6堆栈,数据库是Oracle。

Is there a better way to do this ? 有一个更好的方法吗 ? Any suggestions are welcome. 欢迎任何建议。 Thanks. 谢谢。

Unfortunately, the web-service will NOT accept a list of Ids, and can only give information about a single customer in one network call. 不幸的是,该Web服务将不接受ID列表,并且只能在一个网络呼叫中提供有关单个客户的信息。

You should really take contact with the service provider to get a suitable solution. 您确实应该与服务提供商联系以获得合适的解决方案。

As workaround, if making multiple concurrent invocations is allowed by the SOAP WS, you could make multiple invocations of the WS by multiple Threads. 解决方法是,如果SOAP WS允许进行多个并发调用,则可以通过多个线程对WS进行多次调用。
To achieve that, create a Runnable or a Callable implementation that performs the invocation to the WS with a specific id. 为此,创建一个RunnableCallable实现,该实现执行具有特定ID的WS的调用。

For example to perform concurrently 10 invocations of the WS, with Callable and ExecutorService, you could do something as : 例如,要使用Callable和ExecutorService并发执行WS的10次调用,您可以执行以下操作:

MyWs myWs = ...; // web service stub
List<Long> ids = ...; // ids to search

List<Callable<Double>> callables = ids.stream()
                                      .map(id -> (Callable<Double>) () -> myWs.getBalance(id))
                                      .collect(Collectors.toList());

ExecutorService executorService = Executors.newFixedThreadPool(10)
List<Future<Double>> balanceFutures = executorService.invokeAll(callables);

Of course adjust the number of invocations according to the CPU of the machine that runs the JVM. 当然,可以根据运行JVM的计算机的CPU来调整调用次数。

If you could write a deterministic function that takes the input of the customer id and that gives you a number from 0 to 47 representing the number of hours in the 2 day cycle of sending these email alerts, you could shard the email sending and convert it to a job that runs every hour. 如果您可以编写一个确定性函数来接受客户ID的输入,并为您提供一个介于0到47之间的数字(代表发送这些电子邮件警报的2天周期中的小时数),则可以将电子邮件分片并进行转换到每小时运行的工作。

I know that is changing the requirements a bit, but there isn't much difference between sending a batch every 2 days and a smaller batch every hour. 我知道这会稍微改变需求,但是每两天发送一次批次与每小时发送一次较小的批次之间没有太大区别。 Each customer who remains on your list would continue to get emails every 2 days. 仍留在您列表中的每个客户将继续每2天收到一封电子邮件。

Another possibility is to send queries to the web service in a multi-threaded manner. 另一种可能性是以多线程方式将查询发送到Web服务。
The web service provider should really think about changing their interface. Web服务提供商应该真正考虑更改其界面。

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

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