February 23, 2019
Hot Topics:

Who Says EJB 2.1 is Too Hard?

  • October 11, 2005
  • By Dick Wall
  • Send Email »
  • More Articles »

Creating 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.

  1. In the Projects pane, right-click on the Enterprise Beans node and select New->Session Bean.
  2. For the name, enter PeopleEJB, and select a suitable package, leave the Session Type as Stateless, and check both Remote and Local for the Create Interface options.
  3. Click Finish. A PeopleEJBSB (SB for Session Bean) will appear in the Projects pane. You can think of this as "the" EJB even though there are actually several files behind it. Get used to interacting with this session bean representation in the Projects pane for adding and altering methods. That way, the IDE will do all of the necessary work in all the files behind the scenes.
  4. Right-click on the new PeopleEJBSB and select Add->Business Method.
  5. In the dialog, for the name enter listAll, and for the return type enter PersonDTO[] (for example, an Array of PersonDTO objects).
  6. Make sure both Remote and Local are checked for the Use in interface options, and click OK.
  7. Repeat this for another method called listOnVacation (with the same PersonDTO[] return type).
  8. Create another one called listIncomeAbove with the same return type, but this time add a parameter called amount of type double (you will use this to pass in an amount to filter income by before returning the results).
  9. You need to add method definitions for the EJB methods you just created, so expand the PeopleEJBSB node and one of the Remote or Local nodes inside of it (doesn't matter which).
  10. Double-click on listAll first. The editor window will go to that method definition.
  11. Replace the body of the method with the following code:
  12. return PersonDTO.peopleList;
  13. At this point, you might notice that the editor is complaining about PersonDTO because you haven't imported it yet (unless you happen to have PersonDTO in the same package as PeopleEJB). If it is complaining, right-click in the editor window and select Fix Imports.
  14. Next, you need to flesh out the listOnVacation method. In the Projects pane, find the listOnVacation method and double-click on it.
  15. Replace the method definition with:
  16. ArrayList vacationers = new ArrayList();
    for(int i=0; i<PersonDTO.peopleList.length; i++) {
       if(PersonDTO.peopleList[i].isOnVacation()) {
    // convert to array
    PersonDTO[] retArray =
       (PersonDTO[])vacationers.toArray(new PersonDTO
    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.
  17. Finally, replace the body of the listIncomeAbove method with:
  18. ArrayList higherIncome = new ArrayList();
    for(int i=0; i<PersonDTO.peopleList.length; i++) {
      if(PersonDTO.peopleList[i].getIncome() > amount) {
    // convert to array
    PersonDTO[] retArray =
       (PersonDTO[])higherIncome.toArray(new PersonDTO
    return retArray;
  19. Right-click in the editor again and select Fix Imports a second time, to include any classes you have used and that have not yet been imported.
  20. Finally, do a build all (from the menu, Build->Clean and Build Main Project). If you get errors in other files that need imports, right-click in the offending file and fix the imports; then rebuild until you get a clean build.

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.

Create the EJB Jar file

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.

Deploy to the Java Studio Creator App Server

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.

Deploy Direct to Sun App Server 8.1 Instance in Creator

  1. Start Java Studio Creator 2 EA 2.
  2. In the top left pane, select the Servers tab.
  3. Find the Deployment Server node, and right-click on it. Select Start/Stop Server.
  4. In the dialog, hit the Start Server button (if it is greyed out, the server may already be running).
  5. In a Web browser, browse to http://localhost:24848.
  6. Log in using username: admin and password: adminadmin.
  7. Find EJB Modules under Applications and click on it.
  8. Click the Deploy button in the main frame.
  9. Browse to the EJB jar file you created in NetBeans (probably in the dist folder under your project folder), and open that jar file.
  10. Click Next, use Application Name: CreatorDemoEJB.
  11. Important: Also check the Generate: RMIStubs checkbox (this will create a client you can use in Java Studio Creator).
  12. Click OK. If all goes well, you should be returned to the EJB list and see the newly deployed EJB module.
  13. The client stubs you created can be found in a jar file in your Java Studio Creator installation directory under SunAppServer8/domains/creator/applications/j2ee-modules/CreatorDemoEJB. Remember this location because you will need it in the next section.
  14. 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

Now, you are ready to use the new EJB you just made:

  1. In Java Studio Creator, in the Servers Tab, right-click on the Enterprise Java Beans node and select Add Set of Session EJBs.
  2. In the dialog, enter the name CreatorDemoEJB.
  3. Set the RMI-IIOP port to 23700 (the port number that the Creator app server instance runs on by default).
  4. Click the Add button next to the Client Jar section, and find and open the client jar you created in the last section. Click Next.
  5. This next screen shows you what beans and methods you are importing. You can simply accept them, but read on first:
  6. Because the client creation uses reflection, some information, like the names of the method parameters, is lost by default. This screen lets you edit the parameter names; this will make the API easier to use in Creator.
  7. Because you only have one method with a parameter, select the listIncomeAbove method and in the parameters section, edit arg0 to read the amount (double-click on amount to change it and enter when you have finished changing it).
  8. Click finish when done.

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.

Page 2 of 3

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date