
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:
Now the Developer Studio generates the bean's class along with its home and remote interfaces.
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:
public String reverseString(String string) {
StringBuffer sb = new StringBuffer(string);
String s = sb.reverse().toString();
return s;
}public double squareRoot(double d) {
double dd = Math.sqrt(d);
return dd;
}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 isPrime method 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: