用Eclipse3.0+lomboz3.0+jboss3.2.3开发EJB

开发一个EJB

做了这么多准备工作,下面我们动手写一个例子HelloWorld,选择菜单file-new-other,弹出下面的对话框:

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

图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 ModuleHelloWorld,然后点击Targeted Servers,

图9

选择JBOSS 3.2.x服务器,点击Add按钮,最后点击Finish按钮,这时Package Explorer看上去像图10的样子。

图10

在项目名称HelloWorldProject上点击鼠标右键,在弹出的菜单中选择new,弹出图11对话框,

图11

选择Lomboz EJB Creation Wizard,然后点击Next按钮,

图12

Package 输入com.test,Name输入HelloWorldEJB,EJB Type 选择Stateless Session EJB,然后点击Finish按钮,这时Package Explorer看上去像图13的样子

图13

HelloWorldEJBBean上点击鼠标右键,在弹出的菜单中选择Lomboz J2EE,再选择Add EJB Method,弹出图14对话框,

图14

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所示:

16

HelloWorld上点击鼠标右键,然后在弹出的菜单中选择Lomboz J2EE,再选择Generate EJB Classes,如果没有出现错误,这是自动生成了一个ejbsrc文件夹,里面的文件也是自动生成的。

再在HelloWorld上点击鼠标右键,在弹出的菜单中选择Lomboz J2EE,再选择Deploy Module来部署EJB Module

EJB Module部署完成后,下面我们做一个客户端来调用HelloWorld()方法。

在文件菜单中选择New,再选择Other,在弹出的对话框如图11所示,选择Lomboz EJB Test Client Wizard,按照图17所示,填写相应参数,

图 17

然后点击Finish按钮,生成HelloWorldClient.java文件,编辑这个文件,使代码看上去像下面的样子:

//HelloWorldClient.java

/*

* Created on <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><chsdate isrocdate="False" islunardate="False" day="19" month="7" year="2004" w:st="on">2004-7-19</chsdate>

*

* 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-<chsdate isrocdate="False" islunardate="False" day="30" month="12" year="1899" w:st="on">3.2.3</chsdate>\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-<chsdate isrocdate="False" islunardate="False" day="30" month="12" year="1899" w:st="on">3.2.3</chsdate>\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

也可以。

你可能感兴趣的:(jboss,Security,ejb,Microsoft,Office)