1
<Insert Picture Here>The Java EE 6 Programming Model Explained:How to Write Better ApplicationsJerome DochezGlassFish Arch...
The following is intended to outline our generalproduct direction. It is intended for informationpurposes only, and may no...
Contents• Whats new in Java EE 6?• The core programming model explained• Features you should know about                   ...
Whats New in Java EE 6                          5
Whats new in the Java EE 6 PlatformNew and updated components•   EJB 3.1 (+Lite)          •   Managed Beans 1.0•   JPA 2.0...
Java EE 6 Web ProfileNew, updated and unchanged components•   EJB 3.1 Lite            •   Managed Beans 1.0•   JPA 2.0    ...
Whats New?•   Several new APIs•   Web Profile•   Pluggability/extensibility•   Dependency injection•   Lots of improvement...
The Core Programming Model Explained                                   9
Core of the Java EE 6 Programming Model  JAX-RS 1.1         JSF 2.0             JSP 2.2    B                              ...
EJB 3.1 Lite• Session bean programming model  – Stateful, stateless, singleton beans• Declarative security and transaction...
Managed Beans•   Plain Java objects (POJOs)•   Foundation for other component types•   Fully support injection (@Resource,...
Sample Managed Beans@ManagedBean            @ManagedBean(“foo”)public class A {        public class B {    @Resource      ...
EJB Components are Managed Beans Too!• All managed beans core services available in EJBs• EJB component model extends mana...
Incremental Programming Model•   Start with POJOs (@ManagedBean)•   Use all the basic services•   Turn them into session b...
Incremental Programming Model•   Start with POJOs (@ManagedBean)•   Use all the basic services•   Turn them into session b...
Incremental Programming Model•   Start with POJOs (@ManagedBean)•   Use all the basic services•   Turn them into session b...
Contexts and Dependency Injection (CDI)• Managed beans on steroids• Opt-in technology on a per-module basis  – META-INF/be...
Contexts and Dependency Injection (CDI)• Managed beans on steroids• Opt-in technology on a per-module basis  – META-INF/be...
Contexts and Dependency Injection (CDI)• Managed beans on steroids• Opt-in technology on a per-module basis  – META-INF/be...
APIs That Work Better TogetherExample: Bean Validation• Bean Validation integrated in JSF, JPA• All fields on a JSF page v...
UniformityCombinations of annotations “just work”• Example: JAX-RS resource classes are POJOs• Use JAX-RS-specific injecti...
JAX-RS Sample    Using JAX-RS injection annotations@Path(“root”)public class RootResource {    @Context UriInfo ui;    pub...
JAX-RS Sample    As a managed bean, using @Resource@Path(“root”)@ManagedBeanpublic class RootResource {    @Context UriInf...
JAX-RS Sample    As an EJB, using @Resource and security annotations@Path(“root”)@Statelesspublic class RootResource {    ...
JAX-RS Sample    As a CDI bean, using @Inject and scope annotations@Path(“root”)@ApplicationScopedpublic class RootResourc...
How to Obtain UniformityCombinations of annotations “just work”• Annotations are additive• New behaviors refine pre-existi...
Extensibility• Level playing field for third-party frameworks• Pluggability contracts make extensions look like built-  in...
Servlet 3.0 Pluggability• Drag-and-drop model• Web frameworks as fully configured libraries  – contain “fragments” of web....
Extensibility in CDI• Drag-and-drop model here too• Bean archives can contain reusable sets of beans• Extensions can proce...
Using Extensibility• Look for ready-to-use frameworks, extensions• Refactor your libraries into reusable, autoconfigured  ...
Extensive Tooling from Schema to GUINetBeans 6.9•   Database tables to entity classes•   Entity classes to session beans• ...
Features You Should Know About                                 33
Interceptors and Managed Beans• Interceptors can be engaged directly@Interceptors(Logger.class)@ManagedBeanpublic class A ...
Interceptor Bindings• With CDI, you can use interceptor bindings  @Logged  @ManagedBean  public class A { ... }where  @Int...
Interceptors vs Decorators• Decorators are type-safe• Decorators are bound unobtrusively   @Decorator   class ActionLogger...
Two Ways to Connect Beans in CDI• Dependencies (@Inject)• Strongly coupled• Clients hold references to beans@Inject @Relia...
Two Ways to Connect Beans in CDI• Dependencies (@Inject)• Strongly coupled• Clients hold references to beans@Inject @Relia...
Events are Transaction-Aware• Events queued and delivered near at boundaries• Specify a TransactionPhase  –   BEFORE_COMPL...
Global JNDI Names• Standard names to refer to remote beans in other  applications• java:global/application/module/bean!int...
What is java:global?• New JNDI namespaces to match the packaging hierarchy   java:global   java:app   java:module   java:c...
DataSource Definitions• New annotation @DataSourceDefinition• Define a data source once for the entire app  @DataSourceDef...
Using Producer Fields for Resources• Expose container resources via CDI  public MyResources {    @Produces    @Resource(lo...
Using Producer Fields for Resources• Expose container resources via CDI  public MyResources {    @Produces    @Resource(lo...
The Verdict              45
A Better Platform•   Less boilerplate code than ever•   Fewer packaging headaches•   Uniform use of annotations•   Sharing...
We encourage you to use the newly minted corporate tagline“Software. Hardware. Complete.” at the end of all your presentat...
Upcoming SlideShare
Loading in …5
×

S313557 java ee_programming_model_explained_dochez

947 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
947
On SlideShare
0
From Embeds
0
Number of Embeds
109
Actions
Shares
0
Downloads
28
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

S313557 java ee_programming_model_explained_dochez

  1. 1. 1
  2. 2. <Insert Picture Here>The Java EE 6 Programming Model Explained:How to Write Better ApplicationsJerome DochezGlassFish ArchitectOracle
  3. 3. The following is intended to outline our generalproduct direction. It is intended for informationpurposes only, and may not be incorporated into anycontract. It is not a commitment to deliver anymaterial, code, or functionality, and should not berelied upon in making purchasing decisions.The development, release, and timing of anyfeatures or functionality described for Oracle’sproducts remains at the sole discretion of Oracle. 3
  4. 4. Contents• Whats new in Java EE 6?• The core programming model explained• Features you should know about 4
  5. 5. Whats New in Java EE 6 5
  6. 6. Whats new in the Java EE 6 PlatformNew and updated components• EJB 3.1 (+Lite) • Managed Beans 1.0• JPA 2.0 • Interceptors 1.1• Servlet 3.0 • JSP 2.2• JSF 2.0 • EL 2.2• JAX-RS 1.1 • JSR-250 1.1• Bean Validation 1.0 • JASPIC 1.1• DI 1.0 • JACC 1.5• CDI 1.0 • JAX-WS 2.2• Connectors 1.6 • JSR-109 1.3 6
  7. 7. Java EE 6 Web ProfileNew, updated and unchanged components• EJB 3.1 Lite • Managed Beans 1.0• JPA 2.0 • Interceptors 1.1• Servlet 3.0 • JSP 2.2• JSF 2.0 • EL 2.2 • JSR-250 1.1• Bean Validation 1.0• DI 1.0 • JSR-45 1.0• CDI 1.0 • JSTL 1.2 • JTA 1.1 7
  8. 8. Whats New?• Several new APIs• Web Profile• Pluggability/extensibility• Dependency injection• Lots of improvements to existing APIs 8
  9. 9. The Core Programming Model Explained 9
  10. 10. Core of the Java EE 6 Programming Model JAX-RS 1.1 JSF 2.0 JSP 2.2 B E JSTL 1.2 A N SERVLET 3.0 · EL 2.2 V A L I DI 1.0 · CDI 1.0 · INTERCEPTORS 1.1· JSR-250 1.1 D A T MANAGED BEANS 1.0 EJB 3.1 I O N JPA 2.0 · JTA 1.1 1.0 10
  11. 11. EJB 3.1 Lite• Session bean programming model – Stateful, stateless, singleton beans• Declarative security and transactions• Annotation-based• Descriptor OK but optional 11
  12. 12. Managed Beans• Plain Java objects (POJOs)• Foundation for other component types• Fully support injection (@Resource, @Inject)• @PostConstruct, @PreDestroy callbacks• Optional name• Interceptors 12
  13. 13. Sample Managed Beans@ManagedBean @ManagedBean(“foo”)public class A { public class B { @Resource @Resource A a; DataSource myDB; @PostConstruct public doWork() { init() { ... } // ... } }} 13
  14. 14. EJB Components are Managed Beans Too!• All managed beans core services available in EJBs• EJB component model extends managed beans• New capabilities engaged on demand via annotations – @Statefull, @Stateless, @Singleton – @TransactionAttribute – @RolesAllowed, @DenyAll, @PermitAll – @Remote – @WebService 14
  15. 15. Incremental Programming Model• Start with POJOs (@ManagedBean)• Use all the basic services• Turn them into session beans as needed• Take advantage of new capabilities• No changes to (local) clients 15
  16. 16. Incremental Programming Model• Start with POJOs (@ManagedBean)• Use all the basic services• Turn them into session beans as needed• Take advantage of new capabilities• No changes to (local) clients@ManagedBeanpublic class A { @Resource DataSource myDB; // ...} 16
  17. 17. Incremental Programming Model• Start with POJOs (@ManagedBean)• Use all the basic services• Turn them into session beans as needed• Take advantage of new capabilities• No changes to (local) clients@Statefulpublic class A { @Resource DataSource myDB; // ...} 17
  18. 18. Contexts and Dependency Injection (CDI)• Managed beans on steroids• Opt-in technology on a per-module basis – META-INF/beans.xml – WEB-INF/beans.xml• @Resource only for container-provided objects• Use @Inject for application classes@Inject @LoggedIn User user; 18
  19. 19. Contexts and Dependency Injection (CDI)• Managed beans on steroids• Opt-in technology on a per-module basis – META-INF/beans.xml – WEB-INF/beans.xml• @Resource only for container-provided objects• Use @Inject for application classes@Inject @LoggedIn User user;The type describes the capabilities (= methods) 19
  20. 20. Contexts and Dependency Injection (CDI)• Managed beans on steroids• Opt-in technology on a per-module basis – META-INF/beans.xml – WEB-INF/beans.xml• @Resource only for container-provided objects• Use @Inject for application classes@Inject @LoggedIn User user;Qualifiers describe qualities/characteristics/identity. 20
  21. 21. APIs That Work Better TogetherExample: Bean Validation• Bean Validation integrated in JSF, JPA• All fields on a JSF page validated during the “process validations” stage• JPA entities validated at certain lifecycle points (pre- persist, post-update, pre-remove)• Low-level validation API supports equivalent integration with other frameworks/APIs 21
  22. 22. UniformityCombinations of annotations “just work”• Example: JAX-RS resource classes are POJOs• Use JAX-RS-specific injection capabilities• But you can turn resource classes into managed beans or EJB components...• ... and use all new services! 22
  23. 23. JAX-RS Sample Using JAX-RS injection annotations@Path(“root”)public class RootResource { @Context UriInfo ui; public RootResource() { ... } @Path(“{id}”) public SubResource find(@PathParam(“id”) String id) { return new SubResource(id); }} 23
  24. 24. JAX-RS Sample As a managed bean, using @Resource@Path(“root”)@ManagedBeanpublic class RootResource { @Context UriInfo ui; @Resource DataSource myDB; public RootResource() { ... } @Path(“{id}”) public SubResource find(@PathParam(“id”) String id) { return new SubResource(id); }} 24
  25. 25. JAX-RS Sample As an EJB, using @Resource and security annotations@Path(“root”)@Statelesspublic class RootResource { @Context UriInfo ui; @Resource DataSource myDB; public RootResource() { ... } @Path(“{id}”) @RolesAllowed(“manager”) public SubResource find(@PathParam(“id”) String id) { return new SubResource(id); }} 25
  26. 26. JAX-RS Sample As a CDI bean, using @Inject and scope annotations@Path(“root”)@ApplicationScopedpublic class RootResource { @Context UriInfo ui; @Inject @CustomerDB DataSource myDB; @Inject @LoggedIn User user; public RootResource() { ... } @Path(“{id}”) public SubResource find(@PathParam(“id”) String id) { // ... }} 26
  27. 27. How to Obtain UniformityCombinations of annotations “just work”• Annotations are additive• New behaviors refine pre-existing ones• Strong guidance for new APIs ➱ Classes can evolve smoothly 27
  28. 28. Extensibility• Level playing field for third-party frameworks• Pluggability contracts make extensions look like built- in technologies• Two main sets of extension points: – Servlet 3.0 pluggability – CDI extensions 28
  29. 29. Servlet 3.0 Pluggability• Drag-and-drop model• Web frameworks as fully configured libraries – contain “fragments” of web.xml – META-INF/web-fragment.xml• Extensions can register listeners, servlets, filters dynamically• Extensions can discover and process annotated classes@HandlesTypes(WebService.class)public class MyWebServiceExtension implements ServletContanerInitializer { ... } 29
  30. 30. Extensibility in CDI• Drag-and-drop model here too• Bean archives can contain reusable sets of beans• Extensions can process any annotated types and define beans on-the-fly• Clients only need to use @Inject – No XYZFactoryManagerFactory horrors!@Inject @Reliable PayBy(CREDIT_CARD) PaymentProcessor; 30
  31. 31. Using Extensibility• Look for ready-to-use frameworks, extensions• Refactor your libraries into reusable, autoconfigured frameworks• Take advantage of resource jars• Package reusable beans into libraries 31
  32. 32. Extensive Tooling from Schema to GUINetBeans 6.9• Database tables to entity classes• Entity classes to session beans• Entity classes to JAX-RS resources• Entity classes to JSF front-end• JAX-RS resources to JavaScript client 32
  33. 33. Features You Should Know About 33
  34. 34. Interceptors and Managed Beans• Interceptors can be engaged directly@Interceptors(Logger.class)@ManagedBeanpublic class A { ... }• This kind of strong coupling is a code smell 34
  35. 35. Interceptor Bindings• With CDI, you can use interceptor bindings @Logged @ManagedBean public class A { ... }where @InterceptorBinding public @interface Logged { ... }and @Interceptor @Logged public class Logger { @AroundInvoke void do(InvocationContext c) { ... } } 35
  36. 36. Interceptors vs Decorators• Decorators are type-safe• Decorators are bound unobtrusively @Decorator class ActionLogger { @Inject @Delegate Action action; Result execute(Data someData) { // ...log the action here... return action.execute(someData); } } 36
  37. 37. Two Ways to Connect Beans in CDI• Dependencies (@Inject)• Strongly coupled• Clients hold references to beans@Inject @Reliable OrderProcessor; 37
  38. 38. Two Ways to Connect Beans in CDI• Dependencies (@Inject)• Strongly coupled• Clients hold references to beans@Inject @Reliable OrderProcessor;• Events (@Observes)• Loosely coupled• Observers dont hold references to event sourcespublic voidonProcessed(@Observes OrderProcessedEvent e) { .. . } 38
  39. 39. Events are Transaction-Aware• Events queued and delivered near at boundaries• Specify a TransactionPhase – BEFORE_COMPLETION – IN_PROGRESS – AFTER_COMPLETION – AFTER_FAILURE, AFTER_SUCCESS• Supersedes TransactionSynchronizationRegistry API• Mix transactional and non-transactional observerspublic voidonProcessed(@Observes(during=AFTER_SUCCESS) OrderProcessedEvent e) { .. . } 39
  40. 40. Global JNDI Names• Standard names to refer to remote beans in other applications• java:global/application/module/bean!interface• Also used to lookup EJB components within the standalone container API (EJBContainer) 40
  41. 41. What is java:global?• New JNDI namespaces to match the packaging hierarchy java:global java:app java:module java:comp (legacy semantics)• Share resources as widely as possible java:app/env/customerDB• Refer to resources from any module in the application @Resource(lookup=”java:app/env/customerDB”) DataSource db; 41
  42. 42. DataSource Definitions• New annotation @DataSourceDefinition• Define a data source once for the entire app @DataSourceDefinition( name="java:app/env/customerDB", className="com.acme.ACMEDataSource", portNumber=666, serverName="acmeStore.corp”) @Singleton public class MyBean {} 42
  43. 43. Using Producer Fields for Resources• Expose container resources via CDI public MyResources { @Produces @Resource(lookup=”java:app/env/customerDB”) @CustomerDB DataSource ds; @Produces @PersistenceContext(unitName=”payrollDB”) @Payroll EntityManager em; } 43
  44. 44. Using Producer Fields for Resources• Expose container resources via CDI public MyResources { @Produces @Resource(lookup=”java:app/env/customerDB”) @CustomerDB DataSource ds; @Produces @PersistenceContext(unitName=”payrollDB”) @Payroll EntityManager em; }• Clients use @Inject@Inject @CustomerDB DataSource customers;@Inject @Payroll EntityManager payroll; 44
  45. 45. The Verdict 45
  46. 46. A Better Platform• Less boilerplate code than ever• Fewer packaging headaches• Uniform use of annotations• Sharing of resources to avoid duplication• Transaction-aware observer pattern for beans• Strongly-typed decorators• ... and more! http://www.oracle.com/javaee 46
  47. 47. We encourage you to use the newly minted corporate tagline“Software. Hardware. Complete.” at the end of all your presentations.This message should replace any reference to our previous corporatetagline “Oracle Is the Information Company.” 47

×