[英]How to create threads in Java EE environment?
I have a requirement where I have to persist some data in a table and the persisting may take sometime. 我有一个要求,我必须在表中保留一些数据,持久可能需要一些时间。 Basically I want to persist a log.
基本上我想坚持一个日志。 I don't want the execution to wait till the persisting finishes.
我不希望执行等到持续完成。
I know I have to use threads to accomplish this task and I know that it is discouraged to create threads in an enterprise application. 我知道我必须使用线程来完成这项任务,我知道不鼓励在企业应用程序中创建线程。
So I started reading about worker manager and understood and tried a sample program in websphere application server 8.5. 所以我开始阅读有关工作管理器的知识,并在websphere应用服务器8.5中理解并尝试了一个示例程序。
I used asynchbeans.jar from websphere and now I am bothered that I am writing vendor specific code. 我在websphere中使用了asynchbeans.jar,现在我很困扰我正在编写特定于供应商的代码。
Then I came across commonj work api which is described in oracle java documentation. 然后我遇到了在Oracle java文档中描述的commonj work api。 Now I am thinking to use commonj api from fabric3.
现在我想使用fabric3的commonj api。
My doubt is, is there a better way to accomplish the same task? 我怀疑的是,有没有更好的方法来完成同样的任务? An EJB way?
一种EJB方式? Or work manager is good for my requirement?
或者工作经理对我的要求有好处吗?
You have some options: 你有一些选择:
@Asynchronous
methods. @Asynchronous
方法。 Requires using EJBs (unwanted complexity for some). lookup("java:...")
, JPA, UserTransaction, etc.), then you should be fine. lookup("java:...")
,JPA,UserTransaction等),那么您应该没问题。 JavaEE7 has the managed executor, that you can try. JavaEE7有托管执行程序,您可以尝试。 You can spawn a task with it, and recieve managed callbacks in a handler.
您可以使用它生成任务,并在处理程序中接收托管回调。 This is part of EE standard and should be platform agnostic.
这是EE标准的一部分,应该与平台无关。
See JDoc here: 在这里查看JDoc:
http://docs.oracle.com/javaee/7/api/javax/enterprise/concurrent/ManagedExecutorService.html http://docs.oracle.com/javaee/7/api/javax/enterprise/concurrent/ManagedExecutorService.html
If you need to be sure that all your log entries are safely written, then you probably should use JMS with persistent messages. 如果您需要确保所有日志条目都是安全编写的,那么您可能应该将JMS与持久性消息一起使用。 Otherwise you could use
@Asynchronous
EJB methods. 否则,您可以使用
@Asynchronous
EJB方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.