|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Member
Iscritto dal: Dec 2001
Città: Bari.
Messaggi: 209
|
Primi passi con le EJB e JBoss
Sto compiendo i primi passi con le EJB.
Come container sto adoperando JBoss 3.2.0 che a sua volta si appoggia su Tomcat 4.1.24. Ho scritto uno Stateless Session Bean che dovrebbe semplicemente restituire la stringa "Hello World". La Home Interface (HelloWorldHome.java): Codice:
package helloWorldEjb; import javax.ejb.EJBHome; import java.rmi.RemoteException; import javax.ejb.CreateException; public interface HelloWorldHome extends EJBHome { public HelloWorld create() throws RemoteException, CreateException; } Codice:
package helloWorldEjb; import javax.ejb.EJBObject; import java.rmi.RemoteException; public interface HelloWorld extends EJBObject{ public String hello() throws RemoteException; } Codice:
package helloWorldEjb; import javax.ejb.SessionBean; import javax.ejb.SessionContext; import javax.ejb.EJBException; import java.rmi.RemoteException; public class HelloWorldBean implements SessionBean { private SessionContext ctx; public void ejbActivate() throws EJBException, RemoteException { System.out.println("HelloWorldBean: ejbActivate()"); } public void ejbPassivate() throws EJBException, RemoteException { System.out.println("HelloWorldBean: ejbPassivate()"); } public void ejbRemove() throws EJBException, RemoteException { System.out.println("HelloWorldBean: ejbRemove()"); } public void ejbCreate() { System.out.println("HelloWorldBean: ejbCreate()"); } public void setSessionContext(SessionContext sessionContext) throws EJBException, RemoteException { ctx = sessionContext; } public String hello() { System.out.println("HelloWorldBean: hello()"); return "Hello World"; } } Codice:
<?xml version="1.0" encoding="UTF-8"?> <!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> <description>JBoss Stateless Session Bean Test</description> <display-name>Hello World EJB</display-name> <enterprise-beans> <session> <ejb-name>HelloWorld</ejb-name> <home>helloWorldEjb.HelloWorldHome</home> <remote>helloWorldEjb.HelloWorld</remote> <ejb-class>helloWorldEjb.HelloWorldBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Bean</transaction-type> </session> </enterprise-beans> </ejb-jar> Ho quindi creato l'archivio helloWorld.jar contenente le tre classi compilate e la directory META-INF contenente il Deployment Descriptor (ejb-jar.xml). Fatto questo, ho copiato il helloWorld.jar nella directory C:\Appl\jboss-tomcat\server\default\deploy, dopo aver avviato JBoss. Il deployment è avvenuto correttamente, come segnalato da JBoss. A questo punto ho scritto il client (HelloWorldClient.java): Codice:
import java.util.Hashtable; import javax.naming.Context; import javax.naming.InitialContext; import helloWorldEjb.HelloWorldHome; import javax.rmi.PortableRemoteObject; import helloWorldEjb.HelloWorld; public class HelloWorldClient { public static void main(String[] args) { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); env.put(Context.PROVIDER_URL, "localhost:1099"); env.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces"); try { Context ctx = new InitialContext(env); Object obj = ctx.lookup("HelloWorld"); HelloWorldHome home = (HelloWorldHome)PortableRemoteObject.narrow(obj, HelloWorldHome.class); HelloWorld helloWorld = home.create(); System.out.println(helloWorld.hello()); helloWorld.remove(); } catch (Exception e) { e.printStackTrace(); System.out.println("Eccezione: " + e.getMessage()); } } } - j2ee.jar - jboss-common-client.jar - jboss-client.jar - jboss-system-client.jar - jbosssx-client.jar - jnp-client.jar - log4j.jar - helloWorld.jar All'atto dell'esecuzione del client però, si verifica la seguente eccezione: Codice:
java.lang.NoClassDefFoundError: Lorg/jboss/tm/TransactionPropagationContextFactory; at java.lang.Class.getDeclaredFields0(Native Method) at java.lang.Class.privateGetDeclaredFields(Class.java:1494) at java.lang.Class.getField0(Class.java:1727) at java.lang.Class.getDeclaredField(Class.java:1189) at java.io.ObjectStreamClass.getDeclaredSUID(ObjectStreamClass.java:1430) at java.io.ObjectStreamClass.access$400(ObjectStreamClass.java:45) at java.io.ObjectStreamClass$3.run(ObjectStreamClass.java:331) at java.security.AccessController.doPrivileged(Native Method) at java.io.ObjectStreamClass.<init>(ObjectStreamClass.java:329) at java.io.ObjectStreamClass.lookup(ObjectStreamClass.java:249) at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:449) at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1521) at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1435) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1626) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1274) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:324) at java.util.HashMap.readObject(HashMap.java:986) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:324) at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:824) at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1746) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1646) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1274) at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1845) at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1769) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1646) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1274) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:324) at org.jboss.proxy.ClientContainer.readExternal(ClientContainer.java:109) at java.io.ObjectInputStream.readExternalData(ObjectInputStream.java:1686) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1644) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1274) at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1845) at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1769) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1646) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1274) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:324) at java.rmi.MarshalledObject.get(MarshalledObject.java:135) at org.jnp.interfaces.MarshalledValuePair.get(MarshalledValuePair.java:30) at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:514) at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:471) at javax.naming.InitialContext.lookup(InitialContext.java:347) at HelloWorldClient.main(HelloWorldClient.java:29) Exception in thread "main" Cosa ho sbagliato? Infinite grazie a chiunque possa aiutarmi!
__________________
Se il giudice fosse giusto, forse il criminale non sarebbe colpevole - Dostoevskij |
![]() |
![]() |
![]() |
#2 |
Member
Iscritto dal: Dec 2001
Città: Bari.
Messaggi: 209
|
Ok... ho risolto da me!
Per chi fosse interessato: il problema è nel class-path del client, invece di includere tutti quei files, basta includere jbossall-client.jar
__________________
Se il giudice fosse giusto, forse il criminale non sarebbe colpevole - Dostoevskij |
![]() |
![]() |
![]() |
#3 |
Registered User
Iscritto dal: Dec 2001
Messaggi: 890
|
tutto questo per un'hello world ?!?!?!?
![]() ![]() ![]() |
![]() |
![]() |
![]() |
#4 |
Member
Iscritto dal: Dec 2001
Città: Bari.
Messaggi: 209
|
Già
![]()
__________________
Se il giudice fosse giusto, forse il criminale non sarebbe colpevole - Dostoevskij |
![]() |
![]() |
![]() |
#5 | |
Senior Member
Iscritto dal: Jul 2000
Città: Vignola (MO)
Messaggi: 316
|
Quote:
Ciao
__________________
<Asus A8N-E Deluxe> <Amd64 3000+> <Ram 2.5 Gb> <Geffo3> <2 Maxtor 160 Gb RAID 1> <Adsl ZyXEL 645r> <ESS Maestro> <GNU/Linux Debian Etch> ![]() Wii 1037 4238 6261 1967 |
|
![]() |
![]() |
![]() |
#6 |
Member
Iscritto dal: Dec 2001
Città: Bari.
Messaggi: 209
|
Per l'esecuzione del client basta inserire nel class-path jbossall-client.jar.
__________________
Se il giudice fosse giusto, forse il criminale non sarebbe colpevole - Dostoevskij |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 16:03.