Showing posts with label EJB. Show all posts
Showing posts with label EJB. Show all posts

Sunday, February 26, 2012

Book Review: EJB 3.1 Cookbook

I had the great opportunity to review the EJB 3.1 Cookbook by Richard M. Reese from Packt Publishing. The book provides a number of simple "recipes" for getting specific tasks accomplished in a compact and simple way. I thoroughly enjoyed reading the book. Admittedly it much easier to read a cookbook than some other technical content.

The content is divided into 12 chapters with numerous recipes per chapter. The topics range from an introduction to EJBs to web services. Each section generally has six to eight recipes. Each recipe is easily digestible with a minimum amount of EJB knowledge to start with. Keep in mind, this book will be easy to use if you have a minimum level of experience. Some recipes are sufficiently simple enough for a beginner.

The book will guide a novice to become a practitioner though expertly done examples. Advanced topics are covered in a way to make them seem simple. The practitioner will begin to become an expert.

As a general rule, I would not recommend anyone to read a cookbook from cover to cover. This applies to a "real" cookbook as well. However, if you are trying to get a deeper understanding of the EJB as a tool, then reading the book will at least add these recipes to your toolbox to draw upon when needed.

There are a number of important facts that a developer should keep in mind. These kernels of truth are what makes the difference in a book, and a reference. This manuscript is filled with a number.

In Chapter 2, there is a very succinct explanation of write locks.

A write lock does not permit concurrent access to a method. Once a client begins executing a method marked with a write lock, no other clients are permitted access to the method until the method invocation completes. Other clients are blocked. This is the default concurrent behavior of singletons.
This is a very important default behavior and should be kept in the developers mind when working with singletons. This default behavior means that you don't have to synchronize within singleton methods unless you change this default behavior. This is a great example of what you can find in this book.

Chapter 4 on EJB persistence includes a recipe on validating null and temporal fields. The @Null annotation is mentioned which validates that a field is null. I am not sure I am convinced of the usefulness of this particular annotation, as opposed to the @NotNull annotation which has more direct usefulness. The @Future and @Past annotations are really helpful. These simple annotations have removed a lot of boiler plate code, and made the check of a date much simpler to understand.

Chapter 6 on Transaction Processing is a really well thought out section, and explains the differences between Container Managed Transactions (CMT) (default), and Bean Managed Transactions (BMT). This is a must read for anyone doing anything beyond simple transactions. The explanation on the limitations of BMT are very important to consider. The chapter explains the transaction annotations to a level that I have not really seen in other books. The recipes (examples) are very clear, and make the concepts much easier to understand. For example:

There are a few restrictions on the use of transaction attributes. First, the REQUIRED attribute can not be used for methods of web service endpoints. This is because the caller of the endpoint has not started a transaction and the since the REQUIRED attribute requires one, it will fail.
This is very well written and explains the concept, and gives reasons why this will not work.

The chapter on EJB security mentioned a feature in GlassFish which I think is very cool, and only requires checking a check box to enable. The feature is Default Principal to Role Mapping. This allows you to avoid having to generate a XML deployment configuration files to map the roles from the Web/EJB application to roles defined in a realm on the server.

The chapter on Interceptors provides a number of good use case examples where interceptors really shine. I really liked this chapter and the explanation of annotations for use with interceptors like @ExcludeClassInterceptors and the differentiation between class and method interceptors. The explanation on interceptor chaining is one of the easiest explanations I have seen.

If I had a single complaint about the book, it would be chapter ten on Web Services. It has a couple of recipes, but not nearly enough. Web services play a major role in EE today; especially JAX-RS. The lack of a number clear examples is a missed opportunity.

Overall I found the book to be really well done, and I would gladly keep it as a reference for myself, and team. I would give it (4/5) stars. It is well written and worth purchasing.

Friday, July 01, 2011

Java EE 6: Using an External JPA Library in an EJB

I was looking at the NetBeans Forums for Java Enterprise Edition (EE) Development. I saw a couple of folks who were asking how I did a couple of different projects where I sent them the final result, but not an explanation of how I did it.

I decided to do a YouTube video instead of writing out how I did it. I thought the best way to demonstrate it was in a video. "A picture tells a thousand words."

Saturday, August 16, 2008

How to implement EJB 3.0 <env-entry/> in ejb-jar.xml

A topic which is mentioned in the EJB 3.0 Specification (JSR-220) is the ability to add environment entries in ejb-jar.xml file. What I found interesting is that I could not Google for a good example of how to implement it. As a result, I thought I would provide a good simple example.

The first item you should examine is the schema resources for Java EE. Specifically we want to examine the ejb-jar_3_0.xsd. This file contains all of the information we need to create and validate an ejb-jar.xml file.

First we create an ejb-jar.xml file in our EJB project. This file should contain the following information at a minimum:


1 <?xml version="1.0" encoding="UTF-8"?>
2 <ejb-jar
3 xmlns="http://java.sun.com/xml/ns/javaee"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
6 http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
7 version="3.0">
8 </ejb-jar>

From here we can add our beans and environment variables. I have created an example project which demonstrates how to use the environment variables. Here is my ejb-jar file:

1 <?xml version="1.0" encoding="UTF-8"?>
2 <ejb-jar
3 xmlns="http://java.sun.com/xml/ns/javaee"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
6 http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
7 version="3.0">
8 <enterprise-beans>
9 <session>
10 <ejb-name>EnvironmentalResourceInjectionExampleBean</ejb-name>
11 <env-entry>
12 <env-entry-name>s1</env-entry-name>
13 <env-entry-type>java.lang.String</env-entry-type>
14 <env-entry-value>Hello</env-entry-value>
15 </env-entry>
16 <env-entry>
17 <env-entry-name>s2</env-entry-name>
18 <env-entry-type>java.lang.String</env-entry-type>
19 <env-entry-value>EJB Resource Injection World!</env-entry-value>
20 </env-entry>
21 </session>
22 </enterprise-beans>
23 </ejb-jar>
In my example, I use a stateless session bean with @WebService annotation. This allows me to test it right in GlassFish. I also use the @Resource annotation to inject my environment entries. I have used the name attribute to give it a JNDI lookup value. This works for GlassFish. Other containers may use the mappedName attribute.

Here is my stateless session bean:

1 /*
2 *
3 * Blue Lotus Software, LLC
4 *
5 * $Id: EnvironmentalResourceInjectionExampleBean.java 100 2008-08-16 21:57:19Z jyeary $
6 *
7 * Copyright [2008] [John Yeary]
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package com.bluelotussoftware.ee.examples;
19
20 import javax.annotation.Resource;
21 import javax.ejb.Stateless;
22 import javax.jws.WebMethod;
23 import javax.jws.WebService;
24
25 /**
26 *
27 * @author John Yeary
28 * @version 1.0
29 */
30 @Stateless
31 @WebService
32 public class EnvironmentalResourceInjectionExampleBean implements
33 EnvironmentalResourceInjectionExampleRemote,
34 EnvironmentalResourceInjectionExampleLocal {
35
36 @Resource(name = "s1")
37 String s1;
38 @Resource(name = "s2")
39 String s2;
40
41 public String printEnv() {
42 return s1 + " " + s2;
43 }
44
45 @WebMethod
46 public String printEnvironment() {
47 return printEnv();
48 }
49 }

When I deploy the application to GlassFish, I can use the Web Service Tester to validate that I get the injected values.




As we can see, the values s1 and s2 are sucessfully injected. The complete Netbeans 6.1 project and source can be found here.

Popular Posts