EJB学习笔记(by quqi99)
作者:张华 发表于:2010-01-14
版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明
下面看看如何在eclipse中开发一个EJB,以及一个EJB client
1)新建EJB Project, 名称为SFSBejb, EJB Module version选择3.0,点击“Add project to an EAR"它会自动创建EAR工程,
然后再点击“Create an EJB Client JAR module to hold the client interfaces and classes“,它也会自动
创建名为”SFSBejbClient“的工程了。
2)在SFSBejb工程的ejbModule目录下新建两个源文件SFSB.java与SFSBRemote.java
package ha.failover.beans.sfsb;
import javax.ejb.Remote;
import java.util.Vector;
import java.util.Hashtable;
@Remote
public interface SFSBRemote
{
public void ejbCreate(String name) throws Exception;
public String addAttribute(String name,String value) throws Exception;
public String removeAttribute(String name) throws Exception ;
public Hashtable getAttributes() throws Exception;
}
package ha.failover.beans.sfsb;
import javax.ejb.Stateful;
import javax.ejb.EJB;
import javax.ejb.EJBs;
import javax.ejb.Remove;
import javax.ejb.EJBException;
import javax.ejb.SessionContext;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionAttribute;
import javax.naming.InitialContext;
import java.util.Vector;
import java.util.Hashtable;
import javax.annotation.Resource;
import javax.transaction.UserTransaction;
@Stateful(name="SFSBEjbRef")
public class SFSB implements SFSBRemote
{
String name=null;
Hashtable attribs = new Hashtable();
public void ejbCreate(String name)
{
this.name=name;
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public String addAttribute(String name,String value)
{
System.out.println("addAttribute"+name+" "+value);
attribs.put(name,value);
return ("Attribute "+ name + " added "+ "on instance: "+ System.getProperty("com.sun.aas.instanceName"));
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public String removeAttribute(String name)
{
System.out.println("removeAttribute"+name);
attribs.remove(name);
return ("Attribute "+ name + " removed "+ "on instance: "+ System.getProperty("com.sun.aas.instanceName"));
}
public Hashtable getAttributes() {
return attribs;
}
}
3)在SFSBejbClient的ejbModule目录下新建客户端源代码:
package ha.failover.client;
import java.io.*;
import java.util.*;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.ejb.*;
import java.sql.*;
import java.rmi.*;
import ha.failover.beans.sfsb.*;
/**
* A simple java client. This uses the services provided by
* the SFSB Bean.
*/
public class EJBClient
{
private SFSBRemote sfsbremote;
private Context myEnv;
EJBClient client;
static BufferedReader reader1;
/*
* The main method of the client. This invokes the bean to use its service.
* It then invokes validate method in the bean.
*
*/
public EJBClient()
{
}
public static void main(String[] args)
{
boolean quit=false;
EJBClient client=null;
try
{
client= new EJBClient();
client.lookup();
System.out.println("Bean Lookup Successful");
while(!quit){
client.printQuery();
reader1 = new BufferedReader(new InputStreamReader(System.in));
String line=reader1.readLine();
client.printStars();
quit = client.processQuery(line);
client.printStars();
}
}
catch(Exception exc)
{
System.err.println("Caught an unexpected exception!");
exc.printStackTrace();
}
}
private void printQuery ()
{
System.out.println("Add an Attribute(a)");
System.out.println("Remove an Attribute(r)");
System.out.println("Print Attributes(p)");
System.out.println("Quit(q)");
}
private void lookup()
{
try
{
myEnv = new InitialContext();
sfsbremote = (SFSBRemote) myEnv.lookup("ha.failover.beans.sfsb.SFSBRemote");
}
catch(Exception exc)
{
System.err.println("Caught an unexpected exception during lookup!");
exc.printStackTrace();
System.exit(0);
}
}
private String addAttribute(String name , String value)
{
try
{
return sfsbremote.addAttribute(name,value);
}
catch(Exception exc)
{
exc.printStackTrace();
}
return null;
}
private String removeAttribute(String name)
{
try
{
return sfsbremote.removeAttribute(name);
}
catch(Exception exc)
{
exc.printStackTrace();
}
return null;
}
private void printAttributes() {
try {
Hashtable table = sfsbremote.getAttributes();
for (Enumeration e = table.keys() ; e.hasMoreElements() ;) {
String name = (String)e.nextElement();
System.out.println("Name : " + name +", Value: "+ table.get(name));
}
} catch(Exception exc)
{
exc.printStackTrace();
}
}
private void printStars()
{
System.out.println("**********************************************");
}
private boolean processQuery(String received){
String value=received.toLowerCase();
try
{
if (value.equals("a")){
System.out.print("Enter name :" );
System.out.flush();
String name =reader1.readLine();
System.out.print("Enter value :" );
System.out.flush();
String value1 =reader1.readLine();
System.out.println(addAttribute(name,value1));
return false;
}
if (value.equals("r")) {
System.out.println("Enter name :" );
String name =reader1.readLine();
System.out.println(removeAttribute(name));
return false;
}
if (value.equals("p")) {
printAttributes();
return false;
}
if (value.equals("q")) {
return true;
}
} catch (Exception ex) {
ex.printStackTrace();
}
return true;
}
}
注意,请将SFSBRemote.java类在这里再复制一遍
4)由于在新建工程时,缺少application文件,那么在SFSBejbEAR工程下的META-INF目录下新建application.xml文件,如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE application PUBLIC "-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN" "http://java.sun.com/dtd/application_1_3.dtd">
<application>
<display-name>clusterjsp</display-name>
<description>J2EE Application webslPloginApp</description>
<module>
<java>SFSBejbClient.jar</java>
</module>
<module>
<ejb>SFSBejb.jar</ejb>
</module>
</application>
5) 在SFSBejbClient工程的META-INF目录下的MANIFEST.MF文件的末行添加一句:
Main-Class: ha.failover.client.EJBClient
6)如果要写客户化配置,可在SFSBejb工程下的META-INF文件中添加配置sun-application.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-application PUBLIC "-//Sun Microsystems, Inc.//DTD Sun ONE Application Server 7.0 J2EE Application 1.3//EN" "http://www.sun.com/software/sunone/appserver/dtds/sun-application_1_3-0.dtd">
<sun-application>
</sun-application>
7)假如在glassfish下部署,可用如下命令:
EJB服务:asadmin deploy --host localhost --port 4848 --type application --target cluster1 --retrieve ./ --availabilityenabled=true c:/test/SFSBejbEAR.ear
EJB Client : appclient -xml c:/glassfish/domains/domain1/config/sun-acc.xml -client c:/test/SFSBejbClient.jar
当然,在使用appclient命令时,请先修改sun-acc.xml文件,将你的集群(我这里是cluster1)的所有实例用
target-server元素配上,例如:
<client-container>
<target-server name="zhanghua" address="zhanghua" port="33071"/>
....
</client-contianer>
当然,如果不想用命令执行client,也可以通过main方法像普通JAVA一样执行,只不过要查找JNDI时
要加上属性如下属性:
Properties props = new Properties();
props.setPropery("com.sun.appserv.iiop.endpoints","zhanghua:33700,zhanghua:33701");
InitialContext ctx = new InitialContext(props);