Java EE 6 - Deep Dive  Jagadish Ramu Sun Microsystems
<ul>The following/preceding is intended to outline our general product direction. It is intended for information purposes ...
Compatible Java EE 6 Impls Today: Announced:
Goals for the Java EE 6 Platform <ul><li>Flexible & Light-weight </li><ul><li>Web Profile 1.0
Pruning: JAX-RPC, EJB 2.x Entity Beans, JAXR, JSR 88 </li></ul><li>Extensible </li></ul><ul><ul><ul><li>Embrace Open Sourc...
Java EE 6 Web Profile 1.0 <ul><li>Fully functional mid-sized profile </li></ul><ul><ul><li>Actively discussed in the Java ...
Technologies </li><ul><li>Servlets 3.0, JSP 2.2, EL 2.2, Debugging Support for Other Languages 1.0, JSTL 1.2, JSF 2.0, Com...
Java EE 6 - Done <ul><li>Specifications approved by the JCP
Reference Implementation is GlassFish v3
TCK </li></ul>Dec 2009
Java EE 6 Specifications <ul><li>The Platform
Java EE 6 Web Profile 1.0
Managed Beans 1.0 </li></ul>
Java EE 6 Specifications New <ul><li>Contexts and Dependency Injection for  Java EE (JSR 299)
Bean Validation 1.0 (JSR 303)
Java API for RESTful Web Services (JSR 311)
Dependency Injection for Java (JSR 330) </li></ul>
Java EE 6 Specifications Extreme Makeover <ul><li>Java Server Faces 2.0 (JSR 314)
Java Servlets 3.0 (JSR 315)
Java Persistence 2.0 (JSR 317)
Enterprise Java Beans 3.1 & Interceptors 1.1 (JSR 318)
Java EE Connector Architecture 1.6 (JSR 322) </li></ul>
Java EE 6 Specifications Updates <ul><li>Java API for XML-based Web Services 2.2 (JSR 224)
Java API for XML Binding 2.2 (JSR 222)
Web Services Metadata MR3 (JSR 181)
JSP 2.2/EL 2.2 (JSR 245)
Web Services for Java EE 1.3 (JSR 109)
Common Annotations 1.1 (JSR 250)
Java Authorization Contract for Containers 1.3 (JSR 115)
Java Authentication Service Provider Interface for Containers 1.0 (JSR 196) </li></ul>
Servlets in Java EE 5 At least 2 files <!--Deployment descriptor web.xml --> <web-app> <servlet>   <servlet-name>MyServlet...
...
}
...
} </li></ul>
Servlets 3.0 (JSR 315) Annotations-based @WebServlet http://blogs.sun.com/arungupta/entry/totd_81_getting_started_with pac...
Servlets 3.0 Annotations-based @WebServlet @WebServlet(name=&quot;mytest&quot;,  urlPatterns={&quot;/myurl&quot;},  initPa...
Servlets 3.0 Annotations-based @WebListeners <listener> <listener-class>   server.LoginServletListener   </listener-class>...
Servlets 3.0 Annotations-based @WebFilter <filter> <filter-name>PaymentFilter</filter-name> <filter-class>server.PaymentFi...
Servlets 3.0 Asynchronous Servlets <ul><li>Useful for Comet, long waits
Must declare @WebServlet(asyncSupported=true) </li></ul>AsyncContext context = request.startAsync(); context.addListener(n...
Servlets 3.0 Extensibility <ul><li>Plugin libraries using  web fragments </li><ul><li>Modular web.xml </li></ul><li>Bundle...
Zero-configuration, drag-and-drop for web frameworks </li></ul><ul><ul><li>Servlets, servlet filters, context listeners fo...
<web-fragment>   <filter>   <filter-name>wicket.helloworld</filter-name>   <filter-class>org.apache.wicket.protocol.http.W...
Servlets 3.0  Resource Sharing <ul><li>Static and JSP no longer confined to document root of the web application
May be placed in  WEB-INF/lib/[*.jar]/META-INF/resources
Resources in document root take precedence over those in bundled JAR </li></ul>myapp.war   WEB-INF/lib/catalog.jar   /META...
EJB 3.1 (JSR 318) Package & Deploy in a WAR web.xml ? Java EE 5 Java EE 6 http://blogs.sun.com/arungupta/entry/totd_95_ejb...
EJB 3.1 @Stateless public class App { public String sayHello(String name) { return &quot;Hello &quot; + name; } }
EJB 3.1 <ul><li>No interface view –  one  source file per bean </li><ul><li>Only for Local and within WAR
Required for Remote
No location transparency </li></ul><li>Component initialization in @PostConstruct </li><ul><li>No assumptions on no-arg ct...
Java EE 6 Namespaces <ul><li>Global </li><ul><li>java:global/..(for all applications) </li></ul><li>Application scoped </l...
App-1 binds an Object by name “java:app/myObject”, App-2 cannot see it.
All modules of the app has visibility. </li><ul><li>mod-1, mod-2 of app-1 can see “java:app/myObject” </li></ul></ul></ul>
Java EE 6 Namespaces <ul><li>Module Scoped </li><ul><li>java:module/ .. (visible only for the module)
App-1 has module-1 and module-2 </li><ul><li>Module-1's namespace not accessible by module-2 </li></ul><li>All components ...
EJB 3.1 Portable Global JNDI Name Syntax <ul><li>Portable
Global
Application/Module-scoped
Derived from metadata such as name, component name etc. </li></ul>
EJB 3.1 Portable Global JNDI Name Syntax java:global[/<app-name>]/<module-name>/<bean-name> [!<fully-quali fied-interface-...
Local & Remote business
No-interface
Also in java:app, java:module </li></ul>Only within EAR Base name of EAR (or application.xml) Base name of ejb-jar/WAR (or...
EJB 3.1 Portable Global JNDI Name Syntax <ul>package com.acme; @Stateless public class  FooBean  implements  Foo  { ... } ...
public void testEJB() throws NamingException { EJBContainer ejbC =   EJBContainer.createEJBContainer(); Context ctx = ejbC...
Screencast  JSP, Servlets, EJB
EJB 3.1 Singleton Beans <ul><li>One instance per app/VM, not pooled </li><ul><li>Useful for caching state
CMT/BMT
Access to container services for injection, resource manager, timers, startup/shutdown callbacks, etc.
Enable eager initialization using  @Startup
Always supports concurrent access
Define initialization ordering using  @DependsOn </li></ul></ul>@Singleton public class MyEJB {  . . . }
EJB 3.1 Asynchronous Session Beans <ul><li>Control returns to the client before the container dispatches invocation to a b...
@Asynchronous  – method or class
Return type – void or Future<V>
Transaction context does not propagate </li><ul><li>REQUIRED -> REQUIRED_NEW </li></ul><li>Security principal propagates <...
EJB 3.1 Asynchronous Session Beans – Code Sample @Stateless @Asynchronous public class SimpleAsyncEJB {   public  Future<I...
EJB 3.1 Timers <ul><li>Automatically created EJB Timers
Calendar-based Timers – cron like semantics </li></ul><ul><ul><li>Every 14 th  minute within the hour, for the hours 1 & 2...
EJB 3.1 Timers <ul><li>Single persistent timer across JVMs
Automatically created EJB Timers </li><ul><li>@Schedule(hour=”15”,dayOfWeek=”Fri”) </li></ul><li>Can be chained </li><ul><...
Non-persistent timer, e.g. Cache </li><ul><li>@Schedule(..., persistent=false) </li></ul></ul>
EJB 3.1 EJB 3.1 Lite – Feature Comparison
Managed Beans 1.0 @javax.annotation.ManagedBean EJB CDI JPA JAX-WS JAX-RS JSF ... @Stateful @Stateless @Singleton @Named @...
Managed Beans 1.0 <ul><li>POJO as managed component for the Java EE container </li><ul><li>JavaBeans component model for J...
Upcoming SlideShare
Loading in …5
×

Java EE 6 = Less Code + More Power (Tutorial) [5th IndicThreads Conference On Java, 2010, Pune, India]

5,380 views

Published on

Session Presented at 5th IndicThreads.com Conference On Java held on 10-11 December 2010 in Pune, India
WEB: http://J10.IndicThreads.com

------------
Java Platform, Enterprise Edition 6 (JavaEE 6) provides new capabilities that make it easier to develop and deploy enterprise and Web applications. It provides a simplified developer experience and improves on the developer productivity features introduced in JavaEE 5. It breaks the “one size fits all” approach in previous releases with Profiles and offers a comprehensive Web profile for lightweight, standards-based modern Web applications.The Web profile allows developers to build web applications quickly and prevents proliferation of custom web stacks for easier maintainability.

The platform enables extensibility by embracing open source libraries and frameworks such that they are treated as first class citizens of the platform. Several specifications like Contexts & Dependency Injection, Java Server Faces 2, Java API for RESTful Services, Java Persistence API 2, and Servlets 3 make the platform more powerful. All these specifications are implemented in GlassFish Open Source Edition 3 – a modular (OSGi based) light-weight, embeddable, extensible, and the open source reference implementation for Java EE 6. NetBeans, Eclipse, and IntelliJ provide extensive tooling for Java EE 6 and GlassFish Open Source Edition.

This tutorial session will help the attendees learn the latest APIs and develop a complete Java EE 6 application using NetBeans IDE. The attendees will understand the different tips & tricks such as code completion, templates, and wizards for a rapid application deployment. The techniques like session preservation and deploy-on-save are demonstrated to reduce the development lifecycle.

Published in: Technology
  • Be the first to comment

Java EE 6 = Less Code + More Power (Tutorial) [5th IndicThreads Conference On Java, 2010, Pune, India]

  1. 1. Java EE 6 - Deep Dive Jagadish Ramu Sun Microsystems
  2. 2. <ul>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. </ul>
  3. 3. Compatible Java EE 6 Impls Today: Announced:
  4. 4. Goals for the Java EE 6 Platform <ul><li>Flexible & Light-weight </li><ul><li>Web Profile 1.0
  5. 5. Pruning: JAX-RPC, EJB 2.x Entity Beans, JAXR, JSR 88 </li></ul><li>Extensible </li></ul><ul><ul><ul><li>Embrace Open Source Frameworks </li></ul></ul></ul><ul><li>Easier to use, develop on </li></ul><ul><ul><ul><li>Continue on path set by Java EE 5 </li></ul></ul></ul>
  6. 6. Java EE 6 Web Profile 1.0 <ul><li>Fully functional mid-sized profile </li></ul><ul><ul><li>Actively discussed in the Java EE 6 Expert Group and outside it
  7. 7. Technologies </li><ul><li>Servlets 3.0, JSP 2.2, EL 2.2, Debugging Support for Other Languages 1.0, JSTL 1.2, JSF 2.0, Common Annotations 1.1, EJB 3.1 Lite , JTA 1.1, JPA 2.0, Bean Validation 1.0, Managed Beans 1.0, Interceptors 1.1, Context & Dependency Injection 1.0 , Dependency Injection for Java 1.0 </li></ul></ul></ul>
  8. 8. Java EE 6 - Done <ul><li>Specifications approved by the JCP
  9. 9. Reference Implementation is GlassFish v3
  10. 10. TCK </li></ul>Dec 2009
  11. 11. Java EE 6 Specifications <ul><li>The Platform
  12. 12. Java EE 6 Web Profile 1.0
  13. 13. Managed Beans 1.0 </li></ul>
  14. 14. Java EE 6 Specifications New <ul><li>Contexts and Dependency Injection for Java EE (JSR 299)
  15. 15. Bean Validation 1.0 (JSR 303)
  16. 16. Java API for RESTful Web Services (JSR 311)
  17. 17. Dependency Injection for Java (JSR 330) </li></ul>
  18. 18. Java EE 6 Specifications Extreme Makeover <ul><li>Java Server Faces 2.0 (JSR 314)
  19. 19. Java Servlets 3.0 (JSR 315)
  20. 20. Java Persistence 2.0 (JSR 317)
  21. 21. Enterprise Java Beans 3.1 & Interceptors 1.1 (JSR 318)
  22. 22. Java EE Connector Architecture 1.6 (JSR 322) </li></ul>
  23. 23. Java EE 6 Specifications Updates <ul><li>Java API for XML-based Web Services 2.2 (JSR 224)
  24. 24. Java API for XML Binding 2.2 (JSR 222)
  25. 25. Web Services Metadata MR3 (JSR 181)
  26. 26. JSP 2.2/EL 2.2 (JSR 245)
  27. 27. Web Services for Java EE 1.3 (JSR 109)
  28. 28. Common Annotations 1.1 (JSR 250)
  29. 29. Java Authorization Contract for Containers 1.3 (JSR 115)
  30. 30. Java Authentication Service Provider Interface for Containers 1.0 (JSR 196) </li></ul>
  31. 31. Servlets in Java EE 5 At least 2 files <!--Deployment descriptor web.xml --> <web-app> <servlet> <servlet-name>MyServlet </servlet-name> <servlet-class> com.sun.MyServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>MyServlet </servlet-name> <url-pattern>/myApp/* </url-pattern> </servlet-mapping> ... </web-app> /* Code in Java Class */ package com.sun; public class MyServlet extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse res) <ul><li>{
  32. 32. ...
  33. 33. }
  34. 34. ...
  35. 35. } </li></ul>
  36. 36. Servlets 3.0 (JSR 315) Annotations-based @WebServlet http://blogs.sun.com/arungupta/entry/totd_81_getting_started_with package com.sun; @WebServlet(name=”MyServlet”, urlPatterns={”/myApp/*”}) public class MyServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) { ... } <!--Deployment descriptor web.xml --> <web-app> <servlet> <servlet-name>MyServlet </servlet-name> <servlet-class> com.sun.MyServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/myApp/*</url-pattern> </servlet-mapping> ... </web-app>
  37. 37. Servlets 3.0 Annotations-based @WebServlet @WebServlet(name=&quot;mytest&quot;, urlPatterns={&quot;/myurl&quot;}, initParams={ @WebInitParam(name=&quot;n1&quot;, value=&quot;v1&quot;), @WebInitParam(name=&quot;n2&quot;, value=&quot;v2&quot;) } ) public class TestServlet extends javax.servlet.http.HttpServlet { .... }
  38. 38. Servlets 3.0 Annotations-based @WebListeners <listener> <listener-class> server.LoginServletListener </listener-class> </listener> package server; . . . @WebListener() public class LoginServletListener implements ServletContextListener {
  39. 39. Servlets 3.0 Annotations-based @WebFilter <filter> <filter-name>PaymentFilter</filter-name> <filter-class>server.PaymentFilter</filter-class> <init-param> <param-name>param1</param-name> <param-value>value1</param-value> </init-param> </filter> <filter-mapping> <filter-name>PaymentFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>PaymentFilter</filter-name> <servlet-name>PaymentServlet</servlet-name> <dispatcher>REQUEST</dispatcher> </filter-mapping> package server; . . . @WebFilter ( filterName=&quot;PaymentFilter&quot;, InitParams={ @WebInitParam( name=&quot;param1&quot;, value=&quot;value1&quot;) } urlPatterns={&quot;/*&quot;}, servletNames={&quot;PaymentServlet&quot;}, dispatcherTypes={DispatcherType.REQUEST} ) public class PaymentFilter implements Filter { . . .
  40. 40. Servlets 3.0 Asynchronous Servlets <ul><li>Useful for Comet, long waits
  41. 41. Must declare @WebServlet(asyncSupported=true) </li></ul>AsyncContext context = request.startAsync(); context.addListener(new AsyncListener() { … }); context.dispatch(“/request.jsp”); //context.start(Runnable action); ... context.complete(); //marks completion http://blogs.sun.com/arungupta/entry/totd_139_asynchronous_request_processing
  42. 42. Servlets 3.0 Extensibility <ul><li>Plugin libraries using web fragments </li><ul><li>Modular web.xml </li></ul><li>Bundled in framework JAR file in META-INF directory
  43. 43. Zero-configuration, drag-and-drop for web frameworks </li></ul><ul><ul><li>Servlets, servlet filters, context listeners for a framework get discovered and registered by the container </li></ul></ul><ul><li>Only JAR files in WEB-INF/lib are used </li></ul>
  44. 44. <web-fragment> <filter> <filter-name>wicket.helloworld</filter-name> <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class> <init-param> <param-name>applicationClassName</param-name> <param-value>...</param-value> </init-param> </filter> <filter-mapping> <filter-name>wicket.helloworld</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-fragment> http://blogs.sun.com/arungupta/entry/totd_91_applying_java_ee Servlets 3.0 Extensibility
  45. 45. Servlets 3.0 Resource Sharing <ul><li>Static and JSP no longer confined to document root of the web application
  46. 46. May be placed in WEB-INF/lib/[*.jar]/META-INF/resources
  47. 47. Resources in document root take precedence over those in bundled JAR </li></ul>myapp.war WEB-INF/lib/catalog.jar /META-INF/resources/ catalog/books.html http://localhost:8080/myapp/catalog/books.html
  48. 48. EJB 3.1 (JSR 318) Package & Deploy in a WAR web.xml ? Java EE 5 Java EE 6 http://blogs.sun.com/arungupta/entry/totd_95_ejb_3_1 foo.ear foo_web.war WEB-INF/web.xml WEB-INF/classes com.sun.FooServlet com.sun.TickTock foo_ejb.jar com.sun.FooBean com.sun.FooHelper foo.war WEB-INF/classes com.sun.FooServlet com.sun.TickTock com.sun.FooBean com.sun.FooHelper
  49. 49. EJB 3.1 @Stateless public class App { public String sayHello(String name) { return &quot;Hello &quot; + name; } }
  50. 50. EJB 3.1 <ul><li>No interface view – one source file per bean </li><ul><li>Only for Local and within WAR
  51. 51. Required for Remote
  52. 52. No location transparency </li></ul><li>Component initialization in @PostConstruct </li><ul><li>No assumptions on no-arg ctor </li></ul></ul>
  53. 53. Java EE 6 Namespaces <ul><li>Global </li><ul><li>java:global/..(for all applications) </li></ul><li>Application scoped </li><ul><li>java:app/.. (visibile only for the app.)
  54. 54. App-1 binds an Object by name “java:app/myObject”, App-2 cannot see it.
  55. 55. All modules of the app has visibility. </li><ul><li>mod-1, mod-2 of app-1 can see “java:app/myObject” </li></ul></ul></ul>
  56. 56. Java EE 6 Namespaces <ul><li>Module Scoped </li><ul><li>java:module/ .. (visible only for the module)
  57. 57. App-1 has module-1 and module-2 </li><ul><li>Module-1's namespace not accessible by module-2 </li></ul><li>All components of the module has visibility </li></ul><li>Component </li></ul>
  58. 58. EJB 3.1 Portable Global JNDI Name Syntax <ul><li>Portable
  59. 59. Global
  60. 60. Application/Module-scoped
  61. 61. Derived from metadata such as name, component name etc. </li></ul>
  62. 62. EJB 3.1 Portable Global JNDI Name Syntax java:global[/<app-name>]/<module-name>/<bean-name> [!<fully-quali fied-interface-name>] <ul><li>Until now, only java:comp
  63. 63. Local & Remote business
  64. 64. No-interface
  65. 65. Also in java:app, java:module </li></ul>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
  66. 66. EJB 3.1 Portable Global JNDI Name Syntax <ul>package com.acme; @Stateless public class FooBean implements Foo { ... } <li>FooBean is packaged in fooejb .jar </li></ul>java:global/ fooejb / FooBean java:global/ fooejb / FooBean ! com.acme.Foo java:app/ fooejb / FooBean java:app/ fooejb / FooBean ! com.acme.Foo java:module/ FooBean java:module/ FooBean ! com.acme.Foo
  67. 67. public void testEJB() throws NamingException { EJBContainer ejbC = EJBContainer.createEJBContainer(); Context ctx = ejbC.getContext(); App app = (App) ctx.lookup(&quot; java:global/classes/App &quot;); assertNotNull(app); String NAME = &quot;Duke&quot;; String greeting = app.sayHello(NAME); assertNotNull(greeting); assertTrue(greeting.equals(&quot;Hello &quot; + NAME)); ejbC.close(); } EJB 3.1 Embeddable API – Deploy the Bean http://blogs.sun.com/arungupta/entry/totd_128_ejbcontainer_createejbcontainer_embedded
  68. 68. Screencast JSP, Servlets, EJB
  69. 69. EJB 3.1 Singleton Beans <ul><li>One instance per app/VM, not pooled </li><ul><li>Useful for caching state
  70. 70. CMT/BMT
  71. 71. Access to container services for injection, resource manager, timers, startup/shutdown callbacks, etc.
  72. 72. Enable eager initialization using @Startup
  73. 73. Always supports concurrent access
  74. 74. Define initialization ordering using @DependsOn </li></ul></ul>@Singleton public class MyEJB { . . . }
  75. 75. EJB 3.1 Asynchronous Session Beans <ul><li>Control returns to the client before the container dispatches invocation to a bean instance
  76. 76. @Asynchronous – method or class
  77. 77. Return type – void or Future<V>
  78. 78. Transaction context does not propagate </li><ul><li>REQUIRED -> REQUIRED_NEW </li></ul><li>Security principal propagates </li></ul>
  79. 79. EJB 3.1 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
  80. 80. EJB 3.1 Timers <ul><li>Automatically created EJB Timers
  81. 81. Calendar-based Timers – cron like semantics </li></ul><ul><ul><li>Every 14 th minute within the hour, for the hours 1 & 2 am (minute=”*/14”, hour=”1,2”) </li></ul></ul><ul><ul><li>Every 10 seconds starting at 30 (second=”30/10”) </li></ul></ul><ul><ul><li>Every 5 minutes of every hour (minute=”*/5”, hour=”*”) </li></ul></ul><ul><ul><li>2pm on Last Thur of Nov of every year (hour=”14”, dayOfMonth=”Last Thu”, month=”Nov”) </li></ul></ul><ul><ul><li>Every Mon & Wed midnight @Schedule(dayOfWeek=”Mon,Wed”) </li></ul></ul>
  82. 82. EJB 3.1 Timers <ul><li>Single persistent timer across JVMs
  83. 83. Automatically created EJB Timers </li><ul><li>@Schedule(hour=”15”,dayOfWeek=”Fri”) </li></ul><li>Can be chained </li><ul><li>@Schedules({ @Schedule(hour=”6”,dayOfWeek=”Tue,Thu,Fri-Sun”), @Schedule(hour=”12”,dayOfWeek=”Mon,Wed”) }) </li></ul><li>May be associated with a TimeZone
  84. 84. Non-persistent timer, e.g. Cache </li><ul><li>@Schedule(..., persistent=false) </li></ul></ul>
  85. 85. EJB 3.1 EJB 3.1 Lite – Feature Comparison
  86. 86. Managed Beans 1.0 @javax.annotation.ManagedBean EJB CDI JPA JAX-WS JAX-RS JSF ... @Stateful @Stateless @Singleton @Named @Entity @Web Service @Path @Managed Bean ... EJB
  87. 87. Managed Beans 1.0 <ul><li>POJO as managed component for the Java EE container </li><ul><li>JavaBeans component model for Java EE
  88. 88. Simple and Universally useful
  89. 89. Advanced concepts in companion specs </li></ul><li>Basic Services </li><ul><li>Resource Injection, Lifecycle Callbacks, Interceptors </li></ul><li>Available as </li><ul><li>@Resource / @Inject
  90. 90. java:app/<module-name>/<bean-name>
  91. 91. java:module/<bean-name> </li></ul></ul>
  92. 92. public class MyManagedBean { public void setupResources() { // setup your resources } public void cleanupResources() { // collect them back here } public String sayHello(String name) { return &quot;Hello &quot; + name; } } Managed Beans 1.0 - Sample @Resource MyManagedBean bean; @javax.annotation.ManagedBean @PostConstruct @PreDestroy @Inject MyManagedBean bean; http://blogs.sun.com/arungupta/entry/totd_129_managed_beans_1
  93. 93. <ul><li>Collection of basic types </li></ul>@Entity public class Person { @Id protected String ssn; protected String name; protected Date birthDate; . . . @ElementCollection @CollectionTable(name=”ALIAS”) protected Set<String> nickNames; } Java Persistence API 2 (JSR 317) Sophisticated mapping/modeling options
  94. 94. <ul><li>Collection of embeddables </li></ul>@Embeddable public class Address { String street; String city; String state; . . . } @Entity public class RichPerson extends Person { . . . @ElementCollection protected Set<Address> vacationHomes; . . . } Java Persistence API 2 Sophisticated mapping/modeling options
  95. 95. Java Persistence API 2 Sophisticated mapping/modeling options <ul><li>Multiple levels of embedding </li></ul>@Embeddable public class ContactInfo { @Embedded Address address; . . . } @Entity public class Employee { @Id int empId; String name; ContactInfo contactInfo; . . . }
  96. 96. Java Persistence API 2 Sophisticated mapping/modeling options <ul><li>Improved Map support </li></ul>@Entity public class VideoStore { @Id Integer storeId; Address location; . . . @ElementCollection Map<Movie, Integer> inventory; } @Entity public class Movie { @Id String title; @String director; . . . }
  97. 97. Java Persistence API 2 Metamodel <ul><li>Abstract “schema-level” model over managed classes of a Persistence Context </li><ul><li>Entities, Mapped classes, Embeddables, ... </li></ul><li>Accessed dynamically </li><ul><li>EntityManager or EntityManagerFactory.getMetamodel() </li></ul><li>And/or statically materialized as metamodel classes </li><ul><li>Use annotation processor with javac </li></ul></ul>
  98. 98. Java Persistence API 2 Metamodel Example @Entity public class Customer { @Id Integer custId; String name; ... Address address; @ManyToOne SalesRep rep; @OneToMany Set<Order> orders; } import javax.persistence.metamodel.*; @StaticMetamodel(Customer.class) public class Customer_ { public static SingularAttribute<Customer, Integer> custId; public static SingularAttribute<Customer, String> name; public static SingularAttribute<Customer, Address> address; public static SingularAttribute<Customer, SalesRep> rep; public static SetAttribute<Customer, Order> orders; }
  99. 99. Java Persistence API 2 Caching <ul><li>1st-level Cache by PersistenceContext </li><ul><li>Only one object instance for any database row </li></ul><li>2nd-level by “shared-cache-mode” </li><ul><li>ALL, NONE
  100. 100. UNSPECIFIED – Provider specific defaults
  101. 101. ENABE_SELECTIVE - Only entities with Cacheable(true)
  102. 102. DISABLE_SELECTIVE - All but with Cacheable(false)
  103. 103. Optional feature for PersistenceProvider </li></ul></ul>
  104. 104. Java Persistence API 2 Much more ... <ul><li>New locking modes </li><ul><li>PESSIMISTIC_READ – grab shared lock
  105. 105. PESSIMISTIC_WRITE – grab exclusive lock
  106. 106. PESSIMISTIC_FORCE_INCREMENT – update version
  107. 107. em.find(<entity>.class, id, LockModeType.XXX)
  108. 108. em.lock(<entity>, LockModeType.XXX) </li></ul><li>Standard configuration options </li><ul><li>javax.persistence.jdbc.[driver | url | user | password] </li></ul></ul>
  109. 109. Screencast - JPA
  110. 110. @DataSourceDefinition @DataSourceDefinition ( name=&quot;java:global/MyApp/MyDataSource&quot;, className=&quot;org.apache.derby.jdbc.ClientDataSource&quot; databaseName=”testdb”, serverName=”localhost”, portNumber=”1527”, user=&quot;dbuser&quot;, password=&quot;dbpassword&quot; ) <ul><li>Equivalents in DDs as <datasource-definition> element
  111. 111. Can be defined in Servlet, EJB, Managed Beans, Application Client and their DDs.
  112. 112. Can be defined in 'global', 'app', 'module', 'comp' scopes </li></ul>
  113. 113. Interceptors 1.1 <ul><li>Interpose on invocations and lifecycle events on a target class
  114. 114. Defined </li><ul><li>Using annotations or DD
  115. 115. Default Interceptors (only in DD) </li></ul><li>Class & Method Interceptors </li><ul><li>In the same transaction & security context </li></ul><li>Cross-cutting concerns: logging, auditing, profiling </li></ul>
  116. 116. Interceptors 1.1 - Code @InterceptorBinding @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.TYPE}) public @interface MyInterceptorBinding { } @Interceptor @MyInterceptorBinding public class MyInterceptor { @AroundInvoke public Object intercept(InvocationContext context) { System.out.println(context.getMethod.getName()); System.out.println(context.getParameters()); Object result = context.proceed(); return result; } . . . }
  117. 117. Interceptors 1.1 – Sample Code @Interceptors(MyInterceptor.class) public class MyManagedBean { . . . } http://blogs.sun.com/arungupta/entry/totd_134_interceptors_1_1 @Inject MyManagedBean bean;
  118. 118. Interceptors 1.1 – Sample Code @MyInterceptorBinding public class MyManagedBean { . . . } public class MyManagedBean { @Interceptors(MyInterceptor.class) public String sayHello(String name) { . . . } } Single instance of Interceptor per target class instance
  119. 119. Interceptors 1.1 – Sample Code @Named @Interceptors(MyInterceptor.class) public class MyManagedBean { . . . @Interceptors(AnotherInterceptor.class) @ExcludeDefaultInterceptors @ExcludeClassInterceptors public void someMethod() { . . . } }
  120. 120. Java Server Faces 2.0 <ul><li>Facelets as “templating language” for the page </li></ul><ul><ul><li>Custom components much easier to develop </li></ul></ul><html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xmlns:ui=&quot;http://java.sun.com/jsf/facelets&quot; xmlns:h=&quot;http://java.sun.com/jsf/html&quot;> <h:head> <title>Enter Name &amp; Password</title> </h:head> <h:body> <h1>Enter Name &amp; Password</h1> < h:form> <h:panelGrid columns=&quot;2&quot;> <h:outputText value=&quot;Name:&quot;/> <h:inputText value=&quot;#{simplebean.name}&quot; title=&quot;name&quot; id=&quot;name&quot; required=&quot;true&quot;/> <h:outputText value=&quot;Password:&quot;/> <h:inputText value=&quot;#{simplebean.password}&quot; title=&quot;password&quot; id=&quot;password&quot; required=&quot;true&quot;/> </h:panelGrid> <h:commandButton ac tion=&quot;show&quot; value=&quot;submit&quot;/> </h:form> </h:body> </html>
  121. 121. JSF 2 Composite Components
  122. 122. JSF 2 Composite Components <html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xmlns:ui=&quot;http://java.sun.com/jsf/facelets&quot; xmlns:h=&quot;http://java.sun.com/jsf/html&quot; xmlns:ez=&quot;http://java.sun.com/jsf/composite/ezcomp&quot; > <h:head> <title>Enter Name &amp; Password</title> </h:head> <h:body> <h1>Enter Name &amp; Password</h1> <h:form> <ez:username-password/> <h:commandButton action=&quot;show&quot; value=&quot;submit&quot;/> </h:form> </h:body> </html> http://blogs.sun.com/arungupta/entry/totd_135_jsf2_custom_components . . . WEB-INF index.xhtml resources/ ezcomp/ username-password.xhtml
  123. 123. Screencast - JSF
  124. 124. Contexts & Dependency Injection – CDI (JSR 299) <ul><li>Type-safe Dependency Injection </li><ul><li>No String-based identifiers
  125. 125. Selected at development/deployment time </li></ul><li>Strong typing, Loose coupling
  126. 126. Context & Scope management - extensible
  127. 127. Works with Java EE modular and component architecture </li><ul><li>Integration with Unified Expression Language (UEL) </li></ul></ul>
  128. 128. CDI Injection Points <ul><li>Field, Method, Constructor
  129. 129. 0 or more qualifiers
  130. 130. Type </li></ul>@Inject @LoggedIn User user @Inject @LoggedIn User user Request Injection What ? (Type) Which one ? (Qualifier)
  131. 131. CDI – Sample Client Code Field and Method Injection public class CheckoutHandler { @Inject @LoggedIn User user; @Inject PaymentProcessor processor; @Inject void setShoppingCart(@Default Cart cart) { … } }
  132. 132. CDI – Sample Client Code Constructor Injection public class CheckoutHandler { @Inject CheckoutHandler(@LoggedIn User user, PaymentProcessor processor, @Default Cart cart) { ... } } <ul><li>Only one constructor can have @Inject </li></ul>
  133. 133. CDI - Sample Client Code Multiple Qualifiers and Qualifiers with Arguments public class CheckoutHandler { @Inject CheckoutHandler(@LoggedIn User user, @Reliable @PayBy(CREDIT_CARD) PaymentProcessor processor , @Default Cart cart) { ... } }
  134. 134. CDI - Scopes <ul><li>Beans can be declared in a scope </li><ul><li>Everywhere: @ApplicationScoped, @RequestScoped
  135. 135. Web app: @SessionScoped
  136. 136. JSF app: @ConversationScoped : begin(), end() </li><ul><li>Transient and long-running </li></ul><li>Pseudo-scope (default): @Dependent
  137. 137. Custom scopes via @Scope </li></ul><li>CDI runtime makes sure the right bean is created at the right time
  138. 138. Client do NOT have to be scope-aware </li></ul>
  139. 139. CDI - Named Beans Built-in support for the Unified EL <ul><li>Beans give themselves a name with @Named(“cart”)
  140. 140. Then refer to it from a JSF or JSP page using the EL: </li></ul><h:commandButton value=”Checkout” action=“#{cart.checkout}”/>
  141. 141. CDI - Events Even more decoupling <ul><li>Annotation-based event model
  142. 142. A bean observes an event </li></ul>void logPrintJobs( @Observes PrintEvent event){…} <ul><li>Another bean fires an event @Inject @Any Event<PrintEvent> myEvent; void doPrint() { . . . myEvent.fire(new PrintEvent()); }
  143. 143. Events can have qualifiers too </li></ul>void logPrintJobs( @Observes @LargeFile PrintEvent event){…}
  144. 144. CDI Much more ... <ul><li>Producer methods and fields
  145. 145. Alternatives
  146. 146. Interceptors
  147. 147. Decorators
  148. 148. Stereotypes
  149. 149. . . . </li></ul>
  150. 150. Screencast - CDI
  151. 151. Bean Validation (JSR 303) <ul><li>Tier-independent mechanism to define constraints for data validation </li></ul><ul><ul><li>Represented by annotations
  152. 152. javax.validation.* package </li></ul></ul><ul><li>Integrated with JSF and JPA </li></ul><ul><ul><li>JSF: f:validateRequired, f:validateRegexp
  153. 153. JPA: pre-persist, pre-update, and pre-remove </li></ul></ul><ul><li>@NotNull(message=”...”), @Max, @Min, @Size
  154. 154. Fully Extensible @Email String recipient; </li></ul>
  155. 155. Bean Validation Integration with JPA <ul><li>Managed classes may be configured </li><ul><li>Entities, Mapped superclasses, Embeddable classes </li></ul><li>Applied during pre-persist, pre-update, pre-remove lifecycle events
  156. 156. How to enable ? </li><ul><li>“ validation-mode” in persistence.xml
  157. 157. “ javax.persistence.validation.mode” key in Persistence.createEntityManagerFactory </li></ul><li>Specific set of classes can be targeted </li><ul><li>javax.persistence.validation.group.pre-[persist|update|remove] </li></ul></ul>
  158. 158. Bean Validation Integration with JSF <ul><li>Individual validators not required
  159. 159. Integration with EL </li><ul><li>f:validateBean, f:validateRequired <h:form> <f:validateBean> <h:inputText value=”#{model.property}” /> <h:selectOneRadio value=”#{model.radioProperty}” > … </h:selectOneRadio> <!-- other input components here --> </f:validateBean> </h:form> </li></ul></ul>
  160. 160. JAX-RS 1.1 <ul><li>Java API for building RESTful Web Services
  161. 161. POJO based
  162. 162. Annotation-driven
  163. 163. Server-side API
  164. 164. HTTP-centric </li></ul>
  165. 165. JAX-RS 1.1 Code Sample - Simple public class HelloWorldResource { public String sayHello() { return &quot;Hello World&quot;; } public String morning() { return “Good Morning!”; } } @Path(&quot;helloworld&quot;) @Context UriInfo ui; @GET @Produces(&quot;text/plain&quot;) @GET @Path(&quot;morning&quot;)
  166. 166. JAX-RS 1.1 Code Sample – Specifying Output MIME type @Path(&quot;/helloworld&quot;) @Produces(&quot;text/plain&quot;) public class HelloWorldResource { @GET public String doGetAsPlainText() { . . . } @GET @Produces(&quot;text/html&quot;) public String doGetAsHtml() { . . . } } @GET @Produces({ &quot;application/xml&quot;, &quot;application/json&quot;}) public String doGetAsXmlOrJson() { . . . }
  167. 167. JAX-RS 1.1 Code Sample – Specifying Input MIME type @POST @Consumes(&quot;text/plain&quot;) public String saveMessage() { . . . }
  168. 168. JAX-RS 1.1 Code Sample import javax.inject.Inject; import javax.enterprise.context.RequestScoped; @RequestScoped public class ActorResource { @Inject DatbaseBean db; public Actor getActor(int id) { return db.findActorById(id); } }
  169. 169. JAX-RS 1.1 Code Sample import javax.inject.Inject; import javax.enterprise.context.RequestScoped; @RequestScoped public class ActorResource { @Inject DatbaseBean db; public Actor getActor( int id) { return db.findActorById(id); } } import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.PathParam; @Path(&quot;/actor/{id}&quot;) @GET @Produces(&quot;application/json&quot;) @PathParam(&quot;id&quot;) http://blogs.sun.com/arungupta/entry/totd_124_using_cdi_jpa
  170. 170. JAX-RS 1.1 Integration with Java EE 6 – Servlets 3.0 <ul><li>No or Portable “web.xml” </li></ul><web-app> <servlet> <servlet-name>Jersey Web Application</servlet-name> <servlet-class> com.sun.jersey.spi.container.servlet.ServletContainer </servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>com.foo.MyApplication</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Jersey Web Application</servlet-name> <url-pattern>/ resources /*</url-pattern> </servlet-mapping> </web-app> public class MyApplication extends javax.ws.rs.core.Application { } @ApplicationPath(“resources”)
  171. 171. Screencast - JAX-RS
  172. 172. <ul>GlassFish Distributions </ul><ul>Distribution </ul><ul>License </ul><ul>Features </ul><ul>GlassFish Open Source Edition 3.0.1 </ul><ul>CDDL & GPLv2 </ul><ul><li>Java EE 6 Compatibility
  173. 173. No Clustering
  174. 174. Clustering planned in 3.1
  175. 175. mod_jk for load balancing </li></ul><ul>GlassFish Open Source Edition 2.1.1 </ul><ul>CDDL & GPLv2 </ul><ul><li>Java EE 5 Compatibility
  176. 176. In memory replication
  177. 177. mod_loadbalancer </li></ul><ul>Oracle GlassFish Server 3.0.1 </ul><ul>Commercial </ul><ul><li>GlassFish Open Source Edition 3.0.1
  178. 178. Oracle GlassFish Server Control
  179. 179. Clustering planned in 3.1 </li></ul><ul>Oracle GlassFish Server 2.1.1 </ul><ul>Commercial </ul><ul><li>GlassFish Open Source Edition 2.1.1
  180. 180. Enterprise Manager
  181. 181. HADB </li></ul><ul>Clustering Coming Soon! </ul>
  182. 182. GlassFish 3 & OSGi <ul><li>No OSGi APIs are used in GlassFish </li><ul><li>HK2 provides abstraction layer </li></ul><li>All GlassFish modules are OSGi bundles
  183. 183. Felix is default, also runs on Knopflerfish & Equinox </li><ul><li>Can run in an existing shell
  184. 184. 200+ modules in v3 </li></ul></ul>http://blogs.sun.com/arungupta/entry/totd_103_glassfish_v3_with
  185. 185. Light Weight & On-demand Monitoring <ul><li>Event-driven light-weight and non-intrusive monitoring
  186. 186. Modules provide domain specific probes (monitoring events) </li></ul><ul><ul><li>EJB, Web, Connector, JPA, Jersey, Orb, Ruby </li></ul></ul><ul><li>End-to-end monitoring on Solaris using DTrace
  187. 187. 3 rd party scripting clients </li></ul><ul><ul><li>JavaScript to begin with </li></ul></ul>
  188. 188. Boost your productivity Retain session across deployment asadmin redeploy –properties keepSessions=true helloworld.war
  189. 189. Boost your productivity Deploy-on-Save
  190. 190. <ul></ul><ul>GlassFish Roadmap Detail </ul><ul>©2010 Oracle Corporation </ul>
  191. 191. GlassFish 3.1 = 3.0 + 2.1.1 <ul><li>Main Features </li><ul><li>Clustering and Centralized Administration
  192. 192. High Availability </li></ul><li>Other ... </li><ul><li>Application Versioning
  193. 193. Application-scoped Resources
  194. 194. SSH-based remote management and monitoring
  195. 195. Embedded (extensive)
  196. 196. Admin Console based on RESTful API </li></ul></ul>http://wikis.sun.com/display/glassfish/GlassFishv3.1
  197. 197. References & Queries <ul><li>glassfish.org
  198. 198. blogs.sun.com/theaquarium
  199. 199. oracle.com/goto/glassfish
  200. 200. glassfish.org/roadmap
  201. 201. youtube.com/user/GlassFishVideos
  202. 202. Follow @glassfish
  203. 203. [email_address]
  204. 204. [email_address] </li></ul>
  205. 205. Thank You ! [email_address] Sun Microsystems

×