-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathjavaee7-hol.xml
More file actions
3954 lines (3474 loc) · 164 KB
/
javaee7-hol.xml
File metadata and controls
3954 lines (3474 loc) · 164 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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
<?asciidoc-toc?>
<?asciidoc-numbered?>
<book xmlns="http://docbook.org/ns/docbook" lang="en">
<bookinfo>
<title>Java EE 7 Hands-on Lab</title>
<date>Jun 25, 2014</date>
<author>
<firstname>Arun</firstname>
<surname>Gupta</surname>
<email>https://twitter.com/arungupta[@arungupta]</email>
</author>
<authorinitials>AG</authorinitials>
<revhistory>
<revision>
<revnumber>2.1</revnumber>
<date>Jun 25, 2014</date>
<authorinitials>AG</authorinitials>
</revision>
</revhistory>
</bookinfo>
<chapter id="_introduction">
<title>Introduction</title>
<simpara>The Java EE 7 platform continues the ease of development push that
characterized prior releases by bringing further simplification to
enterprise development. It adds new and important APIs such as the REST
client API in JAX-RS 2.0 and the long awaited Batch Processing API. Java
Message Service 2.0 has undergone an extreme makeover to align with the
improvements in the Java language. There are plenty of improvements to
several other components. Newer web standards like HTML 5, WebSocket,
and JSON processing are embraced to build modern web applications.</simpara>
<simpara>This hands-on lab will build a typical 3-tier end-to-end application
using the following Java EE 7 technologies:</simpara>
<itemizedlist>
<listitem>
<simpara>Java API for WebSocket 1.0 (JSR 356)</simpara>
</listitem>
<listitem>
<simpara>Batch Applications for the Java Platform 1.0 (JSR 352)</simpara>
</listitem>
<listitem>
<simpara>Java API for JSON Processing 1.0 (JSR 353)</simpara>
</listitem>
<listitem>
<simpara>Java API for RESTful Web Services 2.0 (JSR 339)</simpara>
</listitem>
<listitem>
<simpara>Java Message Service 2.0 (JSR 343)</simpara>
</listitem>
<listitem>
<simpara>Java Persistence API 2.1 (JSR 338)</simpara>
</listitem>
<listitem>
<simpara>JavaServer Faces 2.2 (JSR 344)</simpara>
</listitem>
<listitem>
<simpara>Contexts and Dependency Injection 1.1 (JSR 346)</simpara>
</listitem>
<listitem>
<simpara>Bean Validation 1.1 (JSR 349)</simpara>
</listitem>
<listitem>
<simpara>Java Transaction API 1.2 (JSR 907)</simpara>
</listitem>
</itemizedlist>
<simpara>Together these APIs will allow you to be more productive by simplifying enterprise development.</simpara>
<simpara>The latest version of this document can be downloaded from <ulink url="https://github.com/javaee-samples/javaee7-hol/blob/master/docs/asciidoc/javaee7-hol.html">javaee7-hol.html</ulink>. Please file issues or send pull requests for any updates.</simpara>
<section id="_software_requirement">
<title>Software Requirement</title>
<simpara>The following software needs to be downloaded and installed:</simpara>
<itemizedlist>
<listitem>
<simpara>JDK 7 from
<ulink url="http://www.oracle.com/technetwork/java/javase/downloads/index.html">http://www.oracle.com/technetwork/java/javase/downloads/index.html</ulink>.</simpara>
</listitem>
<listitem>
<simpara><emphasis role="strong">Application Server</emphasis>: This lab can use WildFly 8 or GlassFish 4 as the application server. This document provides instructions for
WildFly 8.</simpara>
</listitem>
<listitem>
<simpara><emphasis role="strong">IDE</emphasis>: NetBeans 8.0+, JBoss Developer Studio (Eclipse-based), or IntelliJ IDEA 13 can be used. This document provides instructions for
NetBeans 8.</simpara>
<simpara>Download “All” or “Java EE” version from
<ulink url="http://netbeans.org/downloads/">http://netbeans.org/downloads/</ulink>. A
snapshot of the downloads page is shown and highlights the exact
‘Download’ button to be clicked.</simpara>
<figure>
<title>NetBeans download</title>
<mediaobject>
<imageobject>
<imagedata fileref="images/1.1-netbeans-download.png"/>
</imageobject>
<textobject><phrase>1.1 netbeans download</phrase></textobject>
</mediaobject>
</figure>
<simpara>WildFly 8 needs to be downloaded from <ulink url="http://wildfly.org/downloads/">wildfly.org</ulink> and configured in NetBeans IDE following the instructions in <xref linkend="appendix-wildfly-netbeans"/>.</simpara>
<tip>
<simpara><xref linkend="appendix-wildfly-idea"/> explains how to configure WildFly in IntelliJ IDEA.</simpara>
</tip>
</listitem>
</itemizedlist>
</section>
</chapter>
<chapter id="_problem_statement">
<title>Problem Statement</title>
<simpara>This hands-on lab builds a typical 3-tier Java EE 7 Web application that
allows customers to view the show timings for a movie in a 7-theater
Cineplex and make reservations. Users can add new movies and delete
existing movies. Customers can discuss the movie in a chat room. Total
sales from each showing are calculated at the end of the day. Customers
also accrue points for watching movies.</simpara>
<figure>
<title>Architecture diagram</title>
<mediaobject>
<imageobject>
<imagedata fileref="images/2.0-problem-statement.png"/>
</imageobject>
<textobject><phrase>2.0 problem statement</phrase></textobject>
</mediaobject>
</figure>
<simpara>This figure shows the key components of the application. The User
Interface initiates all the flows in the application. Show Booking,
Add/Delete Movie and Ticket Sales interact with the database; Movie
Points may interact with the database, however, this is out of scope for
this application; and Chat Room does not interact with the database.</simpara>
<simpara>The different functions of the application, as detailed above, utilize
various Java technologies and web standards in their implementation. The
following figure shows how Java EE technologies are used in different
flows.</simpara>
<figure>
<title>Technologies used in the application</title>
<mediaobject>
<imageobject>
<imagedata fileref="images/2.0-technologies.png"/>
</imageobject>
<textobject><phrase>2.0 technologies</phrase></textobject>
</mediaobject>
</figure>
<simpara>The table below details the components and the selected technology used
in its’ implementation.</simpara>
<informaltable frame="all"
rowsep="1" colsep="1">
<tgroup cols="2">
<colspec colname="col_1" colwidth="20*"/>
<colspec colname="col_2" colwidth="80*"/>
<thead>
<row>
<entry align="left" valign="top">Flow</entry>
<entry align="left" valign="top">Description</entry>
</row>
</thead>
<tbody>
<row>
<entry align="left" valign="top"><simpara>User Interface</simpara></entry>
<entry align="left" valign="top"><simpara>Written entirely in <emphasis>JavaServer Faces</emphasis> (JSF)</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>Chat Room</simpara></entry>
<entry align="left" valign="top"><simpara>Utilizes client-side JavaScript and JSON to communicate with a <emphasis>WebSocket</emphasis> endpoint</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>Ticket Sales</simpara></entry>
<entry align="left" valign="top"><simpara>Uses <emphasis>Batch Applications for the Java Platform</emphasis> to calculate the total
sales and persist to the database.</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>Add/Delete Movie</simpara></entry>
<entry align="left" valign="top"><simpara>Implemented using RESTful Web Services. JSON is used as on-the-wire data format</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>Movie Points</simpara></entry>
<entry align="left" valign="top"><simpara>Uses <emphasis>Java Message Service</emphasis> (JMS) to update and obtain loyalty reward
points; an optional implementation using database technology may be
performed</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>Show Booking</simpara></entry>
<entry align="left" valign="top"><simpara>Uses lightweight <emphasis>Enterprise JavaBeans</emphasis> to communicate with the database
using Java Persistence API</simpara></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<simpara>This document is not a comprehensive tutorial of Java EE. The attendees
are expected to know the basic Java EE concepts such as EJB, JPA,
JAX-RS, and CDI. The <ulink url="http://docs.oracle.com/javaee/7/tutorial/doc/">Java
EE 7 Tutorial</ulink> is a good place to learn all these concepts. However
enough explanation is provided in this guide to get you started with the
application.</simpara>
<warning>
<simpara>This is a sample application and the code may not be
following the best practices to prevent SQL injection, cross-side
scripting attacks, escaping parameters, and other similar features
expected of a robust enterprise application. This is intentional such as
to stay focused on explaining the technology. It is highly recommended
to make sure that the code copied from this sample application is
updated to meet those requirements.</simpara>
</warning>
<section id="_lab_flow">
<title>Lab Flow</title>
<simpara>The attendees will start with an existing maven application and by
following the instructions and guidance provided by this lab they will:</simpara>
<itemizedlist>
<listitem>
<simpara>Read existing source code to gain an understanding of the structure of
the application and use of the selected platform technologies.</simpara>
</listitem>
<listitem>
<simpara>Add new and update existing code with provided fragments in order to
demonstrate usage of different technology stacks in the Java EE 7
platform.</simpara>
</listitem>
</itemizedlist>
<simpara>While you are copy/pasting the code from this document into NetBeans,
here are couple of tips that will be really useful and make your
experience enjoyable!</simpara>
<variablelist>
<varlistentry>
<term>Source Code Formatting</term>
<listitem>
<simpara>NetBeans provides capability to neatly format the source code
following conventions. This can be done for any type of source code,
whether its XML or Java or something else. It is highly recommended to
use this functionality after the code is copy/pasted from this document
to the editor. This keeps the code legible.</simpara>
<simpara>This functionality can be accessed by right-clicking in the editor pane
and selecting “Format” as shown.</simpara>
<figure>
<title>Format code in NetBeans</title>
<mediaobject>
<imageobject>
<imagedata fileref="images/2.1-format.png"/>
</imageobject>
<textobject><phrase>2.1 format</phrase></textobject>
</mediaobject>
</figure>
<simpara>This functionality is also accessible using the following keyboard
shortcuts:</simpara>
<informaltable frame="all"
rowsep="1" colsep="1">
<?dbhtml table-width="50%"?>
<?dbfo table-width="50%"?>
<?dblatex table-width="50%"?>
<tgroup cols="2">
<colspec colname="col_1" colwidth="107*"/>
<colspec colname="col_2" colwidth="107*"/>
<thead>
<row>
<entry align="left" valign="top">Shortcut</entry>
<entry align="left" valign="top">Operating System</entry>
</row>
</thead>
<tbody>
<row>
<entry align="left" valign="top"><simpara><keycombo><keycap>Ctrl</keycap><keycap>Shift</keycap><keycap>F</keycap></keycombo></simpara></entry>
<entry align="left" valign="top"><simpara>OSX</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><keycombo><keycap>Alt</keycap><keycap>Shift</keycap><keycap>F</keycap></keycombo></simpara></entry>
<entry align="left" valign="top"><simpara>Windows</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><keycombo><keycap>Alt</keycap><keycap>Shift</keycap><keycap>F</keycap></keycombo></simpara></entry>
<entry align="left" valign="top"><simpara>Linux</simpara></entry>
</row>
</tbody>
</tgroup>
</informaltable>
</listitem>
</varlistentry>
<varlistentry>
<term>Automatic Imports</term>
<listitem>
<simpara>Copy/pasting the Java code from this document in NetBeans editor does
not auto-import the classes. This is required to be done manually in
order for the classes to compile. This can be fixed for each missing
import statement by clicking on the yellow bulb shown in the side bar.</simpara>
<figure>
<title>ServerEndpoint import</title>
<mediaobject>
<imageobject>
<imagedata fileref="images/2.1-server-endpoint.png"/>
</imageobject>
<textobject><phrase>2.1 server endpoint</phrase></textobject>
</mediaobject>
</figure>
<simpara>Alternatively all the imports can be resolved by right-clicking on the
editor pane and selecting “Fix Imports” as shown.</simpara>
<figure>
<title>Fix Imports in NetBeans</title>
<mediaobject>
<imageobject>
<imagedata fileref="images/2.1-fix-imports.png"/>
</imageobject>
<textobject><phrase>2.1 fix imports</phrase></textobject>
</mediaobject>
</figure>
<simpara>This functionality is also accessible using the following keyboard
shortcuts:</simpara>
<informaltable frame="all"
rowsep="1" colsep="1">
<?dbhtml table-width="50%"?>
<?dbfo table-width="50%"?>
<?dblatex table-width="50%"?>
<tgroup cols="2">
<colspec colname="col_1" colwidth="107*"/>
<colspec colname="col_2" colwidth="107*"/>
<thead>
<row>
<entry align="left" valign="top">Shortcut</entry>
<entry align="left" valign="top">Operating System</entry>
</row>
</thead>
<tbody>
<row>
<entry align="left" valign="top"><simpara><keycombo><keycap>Command</keycap><keycap>Shift</keycap><keycap>I</keycap></keycombo></simpara></entry>
<entry align="left" valign="top"><simpara>OSX</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><keycombo><keycap>Ctrl</keycap><keycap>Shift</keycap><keycap>I</keycap></keycombo></simpara></entry>
<entry align="left" valign="top"><simpara>Windows</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><keycombo><keycap>Ctrl</keycap><keycap>Shift</keycap><keycap>I</keycap></keycombo></simpara></entry>
<entry align="left" valign="top"><simpara>Linux</simpara></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<simpara>The defaults may work in most of the cases. Choices are shown in case a
class is available to import from multiple packages. If multiple
packages are available then specific packages to import from are clearly
marked in the document.</simpara>
</listitem>
</varlistentry>
</variablelist>
</section>
<section id="_estimated_time">
<title>Estimated Time</title>
<simpara>Following the complete instructions in this document can take any where
from two to four hours. The wide time range accommodates for learning
the new technologies, finding your way in NetBeans, copy/pasting the
code, and debugging the errors.</simpara>
<simpara>The recommended flow is where you follow through the instructions in all
sections in the listed sequence. Alternatively, you may like to cover
section <xref linkend="walk-through"/> through <xref linkend="jsf"/> in an order of your choice, based upon your
interest and preference of the technology. However section <xref linkend="jaxrs"/> is a
pre-requisite for <xref linkend="json"/>.</simpara>
<simpara>Here is an approximate time estimate for each section:</simpara>
<informaltable frame="all"
rowsep="1" colsep="1">
<tgroup cols="2">
<colspec colname="col_1" colwidth="66*"/>
<colspec colname="col_2" colwidth="33*"/>
<thead>
<row>
<entry align="left" valign="top">Section Title</entry>
<entry align="left" valign="top">Estimated Time</entry>
</row>
</thead>
<tbody>
<row>
<entry align="left" valign="top"><simpara><xref linkend="walk-through"/></simpara></entry>
<entry align="left" valign="top"><simpara>15 - 30 mins</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><xref linkend="websocket"/></simpara></entry>
<entry align="left" valign="top"><simpara>30 - 45 mins</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><xref linkend="batch"/></simpara></entry>
<entry align="left" valign="top"><simpara>30 - 45 mins</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><xref linkend="jaxrs"/></simpara></entry>
<entry align="left" valign="top"><simpara>30 - 45 mins</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><xref linkend="json"/></simpara></entry>
<entry align="left" valign="top"><simpara>30 - 45 mins</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><xref linkend="jms"/></simpara></entry>
<entry align="left" valign="top"><simpara>30 - 45 mins</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><xref linkend="jsf"/></simpara></entry>
<entry align="left" valign="top"><simpara>30 - 45 mins</simpara></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<simpara>The listed time for each section is only an estimate and by no means
restrict you within that. These sections have been completed in much
shorter time, and you can do it too!</simpara>
<tip>
<simpara>The listed time for each section also allows you to create a custom
version of the lab depending upon your target audience and available
time.</simpara>
</tip>
</section>
</chapter>
<chapter id="walk-through">
<title>Walk-through of Sample Application</title>
<simpara><emphasis role="strong">Purpose</emphasis>: This section will download the sample application to be used
in this hands-on lab. A walk-through of the application will be
performed to provide an understanding of the application architecture.</simpara>
<simpara><emphasis role="strong">Estimated Time</emphasis>: 15-30 mins</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>Download the sample application from
<ulink url="https://github.com/javaee-samples/javaee7-hol/blob/master/starting-template/movieplex7-starting-template.zip?raw=true">movieplex7-starting-template.zip</ulink>
and unzip. This will create a ‘movieplex7’ directory and unzips all the
content there.</simpara>
</listitem>
<listitem>
<simpara>In NetBeans IDE, select ‘File’, ‘Open Project’, select the
unzipped directory, and click on ‘Open Project’. The project structure
is shown.</simpara>
<figure>
<title>Project structure in NetBeans</title>
<mediaobject>
<imageobject>
<imagedata fileref="images/3.2-project-structure.png"/>
</imageobject>
<textobject><phrase>3.2 project structure</phrase></textobject>
</mediaobject>
</figure>
</listitem>
<listitem>
<simpara>Maven Coordinates: Expand ‘Project Files’ and double click on
‘pom.xml’. In the ‘pom.xml’, the Java EE 7 API is specified as a
<dependency>:</simpara>
<programlisting language="xml" linenumbering="unnumbered"><dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies></programlisting>
<simpara>This will ensure that Java EE 7 APIs are retrieved from the central
Maven repository.</simpara>
<note>
<simpara>The Java EE 6 platform introduced the notion of ‘profiles’. A profile is
a configuration of the Java EE platform targeted at a specific class of
applications. All Java EE profiles share a set of common features, such
as naming and resource injection, packaging rules, security
requirements, etc. A profile may contain a proper subset or superset of
the technologies contained in the platform.</simpara>
<simpara>The Java EE Web Profile is a profile of the Java EE Platform
specifically targeted at modern web applications. The complete set of
specifications defined in the Web Profile is defined in the Java EE 7
Web Profile Specification.</simpara>
</note>
<simpara>WildFly can be started in Full Platform or Web Profile.</simpara>
</listitem>
<listitem>
<simpara><emphasis role="strong">Default Data Source</emphasis>: Expand ‘Other Sources’,
‘src/main/resources’, ‘META-INF’, and double-click on ‘persistence.xml’.
By default, NetBeans opens the file in Design View. Click on ‘Source’ tab
to view the XML source.</simpara>
<figure>
<title>persistence.xml</title>
<mediaobject>
<imageobject>
<imagedata fileref="images/3.2-persistence-xml.png"/>
</imageobject>
<textobject><phrase>3.2 persistence xml</phrase></textobject>
</mediaobject>
</figure>
<simpara>It looks like:</simpara>
<programlisting language="xml" linenumbering="unnumbered"><?xml version="1.0" encoding="UTF-8"?>
<persistence
version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="movieplex7PU" transaction-type="JTA">
<!--
<jta-data-source>java:comp/DefaultDataSource</jta-data-source>
-->
<properties>
<property
name="javax.persistence.schema-generation.database.action"
value="drop-and-create"/>
<property
name="javax.persistence.schema-generation.create-source"
value="script"/>
<property
name="javax.persistence.schema-generation.drop-source"
value="script"/>
<property
name="javax.persistence.schema-generation.drop-script-source"
value="META-INF/drop.sql"/>
<property
name="javax.persistence.sql-load-script-source"
value="META-INF/load.sql"/>
<property
name="eclipselink.deploy-on-startup"
value="true"/>
<property
name="eclipselink.logging.exceptions"
value="false"/>
</properties>
</persistence-unit>
</persistence></programlisting>
<simpara>Notice <literal><jta-data-source></literal> is commented out, i.e. no data source element
is specified. This element identifies the JDBC resource to connect to in
the runtime environment of the underlying application server.</simpara>
<simpara>The Java EE 7 platform defines a new default data source that must be
provided by the runtime. This pre-configured data source is accessible
under the JNDI name</simpara>
<programlisting language="java" linenumbering="unnumbered">java:comp/DefaultDataSource</programlisting>
<simpara>The JPA 2.1 specification says if neither <literal>jta-data-source</literal> nor
<literal>non-jta-data-source</literal> elements are specified, the deployer must specify a
JTA data source or the default JTA data source must be provided by the
container.</simpara>
<simpara>For WildFly 8, the default data source is bound to the JDBC resource <literal>what name</literal>.</simpara>
<simpara>Clicking back and forth between ‘Design’ and ‘Source’ view may prompt
the error shown below:</simpara>
<figure>
<title>Missing server error from persistence.xml</title>
<mediaobject>
<imageobject>
<imagedata fileref="images/3.4-missing-server.png"/>
</imageobject>
<textobject><phrase>3.4 missing server</phrase></textobject>
</mediaobject>
</figure>
<simpara>This will get resolved when we run the application. Click on ‘OK’ to
dismiss the dialog.</simpara>
</listitem>
<listitem>
<simpara><emphasis role="strong">Schema Generation:</emphasis> JPA 2.1 defines a new set of
<literal>javax.persistence.schema-generation.*</literal> properties that can be used to
generate database artifacts like tables, indexes, and constraints in a
database schema. This helps in prototyping of your application where the
required artifacts are generated either prior to application deployment
or as part of <literal>EntityManagerFactory</literal> creation. This feature will allow
your JPA domain object model to be directly generated in a database. The
generated schema may need to be tuned for actual production environment.</simpara>
<simpara>The “persistence.xml” in the application has the following
<literal>javax.persistence.schema-generation.*</literal> properties. Their meaning and
possible values are explained:</simpara>
<informaltable frame="all"
rowsep="1" colsep="1">
<tgroup cols="3">
<colspec colname="col_1" colwidth="33*"/>
<colspec colname="col_2" colwidth="33*"/>
<colspec colname="col_3" colwidth="33*"/>
<thead>
<row>
<entry align="left" valign="top">Property</entry>
<entry align="left" valign="top">Meaning</entry>
<entry align="left" valign="top">Values</entry>
</row>
</thead>
<tbody>
<row>
<entry align="left" valign="top"><simpara><literal>javax.persistence.schema-generation.database.action</literal></simpara></entry>
<entry align="left" valign="top"><simpara>Specifies the action to be taken by the persistence provider with regard
to the database artifacts.</simpara></entry>
<entry align="left" valign="top"><simpara><literal>none</literal>, <literal>create</literal>, <literal>drop-and-create</literal>, <literal>drop</literal></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>javax.persistence.schema-generation.create-source</literal>
<literal>javax.persistence.schema-generation.drop-source</literal></simpara></entry>
<entry align="left" valign="top"><simpara>Specifies whether the creation or deletion of database artifacts is to
occur on the basis of the object/relational mapping metadata, DDL
script, or a combination of the two.</simpara></entry>
<entry align="left" valign="top"><simpara><literal>metadata</literal>, <literal>script</literal>, <literal>metadata-then-script</literal>, <literal>script-then-metadata</literal></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>javax.persistence.schema-generation.create-script-source</literal>
<literal>javax.persistence.schema-generation.drop-script-source</literal></simpara></entry>
<entry align="left" valign="top"><simpara>Specifies a <literal>java.IO.Reader</literal> configured for reading of the SQL script or a
string designating a file URL for the SQL script to create or delete
database artifacts.</simpara></entry>
<entry align="left" valign="top"></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>javax.persistence.sql-load-script-source</literal></simpara></entry>
<entry align="left" valign="top"><simpara>Specifies a <literal>java.IO.Reader</literal> configured for reading of the SQL load script
for database initialization or a string designating a file URL for the
script.</simpara></entry>
<entry align="left" valign="top"></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<simpara>Refer to the <ulink url="http://jcp.org/en/jsr/detail?id=338">JPA 2.1 Specification</ulink>
for a complete understanding of these properties.</simpara>
<simpara>In the application, the scripts are bundled in the WAR file in
‘META-INF’ directory. As the location of these scripts is specified as a
URL, the scripts may be loaded from outside the WAR file as well.</simpara>
<simpara>Feel free to open ‘create.sql’, ‘drop.sql’ and ‘load.sql’ and read
through the SQL scripts. The database schema is shown.</simpara>
<figure>
<title>Database schema</title>
<mediaobject>
<imageobject>
<imagedata fileref="images/3.5-schema.png"/>
</imageobject>
<textobject><phrase>3.5 schema</phrase></textobject>
</mediaobject>
</figure>
<simpara>This folder also contains ‘sales.csv’ which carries some comma-separated
data, and is used later in the application.</simpara>
</listitem>
<listitem>
<simpara><emphasis role="strong">JPA entities, Stateless EJBs, and REST endpoints</emphasis>: Expand <literal>Source
Packages'. The package `org.javaee7.movieplex7.entities</literal> contains the
JPA entities corresponding to the database table definitions. Each JPA
entity has several convenient <literal>@NamedQuery</literal> defined and uses Bean
Validation constraints to enforce validation.</simpara>
<simpara>The package <literal>org.javaee7.movieplex7.rest</literal> contains stateless EJBs
corresponding to different JPA entities.</simpara>
<simpara>Each EJB has methods to perform CRUD operations on the JPA entity and
convenience query methods. Each EJB is also EL-injectable (@Named) and
published as a REST endpoint (@Path). The AplicationConfig class defines
the base path of REST endpoint. The path for the REST endpoint is the
same as the JPA entity class name.</simpara>
<simpara>The mapping between JPA entity classes, EJB classes, and the URI of the
corresponding REST endpoint is shown.</simpara>
<informaltable frame="all"
rowsep="1" colsep="1">
<tgroup cols="3">
<colspec colname="col_1" colwidth="33*"/>
<colspec colname="col_2" colwidth="33*"/>
<colspec colname="col_3" colwidth="33*"/>
<thead>
<row>
<entry align="left" valign="top">JPA Entity Class</entry>
<entry align="left" valign="top">EJB Class</entry>
<entry align="left" valign="top">RESTful Path</entry>
</row>
</thead>
<tbody>
<row>
<entry align="left" valign="top"><simpara><literal>Movie</literal></simpara></entry>
<entry align="left" valign="top"><simpara><literal>MovieFacadeREST</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis>/webresources/movie</emphasis></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>Sales</literal></simpara></entry>
<entry align="left" valign="top"><simpara><literal>SalesFacadeREST</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis>/webresources/sales</emphasis></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>ShowTiming</literal></simpara></entry>
<entry align="left" valign="top"><simpara><literal>ShowTimingFacadeREST</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis>/webresources/showtiming</emphasis></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>Theater</literal></simpara></entry>
<entry align="left" valign="top"><simpara><literal>TheaterFacadeREST</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis>/webresources/theater</emphasis></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>Timeslot</literal></simpara></entry>
<entry align="left" valign="top"><simpara><literal>TimeslotFacadeREST</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis>/webresources/timeslot</emphasis></simpara></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<simpara>Feel free to browse through the code.</simpara>
</listitem>
<listitem>
<simpara><emphasis role="strong">JSF pages</emphasis>: ‘WEB-INF/template.xhtml’ defines the template of the
web page and has a header, left navigation bar, and a main content
section. ‘index.xhtml’ uses this template and the EJBs to display the
number of movies and theaters.</simpara>
<simpara>Java EE 7 enables CDI discovery of beans by default. No ‘beans.xml’ is
required in ‘WEB-INF’. This allows all beans with bean defining
annotation, i.e. either a bean with an explicit CDI scope or EJBs to be
available for injection.</simpara>
<simpara>Note, ‘template.xhtml’ is in ‘WEB-INF’ folder as it allows the template
to be accessible from the pages bundled with the application only. If it
were bundled with rest of the pages then it would be accessible outside
the application and thus allowing other external pages to use it as
well.</simpara>
</listitem>
<listitem>
<simpara><emphasis role="strong">Run the sample</emphasis>: Right-click on the project and select ‘Run’.
This will download all the maven dependencies on your machine, build a
WAR file, deploy on
WildFly 8
, and show the URL
<ulink url="http://localhost:8080/movieplex7">localhost:8080/movieplex7</ulink> in the
default browser configured in NetBeans. Note that this could take a
while if you have never built a Maven application on your machine.</simpara>
<tip>
<simpara>The project will show red squiggly lines in the source code indicating
that the classes cannot be resolved. This is expected before the
dependencies are downloaded. However these references will be resolved
correctly after the dependencies are downloaded during project building.</simpara>
</tip>
<simpara>During the first run, the IDE will ask you to select a deployment server.
Choose the configured WildFly server and click on ‘OK’.</simpara>
<figure>
<title>WildFly deployment server</title>
<mediaobject>
<imageobject>
<imagedata fileref="images/3.6-wildfly-server.png"/>
</imageobject>
<textobject><phrase>3.6 wildfly server</phrase></textobject>
</mediaobject>
</figure>
<simpara>The output looks like as shown.</simpara>
<figure>
<title>Application main page</title>
<mediaobject>
<imageobject>
<imagedata fileref="images/3.8-first-page.png"/>
</imageobject>
<textobject><phrase>3.8 first page</phrase></textobject>
</mediaobject>
</figure>
</listitem>
</orderedlist>
</chapter>
<chapter id="websocket">
<title>Chat Room (Java API for WebSocket)</title>
<simpara><emphasis role="strong">Purpose</emphasis>: Build a chat room for viewers. In doing so several new
features of Java API for WebSocket 1.0 will be introduced and
demonstrated by using them in the application.</simpara>
<simpara><emphasis role="strong">Estimated Time</emphasis>: 30-45 mins</simpara>
<simpara>WebSocket provide a full-duplex and bi-directional communication
protocol over a single TCP connection. WebSocket is a combination of
<ulink url="http://tools.ietf.org/html/rfc6455">IETF RFC 6455</ulink>
<ulink url="http://tools.ietf.org/html/rfc6455">Protocol</ulink> and
<ulink url="http://www.w3.org/TR/websockets/">W3C JavaScript WebSocket API</ulink> (a
Candidate Recommendation as of this writing). The protocol defines an
opening handshake and data transfer. The API enables Web pages to use
the WebSocket protocol for two-way communication with the remote host.</simpara>
<simpara><ulink url="http://jcp.org/en/jsr/detail?id=356">JSR 356</ulink> defines a standard API for
creating WebSocket applications in the Java EE 7 Platform. The JSR
provides support for:</simpara>
<itemizedlist>
<listitem>
<simpara>Create WebSocket endpoint using annotations and interface</simpara>
</listitem>
<listitem>
<simpara>Initiating and intercepting WebSocket events</simpara>
</listitem>
<listitem>
<simpara>Creation and consumption of WebSocket text and binary messages</simpara>
</listitem>
<listitem>
<simpara>Configuration and management of WebSocket sessions</simpara>
</listitem>
<listitem>
<simpara>Integration with Java EE security model</simpara>
</listitem>