Upload
hoangbao
View
236
Download
3
Embed Size (px)
Citation preview
<Insert Picture Here>
Exploring Java EE 6 The Programming Model Explained
Lee Chuk Munn
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.
What comes to your
mind when you
think of JavaEE?
What People Think...
What Developers Wanted...
What They Got...
Key Themes for JavaEE 6
Developer
Productivity
Flexible
and
Lightweight Extensible
Web Profile
Pruning
Embrace open source
frameworks
Enables Drag & Drop
framework installation
More annotations
POJO development
Less XML configuration
Java EE
What's New in the Java EE 6 Platform?
• EJB 3.1 (+Lite)
• JPA 2.0
• Servlet 3.0
• JSF 2.0
• JAX-RS 1.1
• Bean Validation 1.0
• DI 1.0
• CDI 1.0
• Connectors 1.6
• Managed Beans 1.0
• Interceptors 1.1
• JSP 2.2
• EL 2.2
• JSR 250 1.1
• JASPIC 1.1
• JACC 1.5
• JAX-WS 2.2
• JSR 109 1.3
New and updated components
Changes in Java EE 6
http://blog.eisele.net/2010/12/who-is-afraid-of-java-ee-6-get-rid-of.html
52%
11%
14%
23%
Java EE 6 Web Profile
JPA 2.0 / JTA 1.1
Managed Beans 1.0 EJB 3.1
DI 1.0 / CDI 1.0 / Interceptors 1.1 / JSR 250 1.1
Servlet 3.0 / EL 2.2
JAX-RS 1.1 JSF 2.0 JSP 2.2 JSTL 1.2
Web Container
Extensions CDI
Extensions
Bean
Valid
atio
n 1
.0
Simplfied Packaging
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
Java EE 5 Java EE 6
Web Apps Simplified Packaging
WEB-INF/classes com/acme/MyServlet.class com/acme/MyFilter.class
WEB-INF/web.xml
index.html main.css jquery-1.4.2.js ui/jquery.ui.core.js ui/jquery.ui.widget.js
Web Application
WEB-INF/lib someframework.jar
Web Apps Simplified Packaging Web Application
@WebServlet(urlPatterns=”/foo”) public class MyServlet { … } @WebFilter(urlPatterns=”/foo”) public class MyFilter { … }
WEB-INF/classes com/acme/MyServlet.class com/acme/MyFilter.class
WEB-INF/web.xml
index.html main.css jquery-1.4.2.js ui/jquery.ui.core.js ui/jquery.ui.widget.js
WEB-INF/lib someframework.jar
Web Apps Simplified Packaging
WEB-INF/classes com/acme/MyServlet.class com/acme/MyFilter.class
index.html main.css jquery-1.4.2.js ui/jquery.ui.core.js ui/jquery.ui.widget.js
Web Application
WEB-INF/lib someframework.jar
META-INF/web-fragment.xml com/fw/FwServlet.class com/fw/FwFilter.class
Framework Jar
Web Apps Simplified Packaging
WEB-INF/classes com/acme/MyServlet.class com/acme/MyFilter.class
index.html main.css
Web Application
WEB-INF/lib someframework.jar jquery-ui-1.8.4.jar META-INF/resources/jquery-1.4.2.js
META-INF/resources/ui/jquery.ui.core.js META-INF/resources/ui/jquery.ui.widget.js
jQuery Resource Jar
Web Apps Simplified Packaging
WEB-INF/classes com/acme/MyServlet.class com/acme/MyFilter.class
index.html main.css
Web Application
WEB-INF/lib someframework.jar jquery-ui-1.8.4.jar
• Simpler packaging
• Drag and drop self
configuring frameworks
• Resource JARs
Managed Bean as the Foundation • Plain Java objects (POJOs)
– Lifecycle managed by container
• Foundation for other component types
– Not something that developers would use directly
• Supports injections
– @Resource, @Inject
• Lifecycle callbacks
– @PostConstruct, @PreDestroy
• Optional name
• Interceptors
Managed Bean Example
@ManagedBean(“MyCustomer”)
public class Customer {
@Resource DataSource db;
//Inject another managed bean
@Resource Address address;
@PostConsturct
private void init() {
//
}
}
EJB Components as Managed Beans
• All managed beans core services are available in EJB
• EJB component model extends managed beans by
providing
– State – @Stateful, @Stateless, @Singleton
– Transaction – @TransactionAttribute
– Security – @RolesAllowed, @DenyAll, @PermitAll
– Access semantics – @Remote, @WebService
• EJBs can now be view as features on managed beans
– Just as other types of components
Incremental Programming Model • Application changes over time
– Requirement change, functions of classes/objects changes
• Allows you to evolve your application gracefully
– Start with POJOs @ManagedBean
– Use all the basic services – lifecycle, resource injection, etc
– Turn them in to EJBs, Enitites, etc to take advantage of new
capabilities
– No change to (local) client
EJB 2.1 – Step 1
public interface Converter extends EJBObject {
public BigDecimal dollarToYen(BigDecimal dollars)
throws RemoteException;
public BigDecimal yenToEuro(BigDecimal yen)
throws RemoteException;
}
public interface ConverterHome extends EJBHome {
Converter create()
throws RemoteException, CreateException;
}
EJB 2.1 – Step 2 public class ConverterBean implements SessionBean {
...
public BigDecimal dollarToYen(BigDecimal dollars) {
...
}
public BigDecimal yenToEuro(BigDecimal yen) {
...
}
public ConverterBean() {}
public void ejbCreate() {}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void setSessionContext(SessionContext sc) {}
}
EJB 2.1 – Step 3
...
<session >
<display-name>Converter Session Bean</display-name>
<ejb-name>SimpleConverter</ejb-name>
<home>examples.ConverterHome</home>
<remote>examples.Converter</remote>
<ejb-class>examples.ConverterBean</ejb-class>
<session-type>Stateful</session-type>
<transaction-type>Container</transaction-type>
</session>
...
Consuming EJB 2.1
Context initial = new InitialContext();
Context myEnv = (Context)initial.lookup("java:comp/env");
Object objref = myEnv.lookup("ejb/SimpleConverter");
ConverterHome home =(ConverterHome)PortableRemoteObject
.narrow(objref, ConverterHome.class);
Converter currencyConverter = home.create();
EJB as POJO
@Stateless
public class ConverterBean {
...
public BigDecimal dollarToYen(BigDecimal dollars) {
...
}
public BigDecimal yenToEuro(BigDecimal yen) {
...
}
}
@EJB ConverterBean converterBean;
Common Injection Service Framework
• Injection services available to all objects
– Not restricted to specific components eg. EJB
• Context and Dependency Injection
– Managed beans on steroids
• Opt-in technology on per-module basis
– WEB-INF/beans.xml, META-INF/beans.xml
• Key CDI concepts
– Strongly typed for typesafety
– Loose coupling – events, interceptors, decorators
– Context and scope management
– Integration with EL
• Bridge EJB (business) to JSF (presentation)
CDI Example
@Inject User user;
CDI Example
@Inject User user;
Type describes the capability of the user
CDI Example
@Inject User user;
What type of User?
CDI Example
@Inject @LoggedIn User user;
Qualifier describes the characteristic of the user
Uniformity • Annotations are additive
• New behaviour refine pre-existing ones
– Unless annotation conflicts with the semantics of component
– Eg. session scoped for JAX-RS components
• Eg. JAX-RS resource classes are POJOs
– Use JAX-RS specific injection capabilities
– Additional annotations to turn resource classes into CDI
managed beans or EJBs
– Use the new services with REST
JAX-RS Example
@Path(“root”)
public class RootResource {
@Context UriInfo info;
@Path(“{id}”)
public SubResource find(
@PathParam(“id”) String id) {
return (new SubResource(id));
}
}
JAX-RS Example
@RequestScoped
@Path(“root”)
public class RootResource {
@Context UriInfo info;
@Inject @CustomerDB DataSource myDB;
@Path(“{id}”)
public SubResource find(
@PathParam(“id”) String id) {
return (new SubResource(id));
}
}
As a CDI bean, using @Inject and scoped annotations
APIs That Work Better Together • Previous versions have a bunch of APIs that are quite
independent of each other
• Java EE 5/6 introduces lots of 'services' API
– Managed bean, CDI, validation, interceptors, etc
• Aiming to get API to work better together
– Eg. Bean Validation integrated into JSF, JPA
– But not so well with CDI – working on it
– Use of SPI to provide integration to other frameworks
Java EE 6
After Frameworks
Java EE 6 Platform Extensions
Extensibility • Integrating 3rd party frameworks
• Pluggability contracts make extension look like build-
in technologies
• Two main extension points in Java EE 6
– Servlet 3.0 pluggability
– CDI extensions
Servlet 3.0 Pluggability • Primary to address 3rd party web frames
– Drag and drop model
– Dynamic or static registration for frameworks
– Configuration, initialization, code generation, etc
• Results in ready to use framework JARs
• Modular web.xml and web-fragment.xml
– Framework contains a fragment of web.xml
– META-INF/web-fragment.xml
• Extension can discover and process annotated
classes
Pluggability Example
@MyWebFramework
public class DoSomething {
Register with service loader framework
myframework.jar
META-INF/services/
javax.servlet.ServletContainerInitializer
myframework.FrameworkLoader
Pluggability Example @HandlesTypes(MyWebFramework.class)
public class FrameworkLoader
implements ServletContainerInitializer {
public void onStartup(Set<Class<?>> c
, ServletContext ctx) {
//Load framework controller
ServletRegistration.Dynamic reg = ctx.addServlet(
“MyFrameworkController”
, “myframework.controller.FrameworkController”);
reg.setInitParameter(“config”
, “/WEB-INF/config.properties”);
//Other processing – eg. Code generation
...
}
}
Extending CDI • Portable extension framework provides standard way
of extending CDI
• Drag and drop
• Extensions can process any annotated types
• What can you do with portable extensions
– Introduce new bean types
– Satisfy injection points
– Introduce custom scopes
– Augment or substitute bean's annotations and/or metadata
– Limited by your imagination
Portable Extension Example
@RequestScope
@Message
public class HandleMessage {
@Inject @From JID from;
@PersistenceContext EntityManager em;
@Body(“{body}”)
public Object handle(@Named(“body”) int custId) {
return (em.find(Customer.class, custId));
}
}
New component type
New bean type
Integrates well
Export a named bean to handle method only
Conclusion – A Better Platform • Less boilerplate code
• Simpler packaging
• Uniform use of annotations
• Consistent programming model
• Extensible
Simple things should be simple
complex things should be possible
Alan Kay
For More Information
search.oracle.com
or
oracle.com