EJB 3.2 - Java EE 7 - Java One Hyderabad 2012

Post on 19-Jun-2015

263 views 5 download

Tags:

description

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

transcript

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

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

EJB: Simple, Light & Powerful backbone of Java EE

Jagadish Ramu

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

San Francisco September 30–October 4, 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.

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

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

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

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)

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

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

Session Bean - Local Business Interface

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”);

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

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

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

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

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

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:

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”);

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

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.

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); }

}

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); }}

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(...); } }

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

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() { ... }

}

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

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

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

}}

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

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

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

Timer Service Features

• Calendar-based timeouts • Automatic timer creation

• Non-persistent Timers

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

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 : “*”

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 : “*”

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 : “*”

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”

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() { ... } }

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

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

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

EJB Lite – Feature comparison

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

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

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

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(); }

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

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?

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

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

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

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

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

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

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

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

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

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

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

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

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

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:

users@ejb-spec.java.net– 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…

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

Q&A

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

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

EJB: Simple, Light & Powerful backbone of Java EE

Classic Duke Future Tech Duke

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