1 / 24

EJB3

EJB3. javaBin 02.03.2006 Eivind Waaler, BEKK. Agenda. Some basics Minidemo Service aspects – Spring vs. EJB3. Session Beans. Stateless Session Bean Stateful Session Bean. @Stateless public class CalculatorImpl implements Calculator { public int calculate( int number) {

Download Presentation

EJB3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EJB3 javaBin 02.03.2006 Eivind Waaler, BEKK

  2. Agenda • Some basics • Minidemo • Service aspects – Spring vs. EJB3

  3. Session Beans • Stateless Session Bean • Stateful Session Bean @Stateless public class CalculatorImpl implements Calculator { public int calculate(int number) { return number; } } @Stateful public class CalculatorImpl implements Calculator { private intnumber; public int calculate(int number) { this.number += number; return this.number; } }

  4. Entity Beans – Persistence • Domain objects – POJO Entity Bean + Annotations • DAO interface • DAO implementation – Stateless Session Bean + Annotations @Stateless public class FacadeBean implements Facade { @PersistenceContext protected EntityManager em; public void savePerson(Person person) { em.persist(person); } } @Entity @Table(name="PERSON") public class Person { private Address address; @ManyToOne(optional=false) @JoinColumn(name="ADDRESS_FK_ID") public Address getAddress() { returnaddress; } }

  5. Message Driven Beans • Similar to Stateless Session Bean • Minor changes to 2.x model @MessageDriven public class CalculatorImpl implements MessageListener { public void onMessage(Messagemessage) { System.out.println("A message arrived!"); } }

  6. Demo <minidemo>

  7. Various aspects – Spring vs. EJB3

  8. Transaction Management • Spring • Declarative transaction – XML config • Programmatic transaction support • Annotation support • EJB3 • Declarative transaction – Annotations • Stateful Session Beans – transaction over multiple requests! • JTA

  9. client calculatorProxy ”calculator” txInterceptor calculatorTarget txManager calculatorDAO sessionFactory dataSource DB Example – Spring transaction • Calculator – interface • CalculatorImpl • Implementing class • ”calculatorTarget” bean • Spring • AOP • Transaction Interceptor • Proxy for façade

  10. Example – Spring transaction config <beanid="calculatorTarget"class="facade.CalculatorImpl"/> <beanid="transactionInterceptor"class="org.spring...TransactionInterceptor"> ... <propertyname="transactionAttributeSource"> <value>facade.Calculator.*=PROPAGATION_REQUIRED</value> </property> </bean> <beanid="calculator"class="org.springframework.aop...ProxyFactoryBean"> <propertyname="proxyInterfaces"> <value>facade.Calculator</value> </property> <propertyname="interceptorNames"> <value>transactionInterceptor,calculatorTarget</value> </property> </bean>

  11. Stateless transactions @Stateless public class CalculatorBean implements Calculator { @PersistenceContext(unitName="cal") protected EntityManager em; @TransactionAttribute(TransactionAttributeType.REQUIRED) public int calculate(int param1, int param2) throws Exception { ... } @TransactionAttribute(TransactionAttributeType.NEVER) public double calculate(double param) throws Exception { ... } }

  12. Stateful/multi-request transactions @Stateful public class ShoppingCartBean implements ShoppingCart, Serializable { @PersistenceContext( unitName="cal", type=PersistenceContextType.EXTENDED ) protected EntityManager em; @TransactionAttribute(TransactionAttributeType.REQUIRED) @FlushMode(FlushModeType.NEVER) public void addItem(Item item) throws Exception { ... } @Remove public void checkout() { em.flush(); } }

  13. Transaction Management – Conclusion • Spring advantages • Very dynamic – AOP • Transaction support without container • JTA not required • EJB3 advantages • No setup – default values • Annotations very well suited • Multirequest transactions – SFSB

  14. Security • Spring • Not ”included” • Acegi Security System for Spring • Non-intrusive – AOP • Container integration (optional) • Very fine-grained – secure any object! • EJB 3 • Built-in security system • Annotations @Stateless @SecurityDomain(”mydomain") public class CalculatorBean implements Calculator { @RolesAllowed({”AppAdmin”}) public int calculate(int param1, int param2) throws Exception {

  15. Distribution and remote access • Spring – supports various remote methods: • RMI integration • HTTP Invoker – Spring on client and server • Hessian integration – Caucho binary HTTP protocol • Burlap – Caucho XML HTTP protocol • JAX-RPC – web services • JMS integration • EJB – access and implementation – including EJB3! • EJB3 – remote access built in • RMI/IIOP • Very easy in EJB3

  16. Example – EJB3 remoting • Define Remote interface: • Implement service: • Client code: @Remote public interface RemoteCalculator { ... } @Stateless @Remote ({RemoteCalculator.class}) public class CalculatorBean2 implements Calculator, RemoteCalculator { ... } InitialContext ctx = new InitialContext(); RemoteCalculator calculator = (RemoteCalculator) ctx.lookup(RemoteCalculator.class.getName());

  17. Dependency Injection • Spring • 100% dynamic DI – all POJOs • Setter and constructor DI supported • EJB3 • Only Session or Message Driven Beans • JNDI resources only • Annotations – @EJB, @Resource, @PersistenceContext etc. • Setter or instance variables directly public void setCalculator(Calculator calculator) { ... } <propertyname="calculator"><refbean=”calculator"/></property> @EJB Calculator calculator; @PersistenceContext EntityManager em; @Resource SessionContext ctx;

  18. Redundancy, scalability and fail-over • EJB3 – load balancing and fail-over • Taken care of by container • Spring – needs more thinking and configuration • Containers supporting Spring? • BEA announcement – JavaONE 2005 • JBoss 5?

  19. Configuration and deployment • Configuration • EJB3 – Annotation w/defaults, optional XML configuration • Spring – XML configuration, optional Annotation support • Deployment • EJB3 – standard J2EE • Spring – war file includes everything! • Getting started – EJB3 • Compile  JAR  Deploy • Getting started – Spring • Maven template • AppFuse or similar

  20. Dependencies • Typical Spring application: • Many jar files – Spring, Hibernate, cglib, commons-x, ehcache etc. • Spring XML config file • Hibernate XML mapping files • Domain and DAO classes • EJB3 application: • Domain and DAO classes • Let the EJB3 Vendor deal with dependencies! • Persistence framework • Caching mechanisms • Transaction management

  21. Integration • Spring integrates tightly with EJB3 • EJB3 Entity Manager as data access strategy (JSR-220) • Access beans as Spring components • Expose Spring services as Stateless Session Beans • Expose Spring beans as JNDI @Resource? • Spring makes using EJBs easier! • Spring not competing technology!?

  22. Conclusion • Spring, Hibernate and other OS frameworks • De facto standards for best-practice • Important input to J2EE standard • Are standards good? • Too little, too late? • Need Vendor extensions? • ”New life” to the application server? • EJB3 is easy and great! • We still need Spring and other OS frameworks!

  23. Q & A Thank you for listening! Questions? Contact me: eivind.waaler[at]bekk.no

  24. Resources and tips • EJB3 specification • http://www.jcp.org/en/jsr/detail?id=220 • JBoss pages • EJB3 – http://www.jboss.com/products/ejb3 • TrailBlazer – http://trailblazer.demo.jboss.com/EJB3Trail/ • Oracle pages • http://www.oracle.com/technology/tech/java/ejb30.html • Spring • http://www.springframework.org • Various articles/discussions • http://www.onjava.com/pub/a/onjava/2005/06/29/spring-ejb3.html?page=1 • http://www.theserverside.com/news/thread.tss?thread_id=35645 • http://www.almaer.com/blog/archives/000846.html • http://houseofhaug.net/blog/archives/2005/08/12/hibernate-hates-spring/

More Related