1. <Insert Picture Here>
What's New in Enterprise JavaBean Technology ?
Sanjeeb Sahoo
Sr. Staff Engineer, Sun, an Oracle Company
2. The following/preceding 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.
2
3. Agenda
• Introduction <Insert Picture Here>
• Ease of Use Improvements
• New Functionality
3
4. EJB 3.0 Specification
• Goal
• Ease of use
• Developer productivity
• JSR (Java Specification Request) 220
• Final Specification – 2006
4
5. EJB 3.0 Specification
• Metadata via Java annotations
• Only annotations
• Only XML DD
• Both
• Sensible defaults
• POJO friendly
• Simplification of bean types
• No home interface
• Interceptors
• Entity Beans replaced by JPA
5
6. EJB 3.1 Specification
• Goals
• Continued focus on ease-of-use
• New features
• JSR (Java Specification Request) 318
• Final Specification – December 2009
6
7. Ease of Use Improvements
• Optional Local Business Interfaces
• Simplified Packaging
• EJB 3.1 “Lite” API
• Portable Global JNDI Names
• Simple Component Testing
7
8. Session Bean
With “No-interface” View
@Stateless
public class GreetingBean {
public String greet(String msg) {
return “Hello “ + msg;
}
}
8
15. Portable EJB JNDI Names
Each session bean gets the following entries :
• Globally unique name
java:global[/<app-name>]/<module-name>/<ejb-
name>
• Unique name within same application
java:app/<module-name>/<ejb-name>
• Unique name within defining module
java:module/<ejb-name>
15
16. Session Bean
@Stateless
public class HelloBean implements Hello {
public String sayHello(String msg) {
return “Hello “ + msg;
}
}
If deployed as hello.jar, JNDI entries are:
java:global/hello/HelloBean
java:app/hello/HelloBean
java:module/HelloBean
16
17. Simple Testing : Session Bean
@Stateless
@Local(Bank.class)
public class BankBean implements Bank {
@PersistenceContext EntityManager accountDB;
public String createAccount(AcctDetails d)
{ … }
public void removeAccount(String acctID)
{ … }
17
18. 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”);
testAccountCreation(bank);
container.close();
18
22. Singleton Concurrency Options
• Single threaded (default)
• For consistency with all existing bean types
• Container Managed Concurrency
• Control access via method-level locking metadata
• Bean Managed Concurrency
• All concurrent invocations have access to bean instance
22
24. Timers
• Automatically created EJB 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”)
24
25. Automatic Timer Creation
@Stateless
public class BankBean {
@PersistenceContext EntityManager accountDB;
@Resource javax.mail.Session mailSession;
// Callback the last day of each month at 8 a.m.
@Schedule(hour=”8”, dayOfMonth=”Last”)
void sendMonthlyBankStatements() {
...
}
}
25
26. Asynchronous Session Beans
• Control returns to the client before the container
dispatches invocation to a bean instance
• @Asynchronous – method or class
• Return type – void or Future<V>
• “Fire and and Forget” or async results via
Future<V>
• Best effort delivery – persistent delivery
guarantees are not required by spec
• Transaction context does not propagate
• REQUIRED → REQUIRED_NEW
• Security principal propagates
26
27. Asynchronous Session Beans
Code Sample
@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);
}
}
http://blogs.sun.com/arungupta/entry/totd_137_asynchronous_ejb_a
27