Incubator > Beehive
 

EJB Control Overview

Overview

The EJB control makes it easy for you to use an existing, deployed EJB from within an application by relieving you of all the preparatory work prior to invoking a method on an EJB's interface to perform tasks. The EJB control automatically manages locating and referencing the EJB instance, and directs method invocations to the correct instance of the target EJB.

Here is an example of the code required to invoke a single method on an exposed EJB using standard J2EE APIs:

Trader trader = null;
try
{
     InitialContext ic = new InitialContext();
     TraderHome home = (TraderHome)ic.lookup("MyTraderBean");
     Trader trader = home.create();
     TradeResult tradeResult = trader.buy(stock, shares);
     return  tradeResult;
}
catch (NamingException ne) 
{
     ...
}
catch (CreateException ce)
{
     ...
}
catch (RemoteException re)
{
     ...
}
finally
{
    if (trader != null)
        trader.remove();
}
            

The code can be reduced to the following using the EJB Control:

@Control
TraderControlBean traderControl;

try
{
     TradeResult tradeResult = traderControl.buy(stock, shares);
     return  tradeResult;
}
catch (RemoteException re)
{
     ...
}
finally
{
    if (traderControl != null)
        traderControl.remove();
}
            

You can access EJBs on a different server with an EJB control, provided the server hosting the EJB control and the server to which the target EJB is deployed are in the same domain.

Annotations

The EJB control contains the following TYPE/FIELD level annotation:

@EJBHome

Specifies the target EJB's home interface for the EJB control.

AttributeValueDescription
jndiNameStringSpecifies the JNDI name of the target EJB's home interface (e.g. EJBNameHome) The JNDI name specified may be an URL using the JNDI protocol (e.g. jndi://username:password@host:7001/my.resource.jndi.object).
ejbLinkStringSpecifies the name of the target EJB using the application relative path to the EJB JAR. This syntax causes the runtime to use an application scoped name rather than a global JNDI name when locating the referenced EJB. The naming syntax is EJBJAR#BeanName (e.g. CustomerData.jar#CreditCard).

Note: A value must be specified for exactly one of the attributes.

@JNDIContextEnv

Specifies the environment properties for the JNDI context that will be used to lookup the target EJB. This attribute is optional. If you are using an URL with the JNDI protocol or if you want to use a JNDI context with the default environment properties, you do not need to specify any values for this attribute.

AttributeValueDescription
contextFactoryStringSpecifies the name of the initial context factory class (e.g. weblogic.jndi.WLInitialContextFactory)
providerURLStringSpecifies the URL of the service provider (e.g. t3://localhost:7001)
principalStringSpecifies the identity of the principal for authenticating the caller to the service (e.g. username)
credentialsStringSpecifies the credentials of the principal for authenticating the caller to the service (e.g. password)