43
Hosted by Tikal. w w w . t i k a l k . c o m w w w . t i k a l k . c o m w w w . t i k a l k . c o m w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in EJB 3 Development in EJB 3 Development in EJB 3 Development in EJB 3 Development in EJB 3 Development in EJB 3 Development in EJB 3 Development in JBoss JBoss JBoss JBoss JBoss JBoss JBoss JBoss Environment Environment Environment Environment Environment Environment Environment Environment Israel JBoss User Group Israel JBoss User Group Israel JBoss User Group Israel JBoss User Group Israel JBoss User Group Israel JBoss User Group Israel JBoss User Group Israel JBoss User Group Session 04 / 6.12.2006 Session 04 / 6.12.2006 By : Yanai Franchi, Senior Software Engineer By : Yanai Franchi, Senior Software Engineer By : Yanai Franchi, Senior Software Engineer By : Yanai Franchi, Senior Software Engineer By : Yanai Franchi, Senior Software Engineer By : Yanai Franchi, Senior Software Engineer By : Yanai Franchi, Senior Software Engineer By : Yanai Franchi, Senior Software Engineer Tikal Tikal Tikal Tikal Tikal Tikal Tikal Tikal

Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal.

w w w . t i k a l k . c o m

w w w . t i k a l k . c o m

w w w . t i k a l k . c o m

w w w . t i k a l k . c o m

Cost-Benefit Open Source

EJB 3 Development in

EJB 3 Development in

EJB 3 Development in

EJB 3 Development in

EJB 3 Development in

EJB 3 Development in

EJB 3 Development in

EJB 3 Development in

JBoss

JBoss

JBoss

JBoss

JBoss

JBoss

JBoss

JBossEnvironment

Environment

Environment

Environment

Environment

Environment

Environment

Environment

Israel JBoss User Group

Israel JBoss User Group

Israel JBoss User Group

Israel JBoss User Group

Israel JBoss User Group

Israel JBoss User Group

Israel JBoss User Group

Israel JBoss User Group

Session 04 / 6.12.2006

Session 04 / 6.12.2006

By : Yanai Franchi, Senior Software Engineer

By : Yanai Franchi, Senior Software Engineer

By : Yanai Franchi, Senior Software Engineer

By : Yanai Franchi, Senior Software Engineer

By : Yanai Franchi, Senior Software Engineer

By : Yanai Franchi, Senior Software Engineer

By : Yanai Franchi, Senior Software Engineer

By : Yanai Franchi, Senior Software Engineer

“ ““““ “““ Tikal

Tikal

Tikal

Tikal

Tikal

Tikal

Tikal

Tikal ” ”””” ”””

Page 2: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 2|

www.tikalk.com

Israel JBUG

Agenda

Agenda

Agenda

Agenda

Agenda

Agenda

Agenda

Agenda

�Preface

�EJB3 Core

�Demo

�Advanced EJB3

�Summary

Page 3: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 3|

www.tikalk.com

Israel JBUG

Preface

Preface

Page 4: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 4|

www.tikalk.com

Israel JBUG

EJB 2.1 Ailments

EJB 2.1 Ailments

EJB 2.1 Ailments

EJB 2.1 Ailments

EJB 2.1 Ailments

EJB 2.1 Ailments

EJB 2.1 Ailments

EJB 2.1 Ailments

�EJB 2.1 is ‘noisy’

»EJB-Interfaces and Home-Interfaces (remote & Local)

»XML Hell: Deployment descriptors (standard and proprietary)

»Over verbose, complicated API

»Intrusive framework -Lacks separation of concerns

�Entity Beans:

»OO Killer

»Not portable

»Barely testable

»Incurs serious overhead

Page 5: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 5|

www.tikalk.com

Israel JBUG

EJB 2.1 Ailments

EJB 2.1 Ailments

EJB 2.1 Ailments

EJB 2.1 Ailments

EJB 2.1 Ailments

EJB 2.1 Ailments

EJB 2.1 Ailments

EJB 2.1 Ailments– –––– –––Cont

Cont

Cont

Cont

Cont

Cont

Cont

Cont ’ ’’’’ ’’’

�Many LoCto implement an EJB

�Many LoCto write an EJB Client

�Entity-Beans dictates usage of DTOs

»“Shotgun Smell”

»Anaemic value objects with no behaviour

�Long development cycle: edit-compile-deploy-test

Page 6: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 6|

www.tikalk.com

Israel JBUG

EJB 3.0 Remedies

EJB 3.0 Remedies

EJB 3.0 Remedies

EJB 3.0 Remedies

EJB 3.0 Remedies

EJB 3.0 Remedies

EJB 3.0 Remedies

EJB 3.0 Remedies

�Everything is a POJO

�Facilitates Test Driven Development

�Simplified programming model

»No EJB-Interface to implement or extend

»Homeless

»No EJB callbackmethods (ejbCreate(), ejbRemove()…)

»Uses Java 5 Annotations (optional deployment descriptors)

»Sensible default values

»Dependency Injection

»Runtime Exceptions

Page 7: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 7|

www.tikalk.com

Israel JBUG

EJB3.0 with

EJB3.0 with

EJB3.0 with

EJB3.0 with

EJB3.0 with

EJB3.0 with

EJB3.0 with

EJB3.0 with JBoss

JBoss

JBoss

JBoss

JBoss

JBoss

JBoss

JBoss4.x4.x4.x4.x4.x4.x4.x4.x

�EJB3 container can be integrated in JBoss-4.0.5

�Hibernate 3.2 as persistent provider.

�Extra services extending EJB3.0 standards

»Clustering

»Asynchronous calls to Session Beans.

»Managed Service POJOs

»Message Driven POJO (MDP)

Page 8: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 8|

www.tikalk.com

Israel JBUG

JBoss

JBoss

JBoss

JBoss

JBoss

JBoss

JBoss

JBossAS 5.0

AS 5.0

AS 5.0

AS 5.0

AS 5.0

AS 5.0

AS 5.0

AS 5.0- ---- ---beta1 is out

beta1 is out

beta1 is out

beta1 is out

beta1 is out

beta1 is out

beta1 is out

beta1 is out

�JBossMicrocontainer–

»POJO based microcontainerremoving the dependency on

JMX

�EJB3 container integrated

�Hibernate 3.2 -JPA certified

�JBoss-Messaging 1.2 –

»a complete rewrite of JBossMQ.

�JBossWebServices2.0 –

»New custom built JAX-WS compliant WebServicesstack.

�JBossCache2.0

»Traditional tree-structured and PojoCachesupport

Page 9: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 9|

www.tikalk.com

Israel JBUG

EJB3 Core

EJB3 Core

Page 10: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 10|

www.tikalk.com

Israel JBUG

public interface HotelService{

List<Hotel> findAllHotels();

void saveHotel(HotelnewHotel);

}public interface HotelService{

List<Hotel> findAllHotels();

void saveHotel(HotelnewHotel);

} @Stateless

public class HotelServiceBeanimplements HotelService{

public List<Hotel> findAllHotels() {

//find all Hotels from the DB

} public void saveHotel(HotelnewHotel) {

//save hotel

}

}@Stateless

public class HotelServiceBeanimplements HotelService{

public List<Hotel> findAllHotels() {

//find all Hotels from the DB

} public void saveHotel(HotelnewHotel) {

//save hotel

}

}

Stateless Session Bean

Stateless Session Bean

Stateless Session Bean

Stateless Session Bean

Stateless Session Bean

Stateless Session Bean

Stateless Session Bean

Stateless Session Bean

public interface HotelService{

List<Hotel> findAllHotels();

void saveHotel(Hotel

newHotel);

}public interface HotelService{

List<Hotel> findAllHotels();

void saveHotel(Hotel

newHotel);

} @Remote(HotelService.class)

@Local(HotelService.class)

@Stateless

public class HotelServiceBeanimplements HotelService{

public List<Hotel> findAllHotels() {

//find all Hotels from the DB

} public void saveHotel(HotelnewHotel) {

//save hotel

}

}@Remote(HotelService.class)

@Local(HotelService.class)

@Stateless

public class HotelServiceBeanimplements HotelService{

public List<Hotel> findAllHotels() {

//find all Hotels from the DB

} public void saveHotel(HotelnewHotel) {

//save hotel

}

}@Remote

@Local

public interface HotelService{

List<Hotel> findAllHotels();

void saveHotel(HotelnewHotel);

}@Remote

@Local

public interface HotelService{

List<Hotel> findAllHotels();

void saveHotel(HotelnewHotel);

} @Stateless

public class HotelServiceBeanimplements HotelService{

public List<Hotel> findAllHotels() {

//find all Hotels from the DB

} public void saveHotel(HotelnewHotel) {

//save hotel

}

}@Stateless

public class HotelServiceBeanimplements HotelService{

public List<Hotel> findAllHotels() {

//find all Hotels from the DB

} public void saveHotel(HotelnewHotel) {

//save hotel

}

}

Page 11: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 11|

www.tikalk.com

Israel JBUG

The Client Code

The Client Code

The Client Code

The Client Code

The Client Code

The Client Code

The Client Code

The Client Code

�Beans are created without home

�You still have to create InitialContext, lookup (No

Dependency Injection) and do downcasts �

public class HotelServiceTestextends TestCase{

public void testSaveHotel() throws Exception {

InitialContextctx= new InitialContext();

HotelServicehotelService= (HotelService)

ctx.lookup(“hotel-app/HotelServiceBean/remote");

List<Hotel> hotels = hotelService.findHotels();

}

}public class HotelServiceTestextends TestCase{

public void testSaveHotel() throws Exception {

InitialContextctx= new InitialContext();

HotelServicehotelService= (HotelService)

ctx.lookup(“hotel-app/HotelServiceBean/remote");

List<Hotel> hotels = hotelService.findHotels();

}

}

Jndi

Name

Page 12: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 12|

www.tikalk.com

Israel JBUG

Stateful

Stateful

Stateful

Stateful

Stateful

Stateful

Stateful

StatefulSession Bean

Session Bean

Session Bean

Session Bean

Session Bean

Session Bean

Session Bean

Session Bean

public interface Cart {

void addItem(intprodId, intquantity);

void checkout();

}public interface Cart {

void addItem(intprodId, intquantity);

void checkout();

}

@Stateful

public class CartBeanimplements Cart {

private List<Item> items = new LinkedList<Item>();

public void addItem(intprodId, intquantity){

} @Remove

public void checkout()

}

}@Stateful

public class CartBeanimplements Cart {

private List<Item> items = new LinkedList<Item>();

public void addItem(intprodId, intquantity){

} @Remove

public void checkout()

}

}

Page 13: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 13|

www.tikalk.com

Israel JBUG

Message Driven Bean

Message Driven Bean

Message Driven Bean

Message Driven Bean

Message Driven Bean

Message Driven Bean

Message Driven Bean

Message Driven Bean

@MessageDriven( activationConfig= {

@ActivationConfigProperty(

propertyName="destinationType",

propertyValue="javax.jms.Queue"),

@ActivationConfigProperty(

propertyName="destination,

propertyValue="queue/booking")

})

public class HotelBookingProcessorBean

implements MessageListener{

void onMessage(Messagemsg){

// check user credit with bank system

// send confirmation via mail

// other business logic

}}@MessageDriven( activationConfig= {

@ActivationConfigProperty(

propertyName="destinationType",

propertyValue="javax.jms.Queue"),

@ActivationConfigProperty(

propertyName="destination,

propertyValue="queue/booking")

})

public class HotelBookingProcessorBean

implements MessageListener{

void onMessage(Messagemsg){

// check user credit with bank system

// send confirmation via mail

// other business logic

}}

Page 14: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 14|

www.tikalk.com

Israel JBUG

What is JPA ?

What is JPA ?

What is JPA ?

What is JPA ?

What is JPA ?

What is JPA ?

What is JPA ?

What is JPA ?

�JPA is part of the EJB3 standard (JSR-220)

»The standard ORM API for Java EE

»Simplifies development of JEE/JSE applications via object

persistence

�Originally, an CMP improvement

»Became a separate persistence spec for POJOs

�Usable both with JSE5 and JEE5

�Implementation products

»Hibernate (JBoss)

»TopLinkJPA (Oracle)

»OpenJPA(Apache)

Page 15: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 15|

www.tikalk.com

Israel JBUG

Persistence Beans

Persistence Beans

Persistence Beans

Persistence Beans

Persistence Beans

Persistence Beans

Persistence Beans

Persistence Beans

�Everything is POJO: No interfaces to implement

�Supports all relationships (with cascading)

�Inheritance + Polymorphic Queries

�Eager and Lazy fetching

�Annotation based (XML is optional)

�Simple, lifecycle model –New, Persistent, Detached

�Provides a modern ORM programming model

Page 16: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 16|

www.tikalk.com

Israel JBUG

Persistence Annotations

Persistence Annotations

Persistence Annotations

Persistence Annotations

Persistence Annotations

Persistence Annotations

Persistence Annotations

Persistence Annotations

@Entity @Table(name

= "HOTELS")

public class Hotel implements Serializable

{

private Long id;

private String name;

private String address;

@Id @GeneratedValue

public Long getId() {

return id;

} private void setId(Long

id) {

this.id

= id;

} @Column(name="NAME", length=40)

public String getName() {

return name;

} @OneToMany(cascade=ALL) @JoinColumn(name=“ITEM_ID”)

public Set<Bid> getBids() {

return bids;

}

@Entity @Table(name

= "HOTELS")

public class Hotel implements Serializable

{

private Long id;

private String name;

private String address;

@Id @GeneratedValue

public Long getId() {

return id;

} private void setId(Long

id) {

this.id

= id;

} @Column(name="NAME", length=40)

public String getName() {

return name;

} @OneToMany(cascade=ALL) @JoinColumn(name=“ITEM_ID”)

public Set<Bid> getBids() {

return bids;

}

Page 17: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 17|

www.tikalk.com

Israel JBUG

EntityManager

EntityManager

EntityManager

EntityManager

EntityManager

EntityManager

EntityManager

EntityManagerAPI

API

API

API

API

API

API

API

�Take control the lifecycle entities

�Execute queries

�All access through this service

»Creation, retrieval, removal, and merging

�Analogous to Hibernate Session

Page 18: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 18|

www.tikalk.com

Israel JBUG

Persistence Context

Persistence Context

Persistence Context

Persistence Context

Persistence Context

Persistence Context

Persistence Context

Persistence Context

�What is Persistence Context?

»A set of managed entity instances

»Persistent identity <=> object identity

»Analogous to Hibernate Session cache

»The Entity Manager keep the persistent context.

�What is the scope of Persistence Context?

»Transaction scope?

»Other scope?

»In JPA, a persistence context may span multiple (non-

concurrent) transactions

Page 19: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 19|

www.tikalk.com

Israel JBUG

JPA in Action

JPA in Action

JPA in Action

JPA in Action

JPA in Action

JPA in Action

JPA in Action

JPA in Action

@Stateless

public class HotelServiceBeanimplements HotelService{

@PersistenceContext

private EntityManagerem;

public List<Hotel> findAllHotels() {

return em.createQuery(

"select h from Hotel h order by h.nameasc").

getResultList();

} public void saveHotel(HotelnewHotel) {

em.persist(newHotel);

} …

}@Stateless

public class HotelServiceBeanimplements HotelService{

@PersistenceContext

private EntityManagerem;

public List<Hotel> findAllHotels() {

return em.createQuery(

"select h from Hotel h order by h.nameasc").

getResultList();

} public void saveHotel(HotelnewHotel) {

em.persist(newHotel);

} …

}

Page 20: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 20|

www.tikalk.com

Israel JBUG

What is Embeddable

What is Embeddable

What is Embeddable

What is Embeddable

What is Embeddable

What is Embeddable

What is Embeddable

What is Embeddable JBoss

JBoss

JBoss

JBoss

JBoss

JBoss

JBoss

JBoss? ???? ???

�Java EE 5.0 application servers are no longer the

monolithic beasts of the J2EE 1.4 era.

�Embeddable JBosscan run EJB3 in

»Unit test environment

»Web Server

»JSE5 application.

»Other Application Servers

�Built on top of the new JBossMicrocontainer.

�Currently enabled services are JNDI, JCA, JTA, and

the EJB 3.0 container

Page 21: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 21|

www.tikalk.com

Israel JBUG

Embeddable Current State

Embeddable Current State

Embeddable Current State

Embeddable Current State

Embeddable Current State

Embeddable Current State

Embeddable Current State

Embeddable Current State

�Local JNDI

�Transaction Manager

�Local JMS

�Local TX datasource/

connection pool

�Stateful, Stateless,

Service, Consumer,

Producer, and MDBs

�EJB 3 Persistence

�Hibernate integration

�EJB Security

�XA Connection pool is

not available yet.

�Distributed remote

communication is not

supported yet.

�JNDI is not available

remotely

�You cannot access JMS

remotely.

�Consider it an alpha

release...

Page 22: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 22|

www.tikalk.com

Israel JBUG

Running Embeddable

Running Embeddable

Running Embeddable

Running Embeddable

Running Embeddable

Running Embeddable

Running Embeddable

Running Embeddable JBoss

JBoss

JBoss

JBoss

JBoss

JBoss

JBoss

JBoss

�EJB3StandaloneBootstrap class

EJB3StandaloneBootstrap class

EJB3StandaloneBootstrap class

EJB3StandaloneBootstrap class bootstraps the ejb3

container.

�It find EJB and resources by

ClassLoader.getResource.

EJB3StandaloneBootstrap.boot(null);

EJB3StandaloneDeployer deployer

= new EJB3StandaloneDeployer();

//Add configuration files

deployer.create();

deployer.start();

EJB3StandaloneBootstrap.scanClasspath();

EJB3StandaloneBootstrap.boot(null);

EJB3StandaloneDeployer deployer

= new EJB3StandaloneDeployer();

//Add configuration files

deployer.create();

deployer.start();

EJB3StandaloneBootstrap.scanClasspath();

Page 23: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 23|

www.tikalk.com

Israel JBUGEmbeddable

Embeddable

Embeddable

Embeddable

Embeddable

Embeddable

Embeddable

Embeddable JBoss

JBoss

JBoss

JBoss

JBoss

JBoss

JBoss

JBoss

in Web Application

in Web Application

in Web Application

in Web Application

in Web Application

in Web Application

in Web Application

in Web Application

�JBosscomes with a context listener class that you

can use to configure your application.

�The ServletBootstrapListenerwill automatically

scan all Jars within /WEB-INF/libfor EJBsand

Entity beans that can be deployed.

<listener>

<listener-class>

org.jboss.ejb3.embedded.ServletBootstrapListener

</listener-class>

</listener>

<context-param>

<param-name>jboss-kernel-deployments</param-name>

<param-value>

embedded-jboss-beans.xml, jboss-jms-beans.xml

</param-value>

</context-param>

<listener>

<listener-class>

org.jboss.ejb3.embedded.ServletBootstrapListener

</listener-class>

</listener>

<context-param>

<param-name>jboss-kernel-deployments</param-name>

<param-value>

embedded-jboss-beans.xml, jboss-jms-beans.xml

</param-value>

</context-param>

Page 24: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 24|

www.tikalk.com

Israel JBUG

Demo

Demo

Page 25: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 25|

www.tikalk.com

Israel JBUG

Advanced EJB3

Advanced EJB3

Page 26: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 26|

www.tikalk.com

Israel JBUG

JBoss

JBoss

JBoss

JBoss

JBoss

JBoss

JBoss

JBossAsynchronous Calls

Asynchronous Calls

Asynchronous Calls

Asynchronous Calls

Asynchronous Calls

Asynchronous Calls

Asynchronous Calls

Asynchronous Calls

public class HotelServiceTest

extends TestCase

{

public void testSaveHotel() throws Exception {

InitialContext

ctx

= new InitialContext();

HotelService

hotelService

= (HotelService)

ctx.lookup("sample-ejb3/HotelServiceBean/remote");

HotelService

hotelAsynchService

= (HotelService)

Asynch.getAsynchronousProxy(hotelService);

hotelAsynchService.findAllHotels(); //Asynch

call

Future future

=

((AsynchProvider)hotelAsynchService).getFuture();

//Do some other interesting stuff

while (!future.isDone())

Thread.sleep(100);

List<Hotel> hotels = (List<Hotel>)future.get();

}

}public class HotelServiceTest

extends TestCase

{

public void testSaveHotel() throws Exception {

InitialContext

ctx

= new InitialContext();

HotelService

hotelService

= (HotelService)

ctx.lookup("sample-ejb3/HotelServiceBean/remote");

HotelService

hotelAsynchService

= (HotelService)

Asynch.getAsynchronousProxy(hotelService);

hotelAsynchService.findAllHotels(); //Asynch

call

Future future

=

((AsynchProvider)hotelAsynchService).getFuture();

//Do some other interesting stuff

while (!future.isDone())

Thread.sleep(100);

List<Hotel> hotels = (List<Hotel>)future.get();

}

}

Page 27: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 27|

www.tikalk.com

Israel JBUG

JBoss

JBoss

JBoss

JBoss

JBoss

JBoss

JBoss

JBossService POJOS

Service POJOS

Service POJOS

Service POJOS

Service POJOS

Service POJOS

Service POJOS

Service POJOS

public interface HotelServiceManagement{

List<Hotel> findAllHotels();

}public interface HotelServiceManagement

{

List<Hotel> findAllHotels();

}

@Local(HotelService.class)

@Service

@Management(HotelServiceManagement)

public class HotelServiceBeanimplements

HotelService, HotelServiceManagement{

public List<Hotel> findAllHotels() {

//find all Hotels from the DB

} public void saveHotel(HotelnewHotel) {

//save hotel

}

}@Local(HotelService.class)

@Service

@Management(HotelServiceManagement)

public class HotelServiceBeanimplements

HotelService, HotelServiceManagement{

public List<Hotel> findAllHotels() {

//find all Hotels from the DB

} public void saveHotel(HotelnewHotel) {

//save hotel

}

}

Page 28: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 28|

www.tikalk.com

Israel JBUGJBoss

JBoss

JBoss

JBoss

JBoss

JBoss

JBoss

JBossMessage Driven

Message Driven

Message Driven

Message Driven

Message Driven

Message Driven

Message Driven

Message Driven POJOs

POJOs

POJOs

POJOs

POJOs

POJOs

POJOs

POJOs

(MDP)

(MDP)

(MDP)

(MDP)

(MDP)

(MDP)

(MDP)

(MDP)

@Consumer ( activationConfig

={

@ActivationConfigProperty(

propertyName="destinationType",

propertyValue="javax.jms.Queue"),

@ActivationConfigProperty(

propertyName="destination,

propertyValue="queue/booking")

})

public class HotelBookingProcessorPojo

Implements HotelBookingProcessor

{

public processBooking(Booking

booking){

// check user credit with bank system

// send confirmation via mail

}

}

@Consumer ( activationConfig

={

@ActivationConfigProperty(

propertyName="destinationType",

propertyValue="javax.jms.Queue"),

@ActivationConfigProperty(

propertyName="destination,

propertyValue="queue/booking")

})

public class HotelBookingProcessorPojo

Implements HotelBookingProcessor

{

public processBooking(Booking

booking){

// check user credit with bank system

// send confirmation via mail

}

}

@Producer

public interface HotelBookingProcessor

{

void processBooking(Booking

booking);

}

@Producer

public interface HotelBookingProcessor

{

void processBooking(Booking

booking);

}

Page 29: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 29|

www.tikalk.com

Israel JBUG

JBoss

JBoss

JBoss

JBoss

JBoss

JBoss

JBoss

JBossMDP

MDP

MDP

MDP

MDP

MDP

MDP

MDP – –––– –––Sender Side

Sender Side

Sender Side

Sender Side

Sender Side

Sender Side

Sender Side

Sender Side

InitialContext

ctx

= new InitialContext();

HotelBookingProcessor

hbp

= (HotelBookingProcessor)

ctx.lookup(HotelBookingProcessor.class.getName());

ProducerManager

manager =

((ProducerObject)hbp).getProducerManager();

manager.connect();

try {

hbp.processBooking(booking); //Asynch

invocation!!!

} finally {

manager.close(); // clean up the JMS connection

}

InitialContext

ctx

= new InitialContext();

HotelBookingProcessor

hbp

= (HotelBookingProcessor)

ctx.lookup(HotelBookingProcessor.class.getName());

ProducerManager

manager =

((ProducerObject)hbp).getProducerManager();

manager.connect();

try {

hbp.processBooking(booking); //Asynch

invocation!!!

} finally {

manager.close(); // clean up the JMS connection

}

Page 30: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 30|

www.tikalk.com

Israel JBUG

Transactions

Transactions

Transactions

Transactions

Transactions

Transactions

Transactions

Transactions

�@TransactionAttributedefines per-method transaction

boundary

�Supported same propagation models as in 2.1:

REQUIRED, REQUIRES_NEW, SUPPORTS, MANDATORY, NEVER, NOT_SUPPORTED

@Stateless

public class HotelServiceBeanimplements HotelService{

@TransactionAttribute(

TransactionAttributeType.REQUIRES_NEW)

public void saveHotel(HotelnewHotel) {

//save hotel

}

}@Stateless

public class HotelServiceBeanimplements HotelService{

@TransactionAttribute(

TransactionAttributeType.REQUIRES_NEW)

public void saveHotel(HotelnewHotel) {

//save hotel

}

}

Page 31: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 31|

www.tikalk.com

Israel JBUG

Security

Security

Security

Security

Security

Security

Security

Security

�Specifies the list of roles permitted to access method(s)

in an application

�Use @RolesAllowedfor a list of security role names or

@PermitAllto permit access for all.

�The SecurityDomainspecifies the JAAS repository which

will be used by JBossto authenticate and authorize.

@org.jboss.ejb3.security.SecurityDomain("other")

@Stateless public class HotelServiceBean

implements HotelService{

@RolesAllowed( { “Role_Administrator" } )

public void saveHotel(HotelnewHotel) {

//save hotel

}

}@org.jboss.ejb3.security.SecurityDomain("other")

@Stateless public class HotelServiceBean

implements HotelService{

@RolesAllowed( { “Role_Administrator" } )

public void saveHotel(HotelnewHotel) {

//save hotel

}

}

Page 32: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 32|

www.tikalk.com

Israel JBUG

Clustering

Clustering

Clustering

Clustering

Clustering

Clustering

Clustering

Clustering

�For Session beans use the JBoss@Clustered

annotation on the bean class.

�A clustered bean has load balancing and failover

of a request.

@Remote(HotelService.class)

@org.jboss.annotation.ejb.Clustered

@Statelesspublic class HotelServiceBean

implements HotelService{

public List<Hotel> findAllHotels() {

//find all Hotels from the DB

} public void saveHotel(HotelnewHotel) {

//save hotel

}

}@Remote(HotelService.class)

@org.jboss.annotation.ejb.Clustered

@Statelesspublic class HotelServiceBean

implements HotelService{

public List<Hotel> findAllHotels() {

//find all Hotels from the DB

} public void saveHotel(HotelnewHotel) {

//save hotel

}

}

Page 33: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 33|

www.tikalk.com

Israel JBUG

Web

Web

Web

Web

Web

Web

Web

Web- ---- ---Services with EJB3

Services with EJB3

Services with EJB3

Services with EJB3

Services with EJB3

Services with EJB3

Services with EJB3

Services with EJB3

@WebService

@SOAPBinding(style=Style.RPC)

public interface HotelService

extends Remote{

@WebMethod

List<Hotel> findAllHotels();

@WebMethod

void saveHotel(Hotel

newHotel);

}@WebService

@SOAPBinding(style=Style.RPC)

public interface HotelService

extends Remote{

@WebMethod

List<Hotel> findAllHotels();

@WebMethod

void saveHotel(Hotel

newHotel);

} @Stateless

@WebService(endpointInterface=“com.tikal.sample.HotelService")

public class HotelServiceBean

implements HotelService{

public List<Hotel> findAllHotels() {

//find all Hotels from the DB

} public void saveHotel(Hotel

newHotel) {

//save hotel

}

}@Stateless

@WebService(endpointInterface=“com.tikal.sample.HotelService")

public class HotelServiceBean

implements HotelService{

public List<Hotel> findAllHotels() {

//find all Hotels from the DB

} public void saveHotel(Hotel

newHotel) {

//save hotel

}

}

Page 34: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 34|

www.tikalk.com

Israel JBUG

Dependency Injection

Dependency Injection

Dependency Injection

Dependency Injection

Dependency Injection

Dependency Injection

Dependency Injection

Dependency Injection

�Dependency Injection

»Bean class specifies dependencies instead of lookup

�On Jboss5.x @EJB will work inside a servletor JSP also.

@Stateful

public class HotelBookingProcessorBean

implements HotelBookingProcessor{

@EJB(name=“CreditProcessorEJB”)

private CreditCardProcessorprocessor;

@Resource(jndiName=“java:/DefaultDS”)

private DataSourcejdbc;

...

}

@Stateful

public class HotelBookingProcessorBean

implements HotelBookingProcessor{

@EJB(name=“CreditProcessorEJB”)

private CreditCardProcessorprocessor;

@Resource(jndiName=“java:/DefaultDS”)

private DataSourcejdbc;

...

}

Page 35: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 35|

www.tikalk.com

Israel JBUG

Callbacks

Callbacks

Callbacks

Callbacks

Callbacks

Callbacks

Callbacks

Callbacks

�Optional Annotations replace callbackmethods

»PostConstruct

»PreDestroy

»PostActivate

»PrePassivate

@Stateful

public class FacadeBeanimplements Facade {

private URL propertiesURL;

@PostConstruct

public void init() {

propertiesURL=getClass().getClassLoader().

getResource("MyProperties.txt");

}

}@Stateful

public class FacadeBeanimplements Facade {

private URL propertiesURL;

@PostConstruct

public void init() {

propertiesURL=getClass().getClassLoader().

getResource("MyProperties.txt");

}

}

Page 36: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 36|

www.tikalk.com

Israel JBUG

Timer Callback

Timer Callback

Timer Callback

Timer Callback

Timer Callback

Timer Callback

Timer Callback

Timer Callback

@Stateless

public class ExampleTimerBeanimplements ExampleTimer{

@Resource

private SessionContextctx;

public void scheduleTimer(longmilliseconds) {

ctx.getTimerService().createTimer

(new Date(newDate().getTime() +

milliseconds), "Hello World");

}

@Timeout

public voidtimeoutHandler(Timertimer) {

System.out.println(timer.getInfo());

timer.cancel();

}}@Stateless

public class ExampleTimerBeanimplements ExampleTimer{

@Resource

private SessionContextctx;

public void scheduleTimer(longmilliseconds) {

ctx.getTimerService().createTimer

(new Date(newDate().getTime() +

milliseconds), "Hello World");

}

@Timeout

public voidtimeoutHandler(Timertimer) {

System.out.println(timer.getInfo());

timer.cancel();

}}

Page 37: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 37|

www.tikalk.com

Israel JBUG

Interceptors

Interceptors

Interceptors

Interceptors

Interceptors

Interceptors

Interceptors

Interceptors

�Interceptors are executed in the gap between caller and bean

�Available only for session-beans and MDB

�Provide basic AOP

�Allow custom code to be applied to an EJB

�Simply add @AroundInvoketo a method

�A custom interceptor class can be defined

interceptor

cart.add(product)

public class CartBean{

public void add(…) {}

}

Page 38: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 38|

www.tikalk.com

Israel JBUG

Interceptors

Interceptors

Interceptors

Interceptors

Interceptors

Interceptors

Interceptors

Interceptors

public class TracingInterceptor

{@AroundInvoke

public Object

log(InvocationContext

ctx){

long start = System.currentTimeMillis();

try {

return ctx.proceed();

} catch(Exception

e) {

throw new RuntimeException(e);

} finally {

long time = System.currentTimeMillis() -

start;

String method =

ctx.getBean().getClass().getName()

+"." +

ctx.getMethod().getName()

+ "()";

System.out.println(method+" took " +time+"ms");

}}

}public class TracingInterceptor

{@AroundInvoke

public Object

log(InvocationContext

ctx){

long start = System.currentTimeMillis();

try {

return ctx.proceed();

} catch(Exception

e) {

throw new RuntimeException(e);

} finally {

long time = System.currentTimeMillis() -

start;

String method =

ctx.getBean().getClass().getName()

+"." +

ctx.getMethod().getName()

+ "()";

System.out.println(method+" took " +time+"ms");

}}

}

Page 39: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 39|

www.tikalk.com

Israel JBUG

Interceptors Cont.

Interceptors Cont.

Interceptors Cont.

Interceptors Cont.

Interceptors Cont.

Interceptors Cont.

Interceptors Cont.

Interceptors Cont.

@Stateless

@Interceptors ({"com.tikal.sample.TracingInterceptor"})

public class HotelServiceBeanimplements HotelService{

// ...

}@Stateless

@Interceptors ({"com.tikal.sample.TracingInterceptor"})

public class HotelServiceBeanimplements HotelService{

// ...

}

<assembly-descriptor>

...

<interceptor-binding>

<ejb-name>

con.tikal.sample.HotelServiceBean

</ejb-name>

<interceptor-class>

con.tikal.sample.TracingInterceptor

</interceptor-class>

</interceptor-binding>

...

</assembly-descriptor>

<assembly-descriptor>

...

<interceptor-binding>

<ejb-name>

con.tikal.sample.HotelServiceBean

</ejb-name>

<interceptor-class>

con.tikal.sample.TracingInterceptor

</interceptor-class>

</interceptor-binding>

...

</assembly-descriptor>

Page 40: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 40|

www.tikalk.com

Israel JBUG

A Taste Of Seam

A Taste Of Seam

A Taste Of Seam

A Taste Of Seam

A Taste Of Seam

A Taste Of Seam

A Taste Of Seam

A Taste Of Seam

�JBoss’slatest and greatest web framework

»Gavin King’s new baby…

�Unifies EJB and Web component models

»StatefulEJBscan be JSF Backing Beans!

�Support the notion of Contextual Components

»May be bound to event, page, conversation, session,

process or application Scopes

Page 41: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 41|

www.tikalk.com

Israel JBUG

Summary

Summary

Summary

Summary

Summary

Summary

Summary

Summary - ---- ---EJB3 Pros & Cons

EJB3 Pros & Cons

EJB3 Pros & Cons

EJB3 Pros & Cons

EJB3 Pros & Cons

EJB3 Pros & Cons

EJB3 Pros & Cons

EJB3 Pros & Cons

�Few implementations

�Recently released

�Small user base

�DI for JNDI only

�Minimal AOP support

�ACL Security Standards

�Missing Service-

abstraction

�Too little, too late?

�Standard!

�StatefulModel

�Can span multiple user

requests.

�Enterprise Features

»Clustering

»JTA

»Built-in Security model

Page 42: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 42|

www.tikalk.com

Israel JBUG

Q&A

Q&A

Q&A

Q&A

Q&A

Q&A

Q&A

Q&A

Page 43: Israel JBoss User Group Session 04 / 6.12.2006 EJB 3 ... · Hosted by Tikal. w w w . t i k a l k . c o m Cost-Benefit Open Source EJB 3 Development in JBoss Israel JBoss User Group

Hosted by Tikal

| 43|

www.tikalk.com

Israel JBUG

Thank You

Thank You

Thank You

Thank You

Thank You

Thank You

Thank You

Thank You

[email protected]

[email protected]

[email protected]

[email protected]

[email protected]

[email protected]

[email protected]

[email protected]