1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.jboss.as.quickstarts.ejb.multi.server.app;
18
19 import java.security.Principal;
20 import java.util.Properties;
21
22 import javax.annotation.PostConstruct;
23 import javax.annotation.PreDestroy;
24 import javax.annotation.Resource;
25 import javax.ejb.SessionContext;
26 import javax.ejb.Stateless;
27 import javax.naming.Context;
28 import javax.naming.InitialContext;
29 import javax.naming.NamingException;
30
31 import org.jboss.logging.Logger;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 @Stateless
49 public class MainAppSContextBean implements MainApp {
50 private static final Logger LOGGER = Logger.getLogger(MainAppSContextBean.class);
51
52 private static Integer activeInstances = 0;
53 private static Context appOneScopedEjbContext;
54 private static Context appTwoScopedEjbContextA;
55 private static Context appTwoScopedEjbContextB;
56
57 @Resource
58 SessionContext context;
59
60 private void createAppOneContext() {
61 final Properties ejbClientContextProps = new Properties();
62 ejbClientContextProps.put("endpoint.name", "appMain->appOne_endpoint");
63
64
65 ejbClientContextProps.put("org.jboss.ejb.client.scoped.context", true);
66
67 ejbClientContextProps.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
68 ejbClientContextProps.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED","false");
69
70
71 final String connectionName = "appOneConnection";
72 ejbClientContextProps.put("remote.connections", connectionName);
73
74 ejbClientContextProps.put("remote.connection." + connectionName + ".host", "localhost");
75 ejbClientContextProps.put("remote.connection." + connectionName + ".port", "4547");
76 ejbClientContextProps.put("remote.connection." + connectionName + ".username", "quickuser1");
77 ejbClientContextProps.put("remote.connection." + connectionName + ".password", "quick123+");
78 ejbClientContextProps.put("remote.clusters", "ejb");
79 ejbClientContextProps.put("remote.cluster.ejb.username", "quickuser2");
80 ejbClientContextProps.put("remote.cluster.ejb.password", "quick+123");
81
82 try {
83 appOneScopedEjbContext = (Context)new InitialContext(ejbClientContextProps).lookup("ejb:");
84 } catch (NamingException e) {LOGGER.error("Could not create InitialContext('appOne')", e);}
85 }
86
87 private void createAppTwoContext() {
88 final Properties ejbClientContextProps = new Properties();
89 ejbClientContextProps.put("endpoint.name", "appMain->appTwoA_endpoint");
90
91
92 ejbClientContextProps.put("org.jboss.ejb.client.scoped.context", true);
93
94 ejbClientContextProps.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
95
96 final String connectionName = "appTwoConnection";
97 ejbClientContextProps.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", "false");
98
99 ejbClientContextProps.put("remote.connections", connectionName);
100 ejbClientContextProps.put("remote.connection." + connectionName + ".host", "localhost");
101 ejbClientContextProps.put("remote.connection." + connectionName + ".port", "4647");
102 ejbClientContextProps.put("remote.connection." + connectionName + ".username", "quickuser1");
103 ejbClientContextProps.put("remote.connection." + connectionName + ".password", "quick123+");
104
105 try {
106 appTwoScopedEjbContextA = (Context) new InitialContext(ejbClientContextProps).lookup("ejb:");
107 } catch (NamingException e) {LOGGER.error("Could not create InitialContext('appTwoA')", e);}
108
109
110 ejbClientContextProps.put("endpoint.name", "appMain->appTwoB_endpoint");
111 ejbClientContextProps.put("remote.connection." + connectionName + ".port", "5247");
112 ejbClientContextProps.put("remote.connection." + connectionName + ".username", "quickuser2");
113 ejbClientContextProps.put("remote.connection." + connectionName + ".password", "quick+123");
114
115 try {
116 appTwoScopedEjbContextB = (Context) new InitialContext(ejbClientContextProps).lookup("ejb:");
117 } catch (NamingException e) {LOGGER.error("Could not create InitialContext('appTwoB')", e);}
118 }
119
120 @PostConstruct
121 private void createScopedContext() {
122 synchronized (activeInstances) {
123 if(activeInstances == 0) {
124 LOGGER.info("First instance of " + this.getClass().getSimpleName() + " initializing scoped-context");
125 createAppOneContext();
126 createAppTwoContext();
127 }
128 activeInstances++;
129 }
130 }
131
132 @PreDestroy
133 private void destroyScopedContext() {
134 synchronized (activeInstances) {
135 activeInstances--;
136 if(activeInstances == 0) {
137 LOGGER.info("Last active instance of " + this.getClass().getSimpleName() + " closing scoped-context");
138 try {
139 appOneScopedEjbContext.close();
140 } catch (NamingException e) {
141 LOGGER.error("Could not close the scoped-context of AppOne", e);
142 }
143 appOneScopedEjbContext = null;
144 try {
145 appTwoScopedEjbContextA.close();
146 } catch (NamingException e) {
147 LOGGER.error("Could not close the scoped-context of AppTwo (A)", e);
148 }
149 appTwoScopedEjbContextA = null;
150 try {
151 appTwoScopedEjbContextB.close();
152 } catch (NamingException e) {
153 LOGGER.error("Could not close the scoped-context of AppTwo (B)", e);
154 }
155 appTwoScopedEjbContextB = null;
156 }
157 }
158 }
159
160 @Override
161 public String getJBossNodeName() {
162 return System.getProperty("jboss.node.name");
163 }
164
165 @Override
166 public String invokeAll(String text) {
167 Principal caller = context.getCallerPrincipal();
168 LOGGER.info("[" + caller.getName() + "] " + text);
169 final StringBuilder result = new StringBuilder("MainAppSContext["+ caller.getName() + "]@" + getJBossNodeName());
170
171 try {
172 result.append(" > [ " + invokeAppOne(text));
173 } catch (Exception e) {
174 LOGGER.error("Could not invoke AppOne", e);
175 }
176
177 result.append(" > " + invokeAppTwo(text));
178
179 result.append(" ]");
180
181 return result.toString();
182 }
183
184
185
186
187
188
189
190
191
192
193
194 private String invokeAppOne(String text) {
195 try {
196
197
198
199 final AppOne bean = (AppOne) appOneScopedEjbContext.lookup("jboss-ejb-multi-server-app-one/ejb//AppOneBean!" + AppOne.class.getName());
200
201 StringBuffer result = new StringBuffer("{");
202 for (int i = 0; i < 8; i++) {
203
204 final String appOneResult = bean.invoke(text);
205 if (i > 0) {
206 result.append(", ");
207 }
208 result.append(appOneResult);
209 }
210 result.append("}");
211
212 LOGGER.info("AppOne (loop) returns : " + result);
213 return result.toString();
214
215 } catch (NamingException e) {
216 LOGGER.error("Could not invoke appOne", e);
217 return null;
218 }
219 }
220
221
222
223
224
225
226
227 private static void saveContextClose(Context iCtx) {
228 if (iCtx != null) {
229 try {
230 LOGGER.info("close Context "+ iCtx.getEnvironment().get("endpoint.name"));
231 iCtx.close();
232
233 } catch (NamingException e) {
234 LOGGER.error("InitialContext can not be closed", e);
235 }
236 }
237 }
238
239
240
241
242
243
244
245
246
247
248
249 private String invokeAppTwo(String text) {
250 AppTwo beanA = null;
251 AppTwo beanB = null;
252
253 try {
254 beanA = (AppTwo) appTwoScopedEjbContextA.lookup("jboss-ejb-multi-server-app-two/ejb//AppTwoBean!" + AppTwo.class.getName());
255 } catch (NamingException e) {LOGGER.error("Could not create InitialContext('appTwoA')");}
256
257 try {
258 beanB = (AppTwo) appTwoScopedEjbContextB.lookup("jboss-ejb-multi-server-app-two/ejb//AppTwoBean!" + AppTwo.class.getName());
259 } catch (NamingException e) {
260 LOGGER.error("Could not create InitialContext('appTwoB')");
261 }
262
263 StringBuffer result = new StringBuffer(" appTwo loop(4 time A-B expected){");
264 for (int i = 0; i < 4; i++) {
265
266 String appResult = beanA.invokeSecured(text);
267 if (i > 0) {
268 result.append(", ");
269 }
270 result.append(appResult);
271 appResult = beanB.invokeSecured(text);
272 result.append(", ");
273 result.append(appResult);
274 }
275 result.append("}");
276
277 LOGGER.info("AppTwo (loop) returns : " + result);
278
279 return result.toString();
280 }
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295 @SuppressWarnings("unused")
296 private String invokeAppTwoAlternative(String text) {
297 AppTwo beanA = null;
298 AppTwo beanB = null;
299
300 final Properties ejbClientContextProps = new Properties();
301 ejbClientContextProps.put("endpoint.name", "appMain->appTwoA_endpoint");
302
303
304 ejbClientContextProps.put("org.jboss.ejb.client.scoped.context", true);
305
306 ejbClientContextProps.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
307
308 final String connectionName = "appTwoConnection";
309 ejbClientContextProps.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", "false");
310
311 ejbClientContextProps.put("remote.connections", connectionName);
312 ejbClientContextProps.put("remote.connection." + connectionName + ".host", "localhost");
313 ejbClientContextProps.put("remote.connection." + connectionName + ".port", "4647");
314 ejbClientContextProps.put("remote.connection." + connectionName + ".username", "quickuser1");
315 ejbClientContextProps.put("remote.connection." + connectionName + ".password", "quick123+");
316
317 Context iCtxA = null;
318 try {
319 iCtxA = (Context) new InitialContext(ejbClientContextProps).lookup("ejb:");
320 beanA = (AppTwo) iCtxA.lookup("jboss-ejb-multi-server-app-two/ejb//AppTwoBean!" + AppTwo.class.getName());
321 } catch (NamingException e) {LOGGER.error("Could not create InitialContext('appTwoA')");}
322
323
324 ejbClientContextProps.put("endpoint.name", "appMain->appTwoB_endpoint");
325 ejbClientContextProps.put("remote.connection." + connectionName + ".port", "5247");
326 ejbClientContextProps.put("remote.connection." + connectionName + ".username", "quickuser2");
327 ejbClientContextProps.put("remote.connection." + connectionName + ".password", "quick+123");
328 Context iCtxB = null;
329 try {
330 iCtxB = (Context) new InitialContext(ejbClientContextProps).lookup("ejb:");
331 beanB = (AppTwo) iCtxB.lookup("jboss-ejb-multi-server-app-two/ejb//AppTwoBean!" + AppTwo.class.getName());
332 } catch (NamingException e) {
333 LOGGER.error("Could not create InitialContext('appTwoB')");
334 }
335
336 StringBuffer result = new StringBuffer(" appTwo loop(4 time A-B expected){");
337 for (int i = 0; i < 4; i++) {
338
339 String appResult = beanA.invokeSecured(text);
340 if (i > 0) {
341 result.append(", ");
342 }
343 result.append(appResult);
344 appResult = beanB.invokeSecured(text);
345 result.append(", ");
346 result.append(appResult);
347 }
348 result.append("}");
349
350 LOGGER.info("AppTwo (loop) returns : " + result);
351
352
353 saveContextClose(iCtxA);
354 iCtxA = null;
355 saveContextClose(iCtxB);
356 iCtxB = null;
357
358 return result.toString();
359 }
360 }