Salut à tous,
J'aimerais éclaircir un point des EJB 3 stateless (sans état donc) car quelque chose m'échappe dans mon test :
Mon interface distante expose des services liés à un panier dans lequel on ajoute des produits. On peut aussi consulter les produits de son panier:
Avec son implémentation stateless:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package fr.bnpp.pf; import java.util.List; import javax.ejb.Remote; import fr.bnpp.pf.request.Product; @Remote public interface ShoppingCartServices { /** * Add a product to your shopping cart. * * @param product * the product to add */ void addProductToYourBasket(Product product); /** * Returns the list of products added to the basket * * @return a List made of Product objects */ List<Product> returnListOfProductInBasket(); }
Tout ca dans un projet Eclipse :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 package fr.bnpp.pf; import java.util.ArrayList; import java.util.List; import javax.ejb.Stateless; import javax.ejb.TransactionManagement; import javax.ejb.TransactionManagementType; import fr.bnpp.pf.request.Product; /** * Session Bean implementation class ShoppingCartServices */ @Stateless @TransactionManagement(TransactionManagementType.BEAN) public class ShoppingCartServicesImpl implements ShoppingCartServices { private List<Product> basketOfProduct = null; /** * Default constructor. */ public ShoppingCartServicesImpl() { System.out.println("running ShoppingCartServicesImpl constructor"); basketOfProduct = new ArrayList<Product>(); } @Override public void addProductToYourBasket(Product product) { System.out.println("running addProductToYourBasket method"); basketOfProduct.add(product); } @Override public List<Product> returnListOfProductInBasket() { System.out.println("running returnListOfProductInBasket method"); return basketOfProduct; } }
Et puis, un petit client pour tester ca qui ajoute 3 produits et consulte son panier:
lui aussi dans un petit projet Eclipse:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package fr.bnpp.pf; import java.math.BigDecimal; import java.util.List; import java.util.Properties; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import fr.bnpp.pf.request.Product; public class MainTest { public static void main(String[] args) { Properties properties = new Properties(); properties.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); properties.put("java.naming.factory.url.pkgs", "=org.jboss.naming:org.jnp.interfaces"); properties.put("java.naming.provider.url", "localhost:1099"); ShoppingCartServices shoppingCartServices = null; try { Context context = new InitialContext(properties); shoppingCartServices = (ShoppingCartServices) context .lookup("ShoppingCartServicesImpl/remote"); } catch (NamingException e) { e.printStackTrace(); } shoppingCartServices.addProductToYourBasket(new Product("4543543", "IPOD", new BigDecimal(432.34))); shoppingCartServices.addProductToYourBasket(new Product("99544", "DVD PLAYER", new BigDecimal(2300.34))); shoppingCartServices.addProductToYourBasket(new Product("23466", "TV", new BigDecimal(12345.67))); List<Product> basketOfProducts = shoppingCartServices .returnListOfProductInBasket(); } }
JBOSS démarre bien:
et lors de la première exécution du client de test on voit (dans les traces) :21:24:49,453 INFO [JBossASKernel] jndi:ShoppingCartServicesImpl/remote
21:24:49,453 INFO [JBossASKernel] Class:fr.bnpp.pf.ShoppingCartServices
21:24:49,453 INFO [JBossASKernel] jndi:ShoppingCartServicesImpl/remote-fr.bnpp.pf.ShoppingCartServices
21:24:49,453 INFO [JBossASKernel] Added bean(jboss.j2ee:jar=ShoppingCartApplication.jar,name=ShoppingCartServicesImpl,service=EJB3) to KernelDeployment of: ShoppingCartApplication.jar
21:24:50,296 INFO [SessionSpecContainer] Starting jboss.j2ee:jar=ShoppingCartApplication.jar,name=ShoppingCartServicesImpl,service=EJB3
21:24:50,312 INFO [EJBContainer] STARTED EJB: fr.bnpp.pf.ShoppingCartServicesImpl ejbName: ShoppingCartServicesImpl
21:24:50,421 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
ShoppingCartServicesImpl/remote - EJB3.x Default Remote Business Interface
ShoppingCartServicesImpl/remote-fr.bnpp.pf.ShoppingCartServices - EJB3.x Remote Business Interface
21:24:50,578 INFO [Http11Protocol] D�marrage de Coyote HTTP/1.1 sur http-127.0.0.1-8080
21:24:50,656 INFO [AjpProtocol] Starting Coyote AJP/1.3 on ajp-127.0.0.1-8009
21:24:50,703 INFO [ServerImpl] JBoss (Microcontainer) [5.0.1.GA (build: SVNTag=JBoss_5_0_1_GA date=200902232048)] Started in 43s:203ms
La classe est donc instanciée et on exécute bien coté EJB ce qui est appelé par mon client de test.21:31:23,468 INFO [STDOUT] running ShoppingCartServicesImpl constructor
21:31:23,625 INFO [STDOUT] running addProductToYourBasket method
21:31:23,656 INFO [STDOUT] running addProductToYourBasket method
21:31:23,671 INFO [STDOUT] running addProductToYourBasket method
21:31:23,687 INFO [STDOUT] running returnListOfProductInBasket method
Par contre, je relance ma classe de test et là, on voit s'ajouter dans les traces de la console:
--> donc pas de ré instanciation de mon EJB session stateless21:33:58,687 INFO [STDOUT] running addProductToYourBasket method
21:33:58,703 INFO [STDOUT] running addProductToYourBasket method
21:33:58,718 INFO [STDOUT] running addProductToYourBasket method
21:33:58,734 INFO [STDOUT] running returnListOfProductInBasket method
--> et quand j'inspecte ma liste de retour après l'exécution du second test:
QUESTIONS :
1. Est-ce normal que ma classe ne soit pas ré instanciée lors du second appel client alors que c'est un bean sesssion stateless ET que c'est un autre client?
2. Est-ce normal que mon second appel client rapatrie une liste qui contient les produits ajoutés de lors de l'appel du premier client ?
Merci à tous pour vos éclaircissements
Partager