Bonjour,
voila je travail sur un projet de migration d'une application codé avec des ejb2 vers des des ejb3.
ma configuration:
serveur: JBoss-4.2.2GA
mon soucis est le suivant:
+Est ce que ces deux types d'ejb peuvent cohabiter au sein de la même application ?
+Est ce que ces deux types d'ejb peuvent cohabiter au sein du même ear.
+J'ai lu beaucoup de documentation sur les ejb2 et 3, apparament la version 3 permet une rétrocompatibilité avec les version précédentent, mais alors comment cela fonctionne t-il ?
j'ai effectuer différents test:
un ejb3 seul dans un jar
-> appel client de cet ejb réussi
un ejb2 seul dans un jar
-> appel client de cet ejb réussi
un ejb3 et un ejb2 dans le même jar
-> appel client de l'ejb3 ok
-> appel client de l'ejb2 echec
mes clients:
client ejb2:
client ejb3:
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 package client_test; import interfaces.InterfaceHome; import interfaces.InterfaceLocale; import javax.naming.Context; import javax.naming.InitialContext; public class ClientEjb2 { public static void main(String[] args) throws Exception { Context context = new InitialContext(); Object obj = context.lookup("ejbtest"); InterfaceHome home = (InterfaceHome)javax.rmi.PortableRemoteObject.narrow(obj, InterfaceHome.class); InterfaceLocale locale = home.create(); System.out.println(locale.getMessage()); } }
code pour l'ejb2:
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 package client_test; import java.rmi.RemoteException; import interfaces.InterfaceBean; import javax.ejb.CreateException; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; public class Client_test { public static void main(String[] args) throws RemoteException, CreateException { try { Context context = new InitialContext(); InterfaceBean beanRemote = (InterfaceBean)context.lookup("Bean/remote"); System.out.println(beanRemote.message("Mon ejb 3 me renvoit le message")); } catch (NamingException e) { e.printStackTrace(); } } }
Bean:
InterfaceHome
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 package bean; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBException; import javax.ejb.SessionContext; public class BeanEjb2 implements javax.ejb.SessionBean { private static final long serialVersionUID = -3536373096372630637L; public void ejbCreate() throws CreateException{ } public String getMessage(){ return "coucou"; } public void ejbActivate() throws EJBException, RemoteException { } public void ejbPassivate() throws EJBException, RemoteException { } public void ejbRemove() throws EJBException, RemoteException { } public void setSessionContext(SessionContext ctx) throws EJBException, RemoteException { } }
interfaceRemote (qui s'appelle InterfaceLocale suite à différents test):
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 package interfaces; public interface InterfaceHome extends javax.ejb.EJBHome{ InterfaceLocale create() throws java.rmi.RemoteException,javax.ejb.CreateException; }
enfin, le fichier de déploiement:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 package interfaces; public interface InterfaceLocale extends javax.ejb.EJBObject{ public String getMessage() throws java.rmi.RemoteException; }
erreur pour l'ejb2 lorsque l'ejb2 et l'ejb3 se trouvent dans la même archive jar:
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 <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" " http://java.sun.com/dtd/ejb-jar_2_0.dtd"> <ejb-jar> <enterprise-beans> <session> <display-name>ejbtest</display-name> <ejb-name>ejbtest</ejb-name> <home>interfaces.InterfaceHome</home> <remote>interfaces.InterfaceLocale</remote> <ejb-class>bean.BeanEjb2</ejb-class> <session-type>Stateless</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans> </ejb-jar>
questions:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 Exception in thread "main" java.lang.ClassCastException at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:229) at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:137) at client_test.ClientEjb2.main(ClientEjb2.java:24) Caused by: java.lang.ClassCastException: $Proxy0 at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:212) ... 2 more
Que devient le fichier de déploiement ejb-jar.xml de notre ejb2 lorsque dans la même archive jar se trouve un ejb3 avec annotations ?
Pourquoi une erreur de cast est elle relevée lorsque les deux types d'ejb cohabitent?
j'espère avoir apporté assez de précision !
merci beaucoup
Partager