import java.math.BigDecimal; import javax.naming.Context; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; import converter.Converter; import converter.ConverterHome; import sos.spooler.Job_impl; import sos.spooler.Order; import sos.spooler.Variable_set; public class EJBJob extends Job_impl { private Converter currencyConverter; /* * spooler_init() is called when a task is created */ public boolean spooler_init() throws Exception { try{ spooler_log.debug1("Creating initial context..."); Context initial = new InitialContext(); Context myEnv = (Context) initial.lookup("java:comp/env"); Object objref = myEnv.lookup("ejb/SimpleConverter"); ConverterHome home = (ConverterHome) PortableRemoteObject.narrow(objref, ConverterHome.class); currencyConverter = home.create(); spooler_log.debug1("EJB created."); return true; } catch (Exception e){ spooler_log.error("An error occured connecting to the EJB: "+e); } return false; } /* * spooler_process() is called for every order which is processed * by the task. */ public boolean spooler_process() throws Exception { try { Order order = spooler_task.order(); Variable_set orderParams = order.params(); BigDecimal param = new BigDecimal("100.00"); if (orderParams.value("dollar").length()>0){ param = new BigDecimal(orderParams.value("dollar")); } spooler_log.info("Calling EJB"); spooler_log.debug1("Dollar: "+param); BigDecimal amount = currencyConverter.dollarToYen(param); orderParams.set_var("yen", amount.toString()); spooler_log.debug1("Yen: "+amount); amount = currencyConverter.yenToEuro(param); orderParams.set_var("Euro", amount.toString()); spooler_log.debug1("Euro: "+amount); return true; } catch (Exception ex) { spooler_log.error("Caught an unexpected exception: "+ex); } return false; } }