Creating a JBoss Test Client for an EJB Session Bean

Apart from the JSP client, a standalone Java application can access your Business Components application module which is deployed on the JBoss application server as an EJB Session Bean.

We will create a new class to talk to the Business Components application module deployed as a session bean. This will be a standalone Java application which will lookup the session bean and then use an ApplicationModuleProxy class to obtain a reference to the application module.

To create a JBoss test client for an EJB session bean:

  1. In the Navigator, select the <projectname >.jpr in which you want to create the class file.
  2. Choose File | New to open the New Gallery.
  3. In the Categories tree, expand General and select Simple Files. In the Items list, double-click Java Class.

    If the category or item isn't found, make sure the correct project is selected, and choose All Technologies in the Filter By dropdown list.

  4. In the New Class dialog, click OK to accept the default settings.
  5. Add the following import statements to the newly created Java source file:

        import oracle.jbo.*;
        import javax.naming.*;
        import java.util.*;
        import mypackage2.common.ejb.beanmanaged.Mypackage2ModuleHome;
        import oracle.jbo.client.remote.ejb.ApplicationModuleProxy;
    	            
  6. Add the following main method to the class:

        public static void main(String[] args)
        {
          try
          {
            Hashtable env = new Hashtable();      
            env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");      
            env.put(Context.PROVIDER_URL, "localhost");      
            env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces" );      
            Context ctx = new InitialContext(env);      
    
            Mypackage2ModuleHome beanHome  = (Mypackage2ModuleHome )ctx.lookup("mypackage2.Mypackage2Module");
    
            // Create the proxy. If you have exported methods then you can cast it to your common am interface.
            ApplicationModule am = ApplicationModuleProxy.create(beanHome, env);
    
            //
            // Note: Change this connect string to the connect string you use to connect to your database
            //
            am.getTransaction().connectToDataSource( null, "java:/OracleDS", null, null, false );
            System.out.println( am.getClass() );
            String[] voNames = am.getViewObjectNames();
            ViewObject vo = am.findViewObject("DeptView");
    
            Row row;
            while ( ( row = vo.next() ) != null )
            {
              int numAttrs = row.getAttributeCount();
              for ( int i=0; i  numAttrs; i++ )
              {
                System.out.println( "Name= " + vo.getAttributeDef(i).getName() +
                                    " Value= " + row.getAttribute(i) );
              }
              System.out.println("");
            }
            am.getTransaction().disconnect();
          }
          catch ( Exception ex )
          {
            ex.printStackTrace();
          }
        }
    	        

To run the test client:

  1. Edit your project settings, and remove the J2EE and Oracle Application Server libraries, and add the JBoss library to your project.
  2. Run the test client.
  3. Verify that the contents of the DEPT database table are displayed in the Log window of your application.

Related topics

Working with the JBoss Application Server
Configuring JBoss for ADF Business Components Deployment
Creating an Oracle Data Source in JBoss
Deploying to the JBoss Application Server

 

Copyright © 1997, 2004, Oracle. All rights reserved.