Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Minor corrections to text, page formatted

Introduction

This example shows how to write a Java job which calls a Session EJB.

Downloads

Enterprise Java Beans

Question: How do I call an Enterprise Java Bean from a Job?

...

Instructions

...

  • Deploy the ConverterBean from Chapter 24 of the sun j2ee tutorial at http://java.sun.com/j2ee/1.4/docs/tutorial/doc/ onto your j2ee server.
  • Get the application client from Chapter 24 of the tutorial running. Don't even try to do the rest of this HowTo if you don't get the application client running. This HowTo is not about setting up EJBs on a j2ee server. It is not about connecting EJB clients to the server. It is about writing Java Jobs for the JobScheduler which act as an EJB client. It will tell you how to configure the JobScheduler in order to find all required libraries and get the jndi lookup right.
  • Unzip all files from ejb.zip into the ./config/live folder of your JobScheduler installation.
  • Create a new foler ejb in the lib directory of your JobScheduler installation.
  • Put EJBJob.jar and all the jars which are needed to run the application client (this may depend on the application server you are using) into the lib/ejb directory.
  • If you need to set jndi properties, write a jndi.properties file and put it into the lib/ejb directory.
  • Edit config/factory.ini
  • Look for the class_path entry and add ;${SCHEDULER_HOME}/lib/ejb/*.jar;${SCHEDULER_HOME}/lib/ejb. (The second entry is required to find the jndi.properties file.) On Linux and Unix systems, use ":" as separator instead of ";".
  • Restart the JobScheduler
  • Open the web interface of the JobScheduler in your browser using http://scheduler_host:scheduler_port
  • Open the JOB CHAINS tab and enable "Show orders".
  • Find the job chain samples/ejb/ejb_chain.
  • Find the order my_order, open the order menu and choose "Show log".

...

The order will run through both steps of the job chain. In the first step, the EJB will be called. The second step will log the order parameters, which now include values for Euro an Yen which have been calculated by the EJB in the first step.

How it works

...

The Job Chain ejb_chain consists of two steps. In the first step, the job ejb is called. The implementation of the job will be explained below. In the second step, the readparam job is called. The readparam job is a small javascript job which reads all order parameters and logs them on info-level.

...

If everything worked, spooler_process will return true, which means "the order may proceed to the state configured as next_state" (which is show_params in this job chain). If an error occured, spooler_process will return false, which causes the order to go to the error_state (or to take different actions depending on the on_error attribute of the job chain node. see http://www.sos-berlin.com/doc/en/scheduler.doc/xml/job.xml#attribute_stop_on_error).

Sourcecode of EJBJob.java

...

01: import java.math.BigDecimal; 02: 03: import javax.naming.Context; 04: import javax.naming.InitialContext; 05: import javax.rmi.PortableRemoteObject; 06: 07: import converter.Converter; 08: import converter.ConverterHome; 09: import sos.spooler.Job_impl; 10: import sos.spooler.Order; 11: import sos.spooler.Variable_set; 12: 13: public class EJBJob extends Job_impl { 14: 15: private Converter currencyConverter; 16: 17: 18: /*  19:  * spooler_init() is called when a task is created 20:  */ 21: public boolean spooler_init() throws Exception { 22: try{ 23: spooler_log.debug1("Creating initial context..."); 24: 25: Context initial = new InitialContext(); 26: Context myEnv = (Context) initial.lookup("java:comp/env"); 27: Object objref = myEnv.lookup("ejb/SimpleConverter"); 28: 29: ConverterHome home = 30: (ConverterHome) PortableRemoteObject.narrow(objref, 31: ConverterHome.class); 32: 33: currencyConverter = home.create(); 34: spooler_log.debug1("EJB created."); 35: return true; 36: } catch (Exception e){ 37: spooler_log.error("An error occured connecting to the EJB: "+e); 38: } 39: return false; 40: } 41: 42: /*  43:  * spooler_process() is called for every order which is processed 44:  * by the task. 45:  */ 46: public boolean spooler_process() throws Exception { 47: try { 48: Order order = spooler_task.order(); 49: Variable_set orderParams = order.params(); 50: 51: BigDecimal param = new BigDecimal("100.00"); 52: if (orderParams.value("dollar").length()>0){ 53: param = new BigDecimal(orderParams.value("dollar")); 54: } 55: spooler_log.info("Calling EJB"); 56: spooler_log.debug1("Dollar: "+param); 57: BigDecimal amount = currencyConverter.dollarToYen(param); 58: orderParams.set_var("yen", amount.toString()); 59: spooler_log.debug1("Yen: "+amount); 60: 61: amount = currencyConverter.yenToEuro(param); 62: orderParams.set_var("Euro", amount.toString()); 63: spooler_log.debug1("Euro: "+amount); 64: 65: return true; 66: } catch (Exception ex) { 67: spooler_log.error("Caught an unexpected exception: "+ex); 68: } 69: return false; 70: } 71: 72: }

...

See also