!--a11y-->
Developing the UtilCallback Enterprise
Bean 
The UtilCallback bean is a stateless session bean. It is a very simple bean that has just three business methods. The most interesting of them is the isPrime() method, which that makes remote calls in its body to an object that is implemented on the client side. For these calls the bean behaves as a client to a remote application running on a remote system.
First, go back from the Package Explorer to J2EE Explorer view. Then proceed as follows to create the enterprise bean:
1. Create the UtilCallback session bean. To do this, access the context menu of the UtilCallback project and choose New ® Stateless Session Bean in Package ® examples.iiop.

2. On the Create EnterpriseBean screen, specify UtilCallback as the name of the bean. Select only the remote option that is provided below so that the Developer Studio does not generate local home and remote interfaces. Then choose OK.
Now the Developer Studio generates the bean’s class along with its home and remote interfaces.
3. In order to add business methods to the bean’s class, you must first open it by expanding the ejb-jar.xml node of the project and opening the UtilCallbackBean node. A screen called UtilCallbackBean must appear in the editor on the right-hand side.
4. Choose the Methods tab to open the screen that enables you to define the bean’s business methods. Define the following three methods:
reverseString Method
Field |
Value |
Name |
reverseString |
Return Type |
java.lang.String |
Parameter: Name |
string |
Parameter: Type |
java.lang.String |
Method summary: This method gets a string and prints it on the screen in reverse order.
squareRoot Method
Field |
Value |
Name |
squareRoot |
Return Type |
double |
Parameter: Name |
d |
Parameter: Type |
double |
Method summary: This method calculates the square root of a number.
isPrime Method
Field |
Value |
Name |
isPrime |
Return Type |
boolean |
Parameter1: Name |
number |
Parameter1: Type |
long |
Parameter2: Name |
callback |
Parameter2: Type |
examples.iiop.Notificator |
Method summary: This method tests whether a given number is a primary one. Since it is a slow operation for large numbers (of type long), this method uses the Notificator callback object to provide information about the progress of the operation to the client.
You must save the changes that you made to the bean by choosing File ® Save from the Developer Studio menu.
Your UtilCallbackBean screen must look like the following:

5. Implement the bean’s business methods that you added in the previous step. To do this, choose the Bean tab to open the editor and then enter the following code:
¡ The reverseString Method Body
public String reverseString(String string) { StringBuffer sb = new StringBuffer(string); String s = sb.reverse().toString(); return s; } |
¡ The squareRoot Method Body
public double squareRoot(double d) { double dd = Math.sqrt(d); return dd; } |
¡ The isPrime Method Body
public boolean isPrime(long number, Notificator callback) { try { boolean result = true; long n = (int)Math.floor(Math.sqrt(number)); System.out.println("Testing " + number); System.out.println("Number of maximum trials:" + n); callback.message("Number of maximum trials: " + n);
for (long j = 2; j <=n ; j++){ if (number%j == 0) result = false;
if (j == n/4){ System.out.println("25% completed...."); callback.message("25% completed...."); } if (j == n/2){ System.out.println("50% completed...."); callback.message("50% completed...."); } if (j == 3*n/4){ System.out.println("75% completed...."); callback.message("75% completed...."); } if (j == n){ System.out.println("100% completed ...."); callback.message("100% completed...."); } } return result;
}catch(Exception e){ e.printStackTrace(); throw new javax.ejb.EJBException(); } } |

The algorithm used in the isPrimemethod of the UtilCallback bean to check whether the number is a primary one is quite primitive and slow. However, it has been used on purpose to slow down the operation so that you can easily distinguish between the calls that are made to the callback object (they are printed in the command line when the remote client is executed).
At the end, the structure of the UtilCallback project should be the following:

Generating Stubs for the Notificator Object
