The following sections describe how to get a reference to an EJBHome object and call EJB methods from ActionScript.
Before calling the methods of an EJB from ActionScript, you must get a reference to an EJBHome object.
#include "NetServices.as"
NetServices.setDefaultGatewayUrl("http://localhost/flashservices/gateway");
Note: There are several other ways to specify the gateway URL. For more information, see "Configuring Flash Remoting MX," in Chapter 2.
gatewayConnection = NetServices.createGatewayConnection();
getService function, as shown in the following example where SampleLoan is the JNDI name:SampleLoanHome = gatewayConnection.getService("SampleLoan", this);
The first parameter of the getService function is the JNDI name of the EJBHome object; the JNDI name cannot contain a period (.) character. The second parameter of the getService function, this, specifies that the results of service function calls are returned to this Flash timeline.
Unlike JavaBeans and Java classes, you must invoke the create method of an EJBHome object and return an EJBObject object before calling EJBObject methods. After you call the create method of an EJBHome object, you can use the ActionScript create_Result(result) function to get a reference to the EJBObject object and invoke its methods.
For example, to invoke the following method of a stateless session bean that performs loan calculations based on loan principal, term, and interest rate:
public double calculate(double principal, int months, float rate){
if (rate < 0 || rate>1) return 0.0;
double monthlyPayment = principal * (rate / (1 - Math.pow(1 + rate,-months)));
return monthlyPayment;
}
You could use the following ActionScript code:
function runExample()
{
SampleLoanHome = gatewayConnection.getService("SampleLoanEjbHome", this);
SampleLoanHome.create();
}
function create_Result( result )
{
flashStatelessEJB = result;
calculate();
}
function calculate()
{
flashStatelessEJB.calculate( (number (principalInput.text)), (number (monthsInput.text)), (number (rateInput.text)) );
}
To handle the function results, you use a result handler function like the following:
function calculate_Result (result)
{
payOutput.text = result;
}
For more information about handling function results in ActionScript, see "Handling function results in ActionScript".
The following sections illustrate the three pieces required to call an EJB from Flash Remoting MX:
The example Flash application calculates loan payments by invoking the calculate method on the following stateless session bean:
package ejbeans;
import java.rmi.*;
import java.util.*;
import javax.ejb.*;
public class SampleLoanBean implements SessionBean
{
//////////////////////////////////////////
///// General Enterprise EJBean stuff
//////////////////////////////////////////
private SessionContext sessionContext;
public void ejbCreate() throws CreateException{};
public void ejbRemove() throws RemoteException{};
public void ejbActivate() throws RemoteException{};
public void ejbPassivate() throws RemoteException{};
public void setSessionContext(SessionContext context) throws RemoteException {
sessionContext = context;
}
public double calculate(double principal, int months, float rate){
if (rate < 0 || rate>1) return 0.0;
double monthlyPayment = principal * (rate / (1 - Math.pow(1 + rate,-months)));
// Double payMe=new Double (monthlyPayment);
return monthlyPayment;
}
}
The following figure shows the user interface of the example Flash application with callouts that indicate the field types and variable names referenced in the ActionScript code:
The following code is the ActionScript of the example Flash application:
// Include NetServices library.
#include "NetServices.as"
#include "NetDebug.as"
// --------------------------------------------------
// Start up the application
// --------------------------------------------------
if (inited == null)
{
// do this code only once
inited = true;
// Set the default gateway URL.
NetServices.setDefaultGatewayUrl("http://localhost/flashservices/gateway");
// Connect to the gateway.
gatewayConnection = NetServices.createGatewayConnection();
}
// --------------------------------------------------
// Handlers for user interaction events
// --------------------------------------------------
// Get reference to EJBHome and create EJBObject when user
// clicks the runButton.
function runExample()
{
flashstatelessHome = gatewayConnection.getService
("SampleLoanEjbHome", this);
flashstatelessHome.create();
}
// ---------------------------------------------------
// Business Methods
// ---------------------------------------------------
// Calculate payment based on data user enters in principalInput,
// monthsInput, and rateInput text fields. Function arguments must
// be numbers. Java method expects double, int, and float.
function calculate()
{
flashStatelessEJB.calculate( (number (principalInput.text)),
(number (monthsInput.text)), (number (rateInput.text)) );
}
// --------------------------------------------------
// Handlers for data coming in from server
// --------------------------------------------------
// Get reference to EJBObject and invoke calculatePayment function.
function create_Result( result )
{
flashStatelessEJB = result;
calculate();
}
// Get reference to calculate result object and display result
// in PayOutput text field.
function calculate_Result (result)
{
payOutput.text = result;
}