EJB 2.x / 3.x
Session beans resolved through Home, JNDI and the container.
The lookup of java:comp/env/ejb/RatingService is resolved through the ejb-ref in the deployment descriptor to RatingServiceBean, so the call graph crosses the remote boundary. The surcharge on more than two prior claims becomes rule POL-0213 with its rounding (half-up, two places). The container-managed transaction attribute becomes part of the operation's contract.
1public class PolicyServiceBean implements SessionBean {2 private SessionContext ctx;34 public PolicyQuote quote(QuoteRequest req) throws RatingException {5 try {6 Object ref = new InitialContext().lookup("java:comp/env/ejb/RatingService");7 RatingServiceHome home = (RatingServiceHome) PortableRemoteObject.narrow(ref, RatingServiceHome.class);8 BigDecimal base = home.create().basePremium(req.getState(), req.getClassCode(), req.getLimit());9 if (req.getPriorClaims() > 2) {10 base = base.multiply(new BigDecimal("1.25"));11 }12 return new PolicyQuote(base.setScale(2, BigDecimal.ROUND_HALF_UP));13 } catch (NamingException e) {14 throw new EJBException(e);15 } catch (CreateException e) {16 throw new EJBException(e);17 } catch (RemoteException e) {18 throw new EJBException(e);19 }20 }2122 public void setSessionContext(SessionContext c) { this.ctx = c; }23 public void ejbCreate() {}24 public void ejbActivate() {}25 public void ejbPassivate() {}26 public void ejbRemove() {}27}
1<container-transaction>2 <method>3 <ejb-name>PolicyServiceBean</ejb-name>4 <method-name>quote</method-name>5 </method>6 <trans-attribute>Required</trans-attribute>7</container-transaction>