1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
30
31
32
33
34 public class RemoteEJBClient {
35
36 public static void main(String[] args) throws Exception {
37
38 invokeStatelessBean();
39
40
41 invokeStatefulBean();
42 }
43
44
45
46
47
48
49 private static void invokeStatelessBean() throws NamingException {
50
51 final RemoteCalculator statelessRemoteCalculator = lookupRemoteStatelessCalculator();
52 System.out.println("Obtained a remote stateless calculator for invocation");
53
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
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
78
79
80
81 private static void invokeStatefulBean() throws NamingException {
82
83 final RemoteCounter statefulRemoteCounter = lookupRemoteStatefulCounter();
84 System.out.println("Obtained a remote stateful counter for invocation");
85
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
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
104
105
106
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132 return (RemoteCalculator) context.lookup("ejb:/jboss-ejb-remote-server-side/CalculatorBean!"
133 + RemoteCalculator.class.getName());
134 }
135
136
137
138
139
140
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166 return (RemoteCounter) context.lookup("ejb:/jboss-ejb-remote-server-side/CounterBean!"
167 + RemoteCounter.class.getName() + "?stateful");
168 }
169 }