Mais conteúdo relacionado
Semelhante a EJB 3.2 - Java EE 7 - Java One Hyderabad 2012 (20)
Mais de Jagadish Prasath (8)
EJB 3.2 - Java EE 7 - Java One Hyderabad 2012
- 1. 1 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 2. EJB: Simple, Light & Powerful backbone of
Java EE
Jagadish Ramu
2 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 3. 3 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
San Francisco
September 30–October 4, 2012
- 4. 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.
4 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 5. Program Agenda
• EJB – Evolution
• EJB 3.0
• EJB 3.1 – Features
• EJB 3.2 – In progress
• Q & A
5 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 6. EJB 1.0 – EJB 2.1
Very powerful, but too complex
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
6 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 7. EJB 3.0
Major changes: Ease of Development
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
7 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 8. EJB 3.0
Major changes: Ease of Development (Cont)
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
8 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 9. EJB 3.1
Ease of Use Improvements
• Optional Local Business Interfaces
• Simplified Packaging
• Portable JNDI Names
9 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 10. Session Bean - Local Business Interface
10 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 11. Session Bean with “No-interface” View
@Stateless
public class HelloBean {
public String sayHello(String msg) {
return “Hello “ + msg;
}
}
@EJB
HelloBean h;
...
h.sayHello(“bob”);
11 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 12. EJB 3.1
Ease of Use Improvements
• Optional Local Business Interfaces
• Simplified Packaging
• Portable JNDI Names
12 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 13. JavaTM EE Platform 5 Packaging
foo.ear
foo_web.war
WEB-INF/web.xml
WEB-INF/classes/
com/acme/FooServlet.class
WEB-INF/classes
com/acme/Foo.class
foo_ejb.jar
com/acme/FooBean.class
com/acme/Foo.class
13 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
foo.ear
lib/foo_common.jar
com/acme/Foo.class
foo_web.war
WEB-INF/web.xml
WEB-INF/classes/
com/acme/FooServlet.class
foo_ejb.jar
com/acme/FooBean.class
OR
- 14. EJB 3.1 Simplified Packaging
14 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
foo.war
WEB-INF/classes/com/acme/
FooServlet.class
FooBean.class
- 15. EJB 3.1
Ease of Use Improvements
• Optional Local Business Interfaces
• Simplified Packaging
• Portable JNDI Names
15 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 16. EJB 3.1
Portable Global JNDI Name Syntax
Only within EAR
Base name of EAR
(or application.xml)
java:global[/<appname>]/<
modulename>/<
beanname>
[!<fullyqualifiedinterfacename>]
16 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
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. Portable JNDI Name: Sample
package com.acme;
@Stateless
public 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
17 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
implements Hello
If deployed as hello.jar, JNDI entries are:
- 18. Session Bean lookup
// session bean lookup from a Module
InitialContext ic = new InitialContext();
HelloBean hello = (HelloBean)
ic.lookup(“java:module/HelloBean”);
hello.sayHello(“bob”);
// Global session bean lookup
InitialContext ic = new InitialContext();
Hello hello = (Hello)
ic.lookup(“java:global/hello/HelloBean”);
hello.sayHello(“bob”);
18 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 19. New Features
• Singletons
• Startup / Shutdown callbacks
• Asynchronous session bean invocations
• Calendar-based timers / Automatic timer creation
• EJB 3.1 Lite
• Embeddable API
19 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 20. 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.
20 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 21. Simple Singleton
@Singleton
public class SharedBean {
private SharedData data = new SharedData();
public int getIntData() {
return data.getIntValue();
}
public void setIntValue(int value) {
data.setIntValue(value);
}
}
21 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 22. 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);
}
}
22 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 23. 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(...);
}
}
23 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 24. New Features
• Singletons
• Startup / Shutdown callbacks
• Asynchronous session bean invocations
• Calendar-based timers / Automatic timer creation
• EJB 3.1 Lite
• Embeddable API
24 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 25. Startup / Shutdown Callbacks
@Singleton
@Startup
@DependsOn(“InitializationBean”)
public class StartupBean {
@PostConstruct
private void onStartup() {
// Create a new EJB Timer
}
@PreDestroy
private void onShutdown() { ... }
}
25 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 26. New Features
• Singletons
• Startup / Shutdown callbacks
• Asynchronous session bean invocations
• Calendar-based timers / Automatic timer creation
• EJB 3.1 Lite
• Embeddable API
26 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 27. 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
27 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
delivery guarantees are not required by spec
- 28. Async Computation Example
@Stateless
@Asynchronous
public 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);
}
}
28 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 29. 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()
29 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
returns same exception as would have been thrown by a synchronous
invocation
- 30. New Features
• Singletons
• Startup / Shutdown callbacks
• Asynchronous session bean invocations
• Calendar-based timers / Automatic timer creation
• EJB “Lite”
• Embeddable API
30 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 31. Timer Service Features
• Calendar-based timeouts
• Automatic timer creation
• Non-persistent Timers
31 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 32. EJB 3.1 - Timers
• Calendar-based Timers – cron like semantics
• Every Mon & Wed midnight @Schedule(dayOfWeek=”Mon,Wed”)
• 2pm on Last Thur of Nov of every year
(hour=”14”, dayOfMonth=”Last Thu”, month=”Nov”)
• Every 5 minutes of every hour
(minute=”*/5”, hour=”*”)
• Every 10 seconds starting at 30
(second=”30/10”)
• Every 14th minute within the hour, for the hours 1 and 2 am
(minute=”*/14”, hour=”1,2”)
32 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 33. Calendar Based Expressions
• Numeric attributes
second,
33 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
minute
․Allowable values : [0,59]
․Default : “0”
hour
․Allowable values : [0,23]
․Default : “0”
year
․Allowable values : four-digit value
․Default : “*”
- 34. Calendar Based Expressions
• Other attributes
dayOfWeek
․Allowable values :
[0,7] or {“Sun”, ..., “Sat”}
․Default : “*”
month
․Allowable values :
[1,12] or {“Jan”, ..., “Dec”}
․Default : “*”
34 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 35. 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 : “*”
35 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 36. 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”
36 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 37. Non-persistent Timers
@Singleton
public class CacheBean {
private Cache cache;
@Schedule(minute=”*/5”, hour=”*”,
37 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
persistent=false)
@Lock(WRITE)
private void refresh() { ... }
@Lock(READ)
public String getXYZ() { ... }
}
- 38. New Features
• Singletons
• Startup / Shutdown callbacks
• Asynchronous session bean invocations
• Calendar-based timers / Automatic timer creation
• EJB “Lite”
• Embeddable API
38 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 39. 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
39 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 40. EJB Lite – Feature comparison
40 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 41. New Features
• Singletons
• Startup / Shutdown callbacks
• Asynchronous session bean invocations
• Calendar-based timers / Automatic timer creation
• EJB “Lite”
• Embeddable API
41 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 42. 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) {
...
}
42 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 43. Example: Test Client Execution
% java -classpath bankClient.jar :
43 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
bank.jar :
javaee.jar :
<vendor_rt>.jar
com.acme.BankTester
- 44. 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();
}
44 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 45. 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
45 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 46. Discussions in EJB 3.2 Expert Group
What are we discussing?
Cleanup
Optional features (pruning)
New features
Separating components and services
Further pruning
46 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 47. Optional Features
What we are discussing
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
47 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 48. New SFSB
What we are discussing
Support for transactions in life-cycle callbacks
PostConstruct/PreDestroy
PrePassivate/PostActivate
48 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 49. EJB 3.2 Lite Requirements
What we are discussing
Non-persistent timers
Asynchronous invocations
49 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 50. New in Asynchronous Invocations
What we are discussing
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?
50 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 51. New in MDBs
What we are discussing
How to simplify MDB?
Some requirements from JMS 2.0 Expert Group
– Synchronizing XML with annotations
– Support for simplified API
51 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 52. MDB
Current usage
@MessageDriven(mappedName="jms/ejb_mdb_Queue”)
public class SimpleMDB implements MessageListener {
public void onMessage(Message message) {
…
}
}
52 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 53. MDB – What if?
One of many options
@MessageDriven
public class SimpleMDB {
@TBDAnnotation(tbdAttr="jms/ejb_mdb_Queue”)
public void processMessages(Message message) {
…
}
}
53 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 54. New in Configuration
What we are discussing
QoS annotations or attributes
– @MaxConcurrency ?
• Number of concurrent Async methods that could be served
– @PoolSize ?
• Bean pool size
54 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 55. Common Enterprise Services
What we are discussing
Services available for all (most?) Managed Beans
By default available for EJBs
Can be opt-in in other Managed Beans
55 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 56. Transactions as Common Enterprise Services
What we are discussing
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)
56 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 57. Other Common Enterprise Services
What we are discussing
Asynchronous invocations
– Are they useful to be standardized for all Managed Beans?
– How to reconcile differences
Timer Service?
– ScheduleExpression
– @Schedule
@Lock?
57 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 58. Proposing Optional?
What we are discussing
2.x Home and component interfaces?
– EJBHome/EJBLocalHome/EJBObject/EJBLocalOb ject
CORBA interoperability?
Can become optional in Java EE 8
58 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 59. 59 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 60. EJB 3.2
Get Involved…
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
60 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 61. 61 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Q&A
- 62. 62 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 63. EJB: Simple, Light & Powerful backbone of
Java EE
Classic Duke Future Tech Duke
63 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
- 64. 64 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.