Version 5

    JBoss EJB 3.0 and Extensions

     

    &12488;&12524;&12452;&12523;&65297;&12288;&12473;&12486;&12540;&12488;&12524;&12473;&12475;&12483;&12471;&12519;&12531;Bean&65288;Stateless Session Beans&65289;

     

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

     

    One of the most important functionalities of an EJB container is to manage service objects. The container instantiates objects, manages their life cycles, maintains object pools, and provides means to access those objects from other parts of the application. An EJB 3.0 session bean is a POJO managed by the EJB container. As we will see later in this trailblazer, a session bean allows us to use the container's security, transaction, messaging, persistence, and other services.

     

    EJB&12467;&12531;&12486;&12490;&12398;&12418;&12387;&12392;&12418;&37325;&35201;&12394;&24441;&30446;&12398;&12402;&12392;&12388;&12399;&12289;&12469;&12540;&12499;&12473;&12458;&12502;&12472;&12455;&12463;&12488;&12434;&31649;&29702;&12377;&12427;&12371;&12392;&12391;&12377;&12290;&12467;&12531;&12486;&12490;&12399;&12289;&12469;&12540;&12499;&12473;&12398;&12452;&12531;&12473;&12479;&12531;&12473;&21270;&12289;&12521;&12452;&12501;&12469;&12452;&12463;&12523;&12398;&31649;&29702;&12289;&12458;&12502;&12472;&12455;&12463;&12488;&12398;&12503;&12540;&12522;&12531;&12464;&12434;&34892;&12356;&12289;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12398;&20182;&12398;&37096;&20998;&12363;&12425;&12371;&12428;&12425;&12398;&12458;&12502;&12472;&12455;&12463;&12488;&12395;&12450;&12463;&12475;&12473;&12377;&12427;&26041;&27861;&12434;&25552;&20379;&12375;&12414;&12377;&12290;EJB 3.0&12398;&12475;&12483;&12471;&12519;&12531;Bean&12399;EJB&12467;&12531;&12486;&12490;&12364;&31649;&29702;&12377;&12427;POJO&12391;&12377;&12290;&12371;&12398;TrailBlazer&12398;&20013;&12391;&24460;&12411;&12393;&35500;&26126;&12375;&12414;&12377;&12364;&12289;&12475;&12483;&12471;&12519;&12531;Bean&12391;&12399;&12289;&12467;&12531;&12486;&12490;&12364;&25552;&20379;&12377;&12427;&12475;&12461;&12517;&12522;&12486;&12451;&12289;&12488;&12521;&12531;&12470;&12463;&12471;&12519;&12531;&12289;&12513;&12483;&12475;&12540;&12472;&12531;&12464;&12289;&27704;&32154;&24615;&12394;&12393;&12398;&12373;&12414;&12374;&12414;&12394;&12469;&12540;&12499;&12473;&12364;&21033;&29992;&12391;&12365;&12414;&12377;&12290;

     

    The functionality of a session bean is defined by its service interface (a.k.a. business interface), which is a plain old Java interface. Using the interface class name, the session bean client retrieves a stub object of the bean from the server's JNDI. The stub object implements the bean's service interface. The client can then make calls to the bean interface methods against the stub object. The stub object simply passes the calls to the actual bean instance objects in the container, which have the implementations of those methods and do the actual work. The stub object is automatically generated by the EJB container and it knows how to route the bean method calls to the container -- you do not need to provide an implementation for the stub object.

     

    &12475;&12483;&12471;&12519;&12531;Bean&12398;&27231;&33021;&12399;&12289;&36890;&24120;&12398;Java&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12391;&12354;&12427;&12469;&12540;&12499;&12473;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&65288;&12388;&12414;&12426;&12499;&12472;&12493;&12473;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&65289;&12391;&23450;&32681;&12375;&12414;&12377;&12290;&12475;&12483;&12471;&12519;&12531;Bean&12398;&12463;&12521;&12452;&12450;&12531;&12488;&12399;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12463;&12521;&12473;&21517;&12434;&20351;&12387;&12390;&12469;&12540;&12496;&12398;JNDI&12363;&12425;&12458;&12502;&12472;&12455;&12463;&12488;&12398;&12473;&12479;&12502;&12434;&21462;&24471;&12375;&12414;&12377;&12290;&12473;&12479;&12502;&12458;&12502;&12472;&12455;&12463;&12488;&12399;&12289;Bean&12398;&12469;&12540;&12499;&12473;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12434;&23455;&35013;&12375;&12390;&12356;&12414;&12377;&12398;&12391;&12289;&12463;&12521;&12452;&12450;&12531;&12488;&12399;&21462;&24471;&12375;&12383;&12473;&12479;&12502;&12458;&12502;&12472;&12455;&12463;&12488;&12395;&23550;&12375;&12390;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12513;&12477;&12483;&12489;&12398;&21628;&12403;&20986;&12375;&12434;&34892;&12358;&12371;&12392;&12364;&12391;&12365;&12414;&12377;&12290;&12473;&12479;&12502;&12458;&12502;&12472;&12455;&12463;&12488;&12399;&21336;&12395;&12467;&12531;&12486;&12490;&12395;&12354;&12427;&23455;&38555;&12398;Bean&12452;&12531;&12473;&12479;&12531;&12473;&12395;&21628;&12403;&20986;&12375;&12434;&28193;&12377;&12384;&12369;&12391;&12377;&12290;&12467;&12531;&12486;&12490;&20869;&12398;&12452;&12531;&12473;&12479;&12531;&12473;&12364;&23455;&38555;&12398;&12513;&12477;&12483;&12489;&23455;&35013;&12434;&25345;&12385;&12289;&23455;&38555;&12395;&21205;&20316;&12375;&12414;&12377;&12290;&12473;&12479;&12502;&12458;&12502;&12472;&12455;&12463;&12488;&12399;&12467;&12531;&12486;&12490;&12395;&12424;&12387;&12390;&33258;&21205;&29983;&25104;&12373;&12428;&12289;Bean&12398;&12513;&12477;&12483;&12489;&21628;&12403;&20986;&12375;&12434;&12467;&12531;&12486;&12490;&12395;&37197;&20449;&12377;&12427;&26041;&27861;&12434;&20633;&12360;&12390;&12356;&12414;&12377;&12398;&12391;&12289;&12473;&12479;&12502;&12458;&12502;&12472;&12455;&12463;&12488;&12398;&23455;&35013;&12434;&29992;&24847;&12377;&12427;&24517;&35201;&12399;&12354;&12426;&12414;&12379;&12435;&12290;

     

    Since the container-generated stub object routes the call from the client to the service object, the client does not need to know the actual implementation class of the bean inside the container. Hence, the container can swap in and out alternative service implementations without affecting the client, as long as the bean's service interface remain unchanged. Therefore, the service and the client are only loosely coupled via an interface.

     

    &12467;&12531;&12486;&12490;&12364;&29983;&25104;&12377;&12427;&12473;&12479;&12502;&12458;&12502;&12472;&12455;&12463;&12488;&12364;&12463;&12521;&12452;&12450;&12531;&12488;&12363;&12425;&12398;&21628;&12403;&20986;&12375;&12434;&12469;&12540;&12499;&12473;&12458;&12502;&12472;&12455;&12463;&12488;&12395;&37197;&20449;&12375;&12414;&12377;&12398;&12391;&12289;{FOOTNOTE DEF 1 1}&12467;&12531;&12486;&12490;&20869;&37096;&12398;&23455;&38555;&12398;Bean&12398;&23455;&35013;&12463;&12521;&12473;&12434;&30693;&12427;&24517;&35201;&12399;&12354;&12426;&12414;&12379;&12435;&12290;&12375;&12383;&12364;&12387;&12390;&12289;Bean&12398;&12469;&12540;&12499;&12473;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12434;&22793;&26356;&12375;&12394;&12356;&38480;&12426;&12289;&12463;&12521;&12452;&12450;&12531;&12488;&12395;&24433;&38911;&12434;&19982;&12360;&12427;&12371;&12392;&12394;&12367;&12469;&12540;&12499;&12473;&12398;&23455;&35013;&12434;&20837;&12428;&26367;&12360;&12427;&12371;&12392;&12364;&12391;&12365;&12427;&12398;&12391;&12377;&12290;&12388;&12414;&12426;&12289;&12469;&12540;&12499;&12473;&12392;&12463;&12521;&12452;&12450;&12531;&12488;&12399;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12398;&12415;&12391;&12388;&12394;&12364;&12427;&30094;&32080;&21512;&12392;&35328;&12360;&12414;&12377;&12290;

     

    {FOOTNOTE RED #1 1} &12371;&12371;&12300;&12463;&12521;&12452;&12450;&12531;&12488;&12399;&12301;&12434;&20837;&12428;&12383;&12356; -tokobayashi

     

    In a stateless session bean, the client-side stub object can route your method call to any bean instance that happens to be available in the container-managed object pool. Hence, you should not have any field variables to store the bean state in the bean class. Otherwise, any method that depends on those field variables may have unpredictable behaviors over time since different invocation might reach different bean instances.

     

    &12473;&12486;&12540;&12488;&12524;&12473;&12475;&12483;&12471;&12519;&12531;Bean&12391;&12399;&12289;&12463;&12521;&12452;&12450;&12531;&12488;&20596;&12398;&12473;&12479;&12502;&12458;&12502;&12472;&12455;&12463;&12488;&12399;&12467;&12531;&12486;&12490;&12364;&31649;&29702;&12377;&12427;&12458;&12502;&12472;&12455;&12463;&12488;&12503;&12540;&12523;&20869;&12395;&12354;&12427;&20219;&24847;&12398;&21033;&29992;&21487;&33021;&12394;&12452;&12531;&12473;&12479;&12531;&12473;&12395;&12463;&12521;&12452;&12450;&12531;&12488;&12398;&21628;&12403;&20986;&12375;&12434;&37197;&20449;&12375;&12414;&12377;&12290;&12388;&12414;&12426;&12289;Bean&12463;&12521;&12473;&12395;&12399;Bean&12398;&29366;&24907;&12434;&20445;&25345;&12377;&12427;&12424;&12358;&12394;&12501;&12451;&12540;&12523;&12489;&22793;&25968;&12434;&25345;&12388;&12409;&12365;&12391;&12399;&12354;&12426;&12414;&12379;&12435;&12290;&30064;&12394;&12427;&21628;&12403;&20986;&12375;&12399;&30064;&12394;&12427;&12452;&12531;&12473;&12479;&12531;&12473;&12395;&28193;&12427;&21487;&33021;&24615;&12364;&12354;&12427;&12398;&12391;&12289;&12381;&12398;&12424;&12358;&12394;&12501;&12451;&12540;&12523;&12489;&22793;&25968;&12434;&25345;&12388;&12513;&12477;&12483;&12489;&12399;&20104;&24819;&12375;&12394;&12356;&25391;&12427;&33310;&12356;&12434;&12377;&12427;&12371;&12392;&12395;&12394;&12426;&12414;&12377;&12290;

     

    &12469;&12531;&12503;&12523;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&65288;The Sample Application&65289;

     

    In this trail, we create a stateless session bean that performs investment calculation. The client application (e.g., a servlet or a Swing rich UI client) obtains a reference of a bean instance from the container and make use of its calculation services. As an example, click on the following button to open calculator application in a new window, the JSP page connects to the session bean and uses it to calculate the investment.

     

    &12371;&12398;&12488;&12524;&12452;&12523;&12391;&12399;&12289;&25237;&36039;&35336;&31639;&12434;&34892;&12358;&12473;&12486;&12540;&12488;&12524;&12473;&12475;&12483;&12471;&12519;&12531;Bean&12434;&20316;&25104;&12375;&12414;&12377;&12290;&12463;&12521;&12452;&12450;&12531;&12488;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&65288;&12469;&12540;&12502;&12524;&12483;&12488;&12420;Swing&12463;&12521;&12452;&12450;&12531;&12488;&12394;&12393;&65289;&12399;&12467;&12531;&12486;&12490;&12363;&12425;Bean&12452;&12531;&12473;&12479;&12531;&12473;&12398;&21442;&29031;&12434;&21462;&24471;&12375;&12289;&35336;&31639;&12469;&12540;&12499;&12473;&12434;&21033;&29992;&12375;&12414;&12377;&12290;&19979;&12398;&12508;&12479;&12531;&12434;&12463;&12522;&12483;&12463;&12375;&12390;&12289;&21029;&12454;&12451;&12531;&12489;&12454;&12391;&35336;&31639;&12503;&12525;&12464;&12521;&12512;&12434;&36215;&21205;&12375;&12390;&12415;&12390;&12367;&12384;&12373;&12356;&12290;JSP&12506;&12540;&12472;&12363;&12425;&12475;&12483;&12471;&12519;&12531;Bean&12395;&12450;&12463;&12475;&12473;&12375;&12289;&25237;&36039;&35336;&31639;&12434;&34892;&12356;&12414;&12377;&12290;

     

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

     

    &12475;&12483;&12471;&12519;&12531;Bean&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12398;&23450;&32681;&65288;Define the session bean interface&65289;

     

    To define a session bean, you first need to define the service interface containing all its business methods. The session bean interface is just plain old Java interface without any annotation. The client uses this interface to retrieve a stub object of the session bean from the EJB 3.0 container.

     

    &12475;&12483;&12471;&12519;&12531;Bean&12434;&23450;&32681;&12377;&12427;&12395;&12399;&12289;&12414;&12378;&12377;&12409;&12390;&12398;&12499;&12472;&12493;&12473;&12513;&12477;&12483;&12489;&12434;&21547;&12416;&12469;&12540;&12499;&12473;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12434;&23450;&32681;&12377;&12427;&24517;&35201;&12364;&12354;&12426;&12414;&12377;&12290;&12475;&12483;&12471;&12519;&12531;Bean&12398;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12399;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12394;&12393;&12398;&12394;&12356;&21336;&32020;&12394;Java&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12391;&12377;&12290;&12463;&12521;&12452;&12450;&12531;&12488;&12399;&12371;&12398;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12434;&20351;&29992;&12375;&12390;EJB 3.0&12467;&12531;&12486;&12490;&12363;&12425;&12475;&12483;&12471;&12519;&12531;Bean&12398;&12473;&12479;&12502;&12458;&12502;&12472;&12455;&12463;&12488;&12434;&21462;&24471;&12375;&12414;&12377;&12290;

     

     public interface Calculator {
     
       public double calculate (int start, int end, 
                     double growthrate, double saving);
     
     }
    

     

    &12475;&12483;&12471;&12519;&12531;Bean&12398;&23455;&35013;&65288;The session bean implementation&65289;

     

    With the interfaces defined, you can now provide an implementation of the bean. The bean implementation is a simple POJO. The EJB 3.0 container instantiates and manages the POJO instances automatically. Below is the code for the StatelessCalculator class. The @Stateless annotation indicates that this bean is a stateless session bean.

     

    &12452;&12531;&12479;&12501;&12455;&12540;&12473;&12434;&23450;&32681;&12375;&12383;&12425;&12289;&27425;&12399;Bean&12398;&23455;&35013;&12434;&35352;&36848;&12375;&12414;&12377;&12290;Bean&12398;&23455;&35013;&12399;&21336;&32020;&12394;POJO&12391;&34892;&12356;&12414;&12377;&12290;EJB 3.0&12467;&12531;&12486;&12490;&12399;&33258;&21205;&30340;&12395;POJO&12398;&12452;&12531;&12473;&12479;&12531;&12473;&12434;&29983;&25104;&12375;&31649;&29702;&12375;&12414;&12377;&12290;StatelessCalculator&12463;&12521;&12473;&12398;&12467;&12540;&12489;&12434;&20197;&19979;&12395;&31034;&12375;&12414;&12377;&12290;@Stateless&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12391;&12289;&12371;&12398;Bean&12364;&12473;&12486;&12540;&12488;&12524;&12473;&12475;&12483;&12471;&12519;&12531;Bean&12391;&12354;&12427;&12371;&12392;&12434;&31034;&12375;&12414;&12377;&12290;

     

     @Stateless
     public class StatelessCalculator 
                    implements Calculator, RemoteCalculator {
     
       public double calculate (int start, int end, 
                         double growthrate, double saving) {
         double tmp = Math.pow(1. + growthrate / 12., 
                               12. * (end - start) + 1);
         return saving * 12. * (tmp - 1) / growthrate;
       }
     
     }
    

     

    &12475;&12483;&12471;&12519;&12531;Bean&12398;&12463;&12521;&12452;&12450;&12531;&12488;&65288;The session bean client&65289;

     

    Once the session bean is deployed into the EJB 3.0 container, a stub object is created and it is registered in the server's JDNI registry. The client code obtains a stub of the bean from the JNDI using its default JNDI name formatted as follows.

     

    &12475;&12483;&12471;&12519;&12531;Bean&12364;EJB 3.0&12467;&12531;&12486;&12490;&12395;&12487;&12503;&12525;&12452;&12373;&12428;&12427;&12392;&12289;&12473;&12479;&12502;&12458;&12502;&12472;&12455;&12463;&12488;&12364;&29983;&25104;&12373;&12428;&12289;&12469;&12540;&12496;&12398;JNDI&12524;&12472;&12473;&12488;&12522;&12395;&30331;&37682;&12373;&12428;&12414;&12377;&12290;&12463;&12521;&12452;&12450;&12531;&12488;&12399;&27425;&12398;&24418;&24335;&12398;JNDI&21517;&12391;Bean&12398;&12473;&12479;&12502;&12434;&21462;&24471;&12375;&12414;&12377;&12290;

     

    • If the application is deployed in a EAR file, the default JNDI name is the EAR-FILE-BASE-NAME/EJB-CLASS-NAME/local for the stub for local interface. For the remote interface (see below), it is EAR-FILE-BASE-NAME/EJB-CLASS-NAME/local.

     

    • &12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12364;EAR&12501;&12449;&12452;&12523;&12391;&12487;&12503;&12525;&12452;&12373;&12428;&12390;&12356;&12427;&22580;&21512;&12289;&12525;&12540;&12459;&12523;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12398;&12487;&12501;&12457;&12523;&12488;&12398;JNDI&21517;&12399;&12289;"EAR&12501;&12449;&12452;&12523;&12398;&22522;&26412;&21517;"/"EJB&12463;&12521;&12473;&21517;"/local&12392;&12394;&12426;&12414;&12377;&12290;&12522;&12514;&12540;&12488;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12398;&22580;&21512;&65288;&19979;&35352;&21442;&29031;&65289;&12289;"EAR&12501;&12449;&12452;&12523;&12398;&22522;&26412;&21517;"/"EJB&12463;&12521;&12473;&21517;"/remote&12392;&12394;&12426;&12414;&12377;&12290;

     

    • If the bean is deployed in a JAR file, the JNDI names are EJB-CLASS-NAME/local and EJB-CLASS-NAME/remote.

     

    • Bean&12364;JAR&12501;&12449;&12452;&12523;&12391;&12487;&12503;&12525;&12452;&12373;&12428;&12390;&12356;&12427;&22580;&21512;&12289;JNDI&21517;&12399;&12381;&12428;&12382;&12428;"EJB&12463;&12521;&12473;&21517;"/local&12392;"EJB&12463;&12521;&12473;&21517;"/remote&12392;&12394;&12426;&12414;&12377;&12290;

     

    Below is an example on how to retrieve a stub instance of the Calculator interface for this JSP page. You can make method calls against the stub object and the call is transparently delegated to the bean instance in the EJB 3.0 container.

     

    &20197;&19979;&12395;&31034;&12377;&12398;&12399;&12289;JSP&12506;&12540;&12472;&12363;&12425;Calculator&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12398;&12473;&12479;&12502;&12452;&12531;&12473;&12479;&12531;&12473;&12434;&21462;&24471;&12377;&12427;&12467;&12540;&12489;&12398;&20363;&12391;&12377;&12290;&12473;&12479;&12502;&12458;&12502;&12472;&12455;&12463;&12488;&12395;&23550;&12375;&12390;&12513;&12477;&12483;&12489;&21628;&12403;&20986;&12375;&12434;&34892;&12358;&12392;&12289;&21628;&12403;&20986;&12375;&12399;EJB 3.0&12467;&12531;&12486;&12490;&12395;&12354;&12427;Bean&12398;&12452;&12531;&12473;&12479;&12531;&12473;&12395;&33258;&21205;&30340;&12395;&36879;&36942;&30340;&12395;&22996;&20219;&12373;&12428;&12414;&12377;&12290;

     

     private Calculator cal = null;
     
     public void jspInit () {
         try {
           InitialContext ctx = new InitialContext();
           cal = (Calculator) ctx.lookup(
                       "EJB3Trail/StatelessCalculator/local");
         } catch (Exception e) {
           e.printStackTrace ();
         }
     }
     
     // ... ...
     
     public void service (Request req, Response rep) {
         // ... ...
         double res = cal.calculate(start, end, growthrate, saving);
     }
    

     

    &12522;&12514;&12540;&12488;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12392;&12525;&12540;&12459;&12523;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&65288;Remote and local interface&65289;

     

    A session bean can also implement multiple interfaces. Each interface targeting a different type of client. By default, the interface is for a "local" client that runs in the same JVM as the EJB 3.0 container. For instance, in the example above, both the JSP page and the EJB 3.0 container run in the JBoss JVM. When the client looks up a bean stub via the local interface, the container returns a Java reference of the session bean object. Method call invocations over Java references are very fast and efficient.

     

    &12475;&12483;&12471;&12519;&12531;Bean&12395;&12399;&35079;&25968;&12398;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12434;&23455;&35013;&12391;&12365;&12414;&12377;&12290;&12381;&12428;&12382;&12428;&12398;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12399;&12289;&30064;&12394;&12427;&12479;&12452;&12503;&12398;&12463;&12521;&12452;&12450;&12531;&12488;&21521;&12369;&12391;&12377;&12290;&12487;&12501;&12457;&12523;&12488;&12391;&12399;&12289;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12399;EJB 3.0&12467;&12531;&12486;&12490;&12392;&21516;&12376;JVM&12391;&21205;&12367;&12525;&12540;&12459;&12523;&12463;&12521;&12452;&12450;&12531;&12488;&21521;&12369;&12391;&12377;&12290;&12383;&12392;&12360;&12400;&21069;&36848;&12398;&20363;&12391;&12399;&12289;JSP&12506;&12540;&12472;&12392;EJB 3.0&12467;&12531;&12486;&12490;&12399;&20849;&12395;JBoss&12398;JVM&12391;&31292;&21205;&12375;&12414;&12377;&12290;&12463;&12521;&12452;&12450;&12531;&12488;&12364;&12525;&12540;&12459;&12523;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12391;Bean&12398;&12473;&12479;&12502;&12434;&12523;&12483;&12463;&12450;&12483;&12503;&12377;&12427;&12392;&12289;&12467;&12531;&12486;&12490;&12399;&12475;&12483;&12471;&12519;&12531;Bean&12458;&12502;&12472;&12455;&12463;&12488;&12398;Java&21442;&29031;&12434;&36820;&12375;&12414;&12377;&12290;Java&21442;&29031;&12434;&20171;&12375;&12383;&12513;&12477;&12483;&12489;&21628;&12403;&20986;&12375;&12399;&38750;&24120;&12395;&39640;&36895;&12391;&21177;&29575;&30340;&12391;&12377;&12290;

     

    Another type of session bean interface, the remote interface, is for remote clients. When a client looks up the session bean stub via the remote interface, the container returns a serialized stub object that implements the remote interface. The remote stub knows how to pass remote procedure calls (RPCs) to the server, even in a clustered environment. The remote interface is also a plain old Java interface. The remote interface may contain exactly the same methods as the local interface. But in many cases, you might want to expose different methods in the session bean via local and remote interfaces. For instance, for the CalculatorBean bean, the remote interface might expose an additional method to give the remote client some information about the server.

     

    &12475;&12483;&12471;&12519;&12531;Bean&12398;&12418;&12358;&19968;&12388;&12398;&12479;&12452;&12503;&12398;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12391;&12354;&12427;&12522;&12514;&12540;&12488;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12399;&12289;&12522;&12514;&12540;&12488;&12463;&12521;&12452;&12450;&12531;&12488;&21521;&12369;&12391;&12377;&12290;&12463;&12521;&12452;&12450;&12531;&12488;&12364;&12522;&12514;&12540;&12488;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12391;Bean&12398;&12473;&12479;&12502;&12434;&12523;&12483;&12463;&12450;&12483;&12503;&12377;&12427;&12392;&12289;&12467;&12531;&12486;&12490;&12399;&12522;&12514;&12540;&12488;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12434;&23455;&35013;&12375;&12383;&12473;&12479;&12502;&12398;&30452;&21015;&21270;&12373;&12428;&12383;&12458;&12502;&12472;&12455;&12463;&12488;&12434;&36820;&12375;&12414;&12377;&12290;&12522;&12514;&12540;&12488;&12473;&12479;&12502;&12399;&12289;&36960;&38548;&25163;&32154;&12365;&21628;&12403;&20986;&12375;&65288;RPC&65289;&12434;&12469;&12540;&12496;&12395;&37197;&20449;&12377;&12427;&26041;&27861;&12434;&30693;&12387;&12390;&12356;&12414;&12377;&12290;&12463;&12521;&12473;&12479;&29872;&22659;&12391;&12418;&21516;&27096;&12391;&12377;&12290;&12522;&12514;&12540;&12488;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12418;&21336;&12394;&12427;Java&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12391;&12377;&12290;&12522;&12514;&12540;&12488;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12399;&12525;&12540;&12459;&12523;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12392;&20840;&12367;&21516;&12376;&12513;&12477;&12483;&12489;&12391;&12418;&27083;&12356;&12414;&12379;&12435;&12290;&12375;&12363;&12375;&12289;&12475;&12483;&12471;&12519;&12531;Bean&12398;&12522;&12514;&12540;&12488;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12392;&12525;&12540;&12459;&12523;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12391;&12399;&30064;&12394;&12427;&12513;&12477;&12483;&12489;&12434;&34920;&29694;&12375;&12383;&12356;&22580;&21512;&12364;&22810;&12356;&12391;&12375;&12423;&12358;&12290;&12383;&12392;&12360;&12400;CalculatorBean&12398;&22580;&21512;&12289;&12522;&12514;&12540;&12488;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12391;&12399;&12289;&12469;&12540;&12496;&12398;&24773;&22577;&12434;&12522;&12514;&12540;&12488;&12463;&12521;&12452;&12450;&12531;&12488;&12395;&20253;&12360;&12427;&36861;&21152;&12398;&12513;&12477;&12483;&12489;&12364;&24517;&35201;&12391;&12375;&12423;&12358;&12290;

     

     public interface RemoteCalculator {
     
       public double calculate (int start, int end, 
                                double growthrate, double saving);
       public String getServerInfo ();
     }
    

     

    NOTE: Using the remote interface involves the serialization and de-serialization of the stub, and all calls to the bean instance are made over the network. It is considerably less efficient than using the local interface. You should avoid looking up a remote interface from a local client.

     

    &27880;&24847;&65306;&12522;&12514;&12540;&12488;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12391;&12399;&12289;&12473;&12479;&12502;&12458;&12502;&12472;&12455;&12463;&12488;&12398;&30452;&21015;&21270;&12392;&30452;&21015;&21270;&24489;&20803;&12364;&34892;&12431;&12428;&12289;Bean&12452;&12531;&12473;&12479;&12531;&12473;&12398;&12377;&12409;&12390;&12398;&21628;&12403;&20986;&12375;&12399;&12493;&12483;&12488;&12527;&12540;&12463;&36234;&12375;&12395;&34892;&12431;&12428;&12414;&12377;&12290;&12525;&12540;&12459;&12523;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12424;&12426;&12418;&12363;&12394;&12426;&38750;&21177;&29575;&30340;&12391;&12377;&12290;&12525;&12540;&12459;&12523;&12463;&12521;&12452;&12450;&12531;&12488;&12391;&12398;&12522;&12514;&12540;&12488;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12398;&12523;&12483;&12463;&12450;&12483;&12503;&12399;&12394;&12427;&12409;&12367;&36991;&12369;&12390;&12367;&12384;&12373;&12356;&12290;

     

    In the session bean implementation, you can use the @Local and @Remote annotations to specify the local and remote interfaces for this bean. You can also use the @LocalBinding and @RemoteBinding annotations to specify alternative JNDI names for those two interfaces. Here is an example bean that implements both a local and remote interface with alternative JNDI bindings.

     

    &12475;&12483;&12471;&12519;&12531;Bean&12398;&23455;&35013;&12391;&12399;&12289;@Local&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12392;@Remote&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12434;&29992;&12356;&12390;&12525;&12540;&12459;&12523;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12392;&12522;&12514;&12540;&12488;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12434;&25351;&23450;&12391;&12365;&12414;&12377;&12290;&12414;&12383;&12289;@LocalBinding&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12392;@RemoteBinding&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12391;&12289;&12371;&12428;&12425;&12398;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12398;&20195;&26367;JNDI&21517;&12434;&25351;&23450;&12391;&12365;&12414;&12377;&12290;&27425;&12395;&31034;&12377;&12398;&12399;&12289;&20195;&26367;JNDI&21517;&12434;&12381;&12428;&12382;&12428;&25345;&12388;&12525;&12540;&12459;&12523;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12392;&12522;&12514;&12540;&12488;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12398;&20363;&12391;&12377;&12290;

     

     @Stateless
     @Local ({Calculator.class})
     @LocalBinding (jndiBinding="EJB3Trail/LocalCalculator")
     @Remote ({RemoteCalculator.class})
     @RemoteBinding (jndiBinding="EJB3Trail/RemoteCalculator")
     public class LocalRemoteCalculator implements Calculator, RemoteCalculator {
     
       public double calculate (int start, int end, 
                                double growthrate, double saving) {
         double tmp = Math.pow(1. + growthrate / 12., 12. * (end - start) + 1);
         return saving * 12. * (tmp - 1) / growthrate;
       }
     
       public String getServerInfo () {
         return "This is the JBoss EJB 3.0 TrailBlazer";
       }
     }
    

     

    The @Local and @Remote annotations can also be used to tag session bean interfaces instead of the bean implementation class. For instance, the following code snippet specifies that the RemoteCalculator is a remote interface. With that, you do not need the @Remote tag on CalculatorBean anymore.

     

    @Local&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12392;@Remote&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12399;&12289;Bean&12398;&23455;&35013;&12463;&12521;&12473;&12398;&26367;&12431;&12426;&12395;&12289;&12475;&12483;&12471;&12519;&12531;Bean&12398;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12434;&25351;&23450;&12377;&12427;&12371;&12392;&12364;&12391;&12365;&12414;&12377;&12290;&12383;&12392;&12360;&12400;&27425;&12395;&31034;&12377;&12467;&12540;&12489;&12398;&25244;&31883;&12399;&12289;RemoteCalculator&12364;&12522;&12514;&12540;&12488;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12391;&12354;&12427;&12371;&12392;&12434;&31034;&12375;&12390;&12356;&12414;&12377;&12290;&12371;&12358;&12377;&12427;&12371;&12392;&12391;&12289;CalculatorBean&12395;@Remote&12434;&25351;&23450;&12377;&12427;&24517;&35201;&12399;&12394;&12367;&12394;&12426;&12414;&12377;&12290;

     

     @Remote
     public interface RemoteCalculator {
       // ... ...
     }
    

     

    &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 interface

    • StatelessCalculator.java: The session bean implementation for the local interface

    • RemoteCalculator.java: The remote interface

    • StatelessCalculator2.java: The session bean implementation for the local and remote interface with alternative JNDI binding names

     

    • Calculator.java: &12525;&12540;&12459;&12523;&12452;&12531;&12479;&12501;&12455;&12540;&12473;

    • StatelessCalculator.java: &12525;&12540;&12459;&12523;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12398;&12475;&12483;&12471;&12519;&12531;Bean&23455;&35013;

    • RemoteCalculator.java: &12522;&12514;&12540;&12488;&12452;&12531;&12479;&12501;&12455;&12540;&12473;

    • StatelessCalculator2.java: &20195;&26367;JNDI&21517;&12434;&12381;&12428;&12382;&12428;&25345;&12388;&12525;&12540;&12459;&12523;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12392;&12522;&12514;&12540;&12488;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12398;&12475;&12483;&12471;&12519;&12531;Bean&23455;&35013;

     

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

     

    • calculator.jsp: The client of the calculator session bean (via local interface)

     

    • calculator.jsp: &25237;&36039;&35336;&31639;&12475;&12483;&12471;&12519;&12531;Bean&12398;&12463;&12521;&12452;&12450;&12531;&12488;&65288;&12525;&12540;&12459;&12523;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&32076;&30001;&65289;

     

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

     

    In this trail, you learned how to develop a stateless session bean. In the next trail, we will discuss another type of session beans, stateful session beans, which are very important for web applications.

     

    &12371;&12398;&12488;&12524;&12452;&12523;&12391;&12399;&12289;&12473;&12486;&12540;&12488;&12524;&12473;&12475;&12483;&12471;&12519;&12531;Bean&12398;&38283;&30330;&12395;&12388;&12356;&12390;&23398;&32722;&12375;&12414;&12375;&12383;&12290;&27425;&12398;&12488;&12524;&12452;&12523;&12391;&12399;&12289;Web&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12395;&12399;&27424;&12363;&12379;&12394;&12356;&12418;&12358;&19968;&12388;&12398;&12475;&12483;&12471;&12519;&12531;Bean&12289;&12473;&12486;&12540;&12488;&12501;&12523;&12475;&12483;&12471;&12519;&12531;Bean&12289;&12395;&12388;&12356;&12390;&35696;&35542;&12375;&12414;&12377;&12290;

     

    (c) 2005 JBoss, Inc. All Rights Reserved