View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.jboss.as.quickstarts.ejb.remote.client;
18  
19  import org.jboss.as.quickstarts.ejb.remote.stateful.RemoteCounter;
20  import org.jboss.as.quickstarts.ejb.remote.stateless.RemoteCalculator;
21  
22  import javax.naming.Context;
23  import javax.naming.InitialContext;
24  import javax.naming.NamingException;
25  
26  import java.util.Hashtable;
27  
28  /**
29   * A sample program which acts a remote client for a EJB deployed on AS7 server. This program shows how to lookup stateful and
30   * stateless beans via JNDI and then invoke on them
31   * 
32   * @author Jaikiran Pai
33   */
34  public class RemoteEJBClient {
35  
36      public static void main(String[] args) throws Exception {
37          // Invoke a stateless bean
38          invokeStatelessBean();
39  
40          // Invoke a stateful bean
41          invokeStatefulBean();
42      }
43  
44      /**
45       * Looks up a stateless bean and invokes on it
46       * 
47       * @throws NamingException
48       */
49      private static void invokeStatelessBean() throws NamingException {
50          // Let's lookup the remote stateless calculator
51          final RemoteCalculator statelessRemoteCalculator = lookupRemoteStatelessCalculator();
52          System.out.println("Obtained a remote stateless calculator for invocation");
53          // invoke on the remote calculator
54          int a = 204;
55          int b = 340;
56          System.out.println("Adding " + a + " and " + b + " via the remote stateless calculator deployed on the server");
57          int sum = statelessRemoteCalculator.add(a, b);
58          System.out.println("Remote calculator returned sum = " + sum);
59          if (sum != a + b) {
60              throw new RuntimeException("Remote stateless calculator returned an incorrect sum " + sum + " ,expected sum was "
61                      + (a + b));
62          }
63          // try one more invocation, this time for subtraction
64          int num1 = 3434;
65          int num2 = 2332;
66          System.out.println("Subtracting " + num2 + " from " + num1
67                  + " via the remote stateless calculator deployed on the server");
68          int difference = statelessRemoteCalculator.subtract(num1, num2);
69          System.out.println("Remote calculator returned difference = " + difference);
70          if (difference != num1 - num2) {
71              throw new RuntimeException("Remote stateless calculator returned an incorrect difference " + difference
72                      + " ,expected difference was " + (num1 - num2));
73          }
74      }
75  
76      /**
77       * Looks up a stateful bean and invokes on it
78       * 
79       * @throws NamingException
80       */
81      private static void invokeStatefulBean() throws NamingException {
82          // Let's lookup the remote stateful counter
83          final RemoteCounter statefulRemoteCounter = lookupRemoteStatefulCounter();
84          System.out.println("Obtained a remote stateful counter for invocation");
85          // invoke on the remote counter bean
86          final int NUM_TIMES = 5;
87          System.out.println("Counter will now be incremented " + NUM_TIMES + " times");
88          for (int i = 0; i < NUM_TIMES; i++) {
89              System.out.println("Incrementing counter");
90              statefulRemoteCounter.increment();
91              System.out.println("Count after increment is " + statefulRemoteCounter.getCount());
92          }
93          // now decrementing
94          System.out.println("Counter will now be decremented " + NUM_TIMES + " times");
95          for (int i = NUM_TIMES; i > 0; i--) {
96              System.out.println("Decrementing counter");
97              statefulRemoteCounter.decrement();
98              System.out.println("Count after decrement is " + statefulRemoteCounter.getCount());
99          }
100     }
101 
102     /**
103      * Looks up and returns the proxy to remote stateless calculator bean
104      * 
105      * @return
106      * @throws NamingException
107      */
108     private static RemoteCalculator lookupRemoteStatelessCalculator() throws NamingException {
109         final Hashtable<String, String> jndiProperties = new Hashtable<String, String>();
110         jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
111         final Context context = new InitialContext(jndiProperties);
112 
113         // The JNDI lookup name for a stateless session bean has the syntax of:
114         // ejb:<appName>/<moduleName>/<distinctName>/<beanName>!<viewClassName>
115         //
116         // <appName> The application name is the name of the EAR that the EJB is deployed in
117         // (without the .ear). If the EJB JAR is not deployed in an EAR then this is
118         // blank. The app name can also be specified in the EAR's application.xml
119         //
120         // <moduleName> By the default the module name is the name of the EJB JAR file (without the
121         // .jar suffix). The module name might be overridden in the ejb-jar.xml
122         //
123         // <distinctName> : AS7 allows each deployment to have an (optional) distinct name.
124         // This example does not use this so leave it blank.
125         //
126         // <beanName> : The name of the session been to be invoked.
127         //
128         // <viewClassName>: The fully qualified classname of the remote interface. Must include
129         // the whole package name.
130 
131         // let's do the lookup
132         return (RemoteCalculator) context.lookup("ejb:/jboss-ejb-remote-server-side/CalculatorBean!"
133                 + RemoteCalculator.class.getName());
134     }
135 
136     /**
137      * Looks up and returns the proxy to remote stateful counter bean
138      * 
139      * @return
140      * @throws NamingException
141      */
142     private static RemoteCounter lookupRemoteStatefulCounter() throws NamingException {
143         final Hashtable<String, String> jndiProperties = new Hashtable<String, String>();
144         jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
145         final Context context = new InitialContext(jndiProperties);
146 
147         // The JNDI lookup name for a stateful session bean has the syntax of:
148         // ejb:<appName>/<moduleName>/<distinctName>/<beanName>!<viewClassName>?stateful
149         //
150         // <appName> The application name is the name of the EAR that the EJB is deployed in
151         // (without the .ear). If the EJB JAR is not deployed in an EAR then this is
152         // blank. The app name can also be specified in the EAR's application.xml
153         //
154         // <moduleName> By the default the module name is the name of the EJB JAR file (without the
155         // .jar suffix). The module name might be overridden in the ejb-jar.xml
156         //
157         // <distinctName> : AS7 allows each deployment to have an (optional) distinct name.
158         // This example does not use this so leave it blank.
159         //
160         // <beanName> : The name of the session been to be invoked.
161         //
162         // <viewClassName>: The fully qualified classname of the remote interface. Must include
163         // the whole package name.
164 
165         // let's do the lookup
166         return (RemoteCounter) context.lookup("ejb:/jboss-ejb-remote-server-side/CounterBean!"
167                 + RemoteCounter.class.getName() + "?stateful");
168     }
169 }