简体   繁体   English

在EJB环境中是否有类似.wait()和.notify()的东西?

[英]Is there something that behaves like .wait() and .notify() in EJB environment?

I know i must not tinker with threading in EJB Containers, therefore i do not know how to do the following thing in an EJB Environment the right way: 我知道我不能修改EJB容器中的线程,因此我不知道如何在EJB环境中以正确的方式执行以下操作:

Involved are : 涉及的是

  • Stateless Session Bean "Client" 无状态会话Bean“客户端”
  • Stateless Session Bean "Server" 无状态会话Bean“服务器”
  • MessageQueue "Queue" MessageQueue“队列”
  • Message Driven Bean "Mdb" which processes messages from "Queue" 消息驱动Bean“Mdb”处理来自“队列”的消息
  • n Stateless Session Beans W1 through Wn n无状态会话Bean W1到Wn

Scenario is : 场景是

Client calls a Method of Server , which in turn sends several Messages to Queue . 客户端调用服务器方法,然后将几个消息发送到队列 After that, Server does some other stuff. 之后, Server会做其他一些事情。 In the meantime, Mdb consumes a message, calls Wi which does some rather long calculation, and gets the result. 在此期间, Mdb消耗一条消息,调用Wi进行一些相当长的计算,并得到结果。 Now Mdb gives the result to Server . 现在Mdb将结果提供给Server When Server got all the "results" for every message it sent, it does some more calculations with the results from the W s, and returns that result to Client . 服务器获取它发送的每条消息的所有“结果”时,它会使用W s的结果进行更多计算,并将结果返回给客户端

My Problem : 我的问题

In Java SE I would simply do .wait() to have Server wait for the results of the W s, after Server did the work after sending the messages. 在Java SE,我只想做.wait()来让服务器等待在W S的结果, 服务器没有发送消息后,下班后。 Then mdb would .notify() when it has set the results. 然后mdb在设置结果时会.notify()。 Since I must not tinker with threading in EJB Containers, as the specification states, I am lost cause I did not find any appropriate way to achieve the same behavior in EJB Environment. 因为我不能修改EJB容器中的线程,正如规范所述,我迷失了因为我找不到任何适当的方法来在EJB环境中实现相同的行为。

Any help on that problem would really be appreciated, thanks in advance. 对此问题的任何帮助都将非常感谢,提前感谢。

PS: I am working with JBoss 5.1.0, in case there are any vendor-specific measures to tackle that problem. PS:我正在使用JBoss 5.1.0,以防有任何特定于供应商的措施来解决这个问题。

The appropriate solution for this is the "request/response" pattern for messaging. 对此的适当解决方案是用于消息传递的“请求/响应”模式。 In a nutshell, you can do "synchronous" operations via messaging systems by sending a message and waiting for a response message (all of this is legal in the J2EE world). 简而言之,您可以通过发送消息并等待响应消息来通过消息传递系统执行“同步”操作(所有这些在J2EE世界中都是合法的)。 there are a variety of ways you can achieve this in practice, but the general idea is that you send the request messages with some sort of unique identifier, then wait for response messages using a message filter set for the request id(s) that you sent (this is generally what the "correlationId" field is used for). 有多种方法可以在实践中实现这一点,但一般的想法是你发送带有某种唯一标识符的请求消息,然后使用为你的请求ID设置的消息过滤器等待响应消息已发送(这通常是“correlationId”字段用于)。 the MDB would get the request messages, process them, and send the response messages using the specified unique identifiers from the request messages. MDB将获取请求消息,处理它们,并使用请求消息中指定的唯一标识符发送响应消息。 you can do all of this with one queue, or you can use separate request/response queues, or you can do crazy things like creating "temporary" response queues (per-request). 您可以使用一个队列执行所有这些操作,或者您可以使用单独的请求/响应队列,或者您可以执行创建“临时”响应队列 (每个请求)等疯狂的操作。 you can tell the MDB where to send the request messages using the Message.setJMSReplyTo method. 您可以使用Message.setJMSReplyTo方法告诉M​​DB将请求消息发送到何处。

The general pattern is: 一般模式是:

  1. client calls server 客户端呼叫服务器
  2. server: 服务器:
    1. creates messages, sets correlationId and replyTo 创建消息,设置correlationId和replyTo
    2. creates QueueSender, sends messages 创建QueueSender,发送消息
  3. mdb (repeat for each message): mdb(对每条消息重复):
    1. receives message 收到消息
    2. processes message 处理消息
    3. sends response message with correlationId 使用correlationId发送响应消息
  4. server: 服务器:
    1. creates message filter with correlationId 使用correlationId创建消息过滤器
    2. creates QueueReceiver with message selector 使用消息选择器创建QueueReceiver
    3. calls receive() until all messages are received and processed (or times out) 调用receive()直到收到并处理所有消息(或超时)
    4. does final processing, responds to client 做最后的处理,回应客户

(Obviously, the server proceeds directly from step 2 to step 4, i just wrote it this way to highlight the control flow.) (显然,服务器直接从第2步到第4步,我只是用这种方式写它来突出控制流程。)

what object acts as the Server that the MDB returns all the messages? 什么对象充当MDB返回所有消息的服务器? it probably needs some kind of CountDownLatch to wait for, in the size of number of messages (which is changed by the MDB until reached zero). 它可能需要某种CountDownLatch来等待,消息数量的大小(由MDB改变直到达到零)。 When it become zero, it will wake up can run the code that return to the client. 当它变为零时,它将唤醒可以运行返回客户端的代码。 See the API doc for CountDownLatch. 请参阅CountDownLatch的API文档。

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

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