D
Dr. Leff
I cannot find my Course J2EE bean from a client. (The client
runs on the same computer but a different VM from the bean.)
In Glassfish (Sun Application Server Nine), I set up a simple
Container-Managed Entity Bean. I tried to access it from an external
client as described in the glassfish EJB
FAQ https://glassfish.dev.java.net/javaee5/ejb/EJB-FAQ.html
Here is the client program:
package RS;
--- imports deleted for conciseness
import Debug.*;
public class Client {
public static void main (String arg[]) throws IOException {
try {
InitialContext jndiContext = new InitialContext ();
Object o = jndiContext.lookup("Course");
CourseHome home =
(CourseHome)PortableRemoteObject.narrow(o,CourseHome.class);
Course C = home.create(1);
C.setName("blah");
CoursePK PK = new CoursePK();
PK.number=1;
C = home.findByPrimaryKey(PK);
Debug.P(C.getName());
}
catch (java.rmi.RemoteException re) {Debug.P("Remote Exception" +
re.getMessage());}
catch (javax.naming.NamingException ne){Debug.P("Naming
Exception"+ne.getMessage());}
catch (javax.ejb.CreateException ce){Debug.P("Create
Exception"+ce.getMessage());}
catch (javax.ejb.FinderException fe){Debug.P("Finder
Exception"+fe.getMessage());}
}
}
When I run it, I get the following Naming Exception: Course not found
I confirmed at the administrative console, that I have "Course" listed
under Enterprise Applications:
As per the ejb FAQ, I set up my $CLASSPATH so it starts as follows:
/opt/j2ee/SUNWappserver/lib/appserv-rt.jar:
/opt/j2ee/SUNWappserver/lib/javaee.jar:
I also verifed in my domain.xml that the orb-listener-1, the default
naming service port was at 3700.
Here is the info on the Bean that I have running.
ejb-jar.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd"
version="2.1">
<enterprise-beans>
<entity>
<description>Course</description>
<display-name>CourseBean</display-name>
<ejb-name>CourseBean</ejb-name>
<home>RS.CourseHome</home>
<remote>RS.Course</remote>
<ejb-class>RS.CourseBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>RS.CoursePK</prim-key-class>
<reentrant>false</reentrant>
<abstract-schema-name>Course</abstract-schema-name>
<cmp-field><field-name>number</field-name></cmp-field>
<cmp-field><field-name>name</field-name></cmp-field>
</entity>
</enterprise-beans>
<assembly-descriptor>
<security-role>
<description>Everyone</description>
<role-name>everyone</role-name>
</security-role>
<method-permission>
<unchecked/>
<method>
<ejb-name>CourseBean</ejb-name>
<method-name>*</method-name>
</method>
</method-permission>
<container-transaction>
<method>
<ejb-name>CourseBean</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
Sun-ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD
Application Server 8.1 EJB 2.1//EN"
"http://www.sun.com/software/appserver/dtds/sun-ejb-jar_2_1-1.dtd">
<sun-ejb-jar>
<enterprise-beans>
<name>CourseBean</name>
<ejb>
<ejb-name>CourseBean</ejb-name>
<jndi-name>Course</jndi-name>
</ejb>
<cmp-resource>
<jndi-name>jdbc/__default</jndi-name>
<create-tables-at-deploy>true</create-tables-at-deploy>
<drop-tables-at-undeploy>true</drop-tables-at-undeploy>
</cmp-resource>
</enterprise-beans>
</sun-ejb-jar>
Course.java
package RS;
import java.rmi.RemoteException;
public interface Course extends javax.ejb.EJBObject {
public String getName() throws RemoteException;
public void setName (String Str) throws RemoteException;
public int getNumber() throws RemoteException;
public void setNumber (int n) throws RemoteException;
}
CourseHome.java
package RS;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
public interface CourseHome extends javax.ejb.EJBHome {
public Course create(int id) throws
CreateException,RemoteException;
public Course findByPrimaryKey(CoursePK pk)
throws FinderException,RemoteException;
}
CourseBean.java
package RS;
import javax.ejb.EntityContext;
public abstract class CourseBean implements javax.ejb.EntityBean {
public CoursePK ejbCreate (int number) throws
javax.ejb.CreateException{
this.setNumber(number);
return null;
}
public void ejbPostCreate (int number) {
}
public abstract String getName();
public abstract void setName (String str);
public abstract int getNumber ();
public abstract void setNumber (int number);
public void setEntityContext (EntityContext ctx) {}
public void unsetEntityContext() {}
public void ejbActivate () {}
public void ejbPassivate () {}
public void ejbLoad () {}
public void ejbStore () {}
public void ejbRemove() {}
}
Dr. Laurence L. Leff, Associate Professor of Computer Science, Western
Illinois
University, 1 University Circle, Macomb IL 61455 Pager: 309 367 0787,
Fax 309 298 2302
runs on the same computer but a different VM from the bean.)
In Glassfish (Sun Application Server Nine), I set up a simple
Container-Managed Entity Bean. I tried to access it from an external
client as described in the glassfish EJB
FAQ https://glassfish.dev.java.net/javaee5/ejb/EJB-FAQ.html
Here is the client program:
package RS;
--- imports deleted for conciseness
import Debug.*;
public class Client {
public static void main (String arg[]) throws IOException {
try {
InitialContext jndiContext = new InitialContext ();
Object o = jndiContext.lookup("Course");
CourseHome home =
(CourseHome)PortableRemoteObject.narrow(o,CourseHome.class);
Course C = home.create(1);
C.setName("blah");
CoursePK PK = new CoursePK();
PK.number=1;
C = home.findByPrimaryKey(PK);
Debug.P(C.getName());
}
catch (java.rmi.RemoteException re) {Debug.P("Remote Exception" +
re.getMessage());}
catch (javax.naming.NamingException ne){Debug.P("Naming
Exception"+ne.getMessage());}
catch (javax.ejb.CreateException ce){Debug.P("Create
Exception"+ce.getMessage());}
catch (javax.ejb.FinderException fe){Debug.P("Finder
Exception"+fe.getMessage());}
}
}
When I run it, I get the following Naming Exception: Course not found
I confirmed at the administrative console, that I have "Course" listed
under Enterprise Applications:
As per the ejb FAQ, I set up my $CLASSPATH so it starts as follows:
/opt/j2ee/SUNWappserver/lib/appserv-rt.jar:
/opt/j2ee/SUNWappserver/lib/javaee.jar:
I also verifed in my domain.xml that the orb-listener-1, the default
naming service port was at 3700.
Here is the info on the Bean that I have running.
ejb-jar.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd"
version="2.1">
<enterprise-beans>
<entity>
<description>Course</description>
<display-name>CourseBean</display-name>
<ejb-name>CourseBean</ejb-name>
<home>RS.CourseHome</home>
<remote>RS.Course</remote>
<ejb-class>RS.CourseBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>RS.CoursePK</prim-key-class>
<reentrant>false</reentrant>
<abstract-schema-name>Course</abstract-schema-name>
<cmp-field><field-name>number</field-name></cmp-field>
<cmp-field><field-name>name</field-name></cmp-field>
</entity>
</enterprise-beans>
<assembly-descriptor>
<security-role>
<description>Everyone</description>
<role-name>everyone</role-name>
</security-role>
<method-permission>
<unchecked/>
<method>
<ejb-name>CourseBean</ejb-name>
<method-name>*</method-name>
</method>
</method-permission>
<container-transaction>
<method>
<ejb-name>CourseBean</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
Sun-ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD
Application Server 8.1 EJB 2.1//EN"
"http://www.sun.com/software/appserver/dtds/sun-ejb-jar_2_1-1.dtd">
<sun-ejb-jar>
<enterprise-beans>
<name>CourseBean</name>
<ejb>
<ejb-name>CourseBean</ejb-name>
<jndi-name>Course</jndi-name>
</ejb>
<cmp-resource>
<jndi-name>jdbc/__default</jndi-name>
<create-tables-at-deploy>true</create-tables-at-deploy>
<drop-tables-at-undeploy>true</drop-tables-at-undeploy>
</cmp-resource>
</enterprise-beans>
</sun-ejb-jar>
Course.java
package RS;
import java.rmi.RemoteException;
public interface Course extends javax.ejb.EJBObject {
public String getName() throws RemoteException;
public void setName (String Str) throws RemoteException;
public int getNumber() throws RemoteException;
public void setNumber (int n) throws RemoteException;
}
CourseHome.java
package RS;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
public interface CourseHome extends javax.ejb.EJBHome {
public Course create(int id) throws
CreateException,RemoteException;
public Course findByPrimaryKey(CoursePK pk)
throws FinderException,RemoteException;
}
CourseBean.java
package RS;
import javax.ejb.EntityContext;
public abstract class CourseBean implements javax.ejb.EntityBean {
public CoursePK ejbCreate (int number) throws
javax.ejb.CreateException{
this.setNumber(number);
return null;
}
public void ejbPostCreate (int number) {
}
public abstract String getName();
public abstract void setName (String str);
public abstract int getNumber ();
public abstract void setNumber (int number);
public void setEntityContext (EntityContext ctx) {}
public void unsetEntityContext() {}
public void ejbActivate () {}
public void ejbPassivate () {}
public void ejbLoad () {}
public void ejbStore () {}
public void ejbRemove() {}
}
Dr. Laurence L. Leff, Associate Professor of Computer Science, Western
Illinois
University, 1 University Circle, Macomb IL 61455 Pager: 309 367 0787,
Fax 309 298 2302