JBoss QuickStart之深入

  • EJB-Asynchronous
    EJB中提供异步调用的方法.

    "A session bean can expose methods with asynchronous client invocation semantics. For asynchronous invocations, control returns to the client before the container dispatches the invocation to a bean instance. An asynchronous method is a business method exposed through one or more of the Remote business, Local business, or no-interface session bean views.
    Asynchronous methods can return a Future<V> object that allows the client to retrieve a result value, check for exceptions, or attempt to cancel an in-progress invocation." -From JSR 318: Enterprise JavaBeansTM,Version 3.1EJB Core Contracts and RequirementsNovember 5, 2009Version 3.1, Final Release

   最简单就是使用注解, @Asynchronous, 方法可以返回一个Future<V>对象,当然也可以不返回任何,即void. 譬如如下代码:

    @Asynchronous
    @Override
    public Future<String> longerRunning(long sleepTime) {
        LOGGER.info("Will wait for " + sleepTime + "ms");
        try {
            Thread.sleep(sleepTime);
        } catch (InterruptedException e) {
        }
        LOGGER.info("returning the result");
        return new AsyncResult<String>("returning at " + new Date() + ", duration was " + sleepTime + "ms");
    }

如果异步调用抛出异常咋办?如下例子failure()方法(类似上面的longerRunning方法)为一个声明为异步的方法, 但会抛出异常, 异常会在调用x.get()时候抛出ExecutionException, 然后get出来就可以得到原始的异常.

这个是J2SE的东西, 猜测容器应该使用ExecutorService去调用这些方法,然后封装,返回Future对象.

与Timer不同,timer采用的是J2SE的util包中的TimerTask去实现的(JBoss),而这个异步调用采用cocurrent包中的ExecutorService以及Future实现.

private void callAsyncWithFailure() throws InterruptedException {
        Future<String> x = accessBean.failure(); // this method will return successfully, because the invocation will be successful!

        try {
            x.get(); // this will not return successfully
        } catch (ExecutionException e) {
            // the IllegalAccessException is thrown by the bean method
            if (e.getCause() instanceof IllegalAccessException) {
                // This is the expected behavior
                LOGGER.info("Catch the expected Exception of the asynchronous execution!");
            } else {
                throw new RuntimeException("Unexpected ExecutionException during asynchronous call!", e);
            }
        }
    }
  • tasks

  这个涉及JPA(Java Persistence API), JPA的前世今生需要搞搞明白,否则不明白与Hibernate之间的关系。

      JPA的背景及现在:

      About JPA and Hibernate found in JBoss_Enterprise_Application_Platform-6-Development_Guide-en-US.pdf:
  The Java Persistence API (JPA) is the standard for using persistence in Java projects. Java EE 6
  applications use the Java Persistence 2.0 specification, documented here:
  http://www.jcp.org/en/jsr/detail?id=317.
  Hibernate EntityManager implements the programming interfaces and life-cycle rules defined by the
  specification.

     --From offical JBoss Developer Guide

     

 TBC ...

原文地址:https://www.cnblogs.com/bjfarmer/p/5301792.html