简体   繁体   English

如何在Java EE上下文中合理地延迟处理?

[英]How to delay processing reasonably in Java EE context?

Within a Java EE 5 environment I have the problem to ensure the existence of some data written by another part before continue processing my own data. 在Java EE 5环境中,在继续处理自己的数据之前,我要确保存在由另一部分写入的某些数据。

Historically (J2EE time), it was done by putting the data object to be processed into an internal JMS queue after waiting for eg 500ms via Thread.sleep. 从历史上(J2EE时间)开始,这是通过在等待Thread.sleep等500ms之后将要处理的数据对象放入内部JMS队列来完成的。

But this does not feel like the best way to handle that problem, so I have 2 questions: 但这似乎并不是解决该问题的最佳方法,所以我有两个问题:

  1. Is there any problem with using the sleep method within an Java EE context? 在Java EE上下文中使用sleep方法是否有问题?
  2. What is a reasonable solution to delaying some processing within an Java EE 5 application? 延迟Java EE 5应用程序中某些处理的合理解决方案是什么?

Edit: 编辑:

I should have mentioned, that my processing takes place while handling objects from a JMS queue via an MDB. 我应该提到,我的处理是在通过MDB处理来自JMS队列的对象时发生的。

And it may be the case, that the data for which I'm waiting never shows up, so there must be some sort of timeout, after which I can do some special processing with my data. 可能是这样,我一直在等待的数据永远不会显示,因此必须有某种超时,然后我才能对数据进行一些特殊处理。

You can use EJB TimerService feature. 您可以使用EJB TimerService功能。 Using threads in a managed environment should be avoided. 应避免在托管环境中使用线程。

I agree with @dkaustubh about timers and avoiding threads manipulation in JavaEE. 我同意@dkaustubh关于计时器的观点,并避免在JavaEE中进行线程操作。

Another possibility is to use JMS queue with delayed delivery. 另一种可能性是使用延迟交付的JMS队列。 Although it is not a part of JavaEE API, most of messaging systems vendors supports it. 尽管它不是JavaEE API的一部分,但是大多数消息传递系统供应商都支持它。 check here . 在这里检查

I think, its possible with some advanced Threading approach. 我认为,通过一些高级线程方法可以实现。 More than thinking on manual synchronizations and thread management, you can always use the Java Concurrent package. 除了考虑手动同步和线程管理之外,您始终可以使用Java Concurrent包。

Future can be one of the ways to do this. 未来可能是实现此目标的方法之一。 Please refer to Java Concurrent package. 请参考Java并发包。

使用通知和Object#wait() / Object#notifyAll()即多线程,生产者通知使用者。

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

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