Oracle WebLogic Server 12.1.3 Developer Guide: Enabling Java EE 7 Features


Options



Overview

Purpose

Oracle WebLogic Server 12.1.3 supports several new Java Platform, Enterprise Edition (Java EE) 7 features. This tutorial demonstrates how to enable Java Persistence API (JPA) 2.1, enable Java API for RESTful Web Services (JAX-RS) 2.0, use the WebSocket Protocol, and use the Java API for JSON Processing.

Time to Complete

Approximately 30 minutes.

Introduction

This tutorial uses WebLogic Server 12.1.3 and a Java EE application named ActionBazaar - an application that uses REST services to implement a system for placing real-time bids for auction items, as well as monitoring the bid activity in real-time.

The ActionBazaar Java EE application requires JPA 2.1 and JAX-RS 2.0. This tutorial includes the steps to enable the JPA 2.1 and JAX-RS 2.0 Java EE 7 features in WebLogic Server 12.1.3.

The ActionBazaar Java EE application also demonstrates the use of the WebSockets protocol as well as JSON processing:

  • WebSockets are used in the monitor feature of the ActionBazaar application; when item bids are submitted a monitor page can view the activity in real-time using Websockets.
  • JSON is used as the serialized data representation throughout the application; for bids sent and received as well as the messages for monitoring the bidding activity.

Hardware and Software Requirements

Prerequisites

Completion of the Installing and Configuring the WebLogic Server Zip Distribution and Code Examples Oracle By Example tutorial

Enabling JPA 2.1 in WebLogic Server 12.1.3

While WebLogic Server 12.1.3 supports JPA 2.1, the Java Archive (JAR) files are not configured by default; some additional configuration is required to enable JPA 2.1 support. In this tutorial, the JPA 2.1 implementation is configured on the WebLogic Server classpath.

  1. On the Linux system, open an terminal session as the oracle user.

  2. In the terminal session, navigate to the WebLogic Server domain directory. Execute cd /u01/wls12130/user_projects/domains/mydomain.

  3. Execute vi bin/setDomainEnv.shto edit the setDomainEnv.sh script in the bin directory.

  4. In the vi editor, create a PRE_CLASSPATH variable that prepends both JPA 2.1 JAR files to the classpath.

    PRE_CLASSPATH=/u01/wls12130/oracle_common/modules/javax.persistence_2.1.jar:/u01/wls12130/wlserver/modules/com.oracle.weblogic.jpa21support_1.0.0.0_2-1.jar
    export PRE_CLASSPATH

  5. Execute wq to save and close setDomainEnv.sh script.

  6. Execute  ./startWebLogic.sh to start the WebLogic Server domain. 

    The WebLogic Server domain starts.

    To validate that JPA 2.1 is in use, the two JPA 2.1 JAR files appear in the classpath.

Creating a Datasource Connection to a Derby Database

The ActionBazaar application requires a JDBC Datasource connection to a Derby database named jdbc/ActionBazaarDB.

  1. On the Linux system, open a browser. Navigate to the WebLogic Server console at http://localhost:7001/console.

  2. In the WebLogic Server Administration Console, log in as the weblogic user. The password is welcome1 if this domain was setup by using the previous tutorial.

  3. In the Domain Structure tree, expand Services and click Data Sources.

  4. In the Summary of JDBC Data Sources panel, on the Configuration tab, click the New button and select Generic Data Source.

  5. In the Create a New JDBC Data Source page, enter the information for the data source.

    • For Name, enter ActionBazaarDB.

    • For JNDI Name, enter jdbc/ActionBazaarDB.

    • For Database Type, select Derby.

  6. Click Next.

  7. For Database Driver, select Derby's Driver (Type 4 XA) Versions:Any and click Next.

  8. Click Next.

  9. In the Connection Properties page, enter the connection information.

    • For Database Name, enter actionbazaar.

    • For Host Name, enter localhost.

    • For Port, select 1527.

    • For Database User, enter app.

    • For Password, enter app.

    • For Confirm Password, enter app.

  10. Click Next.

  11. In the Test Database Connection page, click Test Configuration.

  12. The connection test succeeds. Click Next.

  13. In the Select Targets page, select the check box next to myserver. Click Finish.

    The Data Source configuration changes are activated.

Deploying JAX-RS 2.0 in WebLogic Server 12.1.3

The ActionBazaar application requires JAX-RS 2.0 deployed to the WebLogic Server 12.1.3 domain.

  1. In the Domain Structure tree within the WeLogic Server Administration Console, click Deployments.

    The Summary of Deployments is displayed.

  2. Click Install.
  3. In the Install Application Assistant, navigate to /u01/wls12130/wlserver/common/deployable-libraries/ and select the jax-rs-2.0.war file.

  4. Click Next.

  5. Select Install this deployment as a library and click Next.

  6. Accept the default name and security configuration and click Next.

  7. Click Finish.

    The JAX-RS library has been deployed.

Deploying the ActionBazaar Application to WebLogic Server

Now that the requirements are configured (JPA 2.1 and JAX-RS 2.0), the ActionBazaar application can be deployed.

  1. Download the actionbazaar.war file from this Oracle By Example tutorial to the Linux system. In this tutorial the download location is /home/oracle.

  2. In the Domain Structure tree, within the WebLogic Server Administration Console, click Deployments in the Domain Structure.

  3. In the Summary of Deployments click Install.

  4. In the Install Application Assistant, navigate to the actionbazaar.war file location. In this tutorial, the location is /home/oracle/actionbazaar.war.

  5. Select the actionbazaar.war file

  6. Click Next.

  7. In the Install Application Assistant, select Install this deployment as an application and click Next.

  8. In the Install Application Assistant, accept the default name and security configuration and click Next.

  9. In the Install Application Assistant, click Finish.

  10. In the Settings for actionbazaar page, click Save.

  11. In the Domain Structure tree, click Deployments.

    The actionbazaar deployment is in the Active state.

Running the ActionBazaar application on WebLogic Server

  1. In the web browser, open the ActionBazaar application URL: http://localhost:7001/actionbazaar/bid.html.

  2. In the browser, click the Set your name button and enter a name. In this tutorial the name is testuser.

  3. In the browser, click the Add button.

  4. Enter a bid and click OK. In this tutorial the bid is harddrive $100.

  5. The bid is accepted by the system and displayed.

  6. In the web browser, utilize the WebSocket Monitor by opening a new tab and browsing to http://localhost:7001/actionbazaar/bid_live.html.

  7. In the browser, switch back to the Bid Portal tab.

    Click Add, enter a new bid, and click OK. In this tutorial the bid is monitor $200.

  8. In the browser, switch back to the WebSocket Monitor tab.

    The Bid Activity displays the new bid and the Debug Log is displayed.

Using the WebSocket Protocol in WebLogic Server

The ActionBazaar application uses the WebSocket Protocol in WebLogic Server in a scenario where there are two HTML application files: bid.html and bid_live.html.

The scenario includes the following interactions:

  • The bid_live.html file opens a WebSocket connection to the endpoint published by the BidNotifierService class.
  • The BidNotifierService provides a WebSocket handshake response so the bid_live.html file can setup the JavaScript WebSocket handlers such as ws.onmessage.
  • When a user interacts with bid.html to set their name and enter a bid a HTTP POST is performed using the URI /rest/bids along with a JSON payload.
  • The BidRestService accepts the HTTP POST and invokes the BidService EJB to persist the bid using JPA.
  • The BidNotifierService is using the @Observes annotation for JPA transactions in the AFTER_COMPLETION phase; after it receives notification of a new bid it sends a new JSON object to the bid_live.html page.
  • The bid_live.html page receives the message using the ws.onmessage handler and displays the message in the table.

The ActionBazaar source code is supplied as a Zip file in this tutorial.

In the bid_live.html source file displayed below:

  • lines 82-86 construct the WebSocket endpoint URL ending with /bidnotifier
  • line 89 opens the WebSocket connection
  • lines 97-100 define the ws.onmessage function, which parses the JSON payload and displays it in a table on the page

In the BidNotifierService.java source file displayed below and lines 98-123 implement the broadcastBidNotification method which sends notification to all WebSocket sessions by observing the AFTER_COMPLETION transaction phase and using the injected bidEvent object.

Using the Java API for JSON Processing 1.0 (JSR 353) in WebLogic Server

The ActionBazaar application also uses the Java API for JSON Processing to encode/decode WebSocket messages between the JSON format and com.actionbazaar.domain.Bid objects.

In the BidEventEncoderDecoer.java source file displayed below:

  • lines 44-50 construct a JSON representation of a Bid object by using the Json.createObjectBuilder method, which is part of the Java API for JSON Processing
  • lines 71-82 create a Bid object from a JSON representation by using the Json.createReader method, parsing the fields and returning the Bid object.

Deploying a More Recent Version of JAX-RS RI in WebLogic Server

Note that this tutorial has been tested with JDK 1.7.0 release 79 and Jersey jaxrs-ri-2.17. While later releases may work, they have not been tested for this tutorial.

  1. In the browser on the Linux machine, navigate to  https://jersey.java.net/ and click the Download link.

  2. Click the link for the Jersey JAX-RS 2.0 RI bundle.

  3. Save the zip archive to the Linux machine. In this tutorial, the location is /u01.

  4. Open a secure shell into the Linux machine as the oracle user and execute cd /u01 to change to the working directory.

  5. Execute unzip jaxrs-ri-2.17.zip to extract the archive to this location.

    The extracted archive contents are displayed.

  6. Execute mkdir jaxrs-ri-2.17 to create a working directory for the new WebLogic Server JAX-RS library.

  7. Execute cd jaxrs-ri-2.17 to change into the working directory.

  8. Execute mkdir META-INF to create the directory used by a WebLogic Server library.

  9. Execute mkdir WEB-INF to create the directory used by a WebLogic Server library.

  10. Execute mkdir WEB-INF/lib to create the directory to contain the Java JAR files for this WebLogic Server library.

  11. Execute cp /u01/jaxrs-ri/api/* WEB-INF/lib to copy the Java JAR files for this WebLogic Server library.

  12. Execute cp /u01/jaxrs-ri/ext/* WEB-INF/lib to copy the Java JAR files for this WebLogic Server library.

  13. Execute cp /u01/jaxrs-ri/lib/* WEB-INF/lib to copy the Java JAR files for this WebLogic Server library.

  14. Add a MANIFEST.MF file to the /u01/jaxrs-ri-2.17/META-INF directory. A file is supplied with this tutorial and the contents are listed below.

    Manifest-Version: 1.0
    Ant-Version: Apache Ant 1.9.2
    Created-By: 1.7.0_51-b13 (Oracle Corporation)
    Specification-Title: Weblogic JAX-RS 2.0
    Specification-Version: 2.0
    Implementation-Title: Weblogic JAX-RS 2.0 Reference Implementation
    Implementation-Version: 2.17
    Extension-Name: jax-rs
    
  15. Add a weblogic.xml file to the /u01/jaxrs-ri-2.17/WEB-INF directory. A weblogic.xml file is supplied with this tutorial and the contents are listed below.

  16. Execute /u01/jdk1.7.0_79/bin/jar -cvf jaxrs-ri-2.17.war * to create the WebLogic Server library archive.

  17. Execute mv jaxrs-ri-2.17.war ../ to move the WebLogic Server library archive to the /u01 directory.

  18. Execute cd /u01/wls12130/user_projects/domains/mydomain to change to the WebLogic Server domain directory and then execute ./startWebLogic.sh to start WebLogic Server.

  19. In the browser on the Linux machine, navigate to http://localhost:7001/console and the WebLogic Server Administration console is displayed. Login using weblogic for the username and welcome1 for the password.

    The WebLogic Server Administration Console is displayed.

  20. Click Deployments in the Domain Structure.

  21. If the library and application from the previous activity are still deployed, delete these deployments.

    Click the check box next to actionbazaar in the Summary of Deployments and click Delete.

  22. Click the check box next to jax-rs in the Summary of Deployments and click Delete.

  23. Click Deployments in the Domain Structure and click Install.

  24. In the Install Application Assistant, navigate to the /u01 directory and select the jaxrs-ri-2.17.war file and click Next.

  25. Select Install this deployment as a library and click Next.

  26. Accept the Name and click Finish.

  27. The jaxrs-ri-2.17 deploys and displays the settings for this library.

  28. Click Deployments. The jaxrs-ri-2.17 deployment displays as active.

Summary

In this tutorial, you learned to:

  • Enable JPA 2.1 with WebLogic Server 12.1.3
  • Use JAX-RS 2.0 with WebLogic Server 12.1.3
  • Use the WebSocket Protocol with WebLogic Server 12.1.3
  • Use the Java API for JSON Processing (JSR 353) with WebLogic Server 12.1.3

Resources

Credits