简体   繁体   English

如何在java库中处理异步响应?

[英]How to handle an asynchronous response in a java library?

I am developing a library which sends an asynchronous request to a server (in another thread). 我正在开发一个库,它向服务器发送异步请求(在另一个线程中)。

Our clients will use this library, and I am thinking of the best way how to get this handled by them. 我们的客户将使用这个库,我正在考虑如何处理它们的最佳方法。 The response might follow in seconds to a minute. 响应可能在几秒到一分钟之内。 I am doing this using the jersey-client and there already exists a listener which will be called in the other "async" thread. 我正在使用jersey-client执行此操作,并且已经存在一个将在另一个“异步”线程中调用的侦听器。

So I have a shared ressource which will be "filled" with the response by the listener, but the main-thread has to call it to retrieve the response. 所以我有一个共享的ressource,它将被侦听器“填充”响应,但是主线程必须调用它来检索响应。

Is there a better way how the client might access or be "notified" that the response has arrived? 有没有更好的方式来客户端访问或“通知”响应已到达? Something like an EventNotifier? 像EventNotifier这样的东西? (If i implement something like this, shouldn't there exist something like polling on the notifier, but this means that there might exist another thread, which has to do this). (如果我实现这样的东西,不应该在通知程序上存在类似轮询的东西,但这意味着可能存在另一个线程,必须这样做)。

I have no influence on the client who is using my library, I can only make their life easier with an appropriate design of the library-functions they call. 我对使用我的库的客户没有任何影响,我只能通过他们调用的库函数的适当设计来使他们的生活更轻松。

What is the servlet container version you are trying to use ? 您尝试使用的servlet容器版本是什么? Servlet 3.0 has builtin support for it Servlet 3.0 内置了对它的支持

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

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