Version 2

    JBoss EJB 3.0 and Extensitions

     

    &12488;&12524;&12452;&12523;&65298;&65306;&12473;&12486;&12540;&12488;&12501;&12523;&12475;&12483;&12471;&12519;&12531;Bean&65288;Stateful Session Bean&65289;

     

    &12399;&12376;&12417;&12395;&65288;Introduction&65289;

     

    The stateful session bean is a session bean that maintains its internal states. If the client invokes method calls against the same bean stub, the calls are always tunneled to the same bean instance in the container. So, all field variables in the bean instance retain their values as long as the client application retains the bean stub (or reference for a local client). The stateful session bean is typically used to track states in an application:

     

    &12473;&12486;&12540;&12488;&12501;&12523;&12475;&12483;&12471;&12519;&12531;Bean&12399;&20869;&37096;&29366;&24907;&12434;&20445;&25345;&12377;&12427;&12475;&12483;&12471;&12519;&12531;Bean&12391;&12377;&12290;&12463;&12521;&12452;&12450;&12531;&12488;&12364;&21516;&12376;Bean&12473;&12479;&12502;&12395;&23550;&12375;&12390;&12513;&12477;&12483;&12489;&21628;&12403;&20986;&12375;&12434;&34892;&12358;&12392;&12289;&12467;&12531;&12486;&12490;&20869;&12398;&24120;&12395;&21516;&12376;Bean&12452;&12531;&12473;&12479;&12531;&12473;&12395;&21628;&12403;&20986;&12375;&12364;&28193;&12373;&12428;&12414;&12377;&12290;&12388;&12414;&12426;&12289;Bean&12452;&12531;&12473;&12479;&12531;&12473;&12398;&12377;&12409;&12390;&12398;&12501;&12451;&12540;&12523;&12489;&22793;&25968;&12399;&12289;&12463;&12521;&12452;&12450;&12531;&12488;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12364;Bean&12473;&12479;&12502;&65288;&12525;&12540;&12459;&12523;&12463;&12521;&12452;&12450;&12531;&12488;&12398;&22580;&21512;&12399;&21442;&29031;&65289;&12434;&25345;&12385;&32154;&12369;&12427;&38291;&12399;&20516;&12434;&20445;&25345;&12375;&12414;&12377;&12290;&12473;&12486;&12540;&12488;&12501;&12523;&12475;&12483;&12471;&12519;&12531;Bean&12399;&27425;&12398;&12424;&12358;&12394;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12391;&12289;&29366;&24907;&12434;&36861;&36321;&12377;&12427;&12383;&12417;&12395;&12424;&12367;&20351;&12431;&12428;&12414;&12377;&12290;

     

    • In a web application, the servlet (or JSP page) caches the bean stub as an attribute in the HttpSession object. The HTTP session's state is therefore tracked and stored in the bean instance.

     

    • Web&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12391;&12399;&12289;&12469;&12540;&12502;&12524;&12483;&12488;&65288;&12414;&12383;&12399;JSP&12506;&12540;&12472;&65289;&12391;Bean&12473;&12479;&12502;&12434;HttpSession&12458;&12502;&12472;&12455;&12463;&12488;&12398;&23646;&24615;&12392;&12375;&12390;&26684;&32013;&12375;&12414;&12377;&12290;&12371;&12358;&12377;&12427;&12371;&12392;&12391;&12289;HTTP&12475;&12483;&12471;&12519;&12531;&12398;&29366;&24907;&12434;Bean&12452;&12531;&12473;&12479;&12531;&12473;&12395;&20445;&25345;&12375;&12289;&36861;&36321;&12377;&12427;&12371;&12392;&12364;&12391;&12365;&12414;&12377;&12290;

     

    • In a rich client Java application (e.g., Swing application), the application determines what is a "session" and caches the stateful session bean stub accordingly.

     

    • &12522;&12483;&12481;&12463;&12521;&12452;&12450;&12531;&12488;Java&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&65288;&12383;&12392;&12360;&12400;Swing&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&65289;&12391;&12399;&12289;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&29420;&33258;&12398;&12475;&12483;&12471;&12519;&12531;&12434;&20316;&12426;&12289;&12381;&12371;&12395;&12473;&12486;&12540;&12488;&12501;&12523;&12475;&12483;&12471;&12519;&12531;Bean&12398;&12473;&12479;&12502;&12434;&20445;&25345;&12375;&12414;&12377;&12290;

     

    &12469;&12531;&12503;&12523;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&65288;The sample application&65289;

     

    In this trail, let's implement an investment calculator application that keeps track of past calculations in the same session. Within a web session, every time the JSP page is accessed, it retrieves the same calculator bean stub, and hence loads the session history data from the same bean instance in the EJB 3.0 container.

     

    &12371;&12398;&12488;&12524;&12452;&12523;&12391;&12399;&12289;&21516;&12376;&12475;&12483;&12471;&12519;&12531;&12391;&34892;&12387;&12383;&12289;&36942;&21435;&12398;&35336;&31639;&23653;&27508;&12434;&35352;&37682;&12377;&12427;&25237;&36039;&35336;&31639;&12503;&12525;&12464;&12521;&12512;&12434;&20316;&25104;&12375;&12390;&12415;&12414;&12375;&12423;&12358;&12290;&21516;&12376;&12475;&12483;&12471;&12519;&12531;&12363;&12425;JSP&12506;&12540;&12472;&12395;&12450;&12463;&12475;&12473;&12377;&12427;&12392;&12356;&12388;&12418;&21516;&12376;&25237;&36039;&35336;&31639;Bean&12398;&12473;&12479;&12502;&12434;&21462;&24471;&12375;&12289;&12475;&12483;&12471;&12519;&12531;&12398;&23653;&27508;&12487;&12540;&12479;&12434;EJB 3.0&12467;&12531;&12486;&12490;&20869;&12398;&21516;&12376;Bean&12452;&12531;&12473;&12479;&12531;&12473;&12363;&12425;&12525;&12540;&12489;&12375;&12414;&12377;&12290;

     

    &12300;&25237;&36039;&35336;&31639;&12503;&12525;&12464;&12521;&12512;&12434;&36215;&21205;&12375;&12390;&12415;&12427;&12301;

     

    &29366;&24907;&12503;&12525;&12497;&12486;&12451;&12434;&25345;&12388;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&65288;Interface with state properties&65289;

     

    The Calculator interface is the local business interface for this session bean. Notice that we added methods to access session state properties in the bean. The &12399;ArrayList&20869;&12398;&12458;&12502;&12472;&12455;&12463;&12488;&12398;&22411;&12434;&31034;&12375;&12414;&12377;&12290;JDK 1.5&12398;&32207;&31216;&65288;generics&65289;&12395;&12388;&12356;&12390;&12399;[SunのWebサイト|http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html]&12434;&12372;&35239;&12367;&12384;&12373;&12356;&12290;

     

     public interface Calculator {
     
       public double calculate (int start, int end, 
                       double growthrate, double saving);
     
       public ArrayList <Integer> getStarts ();
       public ArrayList <Integer> getEnds ();
       public ArrayList <Double> getGrowthrates ();
       public ArrayList <Double> getSavings ();
       public ArrayList <Double> getResults ();
     
     }
    

     

    &12473;&12486;&12540;&12488;&12501;&12523;&12475;&12483;&12471;&12519;&12531;Bean&12398;&23455;&35013;&65288;The stateful session bean implementation&65289;

     

    The implementation of the stateful bean StatefulCalculator is straightforward. We annotated the implementation class as @Stateful and used several ArrayList objects to backup the bean properties defined in the session bean interface. The ArrayList objects are initialized for each bean instance when it is created at the beginning of a client session. Below is the complete code for the StatefulCalculator class. It is very important to note that the stateful session bean class must implement the Serializable interface so that the container can serialize them and store them to preserve the state information when the bean instances are not in use.

     

    StatefulCalculator&12473;&12486;&12540;&12488;&12501;&12523;Bean&12398;&23455;&35013;&12399;&12392;&12390;&12418;&29575;&30452;&12394;&12467;&12540;&12489;&12391;&12377;&12290;&23455;&35013;&12463;&12521;&12473;&12434;@Stateful&12391;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12375;&12289;ArrayList&12458;&12502;&12472;&12455;&12463;&12488;&12434;&20351;&12387;&12390;&12475;&12483;&12471;&12519;&12531;Bean&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12391;&23450;&32681;&12375;&12383;Bean&12398;&12503;&12525;&12497;&12486;&12451;&12434;&35352;&36848;&12375;&12414;&12377;&12290;ArrayList&12458;&12502;&12472;&12455;&12463;&12488;&12399;&12289;&12463;&12521;&12452;&12450;&12531;&12488;&12475;&12483;&12471;&12519;&12531;&12398;&38283;&22987;&26178;&12395;Bean&12452;&12531;&12473;&12479;&12531;&12473;&12364;&20316;&25104;&12373;&12428;&12427;&38555;&12395;&21021;&26399;&21270;&12373;&12428;&12414;&12377;&12290;&27425;&12395;&31034;&12377;&12398;&12399;&12289;StatefulCalculator&12463;&12521;&12473;&12398;&20840;&12467;&12540;&12489;&12391;&12377;&12290;&12473;&12486;&12540;&12488;&12501;&12523;&12475;&12483;&12471;&12519;&12531;Bean&12399;Serializable&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12434;&23455;&35013;&12377;&12427;&12371;&12392;&12434;&24536;&12428;&12394;&12356;&12391;&12367;&12384;&12373;&12356;&12290;&20351;&29992;&20013;&12391;&12394;&12356;Bean&12452;&12531;&12473;&12479;&12531;&12473;&12434;&12289;&29366;&24907;&12434;&20445;&25345;&12375;&12383;&12414;&12414;&22806;&37096;&23186;&20307;&12395;&20445;&23384;&12377;&12427;&12395;&12399;&12289;&12467;&12531;&12486;&12490;&12399;&12458;&12502;&12472;&12455;&12463;&12488;&12434;&30452;&21015;&21270;&12377;&12427;&24517;&35201;&12364;&12354;&12427;&12363;&12425;&12391;&12377;&12290;

     

     @Stateful
     public class StatefulCalculator implements Calculator, Serializable {
     
       public ArrayList <Integer> starts = new ArrayList <Integer> ();
       public ArrayList <Integer> ends = new ArrayList <Integer> ();
       public ArrayList <Double> growthrates = new ArrayList <Double> ();
       public ArrayList <Double> savings = new ArrayList <Double> ();
       public ArrayList <Double> results = new ArrayList <Double> ();
     
       public double calculate (int start, int end, 
                                double growthrate, double saving) {
         
         // calculate the result ...
         
         starts.add(Integer.valueOf(start));
         ends.add(Integer.valueOf(end));
         growthrates.add(Double.valueOf(growthrate));
         savings.add(Double.valueOf(saving));
         results.add(Double.valueOf(result));
     
         return result;
       }
     
       public ArrayList <Integer> getStarts () {
         return starts;
       }
     
       // Other getter methods for the attributes ...
     }
    

     

    &12463;&12521;&12452;&12450;&12531;&12488;&65288;The client&65289;

     

    In the JSP client, the StatefulCalculator bean stub is retrieved and associated with the HttpSession object so that all HTTP requests from the same session reach the same stub and hence the same bean instance in the container. The following is a code segment in the JSP page.

     

    JSP&12463;&12521;&12452;&12450;&12531;&12488;&12391;&12399;&12289;StatefulCalculator&12398;Bean&12473;&12479;&12502;&12434;&21462;&24471;&12375;&12289;HttpSession&12458;&12502;&12472;&12455;&12463;&12488;&12395;&38306;&36899;&20184;&12369;&12414;&12377;&12290;&12371;&12358;&12377;&12427;&12371;&12392;&12395;&12424;&12426;&21516;&12376;&12475;&12483;&12471;&12519;&12531;&12363;&12425;&12399;&24120;&12395;&21516;&12376;&12473;&12479;&12502;&12289;&12388;&12414;&12426;&12467;&12531;&12486;&12490;&20869;&12398;&21516;&12376;Bean&12452;&12531;&12473;&12479;&12531;&12473;&12395;&12450;&12463;&12475;&12473;&12391;&12365;&12414;&12377;&12290;&27425;&12395;&31034;&12377;&12398;&12399;JSP&12506;&12540;&12472;&12398;&25244;&31883;&12391;&12377;&12290;

     

     Calculator cal =
           (Calculator) session.getAttribute("sfsb_cal");
     if (cal == null) {
       try {
         InitialContext ctx = new InitialContext();
         cal = (Calculator) ctx.lookup(
                 "EJB3Trail/StatefulCalculator/local");
         session.setAttribute ("sfsb_cal", cal);
       } catch (Exception e) {
         e.printStackTrace ();
       }
     }
     
     // Make use of the cal object
    

     

    &12477;&12540;&12473;&12467;&12540;&12489;&21442;&29031;&65288;Complete code reference&65289;

     

    EJB&12469;&12540;&12496;&65288;EJB server&65289;

     

    • Calculator.java: The local business interface of the bean

    • StatefulCalculator.java: The stateful session bean implementation

     

    • Calculator.java: Bean&12398;&12525;&12540;&12459;&12523;&12499;&12472;&12493;&12473;&12452;&12531;&12479;&12501;&12455;&12540;&12473;

    • StatefulCalculator.java: &12473;&12486;&12540;&12488;&12501;&12523;&12475;&12483;&12471;&12519;&12531;Bean&12398;&23455;&35013;

     

    EJB&12463;&12521;&12452;&12450;&12531;&12488;&65288;EJB client&65289;

     

    • calculator.jsp: The client of the stateful calculator session bean

     

    • calculator.jsp: &25237;&36039;&35336;&31639;&12475;&12483;&12471;&12519;&12531;Bean&12398;&12463;&12521;&12452;&12450;&12531;&12488;

     

    &12414;&12392;&12417;&65288;Summary&65289;

     

    In this trail, we discussed how to use stateful session beans to track user sessions in web applications. To manage the internal state of the session bean, you need to understand an important concept: session bean life cycles. So, read on to the next trail.

     

    &12371;&12398;&12488;&12524;&12452;&12523;&12391;&12399;&12289;Web&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12391;&12473;&12486;&12540;&12488;&12501;&12523;&12475;&12483;&12471;&12519;&12531;Bean&12434;&20351;&12387;&12390;&12518;&12540;&12470;&12475;&12483;&12471;&12519;&12531;&12398;&23653;&27508;&12434;&31649;&29702;&12377;&12427;&26041;&27861;&12434;&35696;&35542;&12375;&12414;&12375;&12383;&12290;&12475;&12483;&12471;&12519;&12531;Bean&12398;&12521;&12452;&12501;&12469;&12452;&12463;&12523;&12399;&12289;&12475;&12483;&12471;&12519;&12531;Bean&12398;&20869;&37096;&29366;&24907;&12434;&31649;&29702;&12377;&12427;&12383;&12417;&12398;&12392;&12390;&12418;&37325;&35201;&12394;&12467;&12531;&12475;&12503;&12488;&12391;&12377;&12290;&12391;&12399;&32154;&12369;&12390;&12289;&27425;&12398;&12488;&12524;&12452;&12523;&12395;&34892;&12387;&12390;&12415;&12414;&12375;&12423;&12358;&12290;

     

    (c) 2005 JBoss, Inc. All Rights Reserved