在 Java 中调用外部 Web 服务的最有效方法?

发布于 2024-08-28 02:29:11 字数 360 浏览 10 评论 0原文

在我们的一个应用程序中,我们需要调用 Yahoo Soap Web 服务来获取天气和其他相关信息。

我使用了 axis1.4 中的 wsdl2java 工具并生成了所需的存根并编写了一个客户端。我使用jsp的use bean来包含客户端bean并调用客户端中定义的方法,这些方法依次调用yahoo webservice。

现在的问题是:当用户调用jsp时,Web服务的响应时间差异很大,例如一个用户花费了不到10秒,而同一网络中的另一个用户花费了超过一分钟。

我只是想知道即使 jsps 是多线程的,Axis1.4 是否也会对请求进行排队。

最后有没有一种有效的方法来调用网络服务(雅虎天气)。通常我会同时收到来自用户的大约 200 个请求。

In one of our applications we need to call the Yahoo Soap Webservice to Get Weather and other related info.

I used the wsdl2java tool from axis1.4 and generated th required stubs and wrote a client. I use jsp's use bean to include the client bean and call methods defined in the client which call the yahoo webservice inturn.

Now the problem: When users make calls to the jsp the response time of the webservice differs greatly, like for one user it took less then 10 seconds and the other in the same network took more than a minute.

I was just wondering if Axis1.4 queues the requests even though the jsps are multithreaded.

And finally is there an efficient way of calling the webservice(Yahoo weather). Typically i get around 200 simultaneous requests from my users.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萧瑟寒风 2024-09-04 02:29:11

为什么不安排一个线程每分钟左右获取一次天气情况,并将其公开给 JSP,而不是让每个 JSP 获取自己的天气报告?

这对于您和 Yahoo 来说都更加高效,而且 JSP 只需要查找本地对象(几乎是即时的),而不需要连接到 Web 服务。

编辑

此答案的评论中的一些新要求提出了选择解决方案的不同方式。

Web 服务似乎不仅需要天气(天气不仅不会经常变化,而且对于每个用户来说都是相同的),还需要其他数据(例如航班数据)。

飞行数据检索的要求与天气数据的要求有很大不同。所以我认为你应该定义几种类型的(远程)数据并选择不同的解决方案
对于每个类别。

作为需求的基础,我会使用一些简单的东西:

  • 用户立即喜欢他们的信息,他们不喜欢等待
  • Web 服务器上存储的数据量是有限的
  • 远程 Web 服务有某种 EULA,并且可能对 200 个并发不满意同一来源(您)对同一数据的请求

最好通过在本地保存数据来实现对用户的快速数据访问,无论是暂时的(保存在 bean 中)还是持久的(本地数据库)。这可以通过定期从远程源请求数据并使用 JSP 中的缓存数据来完成。这也能让你清楚第三点。

Web 服务上存储的数据量有限意味着并非所有内容都可以缓存。每个用户不同的数据或在短时间内可能变化的大型数据集无法轻易缓存。每分钟左右加载美国所有机场所有航班的数据并不是一个好主意。必要时运行特定的 Web 服务查询可以更好地满足此类请求。

现在的技巧是确定缓存数据何时可行。如果可行,则执行此操作,否则在后台运行 Web 服务查询。这可以通过now呈现 JSP 并在后台启动 Web 服务查询来完成。 JSP 可以有一个 AJAX 脚本,该脚本查询您的 Web 服务器数据是否已准备好,并在准备好后将该数据插入页面中。

Why don't you schedule one thread to get the weather every minute or so, and expose that to the JSP, in stead of letting each JSP get its own weather report?

That's a lot more efficient for both you and Yahoo, and JSP's only need to lookup a local object (almost instantaneous) in stead of connecting to a web service.

EDIT

Some new requirements in the comments of this answer suggest a different way of choosing solutions.

It seems that not only weather, which not only doesn't change that often but is also the same for every user, is requested by web service but also other data like flight data.

The requirements for flight data retrieval are very much different than for weather data. So I think you should define a few types of (remote) data and choose a different solution
for each category.

As basis for the requirements I'd use something simple:

  • Users like their information promptly, they do not like waiting
  • The amount of data stored on the web server is finite
  • Remote web services have an EULA of sorts and are probably not happy with 200 concurrent requests of the same data by the same source (you)

Fast data access to users is best achieved by having the data locally, be it transient (kept in a bean) or persistent (a local database). That can be done by periodically requesting data from the remote source, and using the cached data in the JSP. That would also keep you in the clear with the third point.

A finite amount of data stored on the web service means that not everything can be cached. Data which differs per user, or large data sets which can vary over small periods of time, cannot readily be cached. It's not really a good idea to load data on all flights of all airports in the US every minute or so. That kind of requests would be better served by running a specific web service query when necessary.

The trick is now to identify when caching data is feasible. If it is feasible, do that, otherwise run the web service query in the background. That can be done by presenting the JSP now and starting the web service query in the background. The JSP can have an AJAX script which queries your web server whether the data is ready, and insert that data in the page when ready.

骷髅 2024-09-04 02:29:11

我会使用 Google 工具来监控对网络服务的调用需要多长时间。

这里发生了几件事:

  1. 将 Java bean 映射到 XML 请求。
  2. 向 Web 服务发送 XML 请求。
  3. 在 Web 服务端解组 XML 请求。
  4. Web 服务处理请求
  5. Web 服务编组 XML 响应
  6. Web 服务将 XML 响应发送到 Java 客户端
  7. 解组 XML 响应并在客户端上显示。

您无法看到雅虎网络服务的内部情况,但请详细分析您在客户端可以看到的内容,以了解时间花在哪里。

还要检查内存。如果 Axis 正在生成 .class 文件,则可能正在消耗您的 Perm 空间。 Visual VM 可与 JDK 一起使用。将其附加到客户端上的 PID,以查看应用服务器内存中发生的情况。

也许这将是 AJAX 调用的好地方。如果您可以在用户做其他事情时在后台获取天气信息,这将是一个很好的解决方案。

I'd use Google tools to monitor how long the call to the web service is taking.

There are several things going on here:

  1. Map Java beans to XML request.
  2. Send XML request to web service.
  3. Unmarshall XML request on web service side.
  4. Web service processes request
  5. Web service marshalles XML response
  6. Web service sends XML response to Java client
  7. Unmarshall XML response and display on client.

You can't see inside the Yahoo web service, but do break out what you can see on the client side to see where the time is spent.

Check memory as well. If Axis is generating .class files, maybe your perm space is being consumed. Visual VM is available to you with the JDK. Attach it to the PID on your client to see what's going on in memory on your app server.

Maybe this would be a good place for an AJAX call. This will be a good solution if you can get the weather in the background while users are doing other things.

临走之时 2024-09-04 02:29:11

我会推荐本地缓存和数据池。不是对相似/相同位置发送 200 个单独的请求,而是运行一个后台线程,该线程仅提取用户感兴趣的位置的天气并将其缓存在本地,该缓存每分钟左右更新一次。当用户请求他们的个人偏好时,请求会命中缓存,如果位置是新的或缓存中的数据已过时,则会重新获取。这样,用户将获得更加无缝的体验,并且您不会遇到雅虎限制并被拒绝服务。

I would recommend local caching and data pooling. Instead of sending out 200 separate requests for similar/same locations run a background thread which pulls the weather for only the locations your users are interested in and caches them locally, this cache updates every minute or so. When users request their personal preferences, the requests hit the cache and refetch if the location is new or the data in the cache is stale. This way the user will have a more seamless experience and you will not hit Yahoo throttles and get denied service.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文