简体   繁体   English

如何在Java EE环境中创建线程?

[英]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: 你有一些选择:

  1. Asynchronous beans. 异步bean。 These are vendor-specific, as you mention. 如您所述,这些是特定于供应商的。
  2. commonj is just barely not vendor-specific. commonj几乎不是特定于供应商的。 As far as I know, it was only implemented by IBM WebSphere Application Server and BEA WebLogic. 据我所知,它只由IBM WebSphere Application Server和BEA WebLogic实现。 The API was effectively superseded by Concurrency Utilities for Java EE , which is really the best choice. 该API实际上被Java EEConcurrency Utilities取代,这是最好的选择。
  3. EJB @Asynchronous methods. EJB @Asynchronous方法。 Requires using EJBs (unwanted complexity for some). 需要使用EJB(某些不需要的复杂性)。
  4. EJB timers. EJB计时器。 Requires using EJBs, requires serializable data. 需要使用EJB,需要可序列化的数据。
  5. JMS. JMS。 Probably requires using MDBs to receive the message, requires serializable data. 可能需要使用MDB来接收消息,需要可序列化的数据。
  6. Actually create threads. 实际上创建线程。 The EE specs do not recommend this, but as long as you don't attempt to use EE constructs ( lookup("java:...") , JPA, UserTransaction, etc.), then you should be fine. EE规范不建议这样做,但只要您不尝试使用EE构造( 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.

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