📄 ejb-core.htm
字号:
}</p>
<p>public void ejbPassivate() {<br>
if (verbose)<br>
System.out.println("ejbPassivate called");<br>
}</p>
<p>/**<br>
* Sets the session context.<br>
*<br>
* @param SessionContext<br>
*/<br>
public void setSessionContext(SessionContext ctx) { <br>
if (verbose)<br>
System.out.println("setSessionContext called");<br>
this.ctx = ctx;<br>
}</p>
<p>/**<br>
* This method corresponds to the create method in<br>
* the home interface HelloHome.java. <br>
* The parameter sets of the two methods are <br>
* identical. When the client calls <br>
* HelloHome.create(), the container allocates an <br>
* instance of the EJBean and calls ejbCreate(). <br>
*/ <br>
public void ejbCreate () {<br>
if (verbose)<br>
System.out.println("ejbCreate called");<br>
}<br>
/**<br>
* **** HERE IS THE BUSINESS LOGIC *****<br>
* the getHello just return a "Hello World" string.<br>
*/<br>
public String getHello()<br>
throws RemoteException<br>
{<br>
return("Hello World");<br>
}<br>
}</p>
<p>5、创建ejb-jar.xml文件<br>
ejb-jar.xml文件是EJB的部署描述文件,包含EJB的各种配置信息,如是有状态Bean(Stateful Bean)
还是无状态Bean(Stateless Bean),交易类型等。ejb-jar.xml文件的详细信息请参阅EJB规范。以下是HelloBean的配置文件:<br>
<?xml version="1.0"?><br>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems Inc.//DTD
Enterprise JavaBeans 1.2//EN" "http://java.sun.com/j2ee/dtds/ejb-jar_1_2.dtd"><br>
<ejb-jar><br>
<enterprise-beans><br>
<session><br>
<ejb-name>Hello</ejb-name><br>
<home>ejb.hello.HelloHome</home><br>
<remote>ejb.hello.Hello</remote><br>
<ejb-class>ejb.hello.HelloBean</ejb-class><br>
<session-type>Stateless</session-type><br>
<transaction-type>Container</transaction-type><br>
</session> <br>
</enterprise-beans><br>
<assembly-descriptor><br>
<container-transaction><br>
<method><br>
<ejb-name>Hello</ejb-name><br>
<method-name>*</method-name><br>
</method><br>
<trans-attribute>Required</trans-attribute><br>
</container-transaction> <br>
</assembly-descriptor><br>
</ejb-jar></p>
<p>6、编译和部署<br>
编译Java源文件并将编译后class和ejb-jar.xml打包到Hello.jar <br>
mkdir build<br>
mkdir build/META-INF<br>
cp ejb-jar.xml build/META-INF<br>
javac -d build *.java<br>
cd build<br>
jar cvf Hello.jar META-INF ejb<br>
cd .. <br>
用EJB工具生成可部署到Apusic Application Server中运行的jar文件: <br>
java com.apusic.ejb.utils.EJBGen -d /usr/apusic/classes/Hello.jar
build/Hello.jar <br>
增加/usr/apusic/classes/Hello.jar到CLASSPATH中 <br>
将Hello.jar加入到Apusic Application Server配置文件中。在/usr/apusic/config/server.xml
加入以下几行: <br>
<module><br>
<ejb><br>
<ejb-uri>classes/Hello.jar</ejb-uri><br>
<bean><br>
<ejb-name>Hello</ejb-name><br>
<jndi-name>HelloHome</jndi-name><br>
</bean><br>
</ejb><br>
</module><br>
启动服务器 <br>
java -Xms64m com.apusic.server.Main -root /usr/apusic</p>
<p>7、写客户端调用程序<br>
您可以从Java Client,JSP,Servlet或别的EJB调用HelloBean。 <br>
调用EJB有以下几个步骤: <br>
通过JNDI(Java Naming Directory Interface)得到EJB Home Interface <br>
通过EJB Home Interface 创建EJB对象,并得到其Remote Interface <br>
通过Remote Interface调用EJB方法 </p>
<p>以下是一个从Java Client中调用HelloBean的例子: <br>
package ejb.hello;</p>
<p>import javax.naming.Context;<br>
import javax.naming.InitialContext;<br>
import java.util.Hashtable;<br>
import javax.ejb.*;<br>
import java.rmi.RemoteException;</p>
<p>/**<br>
* @author Copyright (c) 2000 by Apusic, Inc. All Rights Reserved.<br>
*/<br>
public class HelloClient{<br>
public static void main(String args[]){<br>
String url = "rmi://localhost:6888";<br>
Context initCtx = null;<br>
HelloHome hellohome = null;<br>
try{<br>
Hashtable env = new Hashtable();<br>
env.put(Context.INITIAL_CONTEXT_FACTORY,<br>
"com.apusic.jndi.InitialContextFactory"); <br>
env.put(Context.PROVIDER_URL, url); <br>
initCtx = new InitialContext(env);<br>
}catch(Exception e){<br>
System.out.println("Cannot get initial context: " + e.getMessage());<br>
System.exit(1);<br>
}<br>
try{<br>
hellohome = (HelloHome)initCtx.lookup("HelloHome"); <br>
Hello hello = hellohome.create();<br>
String s = hello.getHello();<br>
System.out.println(s); <br>
}catch(Exception e){<br>
System.out.println(e.getMessage());<br>
System.exit(1);<br>
} <br>
}</p>
<p>}<br>
运行HelloClient,可得到以下输出: <br>
Hello World <br>
</p>
<!-- #EndEditable --></td>
<td width="20"> </td>
</tr>
<tr>
<td width="20" height="11"> </td>
<td width="541" height="11"><!-- #BeginEditable "7" --><!-- #EndEditable --></td>
<td width="101" height="11">
</td>
<td width="20" height="11"> </td>
</tr>
</table><div align="center"> <br>
</div>
</td>
</tr>
</table>
<div align="center">
<br>
</div>
</body>
<!-- #EndTemplate --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -