slide1 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
EJB3 PowerPoint Presentation
Download Presentation
EJB3

Loading in 2 Seconds...

play fullscreen
1 / 24

EJB3 - PowerPoint PPT Presentation


  • 153 Views
  • Uploaded on

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) {

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'EJB3' - admon


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide1

EJB3

javaBin 02.03.2006

Eivind Waaler, BEKK

agenda
Agenda
  • Some basics
  • Minidemo
  • Service aspects – Spring vs. EJB3
session beans
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
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
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!");

}

}

slide6
Demo

<minidemo>

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

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
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 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/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
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
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
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
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
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
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 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
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
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
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!
slide23
Q & A

Thank you for listening!

Questions?

Contact me: eivind.waaler[at]bekk.no

resources and tips
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/