EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

64
1 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

description

Talk on EJB 2.x, 3.0, 3.1 & 3.2

Transcript of EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

Page 1: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

1 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Page 2: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

2 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

EJB: Simple, Light & Powerful backbone of Java EE

Jagadish Ramu

Page 3: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

3 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

San Francisco September 30–October 4, 2012

Page 4: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

4 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

Page 5: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

5 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Program Agenda

• EJB – Evolution• EJB 3.0• EJB 3.1 – Features• EJB 3.2 – In progress• Q & A

Page 6: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

6 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

EJB 1.0 – EJB 2.1

Services designed for container, not application

Got the job done, but at the cost of complexity

Heavyweight programming model

Lots of classes and interfaces needed to be supplied– Lots of boilerplate code

Complex XML deployment descriptor required

Very powerful, but too complex

Page 7: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

7 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

EJB 3.0

Reduced number of classes developer needs to produce

“POJO” classes and interfaces – No need to implement the javax.ejb.* interfaces– Optional home and component interfaces

Leverage Java language metadata annotations– Injection instead of lookup

Optional XML descriptor

Major changes: Ease of Development

Page 8: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

8 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

EJB 3.0

Configuration by exception– Use defaults for common cases

Interceptors

Simplification of entity bean persistence– Lightweight “POJO” entities, not components– Support for inheritance – Standard O/R mapping

Major changes: Ease of Development (Cont)

Page 9: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

9 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

EJB 3.1

• Optional Local Business Interfaces

• Simplified Packaging

• Portable JNDI Names

Ease of Use Improvements

Page 10: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

10 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Session Bean - Local Business Interface

Page 11: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

11 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Session Bean with “No-interface” View@Statelesspublic class HelloBean {

public String sayHello(String msg) { return “Hello “ + msg; }}

@EJB HelloBean h;

...

h.sayHello(“bob”);

Page 12: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

12 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

EJB 3.1

• Optional Local Business Interfaces

• Simplified Packaging

• Portable JNDI Names

Ease of Use Improvements

Page 13: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

13 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

JavaTM EE Platform 5 Packaging

foo.ear

WEB-INF/web.xmlWEB-INF/classes/ com/acme/FooServlet.classWEB-INF/classes com/acme/Foo.class

foo_web.war

com/acme/FooBean.classcom/acme/Foo.class

foo_ejb.jar

foo.ear

lib/foo_common.jar

com/acme/Foo.class

WEB-INF/web.xmlWEB-INF/classes/ com/acme/FooServlet.class

foo_web.war

com/acme/FooBean.class

foo_ejb.jar

OR

Page 14: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

14 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

foo.warWEB-INF/classes/com/acme/ FooServlet.class FooBean.class

EJB 3.1 Simplified Packaging

Page 15: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

15 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

EJB 3.1

• Optional Local Business Interfaces

• Simplified Packaging

• Portable JNDI Names

Ease of Use Improvements

Page 16: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

16 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

EJB 3.1Portable Global JNDI Name Syntax

java:global[/<app­name>]/<module­name>/<bean­name>

[!<fully­qualified­interface­name>]

Only within EAR

Base name of EAR

(or application.xml)

Base name of ejb-jar/WAR

(or ejb-jar.xml/web.xml)

Unqualified name of the bean class

Annotation/name attribute

Or ejb-jar.xml

• Derived from metadata (annotations/ DD)

• Until now, only java:comp

• Local & Remote business

• No-interface

• Also in java:app, java:module

Page 17: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

17 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Portable JNDI Name: Samplepackage com.acme;@Statelesspublic class HelloBean

java:global/hello/HelloBean

java:global/hello/HelloBean!com.acme.Hello

java:app/hello/HelloBean

java:app/hello/HelloBean!com.acme.Hello

java:module/HelloBean

java:module/HelloBean!com.acme.Hello

implements Hello

If deployed as hello.jar, JNDI entries are:

Page 18: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

18 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Session Bean lookup// session bean lookup from a ModuleInitialContext ic = new InitialContext();

HelloBean hello = (HelloBean) ic.lookup(“java:module/HelloBean”);

hello.sayHello(“bob”);

// Global session bean lookupInitialContext ic = new InitialContext();

Hello hello = (Hello) ic.lookup(“java:global/hello/HelloBean”);

hello.sayHello(“bob”);

Page 19: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

19 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

New Features• Singletons

• Startup / Shutdown callbacks

• Asynchronous session bean invocations

• Calendar-based timers / Automatic timer creation

• EJB 3.1 Lite

• Embeddable API

Page 20: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

20 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Singletons• New session bean component type

• Provides easy sharing of state within application

• Designed for concurrent access

• One bean instance per bean type per VM

• Lots in common with stateless / stateful EJBs• Provides Local, Remote, Web Service client view

• Supports CMT/BMT

• Same container services are available­ resource managers, timer service, method authorization, etc.

Page 21: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

21 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Simple Singleton @Singletonpublic class SharedBean {

private SharedData data = new SharedData();

public int getIntData() { return data.getIntValue(); }

public void setIntValue(int value) { data.setIntValue(value); }

}

Page 22: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

22 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Container Managed Concurrency

@Singleton@Lock(READ)public class SharedBean { private SharedData data = newSharedData();

public int getIntValue() { return data.getIntValue(); }

@Lock(WRITE) @AccessTimeout(value=1, unit=SECONDS) public void updateIntValue(int v) { data.update(v); }}

Page 23: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

23 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Bean Managed Concurrency@Singleton@ConcurrencyManagement(BEAN)public class SharedBean { private SharedData data = new SharedData();

public synchronized int getData() { return data.getXYZ(); }

public void update(...) { ... synchronized (this) { data.setXYZ(...); } }

Page 24: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

24 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

New Features• Singletons

• Startup / Shutdown callbacks

• Asynchronous session bean invocations

• Calendar-based timers / Automatic timer creation

• EJB 3.1 Lite

• Embeddable API

Page 25: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

25 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Startup / Shutdown Callbacks

@Singleton@Startup@DependsOn(“InitializationBean”)public class StartupBean {

@PostConstruct private void onStartup() { // Create a new EJB Timer }

@PreDestroy private void onShutdown() { ... }

}

Page 26: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

26 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

New Features• Singletons

• Startup / Shutdown callbacks

• Asynchronous session bean invocations

• Calendar-based timers / Automatic timer creation

• EJB 3.1 Lite

• Embeddable API

Page 27: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

27 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Asynchronous Session Bean Invocations

• Async processing in JavaEE 5 apps­ JavaEE apps resorted to JMS / MDBs­ Must know JMS APIs and setup JMS Queue etc.

• EJB 3.1 makes it very easy ­ Annotate your method with @Asynchronous­ Control returns to the client before actual method invocation­ Persistent delivery guarantees are not required by spec

Page 28: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

28 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Async Computation Example

@Stateless@Asynchronouspublic class SimpleAsyncEJB { public Future<Integer> addNumbers(int n1, int n2) {

Integer result;result = n1 + n2;try {// simulate JPA queries + reading file system

Thread.currentThread().sleep(2000);} catch (InterruptedException ex) {

ex.printStackTrace();}return new AsyncResult(result);

}}

Page 29: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

29 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Asynchronous method results

• If you need to retrieve the results asynchronusly­ Return type java.util.concurrent.Future

• Result value is returned via Future.get()­ Also supports Future.get(long, TimeUnit)­ Can use Future.isDone()

• Client exception wrapped by ExecutionException­ getCause()returns same exception as would have been thrown by a synchronous

invocation

Page 30: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

30 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

New Features• Singletons

• Startup / Shutdown callbacks

• Asynchronous session bean invocations

• Calendar-based timers / Automatic timer creation

• EJB “Lite”

• Embeddable API

Page 31: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

31 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Timer Service Features

• Calendar-based timeouts • Automatic timer creation

• Non-persistent Timers

Page 32: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

32 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

EJB 3.1 - Timers

• Calendar-based Timers – cron like semantics

• Every 14th minute within the hour, for the hours 1 and 2 am

(minute=”*/14”, hour=”1,2”)

• Every 10 seconds starting at 30

(second=”30/10”)

• Every 5 minutes of every hour

(minute=”*/5”, hour=”*”)

• 2pm on Last Thur of Nov of every year

(hour=”14”, dayOfMonth=”Last Thu”, month=”Nov”)

• Every Mon & Wed midnight @Schedule(dayOfWeek=”Mon,Wed”)

Page 33: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

33 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Calendar Based Expressions

• Numeric attributes­ second, minute

․Allowable values : [0,59]․Default : “0”

­ hour

․Allowable values : [0,23]․Default : “0”

­ year

․Allowable values : four-digit value․Default : “*”

Page 34: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

34 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Calendar Based Expressions

• Other attributes­ dayOfWeek

․Allowable values : [0,7] or {“Sun”, ..., “Sat”}

․Default : “*”­ month

․Allowable values : [1,12] or {“Jan”, ..., “Dec”}

․Default : “*”

Page 35: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

35 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Calendar Based Expressions

• Other attributes­ dayOfMonth

․Allowable values : [1,31] or [-7, -1] or “Last” or {“1st”, “2nd”, “3rd”, “4th”, “5th”, “Last”} {“Sun”, “Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”}

․Default : “*”

Page 36: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

36 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Attribute Syntax• Single value : minute = “30”

• List : month = “Jan, Jul, Dec”

• Range : dayOfWeek = “Mon-Fri” or

dayOfMonth = “27-3”

• Wild card : hour = “*”

• Increment : minute = “*/10”

• Range/List Combination : minute = “1-10, 20-30”

Page 37: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

37 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Non-persistent Timers@Singletonpublic class CacheBean { private Cache cache; @Schedule(minute=”*/5”, hour=”*”, persistent=false) @Lock(WRITE) private void refresh() { ... }

@Lock(READ) public String getXYZ() { ... } }

Page 38: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

38 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

New Features• Singletons

• Startup / Shutdown callbacks

• Asynchronous session bean invocations

• Calendar-based timers / Automatic timer creation

• EJB “Lite”

• Embeddable API

Page 39: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

39 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

EJB 3.1 “Lite” API

• Small subset of EJB 3.1 API required by Java EE Platform 6 Web Profile

• Broadens the availability of EJB technology­ Without losing portability

• Any 3.1 Lite application can be deployed to Web Profile and Full Java EE 6 Platform

­ Made possible by simplified .war packaging

Page 40: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

40 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

EJB Lite – Feature comparison

Page 41: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

41 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

New Features• Singletons

• Startup / Shutdown callbacks

• Asynchronous session bean invocations

• Calendar-based timers / Automatic timer creation

• EJB “Lite”

• Embeddable API

Page 42: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

42 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Example: Local Stateless Session Bean

@Stateless@Local(Bank.class)public class BankBean implements Bank {

@PersistenceContext EntityManager accountDB;

public double deposit(double amount) { ... }

public double withdraw(double amount) { ... }

Page 43: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

43 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Example: Test Client Execution

% java -classpath bankClient.jar : bank.jar : javaee.jar : <vendor_rt>.jar

com.acme.BankTester

Page 44: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

44 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Example: Embeddable API

public class BankTester { public static void main(String[] args) {

EJBContainer container = EJBContainer.createEJBContainer();

// Acquire Local EJB reference Bank bank = (Bank) container.getContext(). lookup(“java:global/bank/BankBean”);

double balance = bank.deposit(100.00); ...

container.close(); }

Page 45: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

45 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Embeddable API

• Portable API for running EJB components in same process as client code

• Same component behavior / life cycle as server-side­ CMT/BMT, injection, threading guarantees, etc.

• “Single Application” model

• Only required to support 3.1 “Lite” API ­ Vendors can optionally support additional functionality

Page 46: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

46 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Discussions in EJB 3.2 Expert Group

Cleanup

Optional features (pruning)

New features

Separating components and services

Further pruning

What are we discussing?

Page 47: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

47 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Optional Features

EJB 3.2 Optional Chapters

Separate document

Approved by Java EE Platform EG

EJB Containers will not be required to support: – CMP– BMP– JAX-RPC

What we are discussing

Page 48: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

48 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

New SFSB

Support for transactions in life-cycle callbacks

PostConstruct/PreDestroy

PrePassivate/PostActivate

What we are discussing

Page 49: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

49 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

EJB 3.2 Lite Requirements

Non-persistent timers

Asynchronous invocations

What we are discussing

Page 50: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

50 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

New in Asynchronous Invocations

Client code be able to decide if to call a business method asynchronously?

– Look up special context?– Helper classes?

Do we need AsyncListener to be notified of outcome?

What we are discussing

Page 51: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

51 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

New in MDBs

How to simplify MDB?

Some requirements from JMS 2.0 Expert Group– Synchronizing XML with annotations– Support for simplified API

What we are discussing

Page 52: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

52 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

MDB

@MessageDriven(mappedName="jms/ejb_mdb_Queue”)

public class SimpleMDB implements MessageListener {

public void onMessage(Message message) {

}

}

Current usage

Page 53: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

53 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

MDB – What if?

@MessageDriven

public class SimpleMDB {

@TBDAnnotation(tbdAttr="jms/ejb_mdb_Queue”)

public void processMessages(Message message) {

}

}

One of many options

Page 54: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

54 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

New in Configuration

QoS annotations or attributes– @MaxConcurrency ?

• Number of concurrent Async methods that could be served

– @PoolSize ?• Bean pool size

What we are discussing

Page 55: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

55 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Common Enterprise Services

Services available for all (most?) Managed Beans

By default available for EJBs

Can be opt-in in other Managed Beans

What we are discussing

Page 56: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

56 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Transactions as Common Enterprise Services

Support for Container-Managed Transactions (CMT) in Managed Beans

Subset of transaction attributes(?)– REQUIRED, REQUIRES_NEW, NOT_SUPPORTED– MANDATORY, NEVER– SUPPORTS

BMT are already supported (UserTransaction)

What we are discussing

Page 57: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

57 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Other Common Enterprise Services

Asynchronous invocations– Are they useful to be standardized for all Managed Beans?– How to reconcile differences

Timer Service?– ScheduleExpression– @Schedule

@Lock?

What we are discussing

Page 58: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

58 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Proposing Optional?

2.x Home and component interfaces?– EJBHome/EJBLocalHome/EJBObject/EJBLocalOb ject

CORBA interoperability?

Can become optional in Java EE 8

What we are discussing

Page 59: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

59 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Page 60: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

60 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

EJB 3.2

Find info: http://java.net/projects/ejb-spec– Subscribe, listen and discuss:

[email protected]– Browse email archives:

http://java.net/projects/ejb-spec/lists/jsr345-experts/archive– Read current version of the spec:

http://java.net/projects/ejb-spec/downloads– File issues or feature requests:

http://java.net/jira/browse/EJB_SPEC

Get Involved…

Page 61: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

61 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Q&A

Page 62: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

62 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Page 63: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

63 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

EJB: Simple, Light & Powerful backbone of Java EE

Classic Duke Future Tech Duke

Page 64: EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

64 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.