@Asynchronous 不会导致 JBossAS7 中 EJB 方法的异步调用

发布于 2024-12-13 10:13:47 字数 1290 浏览 4 评论 0原文

我正在努力弄清楚为什么 EJB 中的 @Asynchronous 方法实际上并未被异步调用。我在 JSF2 项目中使用 CDI(带有 beans.xml)在 JBoss AS 7 上运行,并使用 Maven 生成的简单 .war 打包。

EJB 与与其交互的 JSF2 托管 bean 一起打包在 .war 中。这是一个简单的@Stateless EJB。通过将其注入(通过 @Inject)到调用其 @Asynchronous 方法的 JSF2 托管 bean 中来使用它。

@Asynchronous 方法调用不是立即返回 Future,而是同步执行,就像普通的无代理直接调用一样。无论我使用本地无接口视图还是本地业务接口来调用 EJB,都是如此。

@Asynchronous仅支持@Remote bean吗?如果是这样,它可以在 .war 打包中工作吗?还是我必须将 EJB jar 打包到 EAR 中才能获得这一功能?

例如,简化代码,每个类位于 .war 中的同一包中:

public interface SomeEJB {
  public Future<Void> doSomething();
}

@Stateless
@Local(SomeEJB.class)
public class SomeEJBImpl implements SomeEJB {

  @Asynchronous
  @Override
  public Future<Void> doSomething() {
    // Spend a while doing work
    // then:
    return new AsyncResult<Void>(null);
  }

}

@Named
@RequestScoped
public class JSFBean {

  @Inject private transient SomeEJB someEJB;
  private Future<Void> progress;

  // Called from JSF2, starts work and re-displays page
  public String startWorkAction() {
    // This call SHOULD return a Future immediately. Instead it blocks
    // until doWork() completes.
    progress = someEJB.doWork();
  }

  public Boolean isDone() {
    return progress != null && progress.isDone();
  }

}

I'm struggling to figure out why an @Asynchronous method in my EJB isn't actually being invoked asynchronously. I'm running on JBoss AS 7 using CDI (with beans.xml) in a JSF2 project with simple .war packaging produced by Maven.

The EJB is packaged in a .war along with the JSF2 managed beans that interact with it. It's a simple @Stateless EJB. It's used by injecting it (via @Inject) into a JSF2 managed bean that invokes its @Asynchronous method.

Instead of the @Asynchronous method invocation returning a Future immediately, it executes synchronously as if it were an ordinary unproxied direct call. This is true whether I use a local no-interface view or a local business interface to invoke the EJB.

Is @Asynchronous only supported for @Remote beans? If so, can it work within .war packaging or do I have to package an EJB jar in an EAR just to get this one feature?

Simplified code for example's sake, with each class in the same package in a .war:

public interface SomeEJB {
  public Future<Void> doSomething();
}

@Stateless
@Local(SomeEJB.class)
public class SomeEJBImpl implements SomeEJB {

  @Asynchronous
  @Override
  public Future<Void> doSomething() {
    // Spend a while doing work
    // then:
    return new AsyncResult<Void>(null);
  }

}

@Named
@RequestScoped
public class JSFBean {

  @Inject private transient SomeEJB someEJB;
  private Future<Void> progress;

  // Called from JSF2, starts work and re-displays page
  public String startWorkAction() {
    // This call SHOULD return a Future immediately. Instead it blocks
    // until doWork() completes.
    progress = someEJB.doWork();
  }

  public Boolean isDone() {
    return progress != null && progress.isDone();
  }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

俏︾媚 2024-12-20 10:13:47

JBoss AS 7.0.2 默认不支持@Asynchronous。你必须打开它。如果未打开,则不会出现警告或错误消息,异步方法只会同步执行。

是的,这对用户很友好。

要在这个据称已完成并发布*的产品中启用这些功能,您必须使用“standalone-preview.xml”运行 JBoss AS 7.0.2,例如:

bin/standalone.sh --server-config=standalone-preview.xml

或者在 AS 7.1(测试版)或更高版本中:

bin/standalone.sh --server-config=standalone-full.xml

...要异步调用的方法。

  • (诚​​然,AS 7 并未声称符合 Java EE 6 Full Profile 合规性,但如果能有一个警告就好了!或者一些关于已知问题/漏洞的文档!除了静默的未记录的故障之外的任何内容......)

更新:正如 garcia-jj 所指出的,从 standalone.xml 中删除 lite=true 也将使异步 EJB 正常工作。

JBoss AS 7.0.2 doesn't support @Asynchronous by default. You have to turn it on. If it's not turned on there's no warning or error message, asynchronous methods are just executed synchronously.

Yep, that's user friendly.

To enable these features in this supposedly finished and released* product, you must run JBoss AS 7.0.2 with the "standalone-preview.xml", eg:

bin/standalone.sh --server-config=standalone-preview.xml

or in AS 7.1 (beta) or later:

bin/standalone.sh --server-config=standalone-full.xml

... which gets the asynchronous methods to be invoked ... asynchronously.

  • (Admittedly AS 7 doesn't claim Java EE 6 Full Profile compliance, but a warning would be nice! Or some documentation on the known issues/holes! Anything but silent undocumented failure...)

Update: As noted by garcia-jj, removing lite=true from standalone.xml will also get async EJBs working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文