`

jee6 学习笔记 6.3 - @Asynchronous

    博客分类:
  • JEE
阅读更多
the idea is to the EJB3.1 @Asynchronous ejbs

screen shot 1: call async ejb without receiving a result




screen shot 2: call async ejb and get a result



the jsf page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui">
    
<h:head>
    <title>Test EJB3.1 @Asynchronous</title>
</h:head>

<h:body>
	<h:form>
	    <p:panel header="Test EJB3.1 @Asynchronous" toggleable="true" style="width:60%">
	     <h:outputText id="out" value="#{at.message}" escape="false"/>
	     <p:separator/>
	     <p:commandButton value="TestCallAndForget" action="#{at.test1}" update="out"/>
	     <p:spacer width="7"/>
	     <p:commandButton value="TestCallAndGetResult" action="#{at.test2}" update="out"/>
	    </p:panel>
    </h:form>
</h:body>
</html>


the backing bean
package test.jxee.action;

import java.io.Serializable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;

import org.apache.log4j.Logger;

import test.jxee.ejb.AsyncEJB;
import test.jxee.model.TstResult;

@ManagedBean(name="at")
public class AsyncEJBTestBean implements Serializable {

  private static final Logger log = Logger.getLogger(AsyncEJBTestBean.class);
  private String message = "hi there!";
  private @EJB AsyncEJB aejb;

  public String getMessage() {
    return message;
  }

  public void setMessage(String message) {
    this.message = message;
  }
  
  // testCallAndForget 
  public String test1() {
    log.debug("testCallAndForget... " + System.currentTimeMillis());
    this.aejb.callAndForget();
    this.setMessage("-- testCallAndForget finished --");
    log.debug("testCallAndForget...done " + + System.currentTimeMillis());
    return null;
  }
  
  // testCallAndGetResult
  public String test2() {
    log.debug("testCallAndGetResult... " + System.currentTimeMillis());
    
    Future<TstResult> callReslt = this.aejb.callAndGetResult();
    
    // blocked? you might do some other task here...
    while(!callReslt.isDone()) {
      try {
        log.debug("AsyncEJB not finished yet, pause 1 second...");
        Thread.sleep(1000);
      }
      catch(InterruptedException  ie) {
        ie.printStackTrace();
      }
    }
    
    try {
      // get computation result, wait for 1 second if necessary
      TstResult myReslt = callReslt.get(1, TimeUnit.SECONDS);
      StringBuffer msg = new StringBuffer();
      msg.append("-- Result Status: ").append(myReslt.getStatus())
          .append(" : ").append(myReslt.getTimeStampFormatted())
          .append(" --");
      this.setMessage(msg.toString());
    }
    catch(ExecutionException ee) {
      this.setMessage("AsyncEJB has execution error: " + ee.getMessage());
    }
    catch(TimeoutException toe) {
      this.setMessage("AsyncEJB timed out: " + toe.getMessage());
    }
    catch(Exception e) {
      this.setMessage("AsyncEJB has error: " + e.getMessage());
    }
    
    log.debug("testCallAndGetResult...done " + System.currentTimeMillis());
    return null;
  }
}


the async ejb:
package test.jxee.ejb;

import java.util.Date;
import java.util.concurrent.Future;

import javax.annotation.PostConstruct;
import javax.ejb.AsyncResult;
import javax.ejb.Asynchronous;
import javax.ejb.Stateless;

import org.apache.log4j.Logger;

import test.jxee.model.TstResult;

/**
 * test @Asynchronous EJBs 
 */
@Stateless
public class AsyncEJB {

  private static final Logger log = Logger.getLogger(AsyncEJB.class);
  
  @PostConstruct
  public void init() {
    log.debug(">>> AsyncEJB inited: " + this);
  }
  
  @Asynchronous
  public void callAndForget() {
    log.debug(">>> callAndForget() started: " + System.currentTimeMillis());
    try {
      Thread.sleep(3000); // simulate long running tasks
    }
    catch(InterruptedException e) {
      e.printStackTrace();
    }
    log.debug(">>> callAndForget() completed: " + System.currentTimeMillis());
  }
  
  @Asynchronous // returns a result
  public Future<TstResult> callAndGetResult() {
    log.debug(">>> callAndGetResult() started: " + System.currentTimeMillis());
    try {
      Thread.sleep(5000); // simulate long running tasks
    }
    catch(InterruptedException e) {
      e.printStackTrace();
    }
    // send back result
    TstResult reslt = new TstResult(new Date(), TstResult.STAT.OK);
    log.debug(">>> callAndGetResult() about to send result: " + System.currentTimeMillis());
    return new AsyncResult<TstResult>(reslt);
  }
}


result vo:
package test.jxee.model;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TstResult implements Serializable {

  public static enum STAT {OK, ERROR, UNKNOWN};
  
  private Date timeStamp;
  private String status;
  
  public TstResult() {}
  
  public TstResult(Date date, STAT stat) {
    this.setTimeStamp(date);
    this.setStatus(stat.toString());
  }
  
  public String getTimeStampFormatted() {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return format.format(this.timeStamp);
  }
  public Date getTimeStamp() {
    return this.timeStamp;
  }
  public void setTimeStamp(Date timeStamp) {
    this.timeStamp = timeStamp;
  }
  public String getStatus() {
    return status;
  }
  public void setStatus(String status) {
    this.status = status;
  }
}


uploaded the zipped project so far: ProJee6-phase2.zip

  • 大小: 24.5 KB
  • 大小: 34.6 KB
分享到:
评论

相关推荐

    开发工具 eclipse-jee-mars-2-win32

    开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32...

    eclipse-jee-mars-1-win32-x86_64.7z

    eclipse-jee-mars-1-win32-x86_64.7z eclipse-jee-mars-1-win32-x86_64.zip 我打的 7z 压缩包 关于有 Alt + / 不起作用解决办法: window -&gt; General -&gt; Keys -&gt; Content Assist -&gt; Binding: 改为 Alt + / When:...

    eclipse-jee-2021-12-R-win32-x86_64

    eclipse-jee-2021-12-R-win32-x86_64 eclipse-jee-2021-12-R-win32-x86_64 eclipse-jee-2021-12-R-win32-x86_64

    eclipse-jee-neon-1a-win_64

    eclipse-jee-neon-1a-win_64

    eclipse-jee-2022-06-R-win32-x86_64.zip

    在解压eclipse-jee-2022-06-R-win32-x86_64.zip后,我们会得到一个名为“eclipse”的文件夹,这个文件夹包含了整个IDE的所有组件和配置。启动Eclipse IDE,用户会看到熟悉的界面,包括工作区(Workspace)、透视图...

    eclipse-jee-2023-12-R-win32-x86-64.zip

    eclipse-jee-2023-12-R-win32-x86_64.zip 适用于Windows系统

    eclipse-jee-2023-09-R-linux-gtk-x86-64.tar.gz

    "eclipse-jee-2023-09-R-linux-gtk-x86_64.tar.gz" 文件是Eclipse专为Java企业版(Java EE)开发者设计的2023年9月版本,适用于64位的Linux操作系统。这个版本包含了对Java EE开发所需的全部工具和功能,如Web服务器...

    eclipse-jee-2021-12-R-win32-x86_64.zip

    Eclipse IDE for Enterprise Java and Web Developers (eclipse-jee-2021-12-R-win32-x86_64.zip)适用于Windwos x86_64

    eclipse-jee-2020-09-R-win32-x86_64.zip

    标题中的“eclipse-jee-2020-09-R-win32-x86_64.zip”指的是Eclipse IDE for Java EE Developers的2020年9月版本,适用于Windows 32位和64位系统的安装包。Eclipse是一款著名的开源集成开发环境(IDE),广泛用于...

    eclipse-jee-2022-12-R-win32-x86-64

    eclipse-jee-2022-12-R-win32-x86_64,rar压缩包格式,解压即用。 安装教程:https://danbaku.blog.csdn.net/article/details/128600309

    eclipse-jee-mars-R-win32-x86_64位官方绿色版.zip

    Eclipse-jee-mars-R-win32-x86_64位官方绿色版.zip是一个针对Windows平台的64位版本的Eclipse集成开发环境(IDE)的压缩包,特别为Java企业级(J2EE)应用程序开发设计。该版本发布于2015年6月30日,是当时Eclipse ...

    jee6 学习笔记 5 - Struggling with JSF2 binding GET params

    这篇"jee6 学习笔记 5 - Struggling with JSF2 binding GET params"主要探讨了开发者在使用JSF2绑定GET参数时可能遇到的挑战和解决方案。 JSF2是一个基于MVC(模型-视图-控制器)设计模式的Java框架,用于创建交互...

    eclipse-jee-2023-09-R-macosx-cocoa-x86-64.dmg

    eclipse-jee-2023-09-R-macosx-cocoa-x86_64.dmg 适用于macOS Intel芯片系统

    eclipse-jee-2020-12-R-win32-x86_64

    《Eclipse IDE for Java开发者:深入解析eclipse-jee-2020-12-R-win32-x86_64》 Eclipse IDE,全称集成开发环境(Integrated Development Environment),是全球广泛使用的开源Java开发工具。该版本"eclipse-jee-...

    eclipse-jee-2023-09-R-win32-x86-64.zip

    "eclipse-jee-2023-09-R-win32-x86_64.zip" 是Eclipse针对2023年9月发布的一个更新版,专为Windows 64位操作系统设计。 在Windows系统上安装和使用Eclipse-JEE,你需要了解以下关键知识点: 1. **安装步骤**: - ...

    eclipse-jee-2023-06-R-win32-x86-64.zip

    《Eclipse JEE 2023-06-R for Windows x64:深度解析与应用》 Eclipse JEE 2023-06-R-win32-x86_64.zip 是...通过不断学习和熟练掌握Eclipse JEE的各项功能,你可以更高效地构建、测试和部署高质量的Java EE应用程序。

    eclipse-jee-2019-03-R-win32-x86-64.zip

    Eclipse-JEE-2019-03-R-win32-x86-64.zip 是一个专门为Windows 64位系统设计的Eclipse版本,包含了Web开发所需的插件。 这个版本是2019年的第三个发布版(Release),通常每个版本都会带来性能优化、新功能和修复...

    eclipse-jee-2023-12-R-linux-gtk-x86-64.tar.gz

    eclipse-jee-2023-12-R-linux-gtk-x86_64.tar.gz 适用于Linux x86_64位系统

    jee6 学习笔记 1 - 开发环境的配置

    NULL 博文链接:https://jxee.iteye.com/blog/1575432

Global site tag (gtag.js) - Google Analytics