November 26, 2014
Hot Topics:

Creating and Deploying Stateless Session Beans and CMP Entity Beans

  • September 9, 2004
  • By Olexiy & Alexander Prokhorenko
  • Send Email »
  • More Articles »

Now it's time to create all the directories where we will place sources and configuration files. So, they are src/, src/WEB-INF/, src/META-INF/, and src/teste/.

The next step will be creating clients for our session stateless bean and for CMP entity bean. Yes, we do not have these EJBs ready yet, but they will be ready in few minutes, so why should we depend on the order of creating code? So, here they are our clients for the session stateless bean—src/index1.jsp:

<%@page import="java.rmi.*" %>
<%@page import="java.util.*" %>
<%@page import="javax.naming.*" %>
<%@page import="javax.rmi.*" %>
<%@page import="teste.*" %>
<html>
<head><title>Teste</title></head>
<body>
<%
   try {
      Context ic = new InitialContext();
      Object o = ic.lookup("ejb/Example/Teste");
      TesteHome home = (TesteHome)
                PortableRemoteObject.narrow(o, TesteHome.class);
      Teste eo = home.create();
      out.println(eo.say());
   } catch (Exception ex) {
      out.println(ex);
   }
%>
</body>
</html>

and for the CMP entity bean—src/index2.jsp:

<%@page import="java.rmi.*" %>
<%@page import="java.util.*" %>
<%@page import="javax.naming.*" %>
<%@page import="javax.rmi.*" %>
<%@page import="teste.*" %>
<html>
<head><title>Story</title></head>
<body>
<%
   try {
      Context ic = new InitialContext();
      Object o = ic.lookup("ejb/Example/Story");
      StoryHome home = (StoryHome)
                PortableRemoteObject.narrow(o, StoryHome.class);
      Story eo = home.create();
      Collection stories = home.findAll();
      for (Iterator i = stories.iterator(); i.hasNext(); ) {
         Story story = (Story) i.next();
         out.println("<b>"+story.getStoryId()+"</b>"+"
                     "+story.getPubDate()+"<br>");
      }
   } catch (Exception ex) {
      out.println(ex);
   }
%>
</body>
</html>

Let me explain how it works. A session stateless bean itself will be pretty easy. It only has a business method—say(), so our client for this src/index1.jsp bean will run this business method and print the output. That's all. Our entity bean will be a bit more complex. In the src/index2.jsp client, we will create a new entity, and, right after that, we'll grab all entities and will show a list of their fields (they have only two fields—StoryId and PubDate, but you will see that below). So, this will be the only action that our entity bean client does. However, I need to mention that once a CMP entity bean is created, a record in the MySQL database table also will be auto-created. That's the idea of a CMP entity bean. This will be done without any single code line dealing with JDBC operations; not in your client, and neither in your entity bean. You will see how cool it is below.

It's time for beans. First comes the session stateless bean—src/teste/TesteBean.java:

package teste;
import javax.ejb.*;
public class TesteBean implements SessionBean {
   public void setSessionContext(SessionContext ctx) {
      System.out.println("In setSessionContext(...)");
   }
   public void ejbCreate() {
      System.out.println("In ejbCreate()");
   }
   public void ejbActivate() {
      System.out.println("In ejbActivate()");
   }
   public void ejbPassivate() {
      System.out.println("In ejbPassivate()");
   }
   public void ejbRemove() {
      System.out.println("In ejbRemove()");
   }
   public String say() {
      System.out.println("In business method say()");
      return "Hello World! I am just tiny Stateless Session Bean!";
   }
}

Now comes the session bean's remote component interface—src/teste/Teste.java:

package teste;
import javax.ejb.*;
import java.rmi.RemoteException;
public interface Teste extends EJBObject {
   public String say() throws RemoteException;
}

And the session bean's home interface—src/teste/TesteHome.java:

package teste;
import javax.ejb.*;
import java.rmi.RemoteException;
public interface TesteHome extends EJBHome {
   public Teste create() throws CreateException, RemoteException;
}

As mentioned above, there is only one business method, say(), and it does nothing except returning the String value. Okay, once you havecopy-pasted and saved everything, we smoothly come to the entity bean.

The same is true for the session bean. Here comes the entity bean source code—src/teste/StoryBean.java:

package teste;
import java.rmi.*;
import java.util.*;
import javax.ejb.*;
public abstract class StoryBean implements EntityBean {
   private EntityContext ctx;
   private Integer generatePrimaryKey() {
      System.out.println("In generatePrimaryKey()");
      return new Integer((new Object()).hashCode());
   }
   public abstract Integer getStoryId();
      public abstract void setStoryId(Integer storyId);
   public abstract Date getPubDate();
   public abstract void setPubDate(Date pubDate);
   private void create() {
      System.out.println("In create()");
      setStoryId(generatePrimaryKey());
      setPubDate(new Date());
   }
   public Integer ejbCreate() throws RemoteException, CreateException {
      System.out.println("In ejbCreate()");
      create();
      return null;
   }
   public void ejbPostCreate() {
      System.out.println("In ejbPostCreate()");
   }
   public void ejbRemove() {
      System.out.println("In ejbRemove()");
   }
   public void ejbStore() {
      System.out.println("In ejbStore()");
   }
   public void ejbLoad() {
      System.out.println("In ejbLoad()");
   }
   public void ejbActivate() {
      System.out.println("In ejbActivate()");
   }
   public void ejbPassivate() {
      System.out.println("In ejbPassivate()");
   }
   public void setEntityContext(EntityContext ctx) {
      System.out.println("In setEntityContext(...)");
      this.ctx = ctx;
   }
   public void unsetEntityContext() {
      System.out.println("In unsetEntityContext(...)");
      this.ctx = null;
   }
}
(Please note that the bean is abstract because it has abstract methods for CMP. They will be defined in configuration files; you will see it.) Now, let's look at the remote component interface for the entity bean—src/teste/Story.java:

package teste;
import java.rmi.*;
import java.util.*;
import javax.ejb.*;
public interface Story extends EJBObject {
   public Integer getStoryId() throws RemoteException;
  public Date getPubDate() throws RemoteException;
}

There are only two interesting methods for us—getters for StoryId and PubDate fields. And now, let's take a look at the home interface—src/teste/StoryHome.java:

package teste;
import java.rmi.*;
import java.util.*;
import javax.ejb.*;
public interface StoryHome extends EJBHome {
   public Story create() throws RemoteException, CreateException;
   public Story findByPrimaryKey(Integer key) throws RemoteException,
      FinderException;
   public Collection findAll() throws RemoteException, FinderException;
}




Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel