This repository was archived by the owner on Jan 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathconnectorexample003.html
More file actions
591 lines (577 loc) · 20.9 KB
/
Copy pathconnectorexample003.html
File metadata and controls
591 lines (577 loc) · 20.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>The traffic Example</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="css/style.css" rel="stylesheet">
<script src="https://use.fontawesome.com/96c4d89611.js"></script>
</head>
<body>
<table id="doc-title" cellspacing="0" cellpadding="0">
<tr>
<td align="left" valign="top">
<b>Java Platform, Enterprise Edition (Java EE) 8</b><br />
<b>The Java EE Tutorial</b>
<!-- <p class="beta">Beta Draft (Pre-General Availability)</p> -->
</td>
</tr>
</table>
<hr />
<table width="90%" id="top-nav" cellspacing="0" cellpadding="0">
<colgroup>
<col width="12%"/>
<col width="12%"/>
<col width="*"/>
</colgroup>
<tr>
<td align="left">
<a href="connectorexample002.html">
<span class="vector-font"><i class="fa fa-arrow-circle-left" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Previous</span>
</a>
</td>
<td align="left">
<a href="interceptors.html">
<span class=" vector-font"><i class="fa fa-arrow-circle-right vector-font" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Next</span>
</a>
</td>
<td align="right">
<a href="toc.html">
<span class=" vector-font"><i class="fa fa-list vector-font" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Contents</span>
</a>
</td>
</tr>
</table>
<div class="paragraph">
<p>+ The traffic Example</p>
</div>
<div class="paragraph">
<p><a id="CHDJEADB"></a><a id="the-traffic-example"></a></p>
</div>
<div class="sect1">
<h2 id="_the_traffic_example">The traffic Example</h2>
<div class="sectionbody">
<div class="paragraph">
<p>The <code>traffic</code> example demonstrates how to implement and use a simple
inbound resource adapter that receives data from a legacy EIS using a
TCP socket.</p>
</div>
<div class="paragraph">
<p>The example is in the tut-install`/examples/connectors/traffic`
directory. See <a href="usingexamples.html#GFIUD">Chapter 2, "Using the
Tutorial Examples,"</a> for basic information on building and running
sample applications.</p>
</div>
<div class="paragraph">
<p>The example demonstrates the scenario in <a href="#CHDGFGHB">Figure 56-3</a> and
consists of the following modules:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>traffic-eis</code>: A Java SE program that simulates an EIS</p>
</li>
<li>
<p><code>traffic-rar</code>: The inbound resource adapter implementation</p>
</li>
<li>
<p><code>traffic-ejb</code>: A message-driven bean that is the endpoint for incoming
messages</p>
</li>
<li>
<p><code>traffic-war</code>: A web application that displays information from the
message-driven bean</p>
</li>
<li>
<p><code>traffic-ear</code>: An enterprise archive that contains the resource
adapter, the message-driven bean, and the web application</p>
</li>
</ul>
</div>
<div id="CHDGFGHB" class="paragraph">
<div class="title"><strong>Figure 56-3 The traffic Example</strong></div>
<p><span class="image"><img src="img/javaeett_dt_056.png" alt="This figure shows the components of the traffic example: a WAR communicating with an enterprise bean using a JMS topic, and a RAR communicating with an EIS over a TCP socket."></span></p>
</div>
<div class="paragraph">
<p>The <code>traffic-eis</code> module is an auxiliary project that resembles a legacy
traffic information system. It contains a Java SE program that sends
traffic status updates for several cities to any subscribed client. The
program sends the updates in JSON format over a TCP socket. For example,
a traffic update looks like this:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">{"report":[
{"city":"City1", "access":"AccessA", "status":"GOOD"},
{"city":"City1", "access":"AccessB", "status":"CONGESTED"},
...
{"city":"City5", "access":"AccessE", "status":"SLOW"}
]}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The <code>traffic-rar</code> module implements the inbound contract of the Java EE
Connector Architecture. This module subscribes to the traffic
information system using the TCP port indicated by the configuration
provided by the MDB and invokes the methods of the MDB to process
traffic information updates.</p>
</div>
<div class="paragraph">
<p>The <code>traffic-ejb</code> module contains a message-driven bean that activates
the resource adapter with a configuration parameter (the TCP port to
subscribe to the traffic information system). The MDB contains a method
to process the traffic information updates. This method filters the
updates for a particular city and publishes the results to a Java
Message Service (JMS) topic.</p>
</div>
<div class="paragraph">
<p>The <code>traffic-war</code> module contains a message-driven bean that receives
filtered traffic information updates from the JMS topic asynchronously
and sends them to the clients using a WebSocket endpoint.</p>
</div>
<div class="paragraph">
<p><a id="sthref248"></a><a id="using-the-inbound-resource-adapter"></a></p>
</div>
<div class="sect2">
<h3 id="_using_the_inbound_resource_adapter">Using the Inbound Resource Adapter</h3>
<div class="paragraph">
<p>In most cases, Java EE application developers use inbound resource
adapters developed by a third party. To use an inbound resource adapter,
a Java EE application includes a message-driven bean with the following
characteristics.</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The MDB implements the business interface defined by the resource
adapter.</p>
</li>
<li>
<p>The MDB specifies configuration parameters to activate the resource
adapter.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The business interface defined by the resource adapter is not specified
in the Java EE Connector Architecture; it is specific to the EIS.</p>
</div>
<div class="paragraph">
<p>The MDB in this example is defined as follows:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@MessageDriven(
activationConfig = {
@ActivationConfigProperty(propertyName = "port",
propertyValue = "4008")
}
)
public class TrafficMdb implements TrafficListener { ... }</code></pre>
</div>
</div>
<div class="paragraph">
<p>The <code>TrafficListener</code> interface is defined in the API package of the
resource adapter. The resource adapter requires the MDB to provide the
<code>port</code> property.</p>
</div>
<div class="paragraph">
<p>When the MDB is deployed, it activates the <code>traffic-rar</code> resource
adapter, which invokes the methods of the MDB to process traffic
information updates. Then the MDB filters the updates for a particular
city and publishes the results to a JMS topic.</p>
</div>
<div class="paragraph">
<p>In this particular example, the <code>TrafficListener</code> interface is empty. In
addition to this interface, the resource adapter provides the
<code>TrafficCommand</code> annotation and uses reflection to discover which
methods in the MDB are decorated with this annotation:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@MessageDriven(...)
public class TrafficMdb implements TrafficListener {
@TrafficCommand(name="report", info="Process report")
public void processReport(String jsonReport) { ... }
...
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>This approach enables you to adapt the MDB to support new features in
the EIS without having to modify the <code>TrafficListener</code> interface or the
resource adapter module.</p>
</div>
<div class="paragraph">
<p><a id="sthref249"></a><a id="implementing-the-inbound-resource-adapter"></a></p>
</div>
</div>
<div class="sect2">
<h3 id="_implementing_the_inbound_resource_adapter">Implementing the Inbound Resource Adapter</h3>
<div class="paragraph">
<p>The <code>traffic-rar</code> module implements the inbound resource adapter
contract from the Java EE Connector Architecture for the simple traffic
information system (EIS) used in this example. The architecture of the
inbound resource adapter is shown in <a href="#CHDHADDC">Figure 56-4</a>.</p>
</div>
<div id="CHDHADDC" class="paragraph">
<div class="title"><strong>Figure 56-4 Architecture of the traffic Example</strong></div>
<p><span class="image"><img src="img/javaeett_dt_057.png" alt="This figure shows the classes in each of the components of the traffic example."></span></p>
</div>
<div class="paragraph">
<p>The <code>traffic-rar</code> module implements the interfaces listed in
<a href="#CHDEDEAF">Table 56-3</a>.</p>
</div>
<div class="paragraph">
<p><a id="sthref251"></a><a id="CHDEDEAF"></a></p>
</div>
<div class="paragraph">
<p><strong>Table 56-3 Interfaces Implemented in the traffic-rar Module</strong></p>
</div>
<table class="tableblock frame-all grid-all" style="width: 99%;">
<colgroup>
<col style="width: 20%;">
<col style="width: 20%;">
<col style="width: 60%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><strong>Package</strong></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><strong>Interface</strong></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><strong>Description</strong></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>javax.resource.spi</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>ResourceAdapter</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Defines the lifecycle methods
of the resource adapter.</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>javax.resource.spi</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>ActivationSpec</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Defines the configuration
parameters that the MDB provides to activate the inbound resource
adapter.</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>javax.resource.spi</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Work</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">The traffic service subscriber implements
this interface from the work management contract to wait for traffic
updates on a separate thread.</p></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>When an MDB activates the inbound resource adapter, the container
invokes the <code>endpointActivation</code> method in the <code>TrafficResourceAdapter</code>
class:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">@Connector(...)
public class TrafficResourceAdapter implements ResourceAdapter, Serializable {
...
@Override
public void endpointActivation(MessageEndpointFactory endpointFactory,
ActivationSpec spec)
throws ResourceException {
Class endpointClass = endpointFactory.getEndpointClass();
/* this method is called from a new thread in the example:
MessageEndpoint endpoint = endpointFactory.createEndpoint(null); */
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The <code>getEndpointClass</code> method returns the <code>Class</code> type of the MDB
performing the activation, which enables the resource adapter to use
reflection to find methods annotated with <code>@TrafficCommand</code> in the MDB.</p>
</div>
<div class="paragraph">
<p>The <code>createEndpoint</code> method returns an instance of the MDB. The resource
adapter uses this instance to invoke the methods of the MDB when it
receives requests from the EIS.</p>
</div>
<div class="paragraph">
<p>After obtaining the message endpoint instance (MDB), the resource
adapter uses the work management contract to create the traffic service
subscriber thread that receives traffic updates from the EIS. The
resource adapter obtains the <code>WorkManager</code> instance from the bootstrap
context as follows:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">WorkManager workManager;
...
@Override
public void start(BootstrapContext ctx) ... {
workManager = ctx.getWorkManager();
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The resource adapter schedules the traffic service subscriber thread
using the work manager:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">tSubscriber = new TrafficServiceSubscriber(tSpec, endpoint);
workManager.scheduleWork(tSubscriber);</code></pre>
</div>
</div>
<div class="paragraph">
<p>The <code>TrafficServiceSubscriber</code> class implements the
<code>javax.resource.spi.Work</code> interface from the work management contract.</p>
</div>
<div class="paragraph">
<p>The traffic service subscriber thread uses reflection to invoke the
methods in the MDB:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">private String callMdb(MessageEndpoint mdb, Method command,
String... params) ... {
String resp;
/* this code contains proper exception handling in the sources */
mdb.beforeDelivery(command);
Object ret = command.invoke(mdb, (Object[]) params);
resp = (String) ret;
mdb.afterDelivery();
return resp;
}
For more details, see the code and the comments in the traffic-rar module.</code></pre>
</div>
</div>
<div class="paragraph">
<p><a id="sthref252"></a><a id="running-the-traffic-example"></a></p>
</div>
</div>
<div class="sect2">
<h3 id="_running_the_traffic_example">Running the traffic Example</h3>
<div class="paragraph">
<p>You can use either NetBeans IDE or Maven to build, package, deploy, and
run the <code>traffic</code> example.</p>
</div>
<div class="paragraph">
<p>The following topics are addressed here:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#BABIJJEH">To Run the traffic Example Using NetBeans IDE</a></p>
</li>
<li>
<p><a href="#BABBBGBA">To Run the traffic Example Using Maven</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p><a id="BABIJJEH"></a><a id="to-run-the-traffic-example-using-netbeans-ide"></a></p>
</div>
<div class="sect3">
<h4 id="_to_run_the_traffic_example_using_netbeans_ide">To Run the traffic Example Using NetBeans IDE</h4>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Make sure that GlassFish Server has been started (see
<a href="usingexamples002.html#BNADI">Starting and Stopping GlassFish
Server</a>).</p>
</li>
<li>
<p>From the File menu, choose Open Project.</p>
</li>
<li>
<p>In the Open Project dialog box, navigate to:</p>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">tut-install/examples/connectors</code></pre>
</div>
</div>
</li>
<li>
<p>Select the <code>traffic</code> folder.</p>
</li>
<li>
<p>Click Open Project.</p>
</li>
<li>
<p>In the Projects tab, expand the <code>traffic</code> node.</p>
</li>
<li>
<p>Right-click the <code>traffic-eis</code> module and select Open Project.</p>
</li>
<li>
<p>Right-click the <code>traffic-eis</code> project and select Run.</p>
<div class="paragraph">
<p>The messages from the EIS appear on the Output tab:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">Traffic EIS accepting connections on port 4008</code></pre>
</div>
</div>
</li>
<li>
<p>In the Projects tab, right-click the <code>traffic</code> project and select
Clean and Build.</p>
<div class="paragraph">
<p>This command builds and packages the resource adapter, the MDB, and the
web application into an EAR archive and deploys it. The server log shows
the call sequence that activates the resource adapter and the filtered
traffic updates for City1.</p>
</div>
</li>
<li>
<p>Open the following URL in a web browser:</p>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">http://localhost:8080/traffic/</code></pre>
</div>
</div>
<div class="paragraph">
<p>The web interface shows filtered traffic updates for City1 every few
seconds.</p>
</div>
</li>
<li>
<p>After undeploying the <code>traffic-ear</code> application, close the
<code>traffic-eis</code> application from the status bar.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p><a id="BABBBGBA"></a><a id="to-run-the-traffic-example-using-maven"></a></p>
</div>
</div>
<div class="sect3">
<h4 id="_to_run_the_traffic_example_using_maven">To Run the traffic Example Using Maven</h4>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Make sure that GlassFish Server has been started (see
<a href="usingexamples002.html#BNADI">Starting and Stopping GlassFish
Server</a>).</p>
</li>
<li>
<p>In a terminal window, go to:</p>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">tut-install/examples/connectors/traffic/traffic-eis/</code></pre>
</div>
</div>
</li>
<li>
<p>Enter the following command in the terminal window:</p>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">mvn install</code></pre>
</div>
</div>
<div class="paragraph">
<p>This command builds and packages the traffic EIS.</p>
</div>
</li>
<li>
<p>Enter the following command in the terminal window:</p>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">mvn exec:java</code></pre>
</div>
</div>
<div class="paragraph">
<p>The messages from the EIS appear in the terminal window:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">Traffic EIS accepting connections on port 4008</code></pre>
</div>
</div>
<div class="paragraph">
<p>Leave this terminal window open.</p>
</div>
</li>
<li>
<p>Open a new terminal window and go to:</p>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">tut-install/examples/connectors/traffic/</code></pre>
</div>
</div>
</li>
<li>
<p>Enter the following command:</p>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">mvn install</code></pre>
</div>
</div>
<div class="paragraph">
<p>This command builds and packages the resource adapter, the MDB, and the
web application into an EAR archive and deploys it. The server log shows
the call sequence that activates the resource adapter and the filtered
traffic updates for City1.</p>
</div>
</li>
<li>
<p>Open the following URL in a web browser:</p>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-oac_no_warn" data-lang="oac_no_warn">http://localhost:8080/traffic/</code></pre>
</div>
</div>
<div class="paragraph">
<p>The web interface shows the filtered traffic updates for City1 every few
seconds.</p>
</div>
</li>
<li>
<p>After undeploying the <code>traffic-ear</code> application, press Ctrl+C in the
first terminal window to close the <code>traffic-eis</code> application.</p>
</li>
</ol>
</div>
</div>
</div>
</div>
</div>
<hr />
<table width="90%" id="bottom-nav" cellspacing="0" cellpadding="0">
<colgroup>
<col width="12%"/>
<col width="12%"/>
<col width="*"/>
</colgroup>
<tr>
<td align="left">
<a href="connectorexample002.html">
<span class=" vector-font"><i class="fa fa-arrow-circle-left" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Previous</span>
</a>
</td>
<td align="left">
<a href="interceptors.html">
<span class="vector-font"><i class="fa fa-arrow-circle-right vector-font" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Next</span>
</a>
</td>
<td align="right">
<a href="toc.html">
<span class="vector-font"><i class="fa fa-list vector-font" aria-hidden="true"></i></span>
<span style="position:relative;top:-2px;">Contents</span>
</a>
</td>
</tr>
</table>
<span id="copyright">
<a href="img/cpyr.adoc">
<img src="img/oracle.gif" height="10px" alt="Oracle Logo" />
<span>Copyright © 2017, Oracle and/or its affiliates. All rights reserved.</span>
</a>
</span>
<!--<p align="center" class="beta">Beta Draft (Pre-General Availability)</p> -->
</body>
</html>