26
<Insert Picture Here> What's New in Enterprise JavaBean Technology Arun Gupta, Java EE & GlassFish Guy blogs.sun.com/arungupta, @arungupta

S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010

Embed Size (px)

DESCRIPTION

S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010

Citation preview

Page 1: S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010

<Insert Picture Here>

What's New in Enterprise JavaBean TechnologyArun Gupta, Java EE & GlassFish Guyblogs.sun.com/arungupta, @arungupta

Page 2: S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010

2

Beijing 2010

December 13–16, 2010

Page 3: S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010

3

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 4: S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010

4

<Insert Picture Here>

Agenda

• Introduction• Ease of Use Improvements• New Functionality

Page 5: S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010

5

EJB 3.1 Specification

• Goals– Continued focus on ease-of-use– New features

• JSR (Java Specification Request) 318– Expert Group Formed – August 2007– Public Draft – October 2008– Proposed Final Draft – March 2009– Final Specification – December 2009

Page 6: S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010

6

Ease of Use Improvements

• Optional Local Business Interfaces• Simplified Packaging• EJB 3.1 “Lite” API• Portable Global JNDI Names• Simple Component Testing

Page 7: S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010

7

Session BeanWith “No-interface” View

@Statelesspublic class HelloBean {

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

Page 8: S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010

8

No-interface View Client

@EJB HelloBean h;

...

h.sayHello(“bob”);

Page 9: S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010

9

JavaTM EE Platform 5 Packaging

foo.ear

WEB-INF/web.xml

WEB-INF/classes/

com/acme/FooServlet.class

WEB-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.xml

WEB-INF/classes/

com/acme/FooServlet.class

foo_web.war

com/acme/FooBean.class

foo_ejb.jar

OR

Page 10: S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010

10

foo.warWEB-INF/classes/com/acme/

FooServlet.class

FooBean.class

Simplified Packaging

Page 11: S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010

11

EJB 3.1 “Lite”

Page 12: S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010

12

Portable EJB JNDI Names

Each session bean gets the following entries :

• Globally unique namejava:global[/<app-name>]/<module-name>/<ejb-name>

• Unique name within same application java:app/<module-name>/<ejb-name>

• Unique name within defining modulejava:module/<ejb-name>

Page 13: S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010

13

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/HelloBeanjava:app/hello/HelloBeanjava:module/HelloBean

Page 14: S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010

14

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)

{ … }

Page 15: S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010

15

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

Page 16: S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010

16

Test Client Execution

% java -classpath bankClient.jar :

bank.jar :

javaee.jar :

<vendor_rt>.jar

com.acme.BankTester

Page 17: S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010

17

New Features

• Singletons• Startup / Shutdown callbacks• Automatic timer creation

– Cron-like syntax

• Asynchronous session bean invocations

Page 18: S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010

18

Singleton

@Singleton

public class SharedBean {

private SharedData shared;

@PostConstruct

private void init() {

shared = ...;

}

public int getXYZ() {

return shared.xyz;

}

Page 19: S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010

19

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

Page 20: S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010

20

Startup / Shutdown Callbacks

@Singleton

@Startup

public class StartupBean {

@PostConstruct

private void onStartup() { … }

@PreDestroy

private void onShutdown() { … }

}

Page 21: S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010

21

Timers

• Automatically created EJB 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 22: S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010

22

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

...

}

}

Page 23: S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010

23

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

Page 24: S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010

24

Asynchronous Session BeansCode Sample

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

http://blogs.sun.com/arungupta/entry/totd_137_asynchronous_ejb_a

Page 25: S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010

25

References

• glassfish.org• blogs.sun.com/theaquarium• youtube.com/user/GlassFishVideos• facebook.com/glassfish• Follow @glassfish

Page 26: S314168 - What's New in Enterprise Java Bean Technology @ JavaOne Brazil 2010

<Insert Picture Here>

What's New in Enterprise JavaBean Technology

Arun Gupta, Java EE & GlassFish Guyblogs.sun.com/arungupta, @arungupta