http://ajava.org/course/ejb/15636.html
http://davidgjy.javaeye.com/blog/672198
http://www.docin.com/p-66374394.html
http://www.docin.com/p-56736292.html
http://www.cn-doc.com/_soft_java_tech_doc/2006_02_23_16/20060223161000755.htm
下面有个简单的例子:
1 定义接口:
1.1 扩展EJBObject接口
1
package
ejbtest01;
2
3
import
java.rmi.RemoteException;
4
import
javax.ejb.EJBObject;
5
6
public
interface
ConvertEjb
extends
EJBObject {
7
public
double
getRmb(
double
usd)
throws
RemoteException;
8
}
1.1 扩展 EJBHome接口
1
package
ejbtest01;
2
3
import
java.rmi.RemoteException;
4
import
javax.ejb.CreateException;
5
import
javax.ejb.EJBHome;
6
7
public
interface
ConvertEjbHome
extends
EJBHome {
8
public
ConvertEjb create()
throws
CreateException, RemoteException;
9
}
2 定义SessionBean,ejbCreate()至少要有一个
1
package
ejbtest01;
2
3
import
java.rmi.RemoteException;
4
import
javax.ejb.EJBException;
5
import
javax.ejb.SessionBean;
6
import
javax.ejb.SessionContext;
7
8
public
class
ConvertEjbBean
implements
SessionBean {
9
SessionContext sessionContext;
10
11
//
ejbCreate(…)方法 它可以初始化Enterprise Bean
12
//
可以定义不同的ejbCreate(…)方法,每个方法所带的参数不同
13
//
但是,必许要存在至少一种。
14
public
void
ejbCreate() {
15
}
16
17
public
void
ejbActivate()
throws
EJBException, RemoteException {
18
19
}
20
21
public
void
ejbPassivate()
throws
EJBException, RemoteException {
22
23
}
24
25
public
void
ejbRemove()
throws
EJBException, RemoteException {
26
27
}
28
29
public
void
setSessionContext(SessionContext arg0)
throws
EJBException,
30
RemoteException {
31
this
.sessionContext
=
sessionContext;
32
}
33
34
//
用于调用的方法
35
public
double
getRmb(
double
usd) {
36
double
rate
=
6.98
;
37
double
rmb
=
rate
*
usd;
38
return
rmb;
39
}
40
41
}
3 定义客户端JDK 使用1.4
3.1 定义一个工厂
1
package
ejbtest01;
2
3
import
java.util.Hashtable;
4
import
javax.naming.Context;
5
import
javax.naming.InitialContext;
6
7
public
class
EjbFactory {
8
public
static
Object getEjbHome(String jndiName)
throws
Exception {
9
//
连接到Weblogic
10
Hashtable map
=
new
Hashtable();
//
JDK 使用1.4
11
map.put(Context.INITIAL_CONTEXT_FACTORY,
"
weblogic.jndi.WLInitialContextFactory
"
);
12
//
指定了weblogic服务器的名称(IP),远程调用体现在这里
13
map.put(Context.PROVIDER_URL,
"
t3://localhost:7001
"
);
14
map.put(Context.SECURITY_PRINCIPAL,
"
weblogic
"
);
15
map.put(Context.SECURITY_CREDENTIALS,
"
weblogic
"
);
16
17
Context context
=
new
InitialContext(map);
18
//
根据JNDI名称查找,查到的就是home接口
19
Object obj
=
context.lookup(jndiName);
20
21
return
obj;
22
}
23
}
3.2 定义客户端
1
package
ejbtest01;
2
3
import
ejbtest01.ConvertEjb;
4
import
ejbtest01.ConvertEjbHome;
5
import
javax.rmi.PortableRemoteObject;
6
7
public
class
Client{
8
public
static
void
main(String args[])
throws
Exception {
9
Object obj
=
EjbFactory.getEjbHome(
"
jndi/ConvertEjb
"
);
10
11
//
ConvertEjbHome home = (ConvertEjbHome)obj; 性能不好
12
ConvertEjbHome home
=
(ConvertEjbHome)PortableRemoteObject.narrow(obj,ConvertEjbHome.
class
);
13
ConvertEjb remote
=
home.create();
14
15
double
usd
=
456
;
16
double
rmb
=
remote.getRmb(usd);
17
System.out.println(
"
人民币数量:
"
+
rmb);
18
19
}
20
}
4 配置ejb-jar.xml和weblogic-ejb-jar.xml
4.1 配置ejb-jar.xml
<?
xml version="1.0" encoding="UTF-8"
?>
<!
DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd"
>
<
ejb-jar
>
<
display-name
>
EjbTestModule1
</
display-name
>
<
enterprise-beans
>
<
session
>
<
display-name
>
ConvertEjb
</
display-name
>
<
ejb-name
>
ConvertEjb
</
ejb-name
>
<
home
>
ejbtest01.ConvertEjbHome
</
home
>
<
remote
>
ejbtest01.ConvertEjb
</
remote
>
<
ejb-class
>
ejbtest01.ConvertEjbBean
</
ejb-class
>
<
session-type
>
Stateless
</
session-type
>
<
transaction-type
>
Container
</
transaction-type
>
</
session
>
</
enterprise-beans
>
<
assembly-descriptor
>
<
container-transaction
>
<
method
>
<
ejb-name
>
ConvertEjb
</
ejb-name
>
<
method-name
>
*
</
method-name
>
</
method
>
<
trans-attribute
>
Required
</
trans-attribute
>
</
container-transaction
>
</
assembly-descriptor
>
</
ejb-jar
>
4.2 配置weblogic-ejb-jar.xml
<?
xml version="1.0" encoding="UTF-8"
?>
<!
DOCTYPE weblogic-ejb-jar PUBLIC "-//BEA Systems, Inc.//DTD WebLogic 8.1.0 EJB//EN" "http://www.bea.com/servers/wls810/dtd/weblogic-ejb-jar.dtd"
>
<
weblogic-ejb-jar
>
<
weblogic-enterprise-bean
>
<
ejb-name
>
ConvertEjb
</
ejb-name
>
<
stateless-session-descriptor
>
<
pool
>
<
max-beans-in-free-pool
>
42
</
max-beans-in-free-pool
>
</
pool
>
<
stateless-clustering
>
<
stateless-bean-is-clusterable
>
true
</
stateless-bean-is-clusterable
>
<
stateless-bean-methods-are-idempotent
>
true
</
stateless-bean-methods-are-idempotent
>
</
stateless-clustering
>
</
stateless-session-descriptor
>
<
transaction-descriptor
>
<
trans-timeout-seconds
>
20
</
trans-timeout-seconds
>
</
transaction-descriptor
>
<
enable-call-by-reference
>
true
</
enable-call-by-reference
>
<
jndi-name
>
jndi/ConvertEjb
</
jndi-name
>
</
weblogic-enterprise-bean
>
<
idempotent-methods
>
<
method
>
<
ejb-name
>
ConvertEjb
</
ejb-name
>
<
method-name
>
getRmb
</
method-name
>
<
method-params
>
<
method-param
>
double
</
method-param
>
</
method-params
>
</
method
>
</
idempotent-methods
>
</
weblogic-ejb-jar
>
打成jar包之后再放到domain中application文件夹中,并通过weblogic来部署,这样就可以执行客户端程序了。