怎么样使用JNDI查找部署在JBOSS中的EJB对象并调用?
怎么样使用JNDI查找部署在JBOSS中的EJB对象并调用?
使用环境
java1.5.0安装在(JAVA_HOME)C:/Program Files/Java/
JBOSS4.0.2安装在(JBOSS_HOME)C:/Program Files/jboss-4.0.2/
ANT1.6.2安装在(ANT_HOME)C:/Program Files/apache-ant-1.6.2/
文件路径
EjbDemo2(client(HelloClient.java);META-INF(ejb-jar.xml;jboss.xml);src(demo2(Hello.java;HelloLocal.java;HelloHome.java;HelloLocalHome.java.HelloBean.java));build.properties;build.xml;RunClient.bat)
使用的文件:
1.build.xml
<!--================EjbDemo2CreateByIUnknown!2007.01.14==================-->
<projectname="EjbDemo2"default="help"basedir=".">
<!--=====================PropertyDefinitions===========================-->
<propertyname="app.home"value="."/>
<propertyname="app.name"value="EjbDemo2"/>
<propertyname="src.home"value="${app.home}/src"/>
<propertyname="classes.home"value="${app.home}/classes"/>
<propertyname="deploy.home"value="${app.home}/deploy"/>
<propertyname="build.home"value="${app.home}/build"/>
<propertyname="metainf.home"value="${app.home}/META-INF"/>
<propertyname="buildmetainf.home"value="${build.home}/META-INF"/>
<propertyname="client.home"value="${app.home}/client"/>
<!--====================CompilationClasspath===========================-->
<propertyfile="build.properties"/>
<pathid="compile.classpath">
<pathelementlocation="${classes.home}"/>
<filesetdir="${jboss.home}/lib">
<includename="*.jar"/>
</fileset>
<filesetdir="${jboss.home}/client">
<includename="*.jar"/>
</fileset>
</path>
<!--===================="clean-all"Target================================-->
<targetname="clean-all">
<deletedir="${classes.home}"/>
<deletedir="${deploy.home}"/>
<deletedir="${buildmetainf.home}"/>
<deletedir="${build.home}"/>
<deletedir="${jboss.home}/${app.name}"failonerror="false"/>
<deletedir="${jboss.home}/${app.name}.jar"/>
</target>
<!--===================="prepare"Target================================-->
<targetname="perpare"depends="clean-all">
<mkdirdir="${classes.home}"/>
<mkdirdir="${deploy.home}"/>
<mkdirdir="${build.home}"/>
<mkdirdir="${build.home}/META-INF"/>
</target>
<!--===================="compile"Target================================-->
<targetname="compile"depends="perpare">
<javacsrcdir="${src.home}"destdir="${classes.home}"debug="yes">
<classpathrefid="compile.classpath"/>
</javac>
</target>
<!--===================="build"Target================================-->
<targetname="build"depends="compile">
<copytodir="${build.home}">
<filesetdir="${classes.home}"/>
</copy>
<copytodir="${client.home}">
<filesetdir="${classes.home}"/>
</copy>
<copytodir="${buildmetainf.home}">
<filesetdir="${metainf.home}"/>
</copy>
<jarjarfile="${deploy.home}/${app.name}.jar"basedir="${build.home}"/>
</target>
<!--===================="deploy"Target================================-->
<targetname="deploy"depends="build">
<copytodir="${webapps.home}"file="${deploy.home}/${app.name}.jar"/>
</target>
<!--===================="help"Target================================-->
<targetname="help">
<echomessage="EjbDemo2CreateByIUnknown!2007.01.14"/>
</target>
</project>
<projectname="EjbDemo2"default="help"basedir=".">
<!--=====================PropertyDefinitions===========================-->
<propertyname="app.home"value="."/>
<propertyname="app.name"value="EjbDemo2"/>
<propertyname="src.home"value="${app.home}/src"/>
<propertyname="classes.home"value="${app.home}/classes"/>
<propertyname="deploy.home"value="${app.home}/deploy"/>
<propertyname="build.home"value="${app.home}/build"/>
<propertyname="metainf.home"value="${app.home}/META-INF"/>
<propertyname="buildmetainf.home"value="${build.home}/META-INF"/>
<propertyname="client.home"value="${app.home}/client"/>
<!--====================CompilationClasspath===========================-->
<propertyfile="build.properties"/>
<pathid="compile.classpath">
<pathelementlocation="${classes.home}"/>
<filesetdir="${jboss.home}/lib">
<includename="*.jar"/>
</fileset>
<filesetdir="${jboss.home}/client">
<includename="*.jar"/>
</fileset>
</path>
<!--===================="clean-all"Target================================-->
<targetname="clean-all">
<deletedir="${classes.home}"/>
<deletedir="${deploy.home}"/>
<deletedir="${buildmetainf.home}"/>
<deletedir="${build.home}"/>
<deletedir="${jboss.home}/${app.name}"failonerror="false"/>
<deletedir="${jboss.home}/${app.name}.jar"/>
</target>
<!--===================="prepare"Target================================-->
<targetname="perpare"depends="clean-all">
<mkdirdir="${classes.home}"/>
<mkdirdir="${deploy.home}"/>
<mkdirdir="${build.home}"/>
<mkdirdir="${build.home}/META-INF"/>
</target>
<!--===================="compile"Target================================-->
<targetname="compile"depends="perpare">
<javacsrcdir="${src.home}"destdir="${classes.home}"debug="yes">
<classpathrefid="compile.classpath"/>
</javac>
</target>
<!--===================="build"Target================================-->
<targetname="build"depends="compile">
<copytodir="${build.home}">
<filesetdir="${classes.home}"/>
</copy>
<copytodir="${client.home}">
<filesetdir="${classes.home}"/>
</copy>
<copytodir="${buildmetainf.home}">
<filesetdir="${metainf.home}"/>
</copy>
<jarjarfile="${deploy.home}/${app.name}.jar"basedir="${build.home}"/>
</target>
<!--===================="deploy"Target================================-->
<targetname="deploy"depends="build">
<copytodir="${webapps.home}"file="${deploy.home}/${app.name}.jar"/>
</target>
<!--===================="help"Target================================-->
<targetname="help">
<echomessage="EjbDemo2CreateByIUnknown!2007.01.14"/>
</target>
</project>
2.build.properties( 可以从环境变量中提取)
jboss.home=C:/ProgramFiles/jboss-4.0.2
webapps.home=C:/ProgramFiles/jboss-4.0.2/server/default/deploy
webapps.home=C:/ProgramFiles/jboss-4.0.2/server/default/deploy
3.RunClient.bat
@echooff
echo****************************
echoRunCallEjbDemo2Client!
echoCreateByIUnknown
echo2007.01.14
echo****************************
cdclient
SETCLASSPATH=%CLASSPATH%;%JBOSS_HOME%clientjbossall-client.jar
javacHelloClient.java
javaHelloClient
cd..
echo****************************
echoRunCallEjbDemo2Client!
echoCreateByIUnknown
echo2007.01.14
echo****************************
cdclient
SETCLASSPATH=%CLASSPATH%;%JBOSS_HOME%clientjbossall-client.jar
javacHelloClient.java
javaHelloClient
cd..
4./client/HelloClient.java
importdemo2.HelloHome;
importdemo2.Hello;
importjavax.naming.Context;
importjavax.naming.InitialContext;
importjava.util.Hashtable;
importjava.util.Properties;
publicclassHelloClient
{
publicstaticvoidmain(String[]args)
{
try{
Hashtableenv=newHashtable();
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");
Contextctx=newInitialContext(env);
Objectobj=ctx.lookup("HelloDemo2Bean");
if(obj!=null){
HelloHomeHome=(HelloHome)javax.rmi.PortableRemoteObject.narrow(obj,HelloHome.class);
Hellohello=Home.create();
if(hello!=null){
System.out.println(hello.hello());
}
}
}catch(javax.naming.NamingExceptione){
e.printStackTrace();
System.out.println("NamingException.");
}catch(javax.ejb.CreateExceptione){
e.printStackTrace();
System.out.println("CreateEjbException.");
}catch(java.rmi.RemoteExceptione){
e.printStackTrace();
System.out.println("RemotException.");
}catch(Exceptione){
System.out.print("Error!");
}
}
}
importdemo2.Hello;
importjavax.naming.Context;
importjavax.naming.InitialContext;
importjava.util.Hashtable;
importjava.util.Properties;
publicclassHelloClient
{
publicstaticvoidmain(String[]args)
{
try{
Hashtableenv=newHashtable();
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");
Contextctx=newInitialContext(env);
Objectobj=ctx.lookup("HelloDemo2Bean");
if(obj!=null){
HelloHomeHome=(HelloHome)javax.rmi.PortableRemoteObject.narrow(obj,HelloHome.class);
Hellohello=Home.create();
if(hello!=null){
System.out.println(hello.hello());
}
}
}catch(javax.naming.NamingExceptione){
e.printStackTrace();
System.out.println("NamingException.");
}catch(javax.ejb.CreateExceptione){
e.printStackTrace();
System.out.println("CreateEjbException.");
}catch(java.rmi.RemoteExceptione){
e.printStackTrace();
System.out.println("RemotException.");
}catch(Exceptione){
System.out.print("Error!");
}
}
}
5./META-INF/ejb-jar.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<ejb-jarxmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd"version="2.1">
<enterprise-beans>
<session>
<ejb-name>HelloBean</ejb-name>
<home>demo2.HelloHome</home>
<remote>demo2.Hello</remote>
<local-home>demo2.HelloLocalHome</local-home>
<local>demo2.HelloLocal</local>
<ejb-class>demo2.HelloBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
<ejb-jarxmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd"version="2.1">
<enterprise-beans>
<session>
<ejb-name>HelloBean</ejb-name>
<home>demo2.HelloHome</home>
<remote>demo2.Hello</remote>
<local-home>demo2.HelloLocalHome</local-home>
<local>demo2.HelloLocal</local>
<ejb-class>demo2.HelloBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
6./META-INF/jboss.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEjbossPUBLIC"-//JBoss//DTDJBOSS3.2//EN""http://www.jboss.org/j2ee/dtd/jboss_3_2.dtd">
<jboss>
<enterprise-beans>
<session>
<ejb-name>HelloBean</ejb-name>
<jndi-name>HelloDemo2Bean</jndi-name>
</session>
</enterprise-beans>
</jboss>
<!DOCTYPEjbossPUBLIC"-//JBoss//DTDJBOSS3.2//EN""http://www.jboss.org/j2ee/dtd/jboss_3_2.dtd">
<jboss>
<enterprise-beans>
<session>
<ejb-name>HelloBean</ejb-name>
<jndi-name>HelloDemo2Bean</jndi-name>
</session>
</enterprise-beans>
</jboss>
7./src/demo2/Hello.java
//远程接口
//byIUnknown
packagedemo2;
publicinterfaceHelloextendsjavax.ejb.EJBObject
{
publicStringhello()throwsjava.rmi.RemoteException;
}
//byIUnknown
packagedemo2;
publicinterfaceHelloextendsjavax.ejb.EJBObject
{
publicStringhello()throwsjava.rmi.RemoteException;
}
8./src/demo2/HelloLoc