client for EJBs may be any Java program or applet; in
this simple example I will describe a very simple client program that can be
run from the command line. It simply dumps the attributes of all the CD Beans
to standard output. The source code
also provides clients for searching and
uploading to the database, all operating at the command line.
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory java.naming.provider.url=localhost:1099
Of course, if your server and client are on different computers, you will need
to change `localhost' to the real location of the server.
Here is the full listing of the `List' client.
package com.web_tomorrow.cd;
import javax.naming.*;
import java.util.Hashtable;
import javax.rmi.PortableRemoteObject;
import java.util.Properties;
import java.io.FileInputStream;
/**
This is a simple client for the `CD' EJB; it lists (to standard output) all the `CD' instances in
the system. The `main' method allows this class to be run from the command
line.
*/
class List
{
public static void main(String[] args)
{
// Get information about the Bean server from the properties file
Properties props = new Properties();
Properties sysProps = System.getProperties();
try
{
props.load (new FileInputStream ("cd.properties"));
sysProps.putAll(props);
}
catch (Exception e)
{
System.err.println ("Can't read `cd.proprties'");
System.exit (-1);
}
System.setProperties (sysProps);
// Enclosing the whole process in a single `try' block is not an ideal way
// to do exception handling, but I don't want to clutter the program up
// with catch blocks
try
{
// Get a naming context
InitialContext jndiContext = new InitialContext();
// Get a reference to a CD Bean
Object ref = jndiContext.lookup("cd/CDCollection");
// Get a reference from this to the Bean's Home interface
CDCollectionHome home = (CDCollectionHome)
PortableRemoteObject.narrow (ref, CDCollectionHome.class);
CDCollection cdCollection = home.create();
CD[] cds = cdCollection.findAll();
for (int i = 0; i < cds.length; i++)
{
System.out.println (cds[i].getId() + "\t" + cds[i].getTitle() + "\t" +
cds[i].getArtist() + "\t" + cds[i].getType());
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
To run this client you will need to specify in the CLASSPATH the location the jBoss client libraries, so the command line will be like this:
java -classpath $CLASSPATH:/usr/lib/jboss/lib/ext/ejb.jar:/usr/lib/jboss/client/jboss-client.jar \ com.web_tomorrow.cd.List;If all is well, you will get a list of CDs.
ejb-jar.xml file needs to indicate that the object is
persistent, and list the persistent fields. It also needs to specify the name
and class of the primary key field.
jboss.xml file indicate that the configuration for the
Bean is CMP EntityBean
jaws.xml to specify them.
©1994-2003 Kevin Boone, all rights reserved