JEE (5) Getting Started Tutorial

Geplaatst op 4 november 2009 door Marc

Being a long time Spring minded developer, I thought it was time to dust off my JEE knowledge. This was harder than I thought. Mostly due to the lack of complete tutorials to get me going quickly.
So I set myself 2 goals:

  • Build a basic JEE application
  • Blog my findings so others can use this.

Building a basic JEE Application
First I started to think what I wanted to use (tooling). I am a big fan of Maven and OSS, and this is my tooling list:

Next an Idea for a JEE application. I decided not to use JPA, since most developers know how to use this and I wanted to focus upon delivering a maven ready JEE application. With this in mind I decided to create 1 EJB, 1 servlet and 1 web service. Put this in an ear and deploy this on GlassFish.

So I set out to search the web for maven examples. After some time I found an usable example and tweaked this to reduce unnecessary code.

The maven setup for my JEE (5) project
I decided to split up my artifacts into three categories, thus creating a multiproject maven structure. In the enclosed zip the pom and files are included to create the projects.

Project Zip

jee5project
ear
ejb
web

The project
For the rest of this blog, please unzip the enclosed tutorial files and import the project into eclipse as maven project. In eclipse this is file -> import -> general -> maven project. This wil import the project and create the above described structure.

The classes
First the EJB project and the backing domain I am going to use.
So what do we actually need in order to get an EJB going in a JEE project these days. Actually it is very easy to create this, but keep in mind that different JEE container vendors have their own specific requirements. Since I am using Glassfish, I have only found 1 such item when developing my EJB.

First we create a domain object: Mine is a boring Person object.
[code]
package nl.wowww.jeedemo.ejb.model;

import java.io.Serializable;

public class Person implements Serializable {

private static final long serialVersionUID = 3275423032530216173L;

private String name;

public Person() {
super();
}
public Person(String name) {
super();
this.name = name;
}
……
@Override
public String toString() {
return “My name is ” + ((name!=null&&!””.equalsIgnoreCase(name))?name:”Unknown”);
}
}
[/code]

Very exciting stuff :) NOT. Next we need to create an EJB. And here starts the fun. It used to be a hassle to create them, but now it is very easy to do. Below my interface and impl of my PersonService EJB

[code]
package nl.wowww.jeedemo.ejb;

import java.util.List;
import javax.ejb.Local;
import nl.wowww.jeedemo.ejb.model.Person;

@Local
public interface PersonService {
List findAll();
}

package nl.wowww.jeedemo.ejb;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;

import nl.wowww.jeedemo.ejb.model.Person;

@Stateless
public class PersonServiceBean implements PersonService {

public List findAll() {
List persons = new ArrayList();
persons.add(new Person(“Marc”));
persons.add(new Person(“Willem”));
persons.add(new Person(“Harrold”));
persons.add(new Person(“”));
persons.add(new Person(“Marc”));
return persons;
}
}
[/code]

And thats it. Your EJB is ready to go. Yes you can see, no ejb-jar.xml file is needed. You may use it, but it is not required. You can use it to override the annotations. Since my EJB is so simplistic, I do not need it.

A simple mvn clean package will present you with the ejb artifact you can deploy in the Glassfish container. I am not going to do this, since I want my web application and ejb deployed together through an ear-file.

So we continue. The web project.
I want a servlet and a web service to output the persons through the newly created EJB. How do we do this? Well, we create a servlet obviously and create and reference to the EJB there. This is the code:

[code]
package nl.wowww.jeedemo.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nl.wowww.jeedemo.ejb.PersonService;
import nl.wowww.jeedemo.ejb.model.Person;

public class PersonServlet extends HttpServlet {

private static final long serialVersionUID = -7817095420988477060L;

@EJB(beanInterface = PersonService.class, name = “nl.wowww.jeedemo.ejb.PersonService”)
private PersonService personService;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

resp.setContentType(“text/html”);
PrintWriter out = resp.getWriter();

for (Person person : personService.findAll()) {
out.println(person);
}

out.flush();
}

}
[/code]

here it is. Very easy. No Initial context stuff, just an annotation @EJB(beanInterface = PersonService.class, name = “nl.wowww.jeedemo.ejb.PersonService”). The syntax should be like this, otherwise Glassfish cannot find the bean when it is deployed. This is the container specific requirement I talked about earlier.

The web service is also very easy in JEE (5).
[code]
package nl.wowww.jeedemo.ws;

import javax.ejb.EJB;
import javax.jws.WebMethod;
import javax.jws.WebService;

import nl.wowww.jeedemo.ejb.PersonService;
import nl.wowww.jeedemo.ejb.model.Person;

@WebService
public class PersonWebService {

@EJB(beanInterface = PersonService.class, name = “nl.wowww.jeedemo.ejb.PersonService”)
private PersonService personService;

@WebMethod
public String showPersonen() {
StringBuilder builder = new StringBuilder();
for (Person person : personService.findAll()) {
builder.append(person);
}
return builder.toString();
}

}
[/code]

The included zip does not reflect this EJB example. The only thing you have to do is either uncomment the current implementation or replace it with the code here.

BTW. This is all you need to do to get it working :). Neat.
You can generate all kind of extra artifacts using jaxws, but I do not need it now (dependencies are included in the pom).

Do a mvn clean package and you have the war file you could deploy in the glassfish container. But I want the ear file.

The EAR file
The project has been created when you imported the maven project. And we are done :). the only thing you need to do is run the mvn clean package and you will get an ear file containing the web application, web service and the EJB artifact.

This ear you can now deploy in Glassfish, either through the web interface or through the commandline with maven.

When the ear is deployed, you can access the following urls:

Project Zip

Kind regards,

linkedinlogoMarc de kwant

Gepost in categorie: TechTalk

Reacties

Plaats reactie zonder facebook account

Anoniem