zoukankan      html  css  js  c++  java
  • 雷林鹏分享:EJB有状态会话Bean

      有状态会话Bean是一种企业bean保存客户端的会话状态类型。有状态会话bean作为每它的名字相关的客户端状态保持在它的实例变量。 EJB容器创建一个单独的有状态会话bean来处理客户端的每个请求。只要请求范围过,有状态会话bean被销毁。

      以下是创建一个有状态的EJB所需的步骤:

      Create a remote/local interface exposing the business methods.

      This interface will be used by the ejb client application.

      Use @Local annotation if ejb client is in same environment where ejb session bean is to be deployed.

      Use @Remote annotation if ejb client is in different environment where ejb session bean is to be deployed.

      Create a stateful session bean implementing the above interface.

      Use @Stateful annotation to signify it a stateful bean. EJB Container automatically creates the relevant configurations or interfaces required by reading this annotation during deployment.

      远程接口

      import javax.ejb.Remote;

      @Remote

      public interface LibraryStatefulSessionBeanRemote {

      //add business method declarations

      }

      Stateful EJB

      @Stateful

      public class LibraryStatefulSessionBean implements LibraryStatefulSessionBeanRemote {

      //implement business method

      }

      示例应用程序

      让我们创建一个测试测试状态EJB的EJB应用程序。

      Step描述

      1Create a project with a name EjbComponent under a package com.tutorialspoint.stateful as explained in the EJB - Create Application chapter. You can also use the project created in EJB - Create Application chapter as such for this chapter to understand stateful ejb concepts.

      2Create LibraryStatefulSessionBean.java and LibraryStatefulSessionBeanRemote as explained in the EJB - Create Application chapter. Keep rest of the files unchanged.

      3Clean and Build the application to make sure business logic is working as per the requirements.

      4Finally, deploy the application in the form of jar file on JBoss Application Server. JBoss Application server will get started automatically if it is not started yet.

      5Now create the ejb client, a console based application in the same way as explained in theEJB - Create Application chapter under topic Create Client to access EJB.

      EJBComponent (EJB Module)

      LibraryStatefulSessionBeanRemote.java

      package com.tutorialspoint.stateful;

      import java.util.List;

      import javax.ejb.Remote;

      @Remote

      public interface LibraryStatefulSessionBeanRemote {

      void addBook(String bookName);

      List getBooks();

      }

      LibraryStatefulSessionBean.java

      package com.tutorialspoint.stateful;

      import java.util.ArrayList;

      import java.util.List;

      import javax.ejb.Stateful;

      @Stateful

      public class LibraryStatefulSessionBean implements LibraryStatefulSessionBeanRemote {

      List bookShelf;

      public LibraryStatefulSessionBean(){

      bookShelf = new ArrayList();

      }

      public void addBook(String bookName) {

      bookShelf.add(bookName);

      }

      public List getBooks() {

      return bookShelf;

      }

      }

      只要你部署在JBoss EjbComponent项目发现jboss的日志。

      JBoss已经自动创建一个JNDI条目会话bean-LibraryStatefulSessionBean/remote。

      我们将使用这个查询字符串来获得远程类型的业务对象-com.tutorialspoint.stateful.LibraryStatefulSessionBeanRemote

      JBoss应用服务器的日志输出

      ...

      16:30:01,401 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:

      LibraryStatefulSessionBean/remote - EJB3.x Default Remote Business Interface

      LibraryStatefulSessionBean/remote-com.tutorialspoint.stateful.LibraryStatefulSessionBeanRemote - EJB3.x Remote Business Interface

      16:30:02,723 INFO [SessionSpecContainer] Starting jboss.j2ee:jar=EjbComponent.jar,name=LibraryStatefulSessionBean,service=EJB3

      16:30:02,723 INFO [EJBContainer] STARTED EJB: com.tutorialspoint.stateful.LibraryStatefulSessionBeanRemote ejbName: LibraryStatefulSessionBean

      16:30:02,731 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:

      LibraryStatefulSessionBean/remote - EJB3.x Default Remote Business Interface

      LibraryStatefulSessionBean/remote-com.tutorialspoint.stateful.LibraryStatefulSessionBeanRemote - EJB3.x Remote Business Interface

      ...

      EJBTester (EJB Client)

      jndi.properties

      java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory

      java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces

      java.naming.provider.url=localhost

      These properties are used to initialize the InitialContext object of java naming service

      InitialContext object will be used to lookup stateful session bean

      EJBTester.java

      package com.tutorialspoint.test;

      import com.tutorialspoint.stateful.LibraryStatefulSessionBeanRemote;

      import java.io.BufferedReader;

      import java.io.FileInputStream;

      import java.io.IOException;

      import java.io.InputStreamReader;

      import java.util.List;

      import java.util.Properties;

      import javax.naming.InitialContext;

      import javax.naming.NamingException;

      public class EJBTester {

      BufferedReader brConsoleReader = null;

      Properties props;

      InitialContext ctx;

      {

      props = new Properties();

      try {

      props.load(new FileInputStream("jndi.properties"));

      } catch (IOException ex) {

      ex.printStackTrace();

      }

      try {

      ctx = new InitialContext(props);

      } catch (NamingException ex) {

      ex.printStackTrace();

      }

      brConsoleReader =

      new BufferedReader(new InputStreamReader(System.in));

      }

      public static void main(String[] args) {

      EJBTester ejbTester = new EJBTester();

      ejbTester.testStatelessEjb();

      }

      private void showGUI(){

      System.out.println("**********************");

      System.out.println("Welcome to Book Store");

      System.out.println("**********************");

      System.out.print("Options 1. Add Book 2. Exit Enter Choice: ");

      }

      private void testStatelessEjb(){

      try {

      int choice = 1;

      LibraryStatefulSessionBeanRemote libraryBean =

      LibraryStatefulSessionBeanRemote)ctx.lookup("LibraryStatefulSessionBean/remote");

      while (choice != 2) {

      String bookName;

      showGUI();

      String strChoice = brConsoleReader.readLine();

      choice = Integer.parseInt(strChoice);

      if (choice == 1) {

      System.out.print("Enter book name: ");

      bookName = brConsoleReader.readLine();

      Book book = new Book();

      book.setName(bookName);

      libraryBean.addBook(book);

      } else if (choice == 2) {

      break;

      }

      }

      List booksList = libraryBean.getBooks();

      System.out.println("Book(s) entered so far: " + booksList.size());

      int i = 0;

      for (Book book:booksList) {

      System.out.println((i+1)+". " + book.getName());

      i++;

      }

      LibraryStatefulSessionBeanRemote libraryBean1 =

      (LibraryStatefulSessionBeanRemote)ctx.lookup("LibraryStatefulSessionBean/remote");

      List booksList1 = libraryBean1.getBooks();

      System.out.println(

      "***Using second lookup to get library stateful object***");

      System.out.println(

      "Book(s) entered so far: " + booksList1.size());

      for (int i = 0; i < booksList1.size(); ++i) {

      System.out.println((i+1)+". " + booksList1.get(i));

      }

      } catch (Exception e) {

      System.out.println(e.getMessage());

      e.printStackTrace();

      }finally {

      try {

      if(brConsoleReader !=null){

      brConsoleReader.close();

      }

      } catch (IOException ex) {

      System.out.println(ex.getMessage());

      }

      }

      }

      }

      EJBTester is doing the following tasks.

      Load properties from jndi.properties and initialize the InitialContext object.

      In testStatefulEjb() method, jndi lookup is done with name - "LibraryStatefulSessionBean/remote" to obtain the remote business object (stateful ejb).

      Then user is shown a library store User Interface and he/she is asked to enter choice.

      If user enters 1, system asks for book name and saves the book using stateful session bean addBook() method. Session Bean is storing the book in its instance variable.

      If user enters 2, system retrieves books using stateful session bean getBooks() method and exits.

      Then another jndi lookup is done with name - "LibraryStatefulSessionBean/remote" to obtain the remote business object (stateful ejb) again and listing of books is done.

      运行客户端访问EJB

      在项目资源管理器中找到EJBTester.java。右键单击类EJBTester并选择run file.

      在Netbeans控制台验证以下输出。

      run:

      **********************

      Welcome to Book Store

      **********************

      Options

      1. Add Book

      2. Exit

      Enter Choice: 1

      Enter book name: Learn Java

      **********************

      Welcome to Book Store

      **********************

      Options

      1. Add Book

      2. Exit

      Enter Choice: 2

      Book(s) entered so far: 1

      1. Learn Java

      ***Using second lookup to get library stateful object***

      Book(s) entered so far: 0

      BUILD SUCCESSFUL (total time: 13 seconds)

      再次运行客户端访问EJB

      在项目资源管理器中找到EJBTester.java。右键单击类EJBTester选择run file.

      在NetBeans控制台验证以下输出.

      run:

      **********************

      Welcome to Book Store

      **********************

      Options

      1. Add Book

      2. Exit

      Enter Choice: 2

      Book(s) entered so far: 0

      ***Using second lookup to get library stateful object***

      Book(s) entered so far: 0

      BUILD SUCCESSFUL (total time: 12 seconds)

      输出上面显示的状态,对于每个查询不同状态的EJB实例将被返回.

      只适用于单个会话状态EJB对象值。在第二次运行时,我们没有得到任何值. (编辑:雷林鹏 来源:网络|侵删)

  • 相关阅读:
    代码命名,代码里的命名规则:错误的和正确的对比 命名方法总结 “自我描述的源代码”用代码表达出你的思想,让其他人通过代码能明白你的意图。
    201508整理:前端工程师的(现在主要的问题、工作选择、技术前景未来)
    java小程序,用java在指定目录或以及子目录中找出同名文件,java File过滤文件名后找同名文件
    excel中的数据粘贴不全到plsql中,excel 粘贴后空白,Excel复制粘贴内容不全
    漫画的由来,卡通漫画、暴走漫画、先锋漫画、漫画与前卫艺术之间的边缘艺术形式、实用漫画、讽刺漫画、幽默漫画的定义
    人性漫画:一个人成功前和成功后赤裸裸的区别 人成功前后对比 成功人发展由来前后结果
    解决IE弹框提示“是否停止运行此脚本”问题
    用UltraEdit判断打开文件的编码类型 用UltraEdit或notepad记事本查看文件编码格式 用UltraEdit查看当前文件编码
    Oracle pl/sql导入sql文件,插入更新数据,中文乱码问题解决方案
    毕胜自述:我是如何把奢侈品打到白菜价的 制造业的电子商务中国零售 小米启示录
  • 原文地址:https://www.cnblogs.com/pengpeng1208/p/13066287.html
Copyright © 2011-2022 走看看