tssjs java ee 6 roundtable [Read-Only] - Java...

30
Java EE 6 Community Roundtable Reza Rahman Independent Consultant Author, EJB 3 in Action [email protected]

Transcript of tssjs java ee 6 roundtable [Read-Only] - Java...

Page 1: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

Java EE 6 Community Roundtable

Reza Rahman

Independent Consultant

Author, EJB 3 in Action

[email protected]

Page 2: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

Java EE 6: Expanding Horizons

� Java EE 5

• Ease-of-use, annotations, freedom from XML, new APIs

• EJB 3.0, JPA 1.0, JSF 1.0, JAX-WS 2.0

• Demise of EJB 2.x Entity Beans

� Java EE 6

• Pruning: A healthful maturity

• Profiles: Different strokes for different folks

• Innovation: New APIs, new features, further ease-of-use

� JCDI, JSF 2.0, EJB 3.1, JPA 2.0, Servlet 3.0, JAX-RS 1.1

Page 3: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

Pruning

� An important first step in the life-cycle of a mature platform

� The goal is to “deprecate” APIs that are out-of-date or have been

superseded

� Candidates for pruning:

• JAX-RPC: Superseded by the popular JAX-WS API

• EJB 2.x Entity Beans CMP: Dropped in favor of JPA

• JAXR: UDDI not well used

• Java EE Application Deployment (JSR-88): Poor support

• Java EE Management (JSR-77): Poor support

Page 4: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

Profiles

� Specific sub-sets of Java EE APIs intended for specific

environments

� Each Profile is fully integrated and “just works” out-of-the-box,

although integrating add-ons is still possible

� Makes creating modular, lightweight Java EE compliant

application servers a lot easier

� Makes it possible to extend the platform by integrating non-

standard APIs without serious loss of compatibility/fragmentation

� Only one Profile, the “Web Profile” is initially planned

Page 5: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

Suggested Profiles

JACC 1.0

JCA 1.6

JAXB 2.2

JAX-RS 1.1

JAX-WS 2.2

JavaMail 1.4

JMS 1.1

JTA 1.1

JPA 2.0

EJB 3.1

JCDI (?)

JSF 2.0

EL 1.2

JSTL 1.2

JSP 2.2

Servlet 3.0

Full ProfileWeb ProfileAPI

Page 6: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

Major API Changes in Java EE 6� Java Contexts and Dependency Injection

• JSF/EJB 3/JPA integration

• Generic dependency injection

� Java Server Faces (JSF) 2.0

• Ease-of-use, technology adoption, new features

� Enterprise Java Beans (EJB) 3.1

• Ease-of-use, new features

� Java Persistence API (JPA) 2.0

• New features

� Servlet 3.0

• Ease-of-use, new features

� Java API for RESTful Web Services (JAX-RS) 1.1

• API for developing REST based web services

Page 7: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

Java Contexts and Dependency Injection

� Unifies the JSF, JPA and EJB 3 programming models

� Stateful, contextual web development

� Conversations

� Type-safe generic dependency injection

� Enhancements to the Interceptor model, decorators

� Annotations meta-programming

Page 8: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

JSF Page Using JCDI<h:form>

<table>

<tr>

<td>Bidder</td>

<td><h:inputText value="#{bid.bidder}"/></td>

</tr>

<tr>

<td>Item</td>

<td><h:inputText value="#{bid.item}"/></td>

</tr>

<tr>

<td>Bid Amount</td>

<td><h:inputText value="#{bid.bidPrice}"/></td>

</tr>

</table>

...

<h:commandButton type="submit" value="Add Bid"

action="#{placeBid.addBid}"/>

...

</h:form>

Page 9: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

JPA Entity JCDI Component

@Entity

@Named

@Table(name=“BIDS”)

public class Bid {

@Id

@GeneratedValue

@Column(name=“BID_ID”)

public Long bidId;

public String bidder;

public String item;

@Column(name=“BID_PRICE”)

public Double bidPrice;

}

Page 10: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

EJB 3.1 Session Bean JCDI Component@Stateless

@RequestScoped

@Named(“placeBid”)

public class PlaceBidBean {

@PersistenceContext

private EntityManager entityManager;

@Current

private Bid bid;

@Utility

private MathUtil mathUtil;

@Audited

public void addBid() {

bid.setBidPrice(mathUtil.round(

bid.getBidPrice(), 2));

entityManager.persist(bid);

}

}

Page 11: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

Java Bean JCDI Component

@Retention(RUNTIME)

@Target({TYPE, METHOD, FIELD, PARAMETER})

@BindingType

public @interface Utility {}

@Utility

public class MathUtil {

...

public double round(double value, int decimalPlaces) {

BigDecimal converter =

new BigDecimal(Double.toString(value));

converter = converter.setScale(decimalPlaces,

BigDecimal.ROUND_HALF_UP);

return converter.doubleValue();

}

...

}

Page 12: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

JCDI Interceptor

@InterceptorBindingType

@Target({TYPE, METHOD})

@Retention(RUNTIME)

public @interface Audited {}

@Audited @Interceptor

public class AuditInterceptor {

@AroundInvoke

public Object audit(InvocationContext context)

throws Exception {

System.out.println("Entering: "

+ context.getMethod().getName());

System.out.println(" with args: "

+ context.getParameters());

return context.proceed();

}

}

Page 13: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

Java Server Faces 2.0

� First-class support for Facelets as view/custom components in

addition to JSP

� Annotation-driven configuration

� Ajax support in the JSF life-cycle

� Resources

� New JSF standard components

� Many others

Page 14: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

Facelet Component in Use

<html xmlns="http://www.w3.org/1999/xhtml"

xmlns:h="http://java.sun.com/jsf/html"

xmlns:f="http://java.sun.com/jsf/core"

xmlns:ez="http://java.sun.com/jsf/composite/ezcomp">

<h:head>

<title>A simple example of EZComp</title>

</h:head>

<h:body>

<h:form>

<ez:loginPanel id="loginPanel">

<f:actionListener for="loginEvent"

binding="#{bean.loginEventListener}" />

</ez:loginPanel>

</h:form>

</h:body>

</html>

Page 15: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

Facelet Component

<html xmlns="http://www.w3.org/1999/xhtml"

xmlns:h="http://java.sun.com/jsf/html"

xmlns:f="http://java.sun.com/jsf/core"

xmlns:ui="http://java.sun.com/jsf/facelets"

xmlns:composite="http://java.sun.com/jsf/composite">

<body>

<composite:interface>

<composite:actionSource name="loginEvent" />

</composite:interface>

<composite:implementation>

<p>Username: <h:inputText id="usernameInput" /></p>

<p>Password: <h:inputSecret id="passwordInput" /></p>

<p><h:commandButton id="loginEvent" value="login"/>

</composite:implementation>

</body>

</html>

Page 16: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

JSF Annotations

@ManagedBean(name=“accountCreator", scope="request")

public class AccountCreatorBean {

@ManagedProperty(value="#{user}")

private User user;

...

public String createAccount() {

...

}

}

Page 17: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

Enterprise Java Beans 3.1

� Session Bean optional interfaces

� Singleton Beans with concurrency control

� Cron-style declarative and programmatic Timers

� Asynchronous bean invocation

� Simplified WAR packaging

� Java SE support

� Standardized Global JNDI naming

� EJB Lite

Page 18: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

Cron-like Declarative Timers

@Stateless

public class NewsLetterGeneratorBean {

@Resource

private Session mailSession;

@Schedule(second="0", minute="0", hour="0",

dayOfMonth="1", month="*", year="*")

public void generateMonthlyNewsLetter() {

...

}

}

Page 19: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

Asynchronous Session Bean

@Stateless

public class OrderBillingBean {

...

@Asynchronous

public Future<BillingStatus> billOrder(Order order) {

try {

bill(order);

return new AsyncResult<BillingStatus>(

BillingStatus.COMPLETE);

} catch (BillingException be) {

return new AsyncResult<BilllingStatus>(

BillingStatus.BILLING_FAILED);

}

}

...

}

Page 20: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

Java Persistence API 2.0

� Object-relational mapping enhancements

• Collections, maps and ordered lists

• Unidirectional one-to-many mapping

• Join tables for one-to-one, many-to-one

� Query and EntityManager API enhancements

• First result, max result, unwrapping

� JPQL enhancements

• CASE, NULLIF, COALESCE

� Criteria API

� Second-level caching

� Many, many more

Page 21: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

Mapping Collections@Entity

@Table(name=“USERS”)

public class User {

@Id

@GeneratedValue

@Column(name=“USER_ID”)

public long userId;

public String userName;

@Column(name=“BIRTH_DATE”)

public Date birthDate;

...

@ElementCollection

@CollectionTable(name=”ALIASES”)

@Column(name=”ALIAS”)

public Set<String> aliases;

@ElementCollection

public Map<String, String> photos;

}

Page 22: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

Unidirectional One-to-Many Relationship@Entity

public class User {

@Id @GeneratedValue

public long id;

public String userName;

...

@OneToMany

@JoinColumn(name=”USER_ID”)

public Set<Phone> phones;

}

@Entity

public class Phone {

@Id @GeneratedValue

public long id;

public String type;

public String number;

...

}

Page 23: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

Criteria API

QueryDefinition queryDefinition = entityManager

.getQueryBuilder().createQueryDefinition();

DomainObject user = queryDefinition.addRoot(User.class);

user.select(user)

.where(user.get(“firstName").equal(“John”)

.and(user.get("lastName").equal("Smith")));

SELECT user

FROM User user

WHERE user.firstName = ‘John’

AND user.lastName = ‘Smith’

Page 24: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

Servlet 3.0

� Annotations from the ground-up

� Optional web.xml

� Intelligent defaults

� Modular web.xml fragments in framework library jars

� Programmatic addition of Servlets, Filters and Listeners through

the ServletContext

� Asynchronous processing support in Servlets

Page 25: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

Servlet Annotations Example

@WebServlet(name=“PlaceBidServlet”

urlPatterns={“/bid”, “/place-bid”})

public class PlaceBidServlet extends HttpServlet {

@EJB

private PlaceBid placeBid;

public void doGet(HttpServletRequest request,

HttpServletResponse response) {

Bid bid = new Bid();

...

placeBid.placeBid(bid);

...

}

}

Page 26: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

Programmatic Servlet Addition

@WebServletContextListener

public class ActionBazaarListener

implements ServletContextListener {

public void contextInitialized(

ServletContextEvent event) {

ServletContext context = event.getServletContext();

context.addServlet(

“PlaceBidServlet",

“Place bid servlet",

“actionBazaar.PlaceBidServlet",

null, -1);

context.addServletMapping(

“PlaceBidServlet",

new String[]{"/place-bid"});

}

}

Page 27: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

Java API for RESTful Web Services

� Web services through REST instead of SOAP

� REST counterpart of JAX-WS

� Gets rid of low-level code so you can focus on core logic

� Annotations from the ground-up

� Integrated with WebBeans, EJB and JPA

Page 28: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

Session Bean using JAX-RS@Stateless

@Path("/webservices")

public class PlaceBidBean {

@PersistenceContext

private EntityManager entityManager;

@PUT

@Path("/bid/{bidder}")

public void placeBid(

@PathParam(“bidder")

String bidder,

@QueryParam(“item")

String item,

@QueryParam(“bid_price")

Double bidPrice) {

entityManager.persist(

new Bid(bidder, item, bidPrice));

}

}

Page 29: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

Summary

� Pruning

• Chopping dead wood

• JAX-RPC, EJB 2.x Entity Beans, JAXR, JSR-88, JSR-77

� Profiles

• Web profile geared towards majority of Java EE applications

• Lightweight, modular application servers

• Easier to integrate non-standard solutions

� Innovation

• JCDI, JSF 2.0, EJB 3.1, JPA 2.0, Servlet 3.0, JAX-RS 1.1

� Your help is needed

� Send comments to [email protected]!

Page 30: tssjs java ee 6 roundtable [Read-Only] - Java …javasymposium.techtarget.com/html/images/RRahman_JavaEE6...Major API Changes in Java EE 6 Java Contexts and Dependency Injection •

References

� Java EE 6, http://jcp.org/en/jsr/detail?id=316

� WebBeans, http://jcp.org/en/jsr/detail?id=299

� JSF 2.0, http://jcp.org/en/jsr/detail?id=314

� EJB 3.1, http://jcp.org/en/jsr/detail?id=318

� JPA 2.0, http://jcp.org/en/jsr/detail?id=317

� Servlet 3.0, http://jcp.org/en/jsr/detail?id=315

� JAX-RS 1.1, http://jcp.org/en/jsr/detail?id=311