• No results found

Java EE 5, 6 et les EJBs 3.1

N/A
N/A
Protected

Academic year: 2021

Share "Java EE 5, 6 et les EJBs 3.1"

Copied!
79
0
0

Loading.... (view fulltext now)

Full text

(1)

1

Java EE 5, 6

et les EJBs 3.1

Antonio Goncalves

(2)

Agenda

• Présentation

• Java EE 5

• Java EE 5 vs J2EE 1.4

• Java EE 6

• EJB 3.1

– Encore plus facile

– Nouvelles fonctionnalités

(3)

Agenda

• Présentation

Java EE 5

Java EE 5 vs J2EE 1.4

Java EE 6

EJB 3.1

Encore plus facile

Nouvelles fonctionnalités

(4)

Antonio Goncalves

• Consultant /

Architecte

senior

• Spécialiste Java / Java EE

• Ancien de

BEA Systems

• Enseignant à l’université du

CNAM

• Expert Group

– JSR 316 (Java EE 6)

– JSR 317 (JPA 2.0)

– JSR 318 (EJB 3.1)

• Auteur Java EE 5

(5)

Et vous ?

• J2EE 1.4

• Java EE 5

• Spring / Hibernate / Struts

• Java EE 6

(6)

Agenda

Présentation

• Java EE 5

Java EE 5 vs J2EE 1.4

Java EE 6

EJB 3.1

Encore plus facile

Nouvelles fonctionnalités

(7)

Historique de Java EE

Projet JPE

Mai 1998

J2EE 1.2

Servlet

JSP

EJB

JMS

RMI/IIOP

Déc 1999

10 specs

J2EE 1.3

CMP

Connecteur

Sept 2001

13 specs

Robustesse

Application

d’entreprise

J2EE 1.4

Services web

Déploiement

Nov 2003

20 specs

Services web

Java EE 5

Annotations

EJB 3

Persistance

Services web

Mai 2006

23 specs

Facilité de

développement

Java EE 6

Profiles

EJB 3.1

JPA 2.0

Servlet 3

JSF 2.0

JAX-RS

2009

~

27 specs

Facilité de

développement

(web)

(8)

• Développé sous la JSR 244

• Sortie en Mai 2006 (+ GlassFish V1)

• Facilité de développement

– EoD (Ease of Development)

• Compatible avec J2EE 1.4

• Le meilleur des deux mondes

– Spécifications

– Inspiré de l'Open source

(9)

Rupture

par rapport à J2EE 1.4

• Simplification des

EJB

et

WebServices

• Nouvelle API de persistance (

JPA

)

• Model de développement POJO

• Injection de dépendance

• Utilisation des

annotations

• Descripteurs XML optionnel

• Développement par exception

• Facilite les applications web avec

JSF

(10)

Contenu Java EE 5

• 23 specifications

– Web

:

JSF 1.2

JSF 1.2

, JSP 2.1, JSTL 1.2, Servlet 2.5

– Enterprise

: Common Annotations 1.0,

EJB 3.0

EJB 3.0

, JAF

1.1, JavaMail 1.4, JCA 1.5, JMS 1.1,

JPA 1.0

JPA 1.0

, JTA 1.1

– Web Services

: JAX-RPC 1.1, JAX-WS 2.0, JAXB 2.0,

SAAJ 1.0, StAX 1.0,

Web Services 1.2

Web Services 1.2

, Web Services

Metadata 2.0

– Management & Security

: JACC 1.1, Java EE

(11)

Agenda

Présentation

Java EE 5

Java EE 5 vs J2EE 1.4

Java EE 6

EJB 3.1

Encore plus facile

Nouvelles fonctionnalités

(12)

Stateless EJB 2.1

Home interface

public interface HelloHome extends EJBHome {

Hello create() throws RemoteException,

CreateException;

}

Interface locale

public interface Hello extends EJBObject {

String

sayHello()

throws RemoteException;

Date

today()

throws RemoteException;

}

EJB

public class HelloBean implements SessionBean {

public HelloBean() { }

public

String sayHello() {return "Hello Petstore !";}

public

Date today() {return new Date(); }

public void ejbCreate() throws CreateException { }

public void setSessionContext(

SessionContext sessionContext)

throws EJBException { }

public void ejbRemove() throws EJBException { }

public void ejbActivate() throws EJBException { }

public void ejbPassivate() throws EJBException { }

}

Descripteur

<ejb-jar>

<enterprise-beans>

<session>

<display-name>HelloSB</display-name>

<ejb-name>HelloBean</ejb-name>

<home>HelloHome</home>

<remote>Hello</remote>

<ejb-class>HelloBean</ejb-class>

<session-type>Stateless</session-type>

<transaction-type>Container</transaction-type>

</session>

<assembly-descriptor>

<container-transaction>

<method>

<ejb-name>HelloBean</ejb-name>

<method-name>*</method-name>

</method>

<trans-attribute>Required</trans-attribute>

</container-transaction>

</assembly-descriptor>

</enterprise-beans>

</ejb-jar>

(13)

Stateless EJB 3.0

Interface

@Remote

public interface Hello {

String

sayHello()

throws RemoteException;

Date

today()

throws RemoteException;

}

EJB

@Stateless

public class HelloBean implements Hello {

public

String sayHello() {return "Hello Petstore !";}

public

Date today() {return new Date(); }

(14)

Annotations des EJBs 3.0

• @Stateless, @Stateful, @MessageDriven

• @Local, @Remote.

• @PostConstruct, @PreDestroy,

@PrePassivate...

• @Ejb, @Resource, @WebServiceRef

• @TransactionAttribute(REQUIRED)

• @RolesAllowed, @PermitAll

(15)

Entity Bean CMP 2.1

Home interface

public interface HelloHome extends EJBHome {

Hello create(String cle) throws

RemoteException, CreateException;

Hello findByPrimaryKey(String cle) throws

RemoteException, FinderException;

}

Interface locale

public interface Hello extends EJBObject {

String

getCle()

throws RemoteException;

String

getValeur()

throws RemoteException;

void

setValeur(String valeur)

throws RemoteException;

}

EJB

public abstract class

HelloBean

implements EntityBean {

public abstract String

getCle()

throws RemoteException;

public abstract void

setCle(String cle)

throws RemoteException;

public abstract String

getValeur()

throws RemoteException;

public abstract void

setValeur(String valeur)

throws

RemoteException;

public String ejbCreate(String cle) throws RemoteException,

CreateException {

setCle(cle); return null;

}

public void ejbPostCreate(String cle) throws CreateException{ }

public void setEntityContext(EntityContext entityContext)

throws EJBException { }

public void unsetEntityContext() throws EJBException { }

public void ejbRemove() throws RemoveException, EJBException { }

public void ejbActivate() throws EJBException { }

public void ejbPassivate() throws EJBException { }

public void ejbLoad() throws EJBException { }

public void ejbStore() throws EJBException { }

}

Descripteur ejb-jar.xml

<ejb-jar>

<enterprise-beans>

<entity>

<display-name>HelloEB</display-name>

<ejb-name>HelloBean</ejb-name>

<home>HelloHome</home>

<remote>Hello</remote>

<ejb-class>HelloBean</ejb-class>

<persistence-type>Container</persistence-type>

<prim-key-class>java.lang.String</prim-key-class>

<reentrant>False</reentrant>

<cmp-version>2.x</cmp-version>

<abstract-schema-name>Hello</abstract-schema-name>;

<cmp-field>

<field-name>cle</field-name>

</cmp-field>

<cmp-field>

<field-name>valeur</field-name>

</cmp-field>

<primkey-field>cle</primkey-field>

</entity>

</enterprise-beans>

</ejb-jar>

Descripteur propriétaire

<jbosscmp-jdbc>

<defaults>

<datasource>java:/petstoreDS</datasource>

<datasource-mapping>mySQL</datasource-mapping>

</defaults>

<enterprise-beans>

<entity>

<ejb-name>HelloBean</ejb-name>

<table-name>HELLO_PETSTORE</table-name>

<cmp-field>

<field-name>cle</field-name>

<column-name>key</column-name>

<jdbc-type>VARCHAR</jdbc-type>

<sql-type>varchar(10)</sql-type>

</cmp-field>

<cmp-field>

<field-name>valeur</field-name>

<column-name>value</column-name>

<jdbc-type>VARCHAR</jdbc-type>

<sql-type>varchar(50)</sql-type>

</cmp-field>

</entity>

</enterprise-beans>

</jbosscmp-jdbc>

(16)

Entity JPA

POJO

@Entity

public class Hello {

@Id

private Long cle;

private String valeur;

public String

getCle()

{return cle;}

public String

getValeur()

{return valeur;}

public void

setValeur()

{this.valeur = valeur;}

}

(17)

Service Web J2EE 1.4

Service Web

public class HelloServiceImpl implements HelloServiceSEI {

public String

sayHello

(String param)

throws java.rmi.RemoteException {

return “Hello “ + param;

}

}

package endpoint;

import java.rmi.*;

public interface HelloServiceSEI extends java.rmi.Remote {

public String

sayHello

(String param)

throws java.rmi.RemoteException;

}

<?xml version='1.0' encoding='UTF-8' ?>

<webservices

xmlns='http://java.sun.com/xml/ns/j2ee'

version='1.1'>

<webservice-description>

<webservice-description-name>

HelloService</webservice-description-name>

<wsdl-file>

WEB-INF/wsdl/HelloService.wsdl</wsdl-file>

<jaxrpc-mapping-file>

WEB-INF/HelloService-mapping.xml

</jaxrpc-mapping-file>

<port-component

xmlns:wsdl-port_ns='urn:HelloService/wsdl'>

<port-component-name>HelloService</port-component-name>

<wsdl-port>wsdl-port_ns:HelloServiceSEIPort</wsdl-port>

<service-endpoint-interface>

endpoint.HelloServiceSEI</service-endpoint-interface>

<service-impl-bean>

<servlet-link>WSServlet_HelloService</servlet-link>

</service-impl-bean>

</port-component>

</webservice-description>

</webservices>

<?xml version='1.0' encoding='UTF-8' ?>

<configuration

xmlns='http://java.sun.com/xml/ns/jax-rpc/ri/config'>

<service name='HelloService'

targetNamespace='urn:HelloService/wsdl'

typeNamespace='urn:HelloService/types'

packageName='endpoint'>

<interface name='endpoint.HelloServiceSEI'

servantName='endpoint.HelloServiceImpl'>

</interface>

</service>

</configuration>

(18)

Service Web Java EE 5

package endpoint;

import javax.jws.WebService;

@WebService

public class Hello {

public String

sayHello

(String param) {

return “Hello “ + param;

}

}

(19)

JAXB 1.0

• Classe générée

// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> // Any modifications to this file will be lost upon recompilation of the source schema. // Generated on: 2005.04.21 ?? 10:37:56 PDT //

package generated.impl;

public class PointTypeImpl implements generated.PointType, com.sun.xml.bind.JAXBObject, generated.impl.runtime.UnmarshallableObject, generated.impl.runtime.XMLSerializable, generated.impl.runtime.ValidatableObject {

protected boolean has_Y; protected float _Y; protected boolean has_X; protected float _X;

public final static java.lang.Class version = (generated.impl.JAXBVersion.class); private static com.sun.msv.grammar.Grammar schemaFragment; private final static java.lang.Class PRIMARY_INTERFACE_CLASS() { return (generated.PointType.class); } public float getY() { return _Y; } public void setY(float value) { _Y = value; has_Y = true; } public float getX() { return _X; } public void setX(float value) { _X = value; has_X = true; }

public generated.impl.runtime.UnmarshallingEventHandler createUnmarshaller(generated.impl.runtime.UnmarshallingContext context) { return new generated.impl.PointTypeImpl.Unmarshaller(context);

}

public void serializeBody(generated.impl.runtime.XMLSerializer context) throws org.xml.sax.SAXException { if (!has_Y) { context.reportError(com.sun.xml.bind.serializer.Util.createMissingObjectError(this, "Y")); } if (!has_X) { context.reportError(com.sun.xml.bind.serializer.Util.createMissingObjectError(this, "X")); } context.startElement("", "x"); context.endNamespaceDecls(); context.endAttributes(); try { context.text(javax.xml.bind.DatatypeConverter.printFloat(((float) _X)), "X"); } catch (java.lang.Exception e) { generated.impl.runtime.Util.handlePrintConversionException(this, e, context); } context.endElement(); context.startElement("", "y"); context.endNamespaceDecls(); context.endAttributes(); try {

context.text(javax.xml.bind.DatatypeConverter.printFloat(((float) _Y)), "Y"); } catch (java.lang.Exception e) { generated.impl.runtime.Util.handlePrintConversionException(this, e, context); } context.endElement(); }

public void serializeAttributes(generated.impl.runtime.XMLSerializer context) throws org.xml.sax.SAXException { if (!has_Y) { context.reportError(com.sun.xml.bind.serializer.Util.createMissingObjectError(this, "Y")); } if (!has_X) { context.reportError(com.sun.xml.bind.serializer.Util.createMissingObjectError(this, "X")); } }

public void serializeURIs(generated.impl.runtime.XMLSerializer context) throws org.xml.sax.SAXException { if (!has_Y) { context.reportError(com.sun.xml.bind.serializer.Util.createMissingObjectError(this, "Y")); } if (!has_X) { context.reportError(com.sun.xml.bind.serializer.Util.createMissingObjectError(this, "X")); } }

public java.lang.Class getPrimaryInterface() { return (generated.PointType.class); }

public com.sun.msv.verifier.DocumentDeclaration createRawValidator() { if (schemaFragment == null) { schemaFragment = com.sun.xml.bind.validator.SchemaDeserializer.deserialize(( "\u00ac\u00ed\u0000\u0005sr\u0000\u001fcom.sun.msv.grammar.SequenceExp\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0000xr\u0000\u001dcom.su" +"n.msv.grammar.BinaryExp\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0002L\u0000\u0004exp1t\u0000 Lcom/sun/msv/gra" +"mmar/Expression;L\u0000\u0004exp2q\u0000~\u0000\u0002xr\u0000\u001ecom.sun.msv.grammar.Expressi" +"on\u00f8\u0018\u0082\u00e8N5~O\u0002\u0000\u0002L\u0000\u0013epsilonReducibilityt\u0000\u0013Ljava/lang/Boolean;L\u0000\u000b" +"expandedExpq\u0000~\u0000\u0002xpppsr\u0000\'com.sun.msv.grammar.trex.ElementPatt" +"ern\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0001L\u0000\tnameClasst\u0000\u001fLcom/sun/msv/grammar/NameClass;" +"xr\u0000\u001ecom.sun.msv.grammar.ElementExp\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0002Z\u0000\u001aignoreUndecl" +"aredAttributesL\u0000\fcontentModelq\u0000~\u0000\u0002xq\u0000~\u0000\u0003pp\u0000sq\u0000~\u0000\u0000ppsr\u0000\u001bcom.s" +"un.msv.grammar.DataExp\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0003L\u0000\u0002dtt\u0000\u001fLorg/relaxng/dataty" +"pe/Datatype;L\u0000\u0006exceptq\u0000~\u0000\u0002L\u0000\u0004namet\u0000\u001dLcom/sun/msv/util/String" +"Pair;xq\u0000~\u0000\u0003ppsr\u0000\"com.sun.msv.datatype.xsd.FloatType\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002" +"\u0000\u0000xr\u0000+com.sun.msv.datatype.xsd.FloatingNumberType\u00fc\u00e3\u00b6\u0087\u008c\u00a8|\u00e0\u0002\u0000\u0000" +"xr\u0000*com.sun.msv.datatype.xsd.BuiltinAtomicType\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0000xr\u0000" +"%com.sun.msv.datatype.xsd.ConcreteType\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0000xr\u0000\'com.sun" +".msv.datatype.xsd.XSDatatypeImpl\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0003L\u0000\fnamespaceUrit\u0000" +"\u0012Ljava/lang/String;L\u0000\btypeNameq\u0000~\u0000\u0014L\u0000\nwhiteSpacet\u0000.Lcom/sun/" +"msv/datatype/xsd/WhiteSpaceProcessor;xpt\u0000 http://www.w3.org/" +"2001/XMLSchemat\u0000\u0005floatsr\u00005com.sun.msv.datatype.xsd.WhiteSpac" +"eProcessor$Collapse\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0000xr\u0000,com.sun.msv.datatype.xsd.W" +"hiteSpaceProcessor\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0000xpsr\u00000com.sun.msv.grammar.Expre" +"ssion$NullSetExpression\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0000xq\u0000~\u0000\u0003ppsr\u0000\u001bcom.sun.msv.ut" +"il.StringPair\u00d0t\u001ejB\u008f\u008d\u00a0\u0002\u0000\u0002L\u0000\tlocalNameq\u0000~\u0000\u0014L\u0000\fnamespaceURIq\u0000~\u0000" +"\u0014xpq\u0000~\u0000\u0018q\u0000~\u0000\u0017sr\u0000\u001dcom.sun.msv.grammar.ChoiceExp\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0000xq\u0000" +"~\u0000\u0001ppsr\u0000 com.sun.msv.grammar.AttributeExp\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0002L\u0000\u0003expq\u0000" +"~\u0000\u0002L\u0000\005valuexp\u0000psq\u0000~\u0000\u000bppsr\u0000\"com.sun.msv.datatype.xsd.QnameType\u0000\u0000\u0000\u0000" +"\u0000\u0000\u0000\u0001\u0002\u0000\u0000xq\u0000~\u0000\u0011q\u0000~\u0000\u0017t\u0000\u0005QNameq\u0000~\u0000\u001bq\u0000~\u0000\u001dsq\u0000~\u0000\u001eq\u0000~\u0000)q\u0000~\u0000\u0017sr\u0000#com." +"sun.msv.grammar.SimpleNameClass\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0002L\u0000\tlocalNameq\u0000~\u0000\u0014L" +"\u0000\fnamespaceURIq\u0000~\u0000\u0014xr\u0000\u001dcom.sun.msv.grammar.NameClass\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001" +"\u0002\u0000\u0000xpt\u0000\u0004typet\u0000)http://www.w3.org/2001/XMLSchema-instancesr\u00000" +"com.sun.msv.grammar.Expression$EpsilonExpression\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0000x" +"q\u0000~\u0000\u0003sq\u0000~\u0000$\u0001psq\u0000~\u0000+t\u0000\u0001xt\u0000\u0000sq\u0000~\u0000\u0006pp\u0000sq\u0000~\u0000\u0000ppq\u0000~\u0000\u000esq\u0000~\u0000 ppsq\u0000~" +"\u0000\"q\u0000~\u0000%pq\u0000~\u0000&q\u0000~\u0000-q\u0000~\u00001sq\u0000~\u0000+t\u0000\u0001yq\u0000~\u00005sr\u0000\"com.sun.msv.gramma" +"r.ExpressionPool\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0001L\u0000\bexpTablet\u0000/Lcom/sun/msv/gramma" +"r/ExpressionPool$ClosedHash;xpsr\u0000-com.sun.msv.grammar.Expres" +"sionPool$ClosedHash\u00d7j\u00d0N\u00ef\u00e8\u00ed\u001c\u0003\u0000\u0003I\u0000\u0005countB\u0000\rstreamVersionL\u0000\u0006par" +"entt\u0000$Lcom/sun/msv/grammar/ExpressionPool;xp\u0000\u0000\u0000\u0005\u0001pq\u0000~\u0000\u0005q\u0000~\u0000!" +"q\u0000~\u00008q\u0000~\u0000\nq\u0000~\u00007x")); }

return new com.sun.msv.verifier.regexp.REDocumentDeclaration(schemaFragment); }

public class Unmarshaller

extends generated.impl.runtime.AbstractUnmarshallingEventHandlerImpl {

public Unmarshaller(generated.impl.runtime.UnmarshallingContext context) { super(context, "---");

}

protected Unmarshaller(generated.impl.runtime.UnmarshallingContext context, int startState) { this(context);

state = startState; } public java.lang.Object owner() { return generated.impl.PointTypeImpl.this; }

public void enterElement(java.lang.String ___uri, java.lang.String ___local, java.lang.String ___qname, org.xml.sax.Attributes __atts) throws org.xml.sax.SAXException { int attIdx; outer: while (true) { switch (state) { case 3 : if (("y" == ___local)&&("" == ___uri)) { context.pushAttributes(__atts, true); state = 4; return ; } break; case 0 : if (("x" == ___local)&&("" == ___uri)) { context.pushAttributes(__atts, true); state = 1; return ; } break;

308 lignes pour

<point><x>1</x><y>2</y></point>

38 fichiers

• 219 Ko de code

(20)

JAXB 2.0

Classe générée

@XmlAccessorType

(FIELD)

@XmlType

(name = “”, propOrder = {“x”, “Y”})

@XmlRootElement

(name = “point”)

public class Point {

protected float x;

protected flota y;

public float getX() {

return x;

}

public void setX(float value) {

this.x = value;

}

public float getY() {

return y;

}

public void setY(float value) {

this.y = value;

}

}

62 lignes pour

<point><x>1</x><y>2</y></point>

• 2

fichiers

• 3 Ko de code

(21)

Lookup J2EE 1.4

public class MyEJB implements SessionBean {

private DataSource myDS;

public void ejbCreate() {

try {

InitialContext ctx = new InitialContext();

myDS = (DataSource)ctx.

lookup

(“myDS”);

} catch (NamingException ex) {

// Que faire

}

}

}

(22)

Injection en Java EE 5

@Stateless

public class MyEJB {

@Resource

(name=“myDs”) DataSource myDS;

@EJB

private HelloEJB helloEjb;

(23)

Moins de lignes de code

• Adventure Builder

– J2EE 1.4 – 67 classes, 3284 lignes de code

– Java EE 5 – 43 classes, 2777 lignes de code

– 36%

de classes en moins

• RosterApp

– J2EE 1.4 – 17 classes, 987 lignes de code

– Java EE 5 – 7 classes, 716 lignes de code

– J2EE 1.4 descripteurs XML– 9 fichiers, 792 lignes

– Java EE 5 descripteurs XML – 1 fichier, 5 lignes

– 58%

de classes en moins,

89%

de fichiers XML en

moins

(24)

IDEs pour Java EE 5

• NetBeans

• IntelliJ IDEA

• Eclipse

• JBuilder

• JDeveloper

• JBoss IDE

• BEA Workshop Studio

(25)

Les serveurs d’applications

• GlassFish & Sun Java System AppServer

• BEA's WebLogic Server 10

• TmaxSoft JEUS 6

• SAP Netweaver 7.1

• Oracle OC4J 11

• Apache Geronimo 2.0

• JOnAS 5.0

• Websphere 7

• JBoss 5.0 beta

(26)

Tests

• Pas de spécification

– JUnit

– HTTPUnit

– Canoo WebTest

– Mock

– …

• Plus facile à tester

– POJO

– JPA, moins besoin du conteneur (EntityManager)

– Ejb3Unit (out of container EJB 3.0 testing)

– JBoss et GlassFish V3

(27)

Le déploiement est facilité

• Descripteurs de déploiement optionnels

(ejb-jar.xml, application.xml)

– sauf web.xml, faces-config.xml

• Utilisation des annotations

– définir les composants (EJB, WebService…)

– ORM avec JPA (

@Entity, @Id, @Colmumn

…)

– définir sécurité (

@DenyAll, @PermitAll

…)

– mode transactionnel (

@TransactionAttribute

)

• Pour les clients riches

– Java Web Start

(28)

Packaging

• Conventions

.war

pour les applications web

.rar

pour les ressources

– Le répertoire

lib

contient les fichiers jar commun à

l’application (classpath automatique)

.jar

avec un attribut Main-Class pour les applications

clientes (

ACC

)

.jar

avec une classe annotée

@Stateless

pour les

EJBs

(29)
(30)

Compatibilité avec J2EE 1.4

• Compatibilité ascendante

assurée dans la

spécification

• EJB 3.0 peuvent être clients des EJB 2.1 (et

inversement

)

• JPQL peut être utilisé dans les méthodes

finder() des anciens EJB 2.1

• Compatibilité des EL ($, #) entre JSP et JSF

• JSP 2.0 s’exécutent dans un conteneur JSP

(31)

Les patterns ?

• DTO

– Les Entity JPA peuvent servir de DTO

• DAO

– EntityManager pour requêtes simples (CRUD)

– DAO générique

• Service Locator

– Injection, Application Client Container

• Delegate

– Injection

• Action Controller

(32)

Java EE 5 et Couche Web

• Pas autant d’améliorations que la couche EJB

– JSP 2.1, JSF 1.2, JSTL

• Problèmes maintenus

– Back-refresh du navigateur

– Double click, double thread

– Dialogues/flow => Application, Session, Request

=>

JBoss Seam

(future WebBeans avec Java EE 6)

• Utilisation d’autres frameworks

– Clay, Yahoo UI, Facelets, Seam, Shale, Ajax4JSF,

ICEFaces, Avatar

(33)

Agenda

Présentation

Java EE 5

Java EE 5 vs J2EE 1.4

Java EE 6

EJB 3.1

Encore plus facile

Nouvelles fonctionnalités

(34)

A propos de Java EE 6

• Développé sous la JSR 316

• Sortie Q1 2009 (+ GlassFish V3)

• Utilisation de profiles

• Pruning

: suppression progressive

– EJB CMP => JPA

– JAX-RPC => JAX-WS

• Servlet 3.0

et

JSF 2.0

(annotations)

• WebBeans 1.0

(inspiré de JBoss Seam)

(35)

Contenu Java EE 6

• ~27 specifications

– Web

:

JSF 2.0

JSF 2.0

, JSP 2.2, JSTL 1.2,

Servlet

Servlet

3.0, JAX-

3.0,

JAX-RS 1.0, Web Beans 1.0

RS 1.0, Web Beans 1.0

– Enterprise

: Common Annotations 1.0,

EJB 3.1

EJB 3.1

, JAF

1.1, JavaMail 1.4, JCA 1.5, JMS 1.1,

JPA 2.0

JPA 2.0

, JTA 1.1

– Web Services

: JAX-RPC 1.1, JAX-WS 2.0, JAXB 2.0,

SAAJ 1.0, StAX 1.0, Web Services 2.0, Web Services

Metadata 2.0

– Management & Security

: JACC 1.1, Java EE

(36)

Servlet 2.5

Servlet

public class SimpleSample extends

HttpServlet

HttpServlet

{

public void doGet (HttpServletRequest req,

HttpServletResponse res){

}

}

web.xml

<web-app>

<servlet>

<servlet-name>

MyServlet

MyServlet

</servlet-name>

<servlet-class>samples.SimpleSample</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>MyServlet</servlet-name>

<url-pattern>/

MyApp

MyApp

</url-pattern>

</servlet-mapping>

</web-app>

(37)

Servlet 3.0

@Servlet(urlMapping=”MyApp”, name=”MyServlet”)

public class SimpleSample {

@Get

public void handleGet(HttpServletRequest req,

HttpServletResponse res) {

}

(38)

Servlet 3.0

• Développé sous la JSR 315

• web.xml (optionel) modulaire

– Découpé en web-fragment

• Servlet asynchrone (style Comet)

• Programmation par exception

(39)

JSF 2.0

• Développé sous la JSR 314

• Inspiré par Facelet

• Facilite la création de composants graphiques

• Support d'Ajax

• Moins de configuration (faces-config.xml)

• Comet ?

(40)

REST

• REST

(REpresentational State Transfer)

– Architectures orientées ressource

– Tout est ressource

– Toute ressource est adressable par une URL

• Sémantique CRUD

– GET /articles/123

– DELETE /articles/123

HTTP

SQL

DELETE

DELETE

Supprimer

POST

UPDATE

Mise à jour

GET

SELECT

Lire

PUT

INSERT

Créer

(41)

JAX-RS 1.0

@Remote

@Path

("orders/{order_id}")

public class OrderResource {

@Get

Order getOrder(

@PathParam

("order_id") String id) {

...

}

}

(42)

Web Beans 1.0

• Développé sous la JSR 299

• Inspiré de JBoss Seam

• Spec Lead : Gavin King

• Unifie la couche web et enterprise

– Glue entre composants (JSF et les EJBs)

– Contexte conversationel

• Un composant Web Beans peut être :

– EJB

– Managed Bean JSF

– Entity JPA

(43)

Web Beans 1.0

Composant Web Beans

public

@Component

class Hello {

public String say(String name) {

return "hello " + name;

}

}

JSF

<h:commandButton value="Say Hello"

(44)

Web Beans 1.0

Composant Web Beans

@Stateless

public

@Component

class Hello {

public String say(String name) {

return "hello " + name;

}

}

JSF

<h:commandButton value="Say Hello"

(45)

Agenda

Présentation

Java EE 5

Java EE 5 vs J2EE 1.4

Java EE 6

EJB 3.1

Encore plus facile

Nouvelles fonctionnalités

(46)

EJB 3.1

• Goals

– Easier to use

– New features

• EJB 3.1 - JSR 318

– Shipped with Java EE 6 – JSR 316

– Started in August 2007

– ~20 expert members : companies/individuals

– Early draft in February 2008

(47)

Disclaimer

• Still under work

• APIs might change

• Q1 2009

(48)

Agenda

Présentation

Java EE 5

Java EE 5 vs J2EE 1.4

Java EE 6

EJB 3.1

Encore plus facile

Nouvelles fonctionnalités

(49)

Local interface

@Stateless

public class HelloBean implements

Hello

{

public String sayHello() {

return "Hello Tours JUG !";

}

}

@Local

public interface

Hello

{

String sayHello();

}

@EJB

private

Hello

h;

...

h.sayHello();

(50)

Optional local interface

• Interfaces are not always needed

• “no-interface” view

– Just the bean

– All methods are exposed

– Client injection still needed

– All container services still available

(51)

Optional local interface

@Stateless

public class HelloBean {

public String sayHello() {

return "Hello Tours JUG";

}

}

@EJB

private

HelloBean

h;

...

h.sayHello();

(52)
(53)

Packaging

• EJB ==

.ear

lib

directory has simplified deployment

• But

ear

file is still needed to deploy an EJB

• Even for a simple web application

=> Deploy an EJB 3.1 in a

war

file

• Keeping all EJBs functionalities

(54)
(55)

Java EE 6 profiles

• Still in discussion !!!

Web

Servlet 3.0

JSP 2.2

JSR-45

EL 1.2

JSTL 1.2

JSR-250

Web Dev

Servlet 3.0

JSP 2.2

JSR-45

EL 1.2

JSTL 1.2

JSR-250

EJB 3.1 (Lite)

JTA 1.1

JPA 2.0

JSF 2.0

Web Beans 1.0

Servlet 3.0

JSP 2.2

JSR-45

EL 1.2

JSTL 1.2

JSR-250

EJB 3.1

JTA 1.1

JPA 2.0

JSF 2.0

Web Beans 1.0

JAX-RS 1.0

Connectors 1.6

JAX-WS 2.2

JAXB 2.2

JSR-109 1.2

JSR-181 1.1

JMS 1.1

JAF 1.1

JavaMail 1.4

JSR-115

JSR-196

JSR-88 1.2

JSR-77 1.1

JAX-RPC1.1

JAXR 1.0

Full platform

(56)

EJB « lite »

• Subset of the EJB 3.1 API

• To be used in Web profile

Local Session Bean

Annotations

Injection

CMT / BMT

Interceptors

Security

Message Driven Beans

EJB Web Service Endpoint

RMI/IIOP Interoperability

Remote interface

EJB 2.x

Timer service

CMP / BMP

(57)

Global JNDI names

• Client inside a container

• Client outside a container

@EJB

Hello h;

Context ctx = new InitialContext();

Hello h = (Hello) ctx.lookup(

???

);

(58)

Global JNDI names

• Not defined in Java EE

• Vendor specific

– Not portable

• No standard syntax

mappedName

is a partial solution

• Global JNDI names are not just for EJBs

(datasource, connection factories, queues...)

• Work in progress !!!

(59)

Agenda

Présentation

Java EE 5

Java EE 5 vs J2EE 1.4

Java EE 6

EJB 3.1

Encore plus facile

Nouvelles fonctionnalités

(60)

Singleton

• New session bean component

– Looks like a stateless / stateful

– “no interface” view/local/remote interface

• One single EJB per application per JVM

• Use to share state in the entire application

• State not preserved after container shutdown

• Container initialization startup

(61)

Singleton

@Singleton

public class CachingBean {

private Map cache;

@PostConstruct void init() {

cache = ...;

}

public Map getCache() {

return cache;

}

public void addToCache(Object key, Object val) {

cache.put(key, val);

}

}

(62)

Singleton and concurrency

• Single thread (default) too restrictive

– One instance / Multiple threads

• CMC (Container Managed Concurrency)

– Concurrency through annotations

– Exclusive / shared lock

– Timeout

• BMC (Bean Managed Concurrency)

(63)

Read-Only Singleton (with CMC)

@Singleton

public class CachingBean {

private Map cache;

@ReadOnly

public Map getCache() {

return cache;

}

public void addToCache(Object key, Object val) {

cache.put(key, val);

}

}

(64)

Read-Mostly Singleton (with CMC)

@Singleton

@ReadOnly

public class CachingBean {

private Map cache;

public Map getCache() {

return cache;

}

@ReadWrite

public void addToCache(Object key, Object val) {

cache.put(key, val);

}

}

(65)

syncronized Singleton (BMC)

@Singleton

@BeanManagedConcurrency

public class CachingBean {

private Map cache;

public Map getCache() {

return cache;

}

synchronized

public void addToCache(Object key,

Object val){

cache.put(key, val);

}

}

(66)

Singleton Startup

• Richer lifecycle

– Container initialization

– Container shutdown

• Same callback annotations

– @PostConstruct / @PreDestroy

• @Startup

(67)

Singleton Startup

@Singleton

@Startup

public class CachingBean {

private Map cache;

@PostConstruct

void init() {

cache = ...;

}

public Map getCache() {

return cache;

}

public void addToCache(Object key, Object val) {

cache.put(key, val);

}

}

(68)

Timer Service

• Timer Service

– Added in EJB 2.1

– Didn't change in 3.0

– Improved in 3.1

• Programmatic and Calendar based scheduling

(69)

Timer Service

• « Last day of the month »

• « Every Friday at 4pm »

• « Every five minutes on Monday and

Thursday »

• Cron syntax

– second [0..59], minute [0..59], hour [0..23], year

– DayOfMonth [0..31]

– DayOfWeek [0..7] or [sun, mon, tue..]

– Month [1..12] or [jan,feb..]

(70)

Programmatic Timer Creation

@Stateless

public class wakeUpBean {

@Resource TimerService

timer

;

public void createWakeUpCall() {

// Every weekday at 9

ScheduleExpression expr = new ScheduleExpression().

dayOfWeek(“Mon-Fri”).hour(9);

timer.createTimer(expr);

}

@Timeout

void wakeUp(Timer t) {

...

}

}

(71)

Automatic Timer Creation

@Stateless

public class wakeUpBean {

@Schedule(dayOfWeek=“Mon-Fri”, hour=“9”)

void wakeUp() {

...

}

}

(72)

Asynchronous call

• How to have asynchronous call in EJBs ?

• JMS is to send messages not to do

asynchronous calls

• Threads are not allowed (don't integrate well)

• New model

– Annotations

(73)

Asynchronous call returning void

@Stateless

public class OrderBean {

public void createOrder() {

Order order = persistOrder();

sendEmail(order) ;

printOrder(order);

}

public Order persistOrder() {...}

@Asynchronous

public void sendEmail(Order order) {...}

@Asynchronous

public void printOrder(Order order) {...}

}

(74)

Asynchronous call returning value

@Stateless

public class OrderBean {

public void createOrder() {

Task task = new Task(...);

Future<int> computeTask

= compute(task);

...

int result =

computeTask.get()

;

}

@Asynchronous

public

Future<int>

sendEmail(Task task) {

int result = ...;

return new

javax.ejb.AsyncResult<int>(result)

;

}

(75)

Agenda

Présentation

Java EE 5

Java EE 5 vs J2EE 1.4

Java EE 6

EJB 3.1

Encore plus facile

Nouvelles fonctionnalités

(76)

• Couvre

– EJB Stateless, Stateful, MDB (JMS)

– JPA

– JSF

– WebServices

– Glassfish installation & configuration

– …

• Pour les développeurs et architectes

• Inspiré du PetStore de Sun

• 10 chapitres, développement incrémental

(77)

• Java EE 5

– http://java.sun.com/javaee/

– http://java.sun.com/javaee/5/docs/tutorial/doc/

• GlassFish

– https://glassfish.dev.java.net/

– http://www.glassfishwiki.org/

• Livre Java EE 5

– http://www.eyrolles.com/

– http://www.antoniogoncalves.org

Références

(78)
(79)

Java EE 5, 6

et les EJBs 3.1

References

Related documents

secondary  instability,  and  final  breakdown  into  turbulence...  Modelling  and

In this mode, RoadMatrix will run through the decision trees for each year in the programming period to determine a strategy for the program year based on the decision

Lebedev Physical Institute, Moscow, Russia 43: Also at California Institute of Technology, Pasadena, USA 44: Also at Budker Institute of Nuclear Physics, Novosibirsk, Russia 45: Also

• The G20 Development Working Group established a Dialogue Platform for Inclusive Green Invest- ments (DPIGI) that is to help mobilise public and private funds for inclusive

Using available published data from relevant surveys of AMF in flowering plants, we calculated the proportion of non- Glomeraceae DNA sequences and, where possible, the proportion

Longitudinal collection of electronic health information for and about persons, where health information is defined as information pertaining to the health of an

Based on the above considerations, this study sought to: (1) compare the experiences of people who are affected by diabetes-related hypoglycaemia and either do or do not require

This research work reports the outcome of a primary quantitative investigation into the factors responsible for the quick completion of project research by