Step 4: creating a test client

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.
       The client does not interact directly with CD instances, it uses the CDCollection bean as a mediator. CDCollection is a stateless session bean. In this example, the client calls the `findAll' method to get references to all the CD objects currently in the system. To run this client, you will first need to get some CD objects created. You can use the `Upload' client for this, to create CD instances from a text file.
       To avoid the necessity to specify the URL of the Bean server in the client source code, this client reads the required information from a properties file called `cd.properties'. The file should contain the URL and driver for the naming service, like this:


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.
       So we've created an entity EJB, and a client program that uses it. You'll agree, I hope, that it isn't that much more complicated than creating a session EJB. The additional steps required are: If you've followed the tutorial, you might like to read the discussion which gives some suggestions about optimization and suitability of container-managed Beans.

©1994-2003 Kevin Boone, all rights reserved