• Complain

Elder Moraes [Elder Moraes] - Java EE 8 Cookbook

Here you can read online Elder Moraes [Elder Moraes] - Java EE 8 Cookbook full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2018, publisher: Packt Publishing, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

Elder Moraes [Elder Moraes] Java EE 8 Cookbook

Java EE 8 Cookbook: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "Java EE 8 Cookbook" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

A practical guide for building effective enterprise solutions with Java EE 8

About This Book
  • Recipes to get you up-and-running with Java EE 8 application development
  • Learn how to apply the major Java EE 8 APIs and specifications
  • Implement microservices and Reactive programming with Java EE 8
Who This Book Is For

This book is for developers who want to become proficient with Java EE 8 for their enterprise application development. Basic knowledge of Java is assumed

What You Will Learn
  • Actionable information on the new features of Java EE 8
  • Using the most important APIs with real and working code
  • Building server side applications, web services, and web applications
  • Deploying and managing your application using the most important Java EE servers
  • Building and deploying microservices using Java EE 8
  • Building Reactive application by joining Java EE APIs and core Java features
  • Moving your application to the cloud using containers
  • Practical ways to improve your projects and career through community involvement
In Detail

Java EE is a collection of technologies and APIs to support Enterprise Application development. The choice of what to use and when can be dauntingly complex for any developer. This book will help you master this. Packed with easy to follow recipes, this is your guide to becoming productive with Java EE 8.

You will begin by seeing the latest features of Java EE 8, including major Java EE 8 APIs and specifications such as JSF 2.3, and CDI 2.0, and what they mean for you.

You will use the new features of Java EE 8 to implement web-based services for your client applications. You will then learn to process the Model and Streaming APIs using JSON-P and JSON-B and will learn to use the Java Lambdas support offered in JSON-P. There are more recipes to fine-tune your RESTful development, and you will learn about the Reactive enhancements offered by the JAX-RS 2.1 specification.

Later on, you will learn about the role of multithreading in your enterprise applications and how to integrate them for transaction handling. This is followed by implementing microservices with Java EE and the advancements made by Java EE for cloud computing.

The final set of recipes shows you how take advantage of the latest security features and authenticate your enterprise application.

At the end of the book, the Appendix shows you how knowledge sharing can change your career and your life.

Style and approach

Task based learning guide to help ease application development with Java EE.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Elder Moraes [Elder Moraes]: author's other books


Who wrote Java EE 8 Cookbook? Find out the surname, the name of the author of the book and a list of all author's works by series.

Java EE 8 Cookbook — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "Java EE 8 Cookbook" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make
Getting ready

If you don't have an account with Oracle Cloud you can register for a trial at https://cloud.oracle.com/tryit.

That's all you need, beyond having created the Docker image in the first recipe of this chapter.

Starting and stopping

To start GlassFish, just execute this script:

$GLASSFISH_HOME/bin /asadmin start-domain --verbose

To stop it, excecute the following script:

$GLASSFISH_HOME/bin /asadmin stop-domain

Introduction

Threading is a common issue in most software projects, no matter which language or other technology is involved. When talking about enterprise applications, things become even more important, and sometimes harder.

A single mistake in some thread can affect the whole system, or even the whole infrastructure. Think about some resources that are never released, memory consumption that never stops increasing, and so on.

The Java EE environment has some great features for dealing with these and plenty of other challenges, and this chapter will show you some of them.

How to do it...
  1. Let's create a User class as a model for our JSON message:
public class User {
private String name;
private String email;
public User(){
}
public User(String name, String email) {
this.name = name;
this.email = email;
}
@Override
public String toString() {
return "User{" + "name=" + name + ", email=" + email + '}';
}
//DON'T FORGET THE GETTERS AND SETTERS
//THIS RECIPE WON'T WORK WITHOUT THEM
}
  1. Then, let's create a class to use JSON-B to transform an object:
public class JsonBUser {
public static void main(String[] args) throws Exception {
User user = new User("Elder", "elder@eldermoraes.com");
Jsonb jb = JsonbBuilder.create();
String jsonUser = jb.toJson(user);
User u = jb.fromJson(jsonUser, User.class);
jb.close();
System.out.println("json: " + jsonUser);
System.out.println("user: " + u);
}
}

The result printed is:

json: {"email":"elder@eldermoraes.com","name":"Elder"}
user: User{name=Elder, email=elder@eldermoraes.com}

The first line is the object transformed into a JSON string. The second is the same string converted into an object.

See also
  • The full source code of this recipe is at https://github.com/eldermoraes/javaee8-cookbook/tree/master/chapter10/ch10-async-servlet.
Transaction management

Transaction management is one of the trickier subjects in computer science. One single wrong line, one unpredicted situation, and your data and/or your user will suffer the consequences.

So it would be nice if we could count on the server to do it for us. And most of the time we can, so let me show you how to do it.

Getting ready

Let's start by adding the dependency:


javax
javaee-api
8.0
provided
Building Java EE containers using Docker

Since day one, Java EE has been based on containers. If you doubt it, just have a look at this diagram:

Java EE architecture httpsdocsoraclecomjavaee6tutorialdocbnacjhtml - photo 1
Java EE architecture: https://docs.oracle.com/javaee/6/tutorial/doc/bnacj.html

It belongs to Oracle's official documentation for Java EE 6 and, actually, has been much the same architecture since the times of Sun.

If you pay attention, you will notice that there are different containers: a web container, an EJB container, and an application client container. In this architecture, it means that the applications developed with those APIs will rely on many features and services provided by the container.

When we take the Java EE application server and put it inside a Docker container, we are doing the same thing it is relying on some of the features and services provided by the Docker environment.

This recipe will show you how to deliver a Java EE application in a container bundle, which is called an appliance.

Getting ready

Start by adding the Java EE dependency:


javax
javaee-api
8.0
provided
There's more...

A common issue for this clustering with GlassFish arises when you don't have the SSH service running in your nodes; as there are tons of options of them for many operating systems, we won't cover each one of them here.

The best way to set up a Java EE cluster today is using containers (specially Docker containers). So, I'd recommend that you have a look at , Rising to the Cloud Java EE, Containers, and Cloud Computing . If you mix that content with this, you will have a powerful environment for your application.

To allow your application to share its session with all the nodes in the cluster, you need to edit the web.xml file, find the web-app node, and insert this:

< distributable />

Without it, your session clustering will not work. You need also to keep all objects that you are holding in the session as serializable.

Finally, there's a commercial version of GlassFish, which is Payara Server. If you are looking for support and other commercial perks, you should take a look at it.

How it works...

The first important thing in our server is this annotation:

@Singleton

Of course, we must ensure that we have one and only one instance of the server endpoint. This will ensure that all peers are managed under the same umbrella.

Let's move on to talk about peers:

private final List peers = Collections.synchronizedList
(new ArrayList<>());

The list holding them is a synchronized list. This is important because you will add/remove peers while iterating on the list, so things could be messed up if you don't protect it.

All the default websocket methods are managed by the application server:

@OnOpen
public void onOpen(Session peer){
peers.add(peer);
}
@OnClose
public void onClose(Session peer){
peers.remove(peer);
}
@OnError
public void onError(Throwable t){
System.err.println(t.getMessage());
}
@OnMessage
public void onMessage(String message, Session peer){
peers.stream().filter((p) -> (p.isOpen())).forEachOrdered((p) ->
{
p.getAsyncRemote().sendText(message + " - Total peers: "
+ peers.size());
});
}

Also, let's give a special mention to the code on our onMessage method:

@OnMessage
public void onMessage(String message, Session peer){
peers.stream().filter((p) -> (p.isOpen())).forEachOrdered((p)
-> {
p.getAsyncRemote().sendText(message + " - Total peers: "
+ peers.size());
});
}

We are sending a message to the peer only if it is open.

Now looking to our client, we have a reference to the server URI:

private final String asyncServer = "ws://localhost:8080/
ch10-async-websocket/asyncServer";

Note that the protocol is ws, specific to websocket communication.

Then, we have a method to open the connection with the server endpoint:

public void connect() {
WebSocketContainer container =
ContainerProvider.getWebSocketContainer();
try {
container.connectToServer(this, new URI(asyncServer));
} catch (URISyntaxException | DeploymentException | IOException ex) {
System.err.println(ex.getMessage());
}
}

And once we have the message confirmation from the server, we can do something about it:

@OnMessage
public void onMessage(String message, Session session) {
response.resume(message);
}

This response will appear on the endpoint that is calling the client:

@GET
Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «Java EE 8 Cookbook»

Look at similar books to Java EE 8 Cookbook. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «Java EE 8 Cookbook»

Discussion, reviews of the book Java EE 8 Cookbook and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.