Documente Academic
Documente Profesional
Documente Cultură
“In the Java distributed object model, a remote object is the one whose
method can be invoked from another java virtual machine, potentially a
different host.
To make objects in separate JVMs look and act like local objects
On the server side, the skeleton object takes care of all of the
details of “Remoteness” so that the remote object doesn't need ot
worry about them. In other words, You can pretty much code a
remote object the same way as if it were local.
Primitives
are passed by Value
Objects
Think.!!!!
Little More.!!!
In RMI , Objects are passed by value
So RMI sends the object itself, not its reference, between JVMs.
Enterprise Java Beans (EJB)
EJBs are Server Side Components that encapsulates
a bigger chunk of functionality (Business Logic).
Contracts
Container
A component interacts only with its container and the resources the
container provides.
An Application Server
1.Transaction
2.Security
3.Concurrency
4.Networking
5.Resource Management
6.presistence
7.Messaging
8.Deploy-time customization
1. Home Interface
2.Remote Interface
3.Bean Class
4.Deployment Descriptor
Writing Classes for a Component
?????????????
The Client will never access the bean directly, the container
intercepts the call to provide its services. How will it
Intercepts???
Session Beans
Entity Beans
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface TraderHome extends EJBHome
{
Trader create() throws CreateException, RemoteException;
}
Remote Interface
import java.rmi.RemoteException;
import javax.ejb.EJBObject;
public interface Trader extends EJBObject
{
public TradeResult buy (String stockSymbol, int shares)
throws RemoteException;
public TradeResult sell (String stockSymbol, int shares)
throws RemoteException;
}
Bean Class
import javax.ejb.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class TraderBean implements SessionBean {
private SessionContext ctx;
private int tradeLimit;
public void ejbActivate() { }
public void ejbRemove() { }
public void ejbPassivate() { }
public void setSessionContext(SessionContext ctx) { this.ctx = ctx; }
public void ejbCreate () throws CreateException {}
public TradeResult buy(String stockSymbol, int shares) {
if (shares > tradeLimit) {
shares = tradeLimit; }
return new TradeResult(shares, stockSymbol);
}
public TradeResult sell(String stockSymbol, int shares) {
if (shares > tradeLimit) {
shares = tradeLimit; }
return new TradeResult(shares, stockSymbol);
}}
Client Class
// Lookup the beans home using JNDI
Object home = ctx.lookup(JNDI_NAME);
return (TraderHome) PortableRemoteObject.narrow(home, TraderHome.class);
// create a Trader
Trader trader = (Trader) narrow(home.create(), Trader.class);
String [] stocks = {"BEAS", "MSFT", "AMZN", "HWP" };
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>statelessSession</ejb-name>
<method-intf>Remote</method-intf>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor> </ejb-jar>
weblogic-ejb-jar.xml
Stateless Session Bean – weblogic-ejb-jar.xml
<weblogic-ejb-jar>
<weblogic-enterprise-bean>
<ejb-name>statelessSession</ejb-name>
<caching-descriptor>
<max-beans-in-free-pool>100</max-beans-in-free-pool>
</caching-descriptor>
<jndi-name>statelessSession.TraderHome</jndi-name>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>
<weblogic-ejb-jar>
<weblogic-enterprise-bean>
<ejb-name>statefulSession</ejb-name>
<caching-descriptor>
<max-beans-in-cache>100</max-beans-in-cache>
</caching-descriptor>
<jndi-name>statefulSession.TraderHome</jndi-name>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>
Stateful Session Beans
Stateful Session Beans
•
Associate a database table with a class each instance
of the class (each object) is stored in a row
•
Associate a database column with an attribute of the
class
What Makes Entity Beans Different from Session Beans?
• Persistence
•
Shared Access
•
Primary Key
•
Relationships
•
The bean represents a business entity, not a procedure.
•
The bean's state must be persistent.
Life Cycle of Entity Beans
Database Calls
ejbCreate INSERT
ejbFindByPrimaryKey SELECT
ejbFindByLastName SELECT
ejbLoad SELECT
ejbRemove DELETE
ejbStore UPDATE
Container-Managed Persistence
Home Interface
To create a new instance of a CMP entity bean, and therefore insert data into the
•
•A bean's home interface may declare zero or more create() methods, each of
which must have corresponding ejbCreate() and ejbPostCreate() methods in the
bean class.
•Finder Methods are used to query the EJB server for specific entity beans, based
on the name of the method and arguments passed.
•The deployer will use vendor specific tools to tell the container how a particular
find method should behave.
Home Interface
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
import javax.ejb.FinderException;
import java.rmi.RemoteException;
import java.util.Enumeration;
public interface AccountHome extends EJBHome {
The remote interface defines the business methods of the entity bean.
The business methods in the remote interface are delegated to the matching business
methods in the bean instance.
import java.rmi.RemoteException;
import javax.ejb.*;
public interface Account extends EJBObject
{
public double deposit(double amount) throws RemoteException;
public double withdraw(double amount) throws ProcessingException,
RemoteException;
public double balance() throws RemoteException;
public String accountType() throws RemoteException;
}
Bean Class
EJB vendor provides tools for mapping the fields in the Bean
Class to the database.
//Nessary imports
setEntityContext()
unsetEntityContext()
ejbLoad()
ejbStore()
ejbPassivate()
ejbActivate()
ejb-jar.xml
<ejb-jar>
<enterprise-beans>
<entity>
<ejb-name>containerManaged</ejb-name>
<home>examples.ejb.basic.containerManaged.AccountHome</home>
<remote>examples.ejb.basic.containerManaged.Account</remote>
<ejb-class>examples.ejb.basic.containerManaged.AccountBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.String</prim-key-class>
<reentrant>False</reentrant>
<cmp-field>
<field-name>accountId</field-name>
</cmp-field>
<cmp-field>
<field-name>balance</field-name>
</cmp-field>
<cmp-field>
<field-name>accountType</field-name>
</cmp-field>
<primkey-field>accountId</primkey-field>
</entity>
weblogic-ejb-jar.xml
<weblogic-ejb-jar>
<weblogic-enterprise-bean>
<ejb-name>containerManaged</ejb-name>
<caching-descriptor>
<max-beans-in-cache>1000</max-beans-in-cache>
</caching-descriptor>
<persistence-descriptor>
<is-modified-method-name>isModified</is-modified-method-name>
<persistence-type>
<type-identifier>WebLogic_CMP_RDBMS</type-identifier>
<type-version>5.1.0</type-version>
<type-storage>META-INF/weblogic-cmp-rdbms-jar.xml</type-storage>
</persistence-type>
<persistence-use>
<type-identifier>WebLogic_CMP_RDBMS</type-identifier>
<type-version>5.1.0</type-version>
</persistence-use>
</persistence-descriptor>
<jndi-name>containerManaged.AccountHome</jndi-name>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>
Weblogic-cmp-rdbms-jar.xml
<weblogic-rdbms-bean>
<pool-name>demoPool</pool-name>
<table-name>ejbAccounts</table-name>
<attribute-map>
<object-link>
<bean-field>accountId</bean-field>
<dbms-column>id</dbms-column>
</object-link>
<object-link>
<bean-field>balance</bean-field>
<dbms-column>bal</dbms-column>
</object-link></attribute-map>
<finder-list>
<finder>
<method-name>findAccount</method-name>
<method-params>
<method-param>double</method-param>
</method-params>
<finder-query><![CDATA[(= balance $0)]]></finder-query>
</finder> </finder-list>
</weblogic-rdbms-bean>
EJB 2.0
New Features of EJB 2.0
Local Interfaces