Invoking an EJB component method
To
invoke an EJB component method, you need to execute the PowerScript statements
required to perform these operations:
-
Use the Lookup function to access the component’s
home interface. -
Invoke the method on the interface to create or
find an instance of the component and get a reference to the component’s
remote interface. -
Invoke the business methods on the remote interface.
This section applies to client applications that use an EAServer proxy object and PowerScript
functions. For information about invoking EJB methods in client
applications that use an EJB client proxy and EJBConnection methods, see Chapter 27, “Building an EJB client .” However, the
EJBConnection object is deprecated for use with EAServer and the PowerBuilder Application
Server Plug-in.
Specifying the home interface name
PowerBuilder provides an optional third argument to the Lookup function
to specify the name of the home interface. EJB components have a
property in EAServer called com.sybase.jaguar.component.home.ids.
You do not need to specify the third argument to the Lookup function
if the home.ids property looks like this:
1 |
IDL:<span>PackageName</span>/<span>ComponentName</span>Home:1.0 |
For example:
1 |
IDL:vacation/TripFinderHome:1.0 |
In most cases, however, the home.ids property uses the java
package naming scheme and you should use the third argument to make
sure that the EJB home interface can be located. The string that
you specify for this argument must match the component’s
com.sybase.jaguar.component.home.ids property without the leading IDL: and
trailing :1.0.
For example, suppose the home.ids property is this:
1 |
IDL:com/myproj/myejbs/TripFindHome:1.0 |
Your Lookup function call should look like
this::
1 |
myconn.lookup(myTripFindHome,"MyEJBs/TripFindHome", &<br>   "com/myproj/myejbs/TripFinderHome") |
Alternatively, you can use the fully-qualified Java class
name of the home interface specified in dot notation. For example:
1 |
ts.lookup(MyCartHome, "MyEJBs/TripFindHome", &    "com.myproj.myejbs.TripFinderHome") |
Lookup in EAServer is
case sensitive. Make sure that the case in the string you specify
for the arguments to the Lookup function matches
the case in the home.ids property.
Creating or finding an instance of an EJB
EAServer supports three
types of EJBs—session beans, entity beans, and message-driven
beans.
A session bean is created in response
to a client request. A client usually has exclusive use of the session
bean for the duration of that client session.
An entity bean represents persistent information stored in
a database. A client uses an entity bean concurrently with other
clients. Since an entity bean persists beyond the lifetime of the
client, you must use a primary key class name to identify or find a
preexisting component, if the bean has already been created.
A message–driven bean is similar to a stateless session
bean, but it responds only to JMS messages and has no direct client
interface.
The following examples assume that an EJB component that provides e–commerce
shopping cart functionality is running on EAServer.
This component is called Cart and is included in a package called
Shopping.
Example 1
This script instantiates the Cart component and invokes several component
methods. In this example, the second argument to the Lookup method
specifies the component name as well as the EAServer package
name:
1 |
//Instance variable:<br>//Connection myconnect<br> <br>CartHome MyCartHome // EJB's home interface<br>Cart MyShoppingCart // EJB's remote interface<br>long ll_result<br> <br>//Get the home interface<br>ll_result = &<br>myconnect.Lookup(MyCartHome, "Shopping/Cart", &<br>   "com.sybase.shopping.Cart")<br> <br>//Get a reference to Cart component's business logic<br>TRY<br>   MyShoppingCart = MyCartHome.Create()<br>CATCH (ctscomponents_createexception ce)<br>   MessageBox("Create exception", ce.getmessage())<br>   // handle exception<br>END TRY<br> <br>//Use the shopping cart<br>MyShoppingCart.AddItem(66)<br>MyShoppingCart.Purchase() |
Example 2
If the Cart EJB component is defined as an entity bean, then
the script must use the findByPrimaryKey method
to find and reference a preexisting or persistent component if one
exists:
1 |
//Instance variable:<br>//Connection myconnect<br> <br>CartHome MyCartHome // EJB's home interface<br>Cart MyCart // EJB's remote interface<br>long ll_result<br> <br>//Get the home interface<br>ll_result = &<br>myconnect.Lookup(MyCartHome, "Shopping/Cart", &<br>   "com.sybase.shopping.Cart")<br> <br>//Get a reference to Cart from a previous session<br>TRY<br>   MyCart = MyCartHome.findByPrimaryKey("MYkey")<br>CATCH ( ctscomponents_finderexception fe )<br>   MessageBox("Finder exception", &<br>      fe.getmessage())<br>   // handle exception<br>END TRY<br>//Use the shopping cart<br>MyCart.AddItem(66)<br>MyCart.Purchase() |
Restrictions
PowerBuilder clients to EJB objects act as CORBA clients,
which means that they do not have the full capabilities of Java
clients. Java clients can use methods inherited from the javax.ejb.EJBObject
interface.
For example, a Java client can obtain a handle for a remote
interface instance. The handle is a binary encoding of the session
state between the client and the bean. The client can obtain a handle,
save it to disk or mail it to another location, then reestablish
the session at a later time. PowerBuilder clients can obtain similar
functionality using the Object_To_String and String_To_Object functions
of the JaguarORB object.
Handling exceptions
The remote interface of an EJB component can indicate errors
or warnings. Standard exceptions thrown by the EJB component are
mapped to CORBA system exceptions. The EJB component can also throw
user exceptions. For information about handling exceptions thrown
by EAServer components, see “Handling errors “.
For information about calling an EJB component from a PowerBuilder component
in EAServer, see “Accessing an EJB component”.