250 likes | 453 Views
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) {
E N D
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) { return number; } } @Stateful public class CalculatorImpl implements Calculator { private intnumber; public int calculate(int number) { this.number += number; return this.number; } }
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; } }
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!"); } }
Demo <minidemo>
Transaction Management • Spring • Declarative transaction – XML config • Programmatic transaction support • Annotation support • EJB3 • Declarative transaction – Annotations • Stateful Session Beans – transaction over multiple requests! • JTA
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
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>
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 { ... } }
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(); } }
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
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 {
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
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());
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;
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?
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
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
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!?
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!
Q & A Thank you for listening! Questions? Contact me: eivind.waaler[at]bekk.no
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/