EAP7.0.0_DevelopmentGuide.pdf - Red Hat JBoss Enterprise...

This preview shows page 1 out of 336 pages.

Unformatted text preview: Red Hat JBoss Enterprise Application Platform 7.0 Development Guide For Use with Red Hat JBoss Enterprise Application Platform 7 Red Hat Customer Content Services Red Hat JBoss Enterprise Application Platform 7.0 Development Guide For Use with Red Hat JBoss Enterprise Application Platform 7 Legal Notice Copyright © 2016 Red Hat, Inc. The text of and illustrations in this document are licensed by Red Hat under a Creative Commons Attribution–Share Alike 3.0 Unported license ("CC-BY-SA"). An explanation of CC-BY-SA is available at . In accordance with CC-BY-SA, if you distribute this document or an adaptation of it, you must provide the URL for the original version. Red Hat, as the licensor of this document, waives the right to enforce, and agrees not to assert, Section 4d of CC-BY-SA to the fullest extent permitted by applicable law. Red Hat, Red Hat Enterprise Linux, the Shadowman logo, JBoss, MetaMatrix, Fedora, the Infinity Logo, and RHCE are trademarks of Red Hat, Inc., registered in the United States and other countries. Linux ® is the registered trademark of Linus Torvalds in the United States and other countries. Java ® is a registered trademark of Oracle and/or its affiliates. XFS ® is a trademark of Silicon Graphics International Corp. or its subsidiaries in the United States and/or other countries. MySQL ® is a registered trademark of MySQL AB in the United States, the European Union and other countries. Node.js ® is an official trademark of Joyent. Red Hat Software Collections is not formally related to or endorsed by the official Joyent Node.js open source or commercial project. The OpenStack ® Word Mark and OpenStack Logo are either registered trademarks/service marks or trademarks/service marks of the OpenStack Foundation, in the United States and other countries and are used with the OpenStack Foundation's permission. We are not affiliated with, endorsed or sponsored by the OpenStack Foundation, or the OpenStack community. All other trademarks are the property of their respective owners. Abstract This book provides references and examples for Java EE developers using Red Hat JBoss Enterprise Application Platform 7 and its patch releases. Table of Contents Table of Contents .CHAPTER . . . . . . . . .1.. .GET . . . .STARTED . . . . . . . . .DEVELOPING . . . . . . . . . . . . APPLICATIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4. . . . . . . . . . 1.1. INTRODUCTION 4 1.2. BECOME FAMILIAR WITH JAVA ENTERPRISE EDITION 7 4 1.3. SETTING UP THE DEVELOPMENT ENVIRONMENT 6 1.4. USING THE QUICKSTART EXAMPLES 11 1.5. CONFIGURE THE DEFAULT WELCOME WEB APPLICATION 28 .CHAPTER . . . . . . . . .2.. .USING . . . . . .MAVEN . . . . . . .WITH . . . . . JBOSS . . . . . . .EAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31 ........... 2.1. LEARN ABOUT MAVEN 31 2.2. INSTALL MAVEN AND THE JBOSS EAP MAVEN REPOSITORY 34 2.3. USE THE MAVEN REPOSITORY 35 .CHAPTER . . . . . . . . .3.. .CLASS . . . . . . LOADING . . . . . . . . . AND . . . . .MODULES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48 ........... 3.1. INTRODUCTION 48 3.2. ADD AN EXPLICIT MODULE DEPENDENCY TO A DEPLOYMENT 53 3.3. GENERATE MANIFEST.MF ENTRIES USING MAVEN 57 3.4. PREVENT A MODULE BEING IMPLICITLY LOADED 58 3.5. EXCLUDE A SUBSYSTEM FROM A DEPLOYMENT 59 3.6. USE THE CLASS LOADER PROGRAMMATICALLY IN A DEPLOYMENT 3.7. CLASS LOADING AND SUBDEPLOYMENTS 3.8. DEPLOY TAG LIBRARY DESCRIPTORS (TLDS) IN A CUSTOM MODULE 60 64 69 3.9. REFERENCE 71 .CHAPTER . . . . . . . . .4.. .LOGGING . . . . . . . . .FOR . . . . DEVELOPERS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .80 ........... 4.1. INTRODUCTION 80 4.2. LOGGING WITH THE JBOSS LOGGING FRAMEWORK 4.3. PER-DEPLOYMENT LOGGING 83 85 4.4. LOGGING PROFILES 88 .CHAPTER . . . . . . . . .5.. .INTERNATIONALIZATION . . . . . . . . . . . . . . . . . . . . . . AND . . . . .LOCALIZATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .90 ........... 5.1. INTRODUCTION 90 5.2. JBOSS LOGGING TOOLS 90 .CHAPTER . . . . . . . . .6.. .REMOTE . . . . . . . .JNDI . . . . .LOOKUP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .113 ............ 6.1. REGISTERING OBJECTS TO JNDI 113 6.2. CONFIGURING REMOTE JNDI 113 . . . . . . . . . .7.. .CLUSTERING CHAPTER . . . . . . . . . . . .IN . . WEB . . . . .APPLICATIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .114 ............ 7.1. SESSION REPLICATION 7.2. HTTPSESSION PASSIVATION AND ACTIVATION 7.3. PUBLIC API FOR CLUSTERING SERVICES 7.4. CLUSTERED HA SINGLETON SERVICE 114 116 117 118 7.5. HA SINGLETON DEPLOYMENTS 7.6. APACHE MOD_CLUSTER-MANAGER APPLICATION 119 122 .CHAPTER . . . . . . . . .8.. .CONTEXTS . . . . . . . . . . AND . . . . DEPENDENCY . . . . . . . . . . . . . .INJECTION . . . . . . . . . .(CDI) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .124 ............ 8.1. INTRODUCTION TO CDI 124 8.2. USE CDI 124 . . . . . . . . . .9.. .JBOSS CHAPTER . . . . . . MBEAN . . . . . . . SERVICES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .144 ............ 9.1. WRITING JBOSS MBEAN SERVICES 9.2. A STANDARD MBEAN EXAMPLE 9.3. DEPLOYING JBOSS MBEAN SERVICES 144 144 146 . . . . . . . . . .10. CHAPTER . . .CONCURRENCY . . . . . . . . . . . . . . UTILITIES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147 ............ 1 Red Hat JBoss Enterprise Application Platform 7.0 Development Guide .CHAPTER . . . . . . . . .10. . . .CONCURRENCY . . . . . . . . . . . . . . UTILITIES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147 ............ Context Service 148 Managed Thread Factory 148 Managed Executor Service Managed Scheduled Executor Service 149 150 .CHAPTER . . . . . . . . .11. . . .UNDERTOW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .152 ............ 11.1. INTRODUCTION TO UNDERTOW HANDLER 152 11.2. CREATING CUSTOM HANDLERS 153 . . . . . . . . . .12. CHAPTER . . .JAVA . . . . .TRANSACTION . . . . . . . . . . . . . .API . . . (JTA) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .156 ............ 12.1. OVERVIEW 12.2. TRANSACTION CONCEPTS 12.3. TRANSACTION OPTIMIZATIONS 12.4. TRANSACTION OUTCOMES 156 156 161 166 12.5. OVERVIEW OF THE TRANSACTION LIFECYCLE 12.6. TRANSACTION SUBSYSTEM CONFIGURATION 12.7. TRANSACTIONS USAGE IN PRACTICE 12.8. TRANSACTION REFERENCES 167 168 175 181 . . . . . . . . . .13. CHAPTER . . .HIBERNATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .185 ............ 13.1. ABOUT HIBERNATE CORE 13.2. JAVA PERSISTENCE API (JPA) 185 185 13.3. HIBERNATE ANNOTATIONS 201 13.4. HIBERNATE QUERY LANGUAGE 13.5. HIBERNATE SERVICES 207 221 13.6. BEAN VALIDATION 13.7. ENVERS 227 232 13.8. PERFORMANCE TUNING 242 . . . . . . . . . .14. CHAPTER . . .HIBERNATE . . . . . . . . . . .SEARCH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .246 ............ 14.1. GETTING STARTED WITH HIBERNATE SEARCH 14.2. MAPPING ENTITIES TO THE INDEX STRUCTURE 246 252 14.3. QUERYING 14.4. MANUAL INDEX CHANGES 278 306 14.5. INDEX OPTIMIZATION 310 14.6. ADVANCED FEATURES 312 .CHAPTER . . . . . . . . .15. . . .CREATING . . . . . . . . . .WEBSOCKET . . . . . . . . . . . .APPLICATIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .318 ............ Create the WebSocket Application 318 . . . . . . . . . .16. CHAPTER . . .JAVA . . . . .AUTHORIZATION . . . . . . . . . . . . . . . CONTRACT . . . . . . . . . . . FOR . . . . CONTAINERS . . . . . . . . . . . . .(JACC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .323 ............ 16.1. ABOUT JAVA AUTHORIZATION CONTRACT FOR CONTAINERS (JACC) 323 16.2. CONFIGURE JAVA AUTHORIZATION CONTRACT FOR CONTAINERS (JACC) SECURITY 323 .CHAPTER . . . . . . . . .17. . . .JAVA . . . . .AUTHENTICATION . . . . . . . . . . . . . . . . .SPI . . .FOR . . . . CONTAINERS . . . . . . . . . . . . .(JASPI) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .325 ............ 17.1. ABOUT JAVA AUTHENTICATION SPI FOR CONTAINERS (JASPI) SECURITY 325 17.2. CONFIGURE JAVA AUTHENTICATION SPI FOR CONTAINERS (JASPI) SECURITY 325 .CHAPTER . . . . . . . . .18. . . .JAVA . . . . .BATCH . . . . . . .APPLICATION . . . . . . . . . . . . .DEVELOPMENT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .326 ............ 18.1. REQUIRED BATCH DEPENDENCIES 326 2 18.2. JOB SPECIFICATION LANGUAGE (JSL) INHERITANCE 326 18.3. BATCH PROPERTY INJECTIONS 328 Table of Contents 3 Red Hat JBoss Enterprise Application Platform 7.0 Development Guide CHAPTER 1. GET STARTED DEVELOPING APPLICATIONS 1.1. INTRODUCTION 1.1.1. About Red Hat JBoss Enterprise Application Platform 7 Red Hat JBoss Enterprise Application Platform 7 (JBoss EAP) is a middleware platform built on open standards and compliant with the Java Enterprise Edition 7 specification. It integrates WildFly Application Server 10 with messaging, high-availability clustering, and other technologies. JBoss EAP includes a modular structure that allows service enabling only when required, improving startup speed. The management console and management command-line interface (CLI) make editing XML configuration files unnecessary and add the ability to script and automate tasks. JBoss EAP provides two operating modes for JBoss EAP instances: standalone server or managed domain. The standalone server operating mode represents running JBoss EAP as a single server instance. The managed domain operating mode allows for the management of multiple JBoss EAP instances from a single control point. In addition, JBoss EAP includes APIs and development frameworks for quickly developing secure and scalable Java EE applications. 1.2. BECOME FAMILIAR WITH JAVA ENTERPRISE EDITION 7 1.2.1. Overview of EE 7 Profiles Java Enterprise Edition 7 (EE 7) includes support for multiple profiles, or subsets of APIs. The only two profiles that the EE 7 specification defines are the Full Profile and the Web Profile. EE 7 Full Profile includes all APIs and specifications included in the EE 7 specification. EE 7 Web Profile includes a selected subset of APIs, which are designed to be useful to web developers. JBoss EAP is a certified implementation of the Java Enterprise Edition 7 Full Profile and Web Profile specifications. Java Enterprise Edition 7 Web Profile Java Enterprise Edition 7 Full Profile Java Enterprise Edition 7 Web Profile The Web Profile is one of two profiles defined by the Java Enterprise Edition 7 specification, and is designed for web application development. The Web Profile supports the following APIs: Java EE 7 Web Profile Requirements: Java Platform, Enterprise Edition 7 Java Web Technologies: Servlet 3.1 (JSR 340) JSP 2.3 4 CHAPTER 1. GET STARTED DEVELOPING APPLICATIONS Expression Language (EL) 3.0 JavaServer Faces (JSF) 2.2 (JSR 344) Java Standard Tag Library (JSTL) for JSP 1.2 Debugging Support for Other Languages 1.0 (JSR 45) Enterprise Application Technologies: Contexts and Dependency Injection (CDI) 1.1 (JSR 346) Dependency Injection for Java 1.0 (JSR 330) Enterprise JavaBeans 3.2 Lite (JSR 345) Java Persistence API 2.1 (JSR 338) Common Annotations for the Java Platform 1.1 (JSR 250) Java Transaction API (JTA) 1.2 (JSR 907) Bean Validation 1.1 (JSR 349) The other profile defined by the Java EE 7 specification is the Full Profile, and includes several more APIs. Java Enterprise Edition 7 Full Profile The Java Enterprise Edition 7 (EE 7) specification defines a concept of profiles, and defines two of them as part of the specification. The Full Profile supports the following APIs, as well as those supported in the Java Enterprise Edition 7 Web Profile: Included in the EE 7 Full Profile: Batch 1.0 JSON-P 1.0 Concurrency 1.0 WebSocket 1.1 JMS 2.0 JPA 2.1 JCA 1.7 JAX-RS 2.0 JAX-WS 2.2 Servlet 3.1 JSF 2.2 JSP 2.3 EL 3.0 CDI 1.1 5 Red Hat JBoss Enterprise Application Platform 7.0 Development Guide CDI Extensions JTA 1.2 Interceptors 1.2 Common Annotations 1.1 Managed Beans 1.0 EJB 3.2 Bean Validation 1.1 1.3. SETTING UP THE DEVELOPMENT ENVIRONMENT 1.3.1. Download JBoss Developer Studio JBoss Developer Studio can be downloaded from the Red Hat Customer Portal. 1. Log in to the Red Hat Customer Portal. 2. Click Downloads. 3. In the Product Downloads list, click Red Hat JBoss Developer Studio. 4. Select the desired version in the Version drop-down menu. Note It is recommended to use JBoss Developer Studio version 9.1 or later. 5. Find the Red Hat JBoss Developer Studio 9.x.x Stand-alone Installer entry in the table and click Download. 6. Save the JAR file to the desired directory. 1.3.2. Install JBoss Developer Studio 1. Open a terminal and navigate to the directory containing the downloaded JAR file. 2. Run the following command to launch the GUI installation program: $ java -jar jboss-devstudio-BUILD_VERSION-installerstandalone.jar Note Alternatively, you may be able to double-click the JAR file to launch the installation program. 6 CHAPTER 1. GET STARTED DEVELOPING APPLICATIONS 3. Click Next to start the installation process. 4. Select I accept the terms of this license agreement and click Next. 5. Adjust the installation path and click Next. Note If the installation path folder does not exist, a prompt will appear. Click OK to create the folder. 6. Choose a JVM, or leave the default JVM selected, and click Next. 7. Click Next when asked to select platforms and servers. 8. Review the installation details, and click Next. 9. Click Next when the installation process is complete. 10. Configure the desktop shortcuts for JBoss Developer Studio, and click Next. 11. Click Done. 1.3.3. Start JBoss Developer Studio To start JBoss Developer Studio, you can double-click on the desktop shortcut created during the installation, or you can start it from a command line. Follow the below steps to start JBoss Developer Studio using the command line. 1. Open a terminal and navigate to the JBoss Developer Studio installation directory. 2. Run the following command to start JBoss Developer Studio: $ ./jbdevstudio Note For Windows Server, use the jbdevstudio.bat file. 1.3.4. Add the JBoss EAP Server to JBoss Developer Studio These instructions assume that you have not yet added any JBoss EAP servers to JBoss Developer Studio. Use the following steps to add your JBoss EAP server using the Define New Server wizard. 1. Open the Servers tab. Note If the Servers tab is not shown, add it to the panel by selecting Window → Show View → Servers. 7 Red Hat JBoss Enterprise Application Platform 7.0 Development Guide 2. Click on the No servers are available. Click this link to create a new server link. Figure 1.1. Add a New Server 3. Expand Red Hat JBoss Middleware and choose JBoss Enterprise Application Platform 7.0. Enter a server name, for example, JBoss EAP 7.0, then click Next. Figure 1.2. Define a New Server 8 CHAPTER 1. GET STARTED DEVELOPING APPLICATIONS 4. Create a server adapter to manage starting and stopping the server. Keep the defaults and click Next. Figure 1.3. Create a New Server Adapter 5. Enter a name, for example JBoss EAP 7.0 Runtime. Click Browse next to Home Directory and navigate to your JBoss EAP installation directory. Then click Next. Figure 1.4. Add New Server Runtime Environment 9 Red Hat JBoss Enterprise Application Platform 7.0 Development Guide Note Some quickstarts require that you run the server with a different profile or additional arguments. For example, to deploy a quickstart that requires the full profile, you must define a new server and specify standalone-full.xml in the Configuration file field. Be sure to give the new server a descriptive name. 6. Configure existing projects for the new server. Because you do not have any projects at this point, click Finish. Figure 1.5. Modify Resources for the New Server 10 CHAPTER 1. GET STARTED DEVELOPING APPLICATIONS The JBoss EAP 7.0 server is now listed in the Servers tab. Figure 1.6. Server List 1.4. USING THE QUICKSTART EXAMPLES 1.4.1. About Maven Apache Maven is a distributed build automation tool used in Java application development to create, manage, and build software projects. Maven uses standard configuration files called Project Object 11 Red Hat JBoss Enterprise Application Platform 7.0 Development Guide Model (POM) files to define projects and manage the build process. POMs describe the module and component dependencies, build order, and targets for the resulting project packaging and output using an XML file. This ensures that the project is built in a correct and uniform manner. Note The Quickstart examples included in JBoss EAP are Maven projects. Maven achieves this by using a repository. A Maven repository stores Java libraries, plug-ins, and other build artifacts. The default public repository is the Maven 2 Central Repository, but repositories can be private and internal within a company with a goal to share common artifacts among development teams. Repositories are also available from third-parties. For more information, see the Apache Maven project and the Introduction to Repositories guide. JBoss EAP includes a Maven repository that contains many of the requirements that Java EE developers typically use to build applications on JBoss EAP. For more information, see Using Maven with JBoss EAP. 1.4.1.1. Using Maven with the Quickstarts The artifacts and dependencies needed to build and deploy applications to JBoss EAP 7 are hosted on a public repository. Starting with the JBoss EAP 7 quickstarts, it is no longer necessary to configure your Maven settings.xml file to use these repositories when building the quickstarts. The Maven repositories are now configured in the quickstart project POM files. This method of configuration is provided to make it easier to get started with the quickstarts, however, is generally not recommended for production projects because it can slow down your build. Red Hat JBoss Developer Studio includes Maven, so there is no need to download and install it separately. It is recommended to use JBoss Developer Studio version 9.1 or later. If you plan to use the Maven command line to build and deploy your applications, then you must first download Maven from the Apache Maven project and install it using the instructions provided in the Maven documentation. 1.4.2. Download and Run the Quickstart Code Examples 1.4.2.1. Download the Quickstarts JBoss EAP comes with a comprehensive set of quickstart code examples designed to help users begin writing applications using various Java EE 7 technologies. The quickstarts can be downloaded from the Red Hat Customer Portal. 1. Log in to the Red Hat Customer Portal. 2. Click Downloads. 3. In the Product Downloads list, click Red Hat JBoss Enterprise Application Platform. 4. Select the desired version in the Version drop-down menu. 5. Find the Red Hat JBoss Enterprise Application Platform 7.0.0 Quickstarts entry in the table and click Download. 12 CHAPTER 1. GET STARTED DEVELOPING APPLICAT...
View Full Document

  • Spring '17
  • azmat fatma
  • developer, Java Community Process, Red Hat, jboss eap, Red Hat JBoss

{[ snackBarMessage ]}

Get FREE access by uploading your study materials

Upload your study materials now and get free access to over 25 million documents.

Upload now for FREE access Or pay now for instant access
Christopher Reinemann
"Before using Course Hero my grade was at 78%. By the end of the semester my grade was at 90%. I could not have done it without all the class material I found."
— Christopher R., University of Rhode Island '15, Course Hero Intern

Ask a question for free

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern