Developing Middleware in Java EE 8
eBook - ePub

Developing Middleware in Java EE 8

Build robust middleware solutions using the latest technologies and trends

  1. 252 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Developing Middleware in Java EE 8

Build robust middleware solutions using the latest technologies and trends

Book details
Book preview
Table of contents
Citations

About This Book

Use Java features such as JAX-RS, EJBs, and JPAs to build powerful middleware for newer architectures such as the cloudAbout This Bookā€¢ Explore EJBs to build middleware solutions for enterprise and distributed applicationsā€¢ Understand middleware designs such as event-based and message-driven web services ā€¢ Learn to design and maintain large-scale systems and vendor disputesWho This Book Is ForEnterprise architects, designers, developers, and programmers who are interested in learning how to build robust middleware solutions for enterprise software will find this book useful. Prior knowledge of Java EE is essentialWhat You Will Learnā€¢ Implement the latest Java EE 8 APIs and manage dependencies with CDI 2.0ā€¢ Perform CRUD operations and access databases with JPA 2.1ā€¢ Use bean validation API 2.0 to validate dataā€¢ Develop business logic with EJB 3.2ā€¢ Incorporate the REST architecture and RESTful API design patternsā€¢ Perform serialization and deserialization on JSON documents using JSON-Bā€¢ Utilize JMS for messaging and queuing models and securing applicationsā€¢ Test applications using JUnit and Mockito and deploy them using DockerIn DetailMiddleware is the infrastructure in software based applications that enables businesses to solve problems, operate more efficiently, and make money. As the use of middleware extends beyond a single application, the importance of having it written by experts increases substantially. This book will help you become an expert in developing middleware for a variety of applications. The book starts off by exploring the latest Java EE 8 APIs with newer features and managing dependencies with CDI 2.0. You will learn to implement object-to-relational mapping using JPA 2.1 and validate data using bean validation. You will also work with different types of EJB to develop business logic, and with design RESTful APIs by utilizing different HTTP methods and activating JAX-RS features in enterprise applications. You will learn to secure your middleware with Java Security 1.0 and implement various authentication techniques, such as OAuth authentication. In the concluding chapters, you will use various test technologies, such as JUnit and Mockito, to test applications, and Docker to deploy your enterprise applications. By the end of the book, you will be proficient in developing robust, effective, and distributed middleware for your business.Style and approachLearn how to design and implement professional enterprise middleware solutions using the latest techniques and features provided by the Java EE 8 platform.

Frequently asked questions

Simply head over to the account section in settings and click on ā€œCancel Subscriptionā€ - itā€™s as simple as that. After you cancel, your membership will stay active for the remainder of the time youā€™ve paid for. Learn more here.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Both plans give you full access to the library and all of Perlegoā€™s features. The only differences are the price and subscription period: With the annual plan youā€™ll save around 30% compared to 12 months on the monthly plan.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, weā€™ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes, you can access Developing Middleware in Java EE 8 by Abdalla Mahmoud in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming. We have over one million books available in our catalogue for you to explore.

Information

Year
2018
ISBN
9781788392228
Edition
1

Accessing the Database with JPA 2.1

The data access layer is the most fundamental part and the backbone of any enterprise application. The ultimate goal of any enterprise solution is to store and retrieve its data with respect to consistency, availability, and performance. A common problem arises when dealing with a relational database from an object-oriented system. All runtime data is represented as objects, where the real data is stored as rows in tables. When trying to save an object state into a row in a database table, or fetching some data from the database and wrapping the result back into an object again, a set of redundant programmatic statements should be written, which is very boring and actually old-school.
Object-to-relational mapping is a very common approach to overcoming this redundancy by providing a layer above the database access APIs, allowing developers to directly store and retrieve objects, and mapping their attributes directly to database tables and vice versa, providing a virtual object-oriented database interface for the actual relational data.
The Java Persistence API (JPA) does a great job at this by providing Java developers with all the required operations, mappings, and techniques for mapping objects into the relational database. In this chapter, we are going to learn how to use JPA for the following:
  • Creating and using JPA entities
  • Mapping entities to tables and columns
  • Performing CRUD operations
  • Mapping entity relationships
  • Using the JPA query language and criteria APIs
  • Mapping inheritance relationships

What's new in JPA 2.2?

For those who are familiar with JPA, let's first introduce the new features provided by JPA 2.2:
  • Stream query results
  • Repeatable annotations
  • Java 8 date and time support
  • CDI support in converters

Architecture

The Java persistence API is built around the following components:
  • Persistence provider: A persistence provider is a JPA implementation provided by a vendor. As with most Java APIs, JPA is a standard API, where a set of different actual implementations are available from different vendors. The persistence provider means the vendor of the actual implementation we are using.
  • Entities: An entity is a class that represents a domain object in our enterprise application. From a JPA perspective, an entity is represented with a table in the database, and an instance of this entity represents a record in that table.
  • Entity managers: An entity manager is an object that represents a connection to a database, and contains all methods for the different operations that can be performed with the database, such as inserting a new record, retrieving a single record, performing advanced queries, and so on.
  • Entity transaction: An entity transaction represents a database transaction that can be either committed or rolled back according to the application state. Any update operations (insert, update, delete) should be performed within the boundaries of an entity transaction.
  • Query: As the name suggests, a query is an object that is used to perform a database query! In addition to the regular SQL, JPA provides a custom query language (called JPQL) that can be used to perform queries exactly like SQL, but with object-oriented concepts.
  • Persistence unit: A persistence unit is a group of entities involved in a persistence context. A persistence unit is specified using a configuration file (persistence.xml), describing connection information, entities involved, and other useful configurations.

Writing your first JPA application

Let's start learning JPA by writing a simple application that receives some movie data from the user through a web page interface, then saves the user input into a database table.

Step 1: Creating a data source

The first step is to define a data source inside the enterprise application. A data source represents a pool of JDBC connections to a specific database that will be used later by the persistence APIs. A data source is identified by a JNDI name, and includes JDBC connection information. To declare your data source in GlassFish, you should create a glassfish-resources.xml file in the WEB-INF folder for your project, as shown in the following file:
/WEB-INF/glassfish-resources.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd"> <resources> <jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="mysql_cinemasapp_rootPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false"> <property name="serverName" value="localhost"/> <property name="portNumber" value="3306"/> <property name="databaseName" value="cinemasapp"/> <property name="User" value="root"/> <property name="Password" value="root"/> <property name="URL" value="jdbc:mysql://localhost:3306/cinemasapp?zeroDateTimeBehavior=convertToNull"/> <property name="driverClass" value="com.mysql.jdbc.Driver"/> </jdbc-connection-pool> <jdbc-resource enabled="true" jndi-name="java:app/cinemasapp-ds" object-type="user" pool-name="mysql_cinemasapp_rootPool"/> </resources> 

Step 2: Creating a persistence unit

A persistence unit in is named scope where we define persistence contextual details such as data source information, a list of entities to include (or exclude), and any other information that may be useful for the persistence provider to work. A persistence unit is identified by a unit name, and is created by defining the following file:
/META-INF/persistence.xml <?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="jpa-examplesPU" transaction-type="JTA"> <jta-data-source>java:app/cinemasapp-ds</jta-data-source> <exclude-unlisted-classes>false</exclude-unlisted-classes> <properties> <property name="javax.persistence.schema-generation.database.action" value="create"/> </properties>
 </persistence-unit> </persistence> 
Note that by setting the exclude-unlisted-classes property to false, all entity classes found by the persistence provider inside the WAR deployment file will be included in the context of the persistence unit.

Step 3: Creating an entity class

An entity class is the main player in a JPA application. It's a class that represents a table in the database, whose instances, in turn, represent rows inside this table. An entity class is no more than a POJO, annotated with @Entity, with a field elected as a primary key and annotated with @Id, as in the following example:
Movie.java @Entity public class Movie { @Id @GeneratedValue private long id; private String title; public Movie() { } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } } 
Note that you do not have to create a corresponding table in the database yourself, as the persistence unit is declared with the javax.persistence.schema-generation.database.action property set to create, which means that the persistence provider is responsible for creating the ...

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. Dedication
  4. Packt Upsell
  5. Contributors
  6. Preface
  7. Delving into Java EE 8
  8. Dependency Injection Using CDI 2.0
  9. Accessing the Database with JPA 2.1
  10. Validating Data with Bean Validation 2.0
  11. Exposing Web Services with JAX-RS 2.1
  12. Manipulating JSON with JSON-B 1.0
  13. Communicating with Different Systems with JMS 2.0
  14. Sending Mails with JavaMail 1.6
  15. Securing an Application with Java Security 1.0
  16. Making Interactive Applications with WebSockets 1.1
  17. Other Books You May Enjoy