开发一个EJB
做了这么多准备工作,下面我们动手写一个例子HelloWorld,选择菜单file-new-other,弹出下面的对话框:
图5
选择Java-Lomboz J2EE Wizards中选择Lomboz J2EE Project,然后点击Next按钮:
图6
在Project name中输入项目名称,然后点击Next按钮,
图7
在图7Librarles选项卡中点击 Add External JARs按钮,添加C:/Sun/AppServer/lib/j2ee.jar,然后点击Next按钮,
图8
在Ejb Modules选项卡中点击Add按钮,增加一个Ejb Module:HelloWorld,然后点击Targeted Servers,
图9
选择JBOSS 3.2.x服务器,点击Add按钮,最后点击Finish按钮,这时Package Explorer看上去像图10的样子。
选择Lomboz EJB Creation Wizard,然后点击Next按钮,
Package
输入com.test,Name输入HelloWorldEJB,EJB Type 选择Stateless Session EJB,然后点击Finish按钮,这时Package Explorer看上去像图13的样子
在Method Signture中输入 public String helloWorld(),点击Finish按钮,在生成的代码中找到:
public String helloWorld(){
return null;
}
修改为:
public String helloWorld(){
return "Hello World!";
}
在Package Explorer中的HelloWorldEJBBean点击鼠标右键,在弹出的菜单中选择
Lomboz J2EE
,再选择
Add EJB to Module,
弹出图
15
对话框。
图15
选中EJB[HelloWorld],点击OK按钮。在Package Explorer中找到HelloWorld如图16所示:
然后点击Finish按钮,生成HelloWorldClient.java文件,编辑这个文件,使代码看上去像下面的样子:
//HelloWorldClient.java
/*
* Created on 2004-7-19
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.test;
import java.rmi.RemoteException;
import java.util.Hashtable;
import javax.ejb.CreateException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class HelloWorldClient {
private com.test.HelloWorldEJBHome getHome() throws NamingException {
return (com.test.HelloWorldEJBHome) getContext().lookup(
com.test.HelloWorldEJBHome.JNDI_NAME);
}
private InitialContext getContext() throws NamingException {
Hashtable props = new Hashtable();
props.put(InitialContext.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
props.put(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099");
// This establishes the security for authorization/authentication
// props.put(InitialContext.SECURITY_PRINCIPAL,"username");
// props.put(InitialContext.SECURITY_CREDENTIALS,"password");
InitialContext initialContext = new InitialContext(props);
return initialContext;
}
public void testBean() {
try {
com.test.HelloWorldEJB myBean = getHome().create();
//--------------------------------------
//This is the place you make your calls.
//System.out.println(myBean.callYourMethod());
System.out.println(myBean.helloWorld());
} catch (RemoteException e) {
e.printStackTrace();
} catch (CreateException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
HelloWorldClient test = new HelloWorldClient();
test.testBean();
}
}
下面来运行这个程序,进入Jboss的安装目录D:/jboss-3.2.3/bin,运行run.bat来启动服务器,服务器启动完成后 ,在Package Explorer中找到HelloWorldClient.java,在其上面击鼠标右键,在弹出的菜单中选择中选择run,再选择Java Application,如果在控制台中输出Hello World!说明调用成功了,如果出现了异常,那么你需要再看一边这篇文章或者Email给我[email protected]。
还有一个问题,有人会问如何在命令提示符环境下运行客户端呢?
需要编写一个批处理文件runHelloWorldClient.bat
java -classpath "D:/eclipse3/workspace/EJBProject/bin;D:/ jboss-3.2.3/client/concurrent.jar;D:/jboss-3.2.3/client/gnu-regexp.jar;D:/jboss-3.2.3/client/jacorb.jar;D:/jboss-3.2.3/client/jbossall-client.jar;D:/jboss-3.2.3/client/jboss-client.jar;D:/jboss-3.2.3/client/jboss-common-client.jar;D:/jboss-3.2.3/client/jbossha-client.jar;D:/jboss-3.2.3/client/jboss-iiop-client.jar;D:/jboss-3.2.3/client/jboss-j2ee.jar;D:/jboss-3.2.3/client/jboss-jaas.jar;D:/jboss-3.2.3/client/jbossjmx-ant.jar;D:/jboss-3.2.3/client/jbossmq-client.jar;D:/boss-3.2.3/client/jbossmqha.jar;D:/jboss-3.2.3/client/jboss-net-client.jar;D:/jboss-3.2.3/client/jbosssx-client.jar;D:/jboss-3.2.3/client/jboss-system-client.jar;D:/jboss-3.2.3/client/jcert.jar;D:/jboss-3.2.3/client/jmx-connector-client-factory.jar;D:/jboss-3.2.3/client/jmx-ejb-connector-client.jar;D:/jboss-3.2.3/client/jmx-rmi-connector-client.jar;D:/jboss-3.2.3/client/jnet.jar;D:/jboss-3.2.3/client/jnp-client.jar;D:/jboss-3.2.3/client/jsse.jar;D:/jboss-3.2.3/client/log4j.jar" com.test.HelloWorldClient
把它放在D:/Eclipse3/workspace/HelloWorldProject/bin目录(com/test所在的目录),然后运行,或者将上面批处理中引号内的路径加到环境变量ClassPath中,然后运行
Java com.test.HelloWorldClient
也可以。