http://www.developer.com/java/ejb/article.php/3555301/Who-Says-EJB-21-is-Too-Hard.htm
EJBs, and in particular the EJB 2.1 spec, have a reputation of being powerful but complex. I have seen more arguments against using Java/J2EE because EJBs are too complex than for any other reason, and from many different sources. Common wisdom seems to be that J2EE and EJBs are synonymous; hence, J2EE is too complex to use. Obviously, even casual inspection of this reasoning shows the flaws. For one thing, EJBs are just a part of the J2EE specification (or the Java EE specification as it is now known), and need not be used at all. I also take issue that the complexity is somehow the fault of EJBs. Certainly, they could be simplified (and EJB 3.0 goes a long way to doing that), but the real problem is that they deal with a very complex problem domain. Delivering components that have transactional support, security, location transparency, discovery, scalability and concurrency is not easy. However, I will admit that using EJB 2.1 as detailed in the specification, with deployment descriptors, containers, and EJB lookup is quite a steep learning curve. It is a tough architecture to roll out to developers who need to be up and running fast, unless you have good tools that you can rely on to simplify the whole thing. And that is what this article is about. It is intended to demonstrate that EJBs, even in their 2.1 form, need not be so scary, that you can jump in and with little time and effort have a working system that uses the full strength EJB architecture. Between using NetBeans to create the EJB, Sun App Server 8.1 to deploy it, and Java Studio Creator to consume it, you will not see a deployment descriptor, lookup code, or anything else complex (unless you really want to). This article can either be used as an introduction to the approach and tools available, or as a click-along tutorial. If you intend to do the latter, you will need to download NetBeans and Java Studio Creator 2 EA 2: For NetBeans, you will need to download at least NetBeans 4.1. You can choose to download the NetBeans 5.0 Beta, but should know that if you do so you should get a package that includes the Sun App Server PE 8.1 (or download it separately). Unless you have the app server and have configured NetBeans to know about it, you will not be able to create an enterprise project in NetBeans. For Java Studio Creator, you need to get the Early Access 2 release of Creator 2. To do this, go to http://developers.sun.com/prodtech/javatools/jscreator/ea/jsc2/index.html and register; you then will be able to download the EA2 version of Creator 2 that has all of the new EJB support. This article demonstrates a very narrow part of the whole field of EJB creation and consumption. For one thing, I intend to demonstrate Session Beans only (I prefer using only Session beans for remote APIs) and will also skip the persistence part, prefering to concentrate on the creation of an API and the consumption within Java Studio Creator. However, there are some excellent guides and tutorials that do a better job of demonstrating Entity EJBs (for persistence) in NetBeans, and also an alternative one that shows how Hibernate could be used for persistence instead (should that be your preference). First, you are going to use NetBeans to create your Session EJB-based API. To do this, start NetBeans and: Before you do anything else, you need an object to pass data from the server to the client. You could certainly do this using arrays, but a full DTO (Data Transfer Object) lets you send structured data across from the server, and this will demonstrate some of the richer features in Java Studio Creator. For this demo, you are going to create an object that holds some typical details about a person (like first and last name, telephone numbers, age, salary, and whether the person is on vacation—this last one is in there as a way to demonstrate boolean values). So, create a new class called PersonDTO: That's it for your DTO object. Now, on to the EJB. For the sake of this demo, call your EJB "PeopleEJB". The API you write will be pretty simple (deliberately) but will provide some methods to list back the data as collections of the PersonDTO objects you just created. Believe it or not, that's your EJB written. Gotcha: Although life is definitely not that hard when using the SB representation in the Projects pane, you must remember to use it for any changes to the method name, return type, or parameters. This is because if you just change it in the class you have been editing the method bodies in, it will not update the other files that make up the EJB. It is very important to remember to go through the SB representation for all method additions, changes, or removals! To make changes to a method, just right-click on the method in the SB representation object, select Refactor->, and choose the type of change you want to make; then follow through the dialog. Sometimes, you may find that it will be necessary to remove an old method and replace it with a completely new one to change the details; this is ugly but not particularly hard. Hint: You can always copy the body of the method before removing it, and then paste it into the new method afterwards and make any necessary changes. The final step in NetBeans is to create the EJB Jar file. To do this, simply go to the menu and select Build->Build Main Project. Look in the output window to see where the jar file is created. The next step is to take the EJB Jar file you just created and deploy it to the App Server instance inside Java Studio Creator. Now, you are ready to use the new EJB you just made: This imports the EJB. You can see it in the Enterprise Java Beans section in the Servers tab of the top right pane. Expand it and you should see the methods you created as well. Almost there now. Throw a quick project together and a couple of pages to demonstrate the new easy binding features for EJBs in Creator. You will demonstrate it using Data Tables, but the same EJB methods can be bound to other controls using the same tricks. For more information on general binding in Java Studio Creator, see the tutorials at http://developers.sun.com/prodtech/javatools/jscreator/ea/jsc2/learning/tutorials/index.html. First, get a simple project and page to list all the people: Now, run the project (Run->Run Main Project from the menu). You should see a page like this: If you have not yet played with the new Java Studio Creator 2 controls, go ahead and click on the column headings; this allows you to sort the data displayed in the table by the different columns (and, by using the + symbols that appear after you first select a sort order, you can create a sort hierarchy as well). So, you just bound the results from a simple EJB list method directly into a data table. Very neat, but what if you want to be a little more selective about what you show? If you remember, you created a method called listIncomeAbove that took a value to filter the details returned by. Create a page that uses that. Well, once again you barely scratched the surface of what is possible, but if you think about the ground covered in this article, it is quite impressive. You have put together an EJB API, deployed it to the App Server Instance in Java Studio Creator, consumed it in Creator, and then bound it to a control in a JSF page. More to the point, you have done this without seeing a deployment descriptor, Home and Remote interface classes, JNDI lookup code, or anything else that might be considered tricky or confusing. The new EJB features in Java Studio Creator 2 truly do extend the component model and ease of use data bindings into the world of full EJB 2.1 architecture. Many people are eagerly awaiting the release of EJB 3.0 to help simplify EJB development, but with suitable use of the right IDE tools, simple development using EJBs is available right now, I also look forward to EJB 3.0, but it's hard to imagine how much easier it will make things for the demo we have seen. Download the source code for this article here and here. Dick Wall is a Lead Systems Engineer for NewEnergy Associates, A Siemens Company based in Atlanta, GA that provides energy IT and consulting solutions for decision support and energy operations. He can be reached for comment on this and other matters at dick.wall@newenergyassoc.com. He also co-hosts the Java Posse with Tor Norbye and Carl Quinn, a podcast devoted to Java News and Interviews, which can be found at http://javaposse.com.
Who Says EJB 2.1 is Too Hard?
October 11, 2005
First Steps
Using NetBeans to Create the Session API
Create a DTO Class
public static PersonDTO[] peopleList = null;
static {
// set up some demo data for the people list
peopleList = new PersonDTO[10];
peopleList[0]=new PersonDTO("Bloggs","Fred","770-555-1212",
"770-555-2121",55000.00,25,true);
peopleList[1]=new PersonDTO("Schmo","Joe","404-555-3434",
"404-555-4343",125000.00,62,false);
peopleList[2]=new PersonDTO("McTog","Nog","770-555-5555",
"770-555-6666",25000.00,17, true);
peopleList[3]=new PersonDTO("Pepper","Petey","770-555-5432",
"770-555-3456",84378.12,33,false);
peopleList[4]=new PersonDTO("Fairlane","Sandy","770-555-9999",
"770-555-9898",142222.00,55,true);
peopleList[5]=new PersonDTO("Lane","Lois","123-555-8833",
"123-555-5544",95000.00,22,false);
peopleList[6]=new PersonDTO("Wesley","Tom","770-555-7654",
"770-555-6756",48000.00,44,false);
peopleList[7]=new PersonDTO("McDunne","Diane","770-555-5275",
"770-555-6641",76543.21,51,false);
peopleList[8]=new PersonDTO("Burt","George","770-555-8111",
"770-555-7222",70000.00,41,false);
peopleList[9]=new PersonDTO("Smith","Anne","770-555-5656",
"770-555-7878",90000.00,24,true);
}
/**
* PersonDTO constructor. Takes all fields to produce a ready
* populated Person Data Transfer Object
* @param last String containing last name
* @param first String containing first name
* @param homeTel String containing home telephone
* @param workTel String containing work telephone
* @param income double containing person's income
* @param age int age of person
* @param onVac boolean flag set to true if person is on vacation
*/
public PersonDTO(String last,String first,String homeTel,
String workTel,double income,int age,
boolean onVac) {
this.lastName = last;
this.firstName = first;
this.homeTel = homeTel;
this.workTel = workTel;
this.income = income;
this.age = age;
this.onVacation = onVac;
}
Creating the EJB
return PersonDTO.peopleList;
ArrayList vacationers = new ArrayList();
for(int i=0; i<PersonDTO.peopleList.length; i++) {
if(PersonDTO.peopleList[i].isOnVacation()) {
vacationers.add(PersonDTO.peopleList[i]);
}
}
// convert to array
PersonDTO[] retArray =
(PersonDTO[])vacationers.toArray(new PersonDTO
[vacationers.size()]);
return retArray;
Note: If you are using JDK 1.5 and are comfortable with it, you can always use the forEach notation instead of the above.
ArrayList higherIncome = new ArrayList();
for(int i=0; i<PersonDTO.peopleList.length; i++) {
if(PersonDTO.peopleList[i].getIncome() > amount) {
higherIncome.add(PersonDTO.peopleList[i]);
}
}
// convert to array
PersonDTO[] retArray =
(PersonDTO[])higherIncome.toArray(new PersonDTO
[higherIncome.size()]);
return retArray;
Create the EJB Jar file
Deploy to the Java Studio Creator App Server
Deploy Direct to Sun App Server 8.1 Instance in Creator
Important: Also check the Generate: RMIStubs checkbox (this will create a client you can use in Java Studio Creator).
Note: Keep this location noted somewhere, or remember to use the find tool on your OS to locate it. It can be tricky to remember.
Consume the EJB in Creator
Create a Demo Client in Java Studio Creator
Note: If you get a dialog asking you to select a target, make sure you select the whole table, probably called table 1).

Click here for a larger image.
A More Involved Example

double amount =
Double.parseDouble((String)this.amountInput.getValue());
this.peopleEJBRemoteListIncomeAbove1.setAmount(amount);
this.peopleEJBRemoteListIncomeAbove1.refresh();
Conclusion
About the Author