EJB et WS (Montreal JUG - 12 mai 2011)
-
Upload
montreal-jug -
Category
Documents
-
view
3.276 -
download
2
description
Transcript of EJB et WS (Montreal JUG - 12 mai 2011)
![Page 1: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/1.jpg)
EJB and SOAP WS
Par Romain Rocèspour le Montréal JUG
![Page 2: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/2.jpg)
About me
Teacher at Supinfo Montreal
Romain Rocès
Blue belt on BlackBelt Factory
Romain Rocès
![Page 3: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/3.jpg)
Concept
![Page 4: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/4.jpg)
Concept
Provides different services (Session Bean), communicates with another application (Message Bean), saves information (Entity Bean).
EJB regroups several concepts
Just like a brain, EJB is the center of the application.It proposes many functions.
![Page 5: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/5.jpg)
Version 3
The previous version (2.1) was too complex
The version 3.0 tends to simplification :Less steps, less classes, less configurationImprovements from Java EE 5
AnnotationsGenericsJava Persistance API
Concepts remain the same, but Sun integrated many ideas from popular open-source projects like Spring or Hibernate.
![Page 6: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/6.jpg)
Application servers
WebLogic, by BEA
Oracle Application Server, by Oracle
JBoss, by RedHat
GlassFish, by Sun MicroSystems
![Page 7: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/7.jpg)
EJB session
Singleton, Stateless, Stateful
![Page 8: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/8.jpg)
Facade pattern
The Session Bean acts as a “facade”.It's the client's interlocutor
![Page 9: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/9.jpg)
Client accessDifferent clients can call the Session Bean methods if they possess its interface
Session Bean deployed on a serverWeb application
Desktop application
InterfaceImplementation
![Page 10: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/10.jpg)
![Page 11: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/11.jpg)
![Page 12: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/12.jpg)
Session Bean Local
Set @Local on the interface (not mandatory)
Used when the client is deployed in the same virtual machine
Example :A Web application deployed in the same server as the Session Bean
Advantage :Resource-friendlyMore secure
Disadvantage :Local scope
![Page 13: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/13.jpg)
Session Bean Local
Local interfaces are not mandatory
@Statelesspublic class HelloServiceBean { public String sayHello(){ return "Hello World"; }}
![Page 14: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/14.jpg)
Session Bean RemoteSet @Remote on the interface
Used when the client is located in a different virtual machine
Example :A web application deployed in a different server than the Session BeanA rich-client
Advantage :Open on the network
Disadvantage :Consumes more resources (uses RMI)Security
![Page 15: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/15.jpg)
Session Bean Remote
@Remotepublic interface HelloService {public String sayHello();}
@Statelesspublic class HelloServiceBean implements HelloService{ public String sayHello(){ return "Hello World"; }}
Interface
Implementation
![Page 16: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/16.jpg)
Stateless modeA Stateless Bean is not bound to any client
getPlaces() getPlaces()getTrips()
Exemples :Retrieve a list of productsHelloService
Advantage : Resource-friendly
![Page 17: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/17.jpg)
Stateless mode
@Statelesspublic class HelloWorld { public String sayHelloTo(String name){ return "Hello " + name; }}
![Page 18: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/18.jpg)
Stateful mode
getPlaces() getPlaces()
getTrips()
A Statefull Bean is bound to a client
Exemples :CartOrderService
Advantage : Impact on server performance
![Page 19: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/19.jpg)
Stateful mode
@Statefulpublic class OrderService { public void setName(String name){…}; public void setAddress(String address){…}; public void buyDog(){…}; …}
![Page 20: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/20.jpg)
Singleton modeOne Singleton Bean per JVM
Exemples :CounterCache
Advantage :Resource-friendlyOne instance
Disadvantage :One instance
![Page 21: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/21.jpg)
Singleton mode
@Singletonpublic class Counter { private int i = 0; public int getCount(){ return ++i; }}
![Page 22: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/22.jpg)
Asynchronous calls
How to have asynchronous call in EJBs ?
Threads don't integrate well
@Asynchronous
Method returns void or java.util.concurrent.Future<T>
![Page 23: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/23.jpg)
Asynchronous calls@Statelesspublic class HelloWorld { @EJB MailManager mailManager; public String sayHelloTo(String name){ mailManager.sendMail(); return "Hello " + name; }}
@Statelesspublic class MailManager { @Asynchronous public void sendMail(){ ... }}
![Page 24: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/24.jpg)
Timer Service
Programmatic and Calendar based scheduling« Last day of the month »« Every five minutes on Monday and Friday »
Cron-like syntaxsecond [0..59], minute[0..59], hour[0..23], yearDayOfMonth[1..31]dayOfWeek[0..7] or [sun, mon, tue..]Month[0..12] or [jan,feb..]
![Page 25: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/25.jpg)
Timer Service
@Statelesspublic class WakeUpBean { @Schedule(dayOfWeek=“Mon-Fri”, hour=“9”) void wakeUp() { ... }}
![Page 26: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/26.jpg)
Unit Test
![Page 27: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/27.jpg)
JUnit
Not in EJB context
@Test public void myTest(){ StatelessBean statelessBean = new StatelessBean(); statelessBean.getCounter(); }
![Page 28: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/28.jpg)
JUnit
With EJB context : embedded glassfish
@Testpublic void myTest() throws NamingException{ EJBContainer createEJBContainer = EJBContainer.createEJBContainer(); StatelessBean statelessBean = (StatelessBean)container.getContext() .lookup("java:global/classes/StatelessBean"); statelessBean.getCounter(); container.close();}
![Page 29: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/29.jpg)
Unit Test
Cactus is a simple test framework for unit testing server-side java code (Servlets, EJBs, Tag Libs, Filters, ...).
The Ejb3Unit project automates Entity and Session bean testing outside the container for the EJB 3.0 specification.
![Page 30: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/30.jpg)
Client connection
lookup & @EJB
![Page 31: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/31.jpg)
EJB connection with lookup
The client needs the JNDI context to connect to the server.
The client also needs the Session Bean interface
Retrieve the Session Bean with a lookup()
Then it's possible to call methods from the Bean
Context context = new InitialContext(); HelloService hello = (HelloService) context.lookup(HelloService.class.getName()); System.out.println(hello.sayHello());
The Session Bean will send you a message !
![Page 32: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/32.jpg)
EJB connection with lookup
GlassFish parametersjava.naming.factory.initial=com.sun.enterprise.naming.SerialInitContextFactoryjava.naming.factory.url.pkgs=com.sun.enterprise.namingjava.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl
jndi.properties file example
JBoss Parametersjava.naming.factory.initial=org.jnp.interfaces.NamingContextFactoryjava.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfacesjava.naming.provider.url = 127.0.0.1:1099
![Page 33: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/33.jpg)
EJB injection
In the same JVM, it's not necessary to do a lookup()
Obtain a Session Bean with resource injection
Used in other EJBs, web applications
public class ClientServiceBean implements ClientService {
@EJB private OrderService orderService;
...}
![Page 34: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/34.jpg)
Soap WS in EJB moduleAdd @WebService and @Stateless annotation on a classIt run !
@WebService@Statelesspublic class MyBeanPublic { @WebMethod public String helloWorld() { return null; }}
Default WSDL address : http://localhost:8080/MyBeanPublicService/MyBeanPublic?wsdl
![Page 35: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/35.jpg)
Soap WS in EJB moduleNow, we use our EJB @stateless in our SOAP WS.
@WebService@Statelesspublic class MyBeanPublic {
@EJB private MyBeanLocal ejbRef;
@WebMethod public String helloWorld() { return ejbRef.helloWorld(); }}
![Page 36: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/36.jpg)
Soap WS in EJB moduleExemple with NetBeans 7
![Page 37: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/37.jpg)
EJB entity
![Page 38: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/38.jpg)
Persistence UnitThe persistence unit makes the link between your application and a DataSource
![Page 39: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/39.jpg)
Persistence Unit
Different providers
Hibernate (use by default in JBoss)
TopLink (use by default in Glassfish v2)
EclipseLink (use by default in Glassfish v3)
![Page 40: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/40.jpg)
Persistence Unit
<?xml version="1.0" encoding="UTF-8"?><persistence version="2.0" ...> <persistence-unit name="montrealjugPU" transaction-type="JTA"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/firone</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes> <properties> <property name="eclipselink.ddl-generation" value="create-tables"/> </properties> </persistence-unit></persistence>
persistence.xml
![Page 41: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/41.jpg)
Persistence Unit
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd"><resources> <jdbc-connection-pool ...> <property name="serverName" value="localhost"/> <property name="portNumber" value="1527"/> <property name="databaseName" value="firone"/> <property name="User" value="firone"/> <property name="Password" value="firone"/> <property name="URL" value="jdbc:derby://localhost:1527/firone"/> <property name="driverClass" value="org.apache.derby.jdbc.ClientDriver"/> </jdbc-connection-pool> <jdbc-resource enabled="true" jndi-name="jdbc/firone" object-type="user" pool-name="derby_net_firone_fironePool"/></resources>
glassfish-resources.xml
![Page 42: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/42.jpg)
EntityManager in EJB
Obtain an EntityManager with injection
@Statelesspublic class DAO {
@PersistenceContext(unitName="montrealjugPU") protected EntityManager em;
public void createCat(Cat cat){ em.persist(cat); }}
![Page 43: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/43.jpg)
EJB message
Java Message Service
![Page 44: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/44.jpg)
JMS presentation
The same since 2002
Used when some information should be exchanged between
Two applications : point-to-point modelSeveral applications : publish and subscribe model
Asynchronous system : messages are received when the client request them
Similar to a mail system
![Page 45: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/45.jpg)
Queue mode
![Page 46: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/46.jpg)
Topic mode
![Page 47: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/47.jpg)
Messages
There are three different types of messages
TextMessage to send simple text
ObjectMessage for a serialized object
MapMessage contains a map with strings as keys and objects as values
![Page 48: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/48.jpg)
Send a message
In order to send a message, we have to:
Reclaim required objects via JNDIA ConnectionFactory (service provider)A Destination (Queue or Topic)
Create a Connection using the factory
Open a Session using the connection
Create a MessageProducer
Send the message
![Page 49: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/49.jpg)
Send a message
Context ctx = new InitialContext();ConnectionFactory connectionFactory = (ConnectionFactory) ctx .lookup("ConnectionFactory");Destination destination = (Destination) ctx.lookup("queue/StockValue");
Connection cnx = connectionFactory.createConnection();
Session session = cnx.createSession(false, Session.AUTO_ACKNOWLEDGE);MessageProducer producer = session.createProducer(destination);
TextMessage message = session.createTextMessage();message.setText("Your'microsoft' stock has been sold !");producer.send(message); cnx.close();
![Page 50: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/50.jpg)
Receive a message
Two ways to receive a messageBlocking, waiting for a messageNon-blocking, using a message listener
A message listener is similar to an event listener : it "subscribes" to a particular destination and receives messages each time there's a new one
![Page 51: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/51.jpg)
Receive a message
Blocking modeMessageConsumer consumer = session.createConsumer(destination);// Retrieve a single messageMessage receivedMessage = consumer.receive();
Non-blocking mode, using a listenerMessageConsumer consumer = session.createConsumer(destination);// Set the listenerconsumer.setMessageListener(new MessageListener() { public void onMessage(Message message) { // Will be called each time a message is received}});
![Page 52: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/52.jpg)
Receive a message : Message Driven Bean
A Message Driven Bean is a specific component for receiving messages
Annotation used is @MessageDriven
Destination name and type are declared in the annotation
Implements javax.jms.MessageListenerMethod public void onMessage(Message m)Called at the moment of receipt
![Page 53: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/53.jpg)
Receive a message : Message Driven Bean
@MessageDriven( mappedName="queue/StockValue", activationConfig = {@ActivationConfigProperty( propertyName = "destinationType", propertyValue = "javax.jms.Queue")})class MyDrivenBean implements MessageListener { public void onMessage(Message message) {TextMessage textMessage = (TextMessage) message;System.out.println(textMessage.getText());}}
![Page 54: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/54.jpg)
Merci de votre attention
![Page 55: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/55.jpg)
Sources
Supinfo : www.supinfo.com
ParisJUG : www.parisjug.org
![Page 56: EJB et WS (Montreal JUG - 12 mai 2011)](https://reader031.fdocuments.us/reader031/viewer/2022020217/54809b39b4795950578b471a/html5/thumbnails/56.jpg)
License
http://creativecommons.org/licenses/by-nc-sa/2.0/fr/