Java应用程序与jax-ws Web服务之间的线程间通信 [英] inter-thread communication between java application and jax-ws web service

查看:90
本文介绍了Java应用程序与jax-ws Web服务之间的线程间通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的jax-ws Web服务(在被客户端请求击中之后),将请求发送到后端旧平台(通过套接字),然后等待20秒以获取响应.

my jax-ws web service (after it gets hit by the client request), sends a request to backend legacy platform (through a socket) and then waits for 20 seconds for the response.

在发出请求之前,它将使用其唯一的交易号更新表.

Before it makes that request it updates a table with its unique transaction number.

一个单独的侦听器线程(独立的Java应用程序)正在该套接字上等待响应,每个响应中都将具有事务号,以标识与之关联的请求(先前发送).

A separate listener thread (standalone java application) is waiting on that socket for the responses, each response will have the transaction number in it to identify which request (previously sent) its associated with.

这个独立的Java应用程序如何与正在Web服务中等待其响应已到达的线程通信,并且它可以继续进行处理.

how can this standalone java application communicate to the thread waiting in the web service that its response has come and it can continue its processing.

当前,我认为侦听器线程可以更新表(基于事务号)以指示响应已到达. Web服务线程可以在数据库中继续轮询(每2秒检查一次)响应是否到达.

Currently i am thinking the listener thread can update the table (based on the transaction number) to indicate the response has arrived. And the web service thread can keep polling (check every 2 seconds) in the database whether the response has arrived or not.

在这种情况下,我正在寻找一些推送通知,而不是轮询.响应到达时如何通知我的Web服务线程.

I am looking for some push notification in this case instead of polling. How can my web service thread be notified when its response has arrived.

推荐答案

假定用于服务请求并发出套接字请求的线程以及接收套接字响应的线程都在同一jvm中:
您可以使用标准的Java等待/通知模式.
http://docs.oracle.com/javase/tutorial/essential/concurrency /guardmeth.html .

Assuming the thread to service the request and make the socket request, and the thread receiving the socket response, are all in the same jvm:
You can use the standard Java wait/notify pattern.
http://docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html.

基本上,您的req/socket线程将在单例(或类似)映射中创建一个对象(锁"对象),密钥是您的唯一交易号.然后,该线程将在该对象上等待.这将阻塞该线程,直到另一个线程对该事务ID的锁定对象发出通知为止.
请注意,锁定对象可以只是一个对象.
您还可以在等待中设置超时,以避免永远等待.

Basically your req/socket thread will create an object (a 'lock' object) in a singleton (or similar) map, the key being your unique transaction number. That thread will then wait on that object. This blocks that thread until another thread does a notify on that transaction id's lock object.
Note the lock object can be just an Object.
You can also put a timeout on the wait to avoid waiting forever.

您的其他线程随后将获取该事务ID的锁对象并对其进行通知.然后,这将取消阻塞第一个线程.
如果第二个线程没有为它的事务ID找到锁对象,那么出了点问题,您将不得不记录一个错误或类似的错误.

Your other thread will then grab the lock object for the transaction id and do a notify on it. This then unblocks the first thread.
If the second thread finds no lock object for its transaction id then something has gone wrong and you'll have to log an error or similar.

由于地图是共享资源,因此您可能需要ConcurrentHashMap.否则,您可能会遇到多个线程同时更新它的问题.

Ps as the map is a shared resource you will probably need ConcurrentHashMap. Otherwise you'll probably hit issues with multiple threads updating it concurrently.

这篇关于Java应用程序与jax-ws Web服务之间的线程间通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆