|
First.java :
Now create a new First.java source file in com/stardeveloper/ejb/session folder. Copy and paste following
code in it:
/* First.java */
package com.stardeveloper.ejb.session;
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface First extends EJBObject {
public String getTime() throws RemoteException;
}
Hit the 'save' button to save First.java source file.
Explanation :
First line is the package statement which tells that First interface belongs in the com.stardeveloper.ejb.session
package :
package com.stardeveloper.ejb.session;
Next two lines are import statements for importing required classes.
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
Then comes interface declaration line which tells that this is an interface with name "First" which
extends an existing interface javax.ejb.EJBObject.
Note: Every Remote interface *must* always extend EJBObject interface. It is a requirement,
not an option.
public interface First extends EJBObject {
Now as I said on the last page, we have to declare methods in Remote interface which we want to be called by
the client ( which the client can access and call ). For simplicity, we will only declare a single method, getTime();
which will return a String object containing current time.
public String getTime() throws RemoteException;
Notice that there are no {} parenthesis with this method as it has been declared in an interace.
Remote interface method's must also throw RemoteException because EJBs are distributed components and
during the call to an EJB, due to some network problem, exceptional events can arise, so all Remote interface
method's must declare that they can throw RemoteException in Remote interfaces.
FirstHome.java :
Let's now create the Home interface for our FirstEJB. Home interface is used to create and get access to Remote
interfaces. Create a new FirstHome.java source file in com/stardeveloper/ejb/session package. Copy and paste the
following code in it :
/* FirstHome.java */
package com.stardeveloper.ejb.session;
import javax.ejb.EJBHome;
import javax.ejb.CreateException;
import java.rmi.RemoteException;
public interface FirstHome extends EJBHome {
public First create() throws CreateException, RemoteException;
}
Hit the 'save' button to save FirstHome.java source file.
Explanation :
First few lines are package and import statements. Next we declare our FirstHome interface which extends
javax.ejb.EJBHome interface.
Note: All Home interfaces *must* extend EJBHome interface.
public interface FirstHome extends EJBHome {
We continue exploring FirstHome.java on the next page.
FirstHome.java :
Then we declare a single create() method which returns an instance of First Remote interface. Notice
that all methods in Home interfaces as well must also declare that they can throw RemoteException. One other
exception that they *must* declare that they can throw is CreateException.
public First create() throws CreateException, RemoteException;
We are done with creating Remote and Home interfaces for our FirstEJB. These two are the only things which our
client will see, the client will remain absolutely blind as far as the actual implementation class, FirstEJB
is concerned.
FirstEJB.java :
FirstEJB is going to be our main EJB class. Create a new FirstEJB.java source file in com/stardeveloper/ejb/session
folder. Copy and paste following text in it :
/* FirstEJB.java */
package com.stardeveloper.ejb.session;
import javax.ejb.SessionBean;
import javax.ejb.EJBException;
import javax.ejb.SessionContext;
import java.rmi.RemoteException;
import java.util.Date;
public class FirstEJB implements SessionBean {
public String getTime() {
return "Time is : " + new Date().toString();
}
public void ejbCreate() {}
public void ejbPassivate() {}
public void ejbActivate() {}
public void ejbRemove() {}
public void setSessionContext(SessionContext context) {}
}
Hit the 'save' button to save FirstEJB.java source file.
Explanation :
First few lines are package and import statements. Next we declare our FirstEJB class and make it implement
javax.ejb.SessionBean interface.
Note: All Session bean implementation classes *must* implement SessionBean
interface.
public class FirstEJB implements SessionBean
Our first method is getTime() which had declared in our First Remote interface. We implement that method here in
our FirstEJB class. It simply returns get date and time as you can see below :
public String getTime() {
return "Time is : " + new Date().toString();
}
Then come 5 callback methods which are part of SessionBean interface and since we are implementing SessionBean
interface, we have to provide empty implementations of these methods. Later in other articles when will build some
really useful Session beans, we will then see some use of these callback methods there, for now we don't need them.
ejb-jar.xml :
Let's now create the EJB deployment descriptor file for our FirstEJB Session bean. Create a new ejb-jar.xml
file in the FirstEJB/META-INF folder. Copy and paste following text in it :
<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems,
Inc.//DTD Enterprise JavaBeans 2.0//EN"
"http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd">
<ejb-jar>
<description></description>
<enterprise-beans>
<session>
<display-name>FirstEJB</display-name>
<ejb-name>First</ejb-name>
<home>com.stardeveloper.ejb.session.FirstHome</home>
<remote>com.stardeveloper.ejb.session.First</remote>
<ejb-class>com.stardeveloper.ejb.session.FirstEJB</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>First</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Supports</trans-attribute>
</container-transaction>
<security-role>
<description>Users</description>
<role-name>users</role-name>
</security-role>
</assembly-descriptor>
</ejb-jar>First.java :
Now create a new First.java source file in com/stardeveloper/ejb/session folder. Copy and paste following
code in it:
/* First.java */
package com.stardeveloper.ejb.session;
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface First extends EJBObject {
public String getTime() throws RemoteException;
}
Hit the 'save' button to save First.java source file.
Explanation :
First line is the package statement which tells that First interface belongs in the com.stardeveloper.ejb.session
package :
package com.stardeveloper.ejb.session;
Next two lines are import statements for importing required classes.
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
Then comes interface declaration line which tells that this is an interface with name "First" which
extends an existing interface javax.ejb.EJBObject.
Note: Every Remote interface *must* always extend EJBObject interface. It is a requirement,
not an option.
public interface First extends EJBObject {
Now as I said on the last page, we have to declare methods in Remote interface which we want to be called by
the client ( which the client can access and call ). For simplicity, we will only declare a single method, getTime();
which will return a String object containing current time.
public String getTime() throws RemoteException;
Notice that there are no {} parenthesis with this method as it has been declared in an interace.
Remote interface method's must also throw RemoteException because EJBs are distributed components and
during the call to an EJB, due to some network problem, exceptional events can arise, so all Remote interface
method's must declare that they can throw RemoteException in Remote interfaces.
FirstHome.java :
Let's now create the Home interface for our FirstEJB. Home interface is used to create and get access to Remote
interfaces. Create a new FirstHome.java source file in com/stardeveloper/ejb/session package. Copy and paste the
following code in it :
/* FirstHome.java */
package com.stardeveloper.ejb.session;
import javax.ejb.EJBHome;
import javax.ejb.CreateException;
import java.rmi.RemoteException;
public interface FirstHome extends EJBHome {
public First create() throws CreateException, RemoteException;
}
Hit the 'save' button to save FirstHome.java source file.
Explanation :
First few lines are package and import statements. Next we declare our FirstHome interface which extends
javax.ejb.EJBHome interface.
Note: All Home interfaces *must* extend EJBHome interface.
public interface FirstHome extends EJBHome {
We continue exploring FirstHome.java on the next page.
FirstHome.java :
Then we declare a single create() method which returns an instance of First Remote interface. Notice
that all methods in Home interfaces as well must also declare that they can throw RemoteException. One other
exception that they *must* declare that they can throw is CreateException.
public First create() throws CreateException, RemoteException;
We are done with creating Remote and Home interfaces for our FirstEJB. These two are the only things which our
client will see, the client will remain absolutely blind as far as the actual implementation class, FirstEJB
is concerned.
FirstEJB.java :
FirstEJB is going to be our main EJB class. Create a new FirstEJB.java source file in com/stardeveloper/ejb/session
folder. Copy and paste following text in it :
/* FirstEJB.java */
package com.stardeveloper.ejb.session;
import javax.ejb.SessionBean;
import javax.ejb.EJBException;
import javax.ejb.SessionContext;
import java.rmi.RemoteException;
import java.util.Date;
public class FirstEJB implements SessionBean {
public String getTime() {
return "Time is : " + new Date().toString();
}
public void ejbCreate() {}
public void ejbPassivate() {}
public void ejbActivate() {}
public void ejbRemove() {}
public void setSessionContext(SessionContext context) {}
}
Hit the 'save' button to save FirstEJB.java source file.
Explanation :
First few lines are package and import statements. Next we declare our FirstEJB class and make it implement
javax.ejb.SessionBean interface.
Note: All Session bean implementation classes *must* implement SessionBean
interface.
public class FirstEJB implements SessionBean
Our first method is getTime() which had declared in our First Remote interface. We implement that method here in
our FirstEJB class. It simply returns get date and time as you can see below :
public String getTime() {
return "Time is : " + new Date().toString();
}
Then come 5 callback methods which are part of SessionBean interface and since we are implementing SessionBean
interface, we have to provide empty implementations of these methods. Later in other articles when will build some
really useful Session beans, we will then see some use of these callback methods there, for now we don't need them.
ejb-jar.xml :
Let's now create the EJB deployment descriptor file for our FirstEJB Session bean. Create a new ejb-jar.xml
file in the FirstEJB/META-INF folder. Copy and paste following text in it :
<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems,
Inc.//DTD Enterprise JavaBeans 2.0//EN"
"http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd">
<ejb-jar>
<description></description>
<enterprise-beans>
<session>
<display-name>FirstEJB</display-name>
<ejb-name>First</ejb-name>
<home>com.stardeveloper.ejb.session.FirstHome</home>
<remote>com.stardeveloper.ejb.session.First</remote>
<ejb-class>com.stardeveloper.ejb.session.FirstEJB</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>First</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Supports</trans-attribute>
</container-transaction>
<security-role>
<description>Users</description>
<role-name>users</role-name>
</security-role>
</assembly-descriptor>
</ejb-jar>
|