Discover millions of ebooks, audiobooks, and so much more with a free trial

From $11.99/month after trial. Cancel anytime.

Building Modern Web Applications With JakartaEE, NoSQL Databases and Microservices: Create Web Applications Jakarta EE with Microservices, JNoSQL, ... and MicroProfile easily (English Edition)
Building Modern Web Applications With JakartaEE, NoSQL Databases and Microservices: Create Web Applications Jakarta EE with Microservices, JNoSQL, ... and MicroProfile easily (English Edition)
Building Modern Web Applications With JakartaEE, NoSQL Databases and Microservices: Create Web Applications Jakarta EE with Microservices, JNoSQL, ... and MicroProfile easily (English Edition)
Ebook589 pages4 hours

Building Modern Web Applications With JakartaEE, NoSQL Databases and Microservices: Create Web Applications Jakarta EE with Microservices, JNoSQL, ... and MicroProfile easily (English Edition)

Rating: 0 out of 5 stars

()

Read preview

About this ebook

For many years, Java EE has been an important platform for mission-critical enterprise applications. To accelerate the development of enterprise applications for a cloud-native world, leading software vendors collaborated to transfer Java EE technologies to the Eclipse Foundation, where they will evolve under the Jakarta EE brand.

This book will be your comprehensive guide to creating Jakarta EE applications and microservices with Microprofile. The book begins with an introduction to Jakarta EE and quickly goes on to teach you about the various databases and their advantages. After this, you will explore the JNoSQL and Jmoordb frameworks to understand how to build Jakarta EE applications with NoSQL databases. Moving forward, you'll explore Eclipse MicroProfile and see how it helps build microservices with Java. Also, you will learn about various development applications such as Java Server Faces, Eclipse Krazos, PrimeFaces, Vaadin, and understand how to integrate them with your backend. Towards the end, you will learn about security, testing, and understanding continuous integration.
LanguageEnglish
Release dateSep 29, 2020
ISBN9789389423358
Building Modern Web Applications With JakartaEE, NoSQL Databases and Microservices: Create Web Applications Jakarta EE with Microservices, JNoSQL, ... and MicroProfile easily (English Edition)

Related to Building Modern Web Applications With JakartaEE, NoSQL Databases and Microservices

Related ebooks

Programming For You

View More

Related articles

Reviews for Building Modern Web Applications With JakartaEE, NoSQL Databases and Microservices

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Building Modern Web Applications With JakartaEE, NoSQL Databases and Microservices - Geovanny Mendoza Gonzalez

    CHAPTER 1

    Jakarta EE Platform

    Today the Java EE/Jakarta EE platform has proven to be one of the leading platforms for developing cloud applications.

    This chapter is an introduction to The Jakarta EE platform. It will present a feedback of its history of the platform, a set of specifications and simple examples that will help you to use the potential of the platform for building enterprise applications ready for use in the cloud.

    Numbered APIs and frameworks will be used to demonstrate our goal: Jakarta NoSQL, Jmoordb, Vaadin, and Java Server Faces.

    Structure

    Jakarta EE platform

    Context and Dependency Injection (CDI) 2.0 (JSR 365)

    Enterprise Java Beans 3.2 (JSR-345)

    Java Servlet 4.0 (JSR-369)

    Java API for RESTful Web Services JAX-RS 2.1 (JSR-370)

    Java EE Security API 1.0 (JSR -375)

    The Java API for JSON Binding JSON-B 1.0 (JSR-367)

    JavaServer Faces JSF 2.3 (JSR-372)

    Java Persistence API 2.2 (JSR-338)

    Objective

    This chapter is an introduction to the APIs that are part of the Java Enterprise Edition platform and Jakarta EE.

    Jakarta EE platform

    Jakarta EE is a platform that offers a set of components and APIs for the development of Java business applications. It orients towards the support of HTML5, Enterprise Java Beans, Web Services, JSON, Databases, Microservices, WebSocket among others.

    These components are based on the combination of specifications (JSR) coming from the ecosystem of JavaEE platform led by Java Community Process (http://jcp.org) but rebranded under Eclipse Jakarta EE and Eclipse Microprofile standards (https://microprofile.io/); formed by companies, user groups, communities, which establish consensus on them, to be adaptable to the new requirements of the industry.

    The APIs that are part of the specifications are known as Java Specification Request (JSR).

    History

    The Java EE platform has incorporated major changes in recent years, allowing a simpler development, performance improvements through reusable components resulting in lighter and scalable applications adapting to environments such as cloud, and Microservices.

    We will briefly review the evolution of the platform over the last few years, showing each version along with the corresponding specifications.

    The following table shows the different versions of the platform:

    Table 1.1

    From JavaEE to Jakarta EE

    Over time companies and developer communities began to show concern for the evolution of the platform. Faced with the slowness in the evolution of specifications two important initiatives emerged in recent years; on the one hand, Microprofile (http://microprofile.io) and on another one Java EE Guardian (https://javaee-guardians.io/).

    Microprofile brings an innovative standardization path to produce new APIs based on a consensus of large companies intending to optimize Java business applications to support Microservices. In 2017, Reza Rahman and Dr. James Gosling, created the Java EE Guardian formed by enthusiasts, to achieve a more open and agile process to implement new advances in the Java EE platform.

    These efforts led Oracle to relinquish control of the Java EE platform to the Eclipse foundation. In what has been called Jakarta EE (https://jakarta.ee), whose purpose is focused on the active participation of the community to achieve progress more quickly and adapt to changing market needs.

    Throughout this book we will develop a series of examples, so we specify some requirements that you must have installed on your computer.

    We assume that the reader has prior knowledge of Java and the process of installing the development tools.

    We will begin by briefly describing some of the JSR with code segments that will help you understand these APIs. If you already have experience with the Java EE platform, you will realize that they are very basic examples and concepts as they are aimed at new developers who want to know and develop with the Java EE/Jakarta EE platform.

    You will see many definitions of the specification to guide the reader through the rules established through the JSR.

    Above all let us mention some APIs grouped around the usage: web application, enterprise application, Web service and security management, microservices application.

    The following table shows the APIs of the Web application technologies:

    Table 1.2

    The following table shows APIs stack of the Enterprise application technologies:

    Table 1.3

    The following table shows APIs stack of the Web services technologies:

    Table 1.4

    The following table shows APIs of the management and security technologies:

    Table 1.5

    The following table shows APIs of the Java EE-related Specs in Java SE

    Table 1.6

    Java EE/Jakarta EE application development

    In this part, we will show you how to develop an application based Jakarta EE. Below a list of requirements we defined to develop our samples:

    Java Development Kit

    JDK 8 or higher

    Build Tools

    Maven or Gradle

    Integrated Development Environment

    Apache NetBeans

    Plugin for Payara Micro

    Eclipse

    IntelliJ IDEA

    NoSQL Databases

    MongoDB

    Application Server

    Payara or any Jakarta EE certified application server

    Dockers

    The source code of the projects are available at the following link:

    https://github.com/avbravo/Building-JakartaEE-Applications-Microservices

    To create a Web project that supports the Jakarta EE 8 specification, you must just include your Maven project dependency:

    jakarta.platform

    jakarta.jakartaee-api

    8.0.0

    A Jakarta EE applications run in containers that provide a running environment for the components and APIs of the Jakarta EE platform.

    Java EE defines four main container types which are the following:

    Application client container: They are usually applications that run on the desktop, with a user interface and use the platform components.

    Applet container: Manages the execution of applets.

    Web container: Servlet. JSP, Java Server Faces are applications that run in a Web container using HTTP protocols.

    EJB container: Manages the execution of Enterprise Java Beans, which runs inside a Java EE server.

    Context and Dependency Injection CDI 2.0 (JSR 365)

    Context and Dependency Injection (CDI) was introduced from Java EE 6, and the specification defines it as a set of services that allow objects to be linked to the context life cycle, injected, and associated with Interceptor and Decorator.

    It allows interacting with events. Offers integration with Enterprise Java Beans components, Java Server Faces Managed Beans, Bean Validation. It supports asynchronous events.

    Injection of dependencies can be performed at the level of:

    Attributes

    Constructor

    Method

    The permitted scoped are:

    RequestScoped

    SessionScoped

    ApplicationScoped

    ConversationScoped

    DependentScoped

    The specification defines some annotations that we will show in the following table:

    Table 1.7

    The @Model annotation helps us define a model that we can use with other components:

    @Model

    public class Dog {

    private Integer id;

    private String name;

    //Setters/Getters

    }

    define @Qualifier Domestic:

    @Qualifier

    @Retention(RUNTIME)

    @Target({METHOD, FIELD, PARAMETER, TYPE})

    public @interface Domestic {

    }

    The @Qualifier can be applied to a class or attribute definition levels:

    @Domestic

    public class AnimalProcessor {

    }

    Or by dependency injection:

    public class AnimalController{

    @Inject

    @Domestic

    AnimalProcessor animalProcessor;

    }

    To declare a Bean from a session bean we use the annotation @SessionScoped:

    @Named

    @SessionScoped

    public class MySession implements Serializable {

    private Dog dog;

    //Setters/Getters

    }

    CDI allows easy integration with Java Server Faces components through Expression Language (EL):

    Id/>

    #{mySession.dog.id}/>

    @Interceptor example

    We’ll develop an example of an interceptor. When the start() method is executed through the interceptor, it will invoke the authorize() method defined in ExerciseInterceptor, and then the control is returned to the start() method.

    Figure 1.1

    Steps to show the use of interceptor in a project that we will use as a very basic example:

    Firstly, we will define the Model as shown below:

    @Model

    public class Person {

    private Integer id;

    private String name;

    private Boolean walk;

    public Person() {

    }

    //Setter/Getter

    }

    Next, we will define the Feed interface for use with the interceptor:

    Inherited

    @InterceptorBinding

    @Target({TYPE, METHOD})

    @Retention(RUNTIME)

    public @interface Exercise {

    }

    We declare the FeedInterceptor to intercept the event of the method where we use the @Exercise interface:

    @Exercise

    @Interceptor

    public class Exercisenterceptor {

    @Inject

    Person person;

    @AroundInvoke

    public Object authorize(InvocationContext ic) throws Exception {

    try {

    if (!person.getWalk()) {

    return ic.proceed();

    } else {

    throw new Exception();

    }

    } catch (Exception e) {

    throw e;

    }

    }

    }

    Create the beans.xml file in the WEB-INF directory and specify the class that was created of Interceptor type:

    1.0 encoding=UTF-8?>

    http://java.sun.com/xml/ns/javaee

    xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance

    xsi:schemaLocation=http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd>

    com.book.mycdi.interceptor.ExerciseInterceptor

    Use the @Exercise annotation in the start() method to intercept invocations to this method that will be trapped by ExerciseInterceptor:

    @Model

    public class ExerciseController implements Serializable {

    @Inject

    Person person;

    public Person getPerson() {

    return person;

    }

    public void setPerson(Person person) {

    this.person = person;

    }

    @Feed

    public String start() {

    try {

    System.out.println(The person started the walk);

    } catch (Exception e) {

    System.out.println(error + e.getLocalizedMessage());

    }

    return ;

    }

    }

    Enterprise Java Beans 3.2 (JSR-345)

    The specification defines the Enterprise Java Beans architecture (EJB) as architecture for the development and deployment of components for enterprise applications.

    The EJBs have as goals to be:

    Standard component architecture for enterprise applications in Java.

    Development, deployment and distribution of enterprise components in Java.

    Development and deployment of Web services.

    Make easy the creation of business applications.

    The session types used by EJBs are:

    Singleton

    Stateful

    Stateless

    The components can be used simply utilizing annotations @EJB:

    @EJB

    MyResources myResources;

    This example implements a @Schedule that sends a notification every 10 minutes:

    @Singleton

    public class SendCongratulations {

    @Schedule(minute = */10, hour = *, persistent = false)

    public void send() {

    // …

    }

    }

    @Producer example

    Create an Enterprise Java Beans project from NetBeans IDE to manage a connection to a MongoDB database using a @Producer.

    Select from the menu File | New | Project. In Categories, select Java With Maven option and in Projects, select EJB Module option:

    Figure 1.2

    Indicate the name of the project as nosqlejb

    Figure 1.3

    In the following dialog we leave the Server at in our case because we are using Payara Micro from NetBeans, but if you have another server installed select from the list. And in Java EE Version select Java EE 8:

    Figure 1.4

    At the end of the process, the project structure is generated with all the necessary files. Our next step is to configure it according to our requirements:

    Figure 1.5

    Edit the pom.xml file and locate the javaee-api dependency:

    javax

    javaee-api

    8.0

    provided

    And replace with Jakarta EE

    Enjoying the preview?
    Page 1 of 1