组件接口类
public interface Hello extends EJBObject{
public String getHello() throws RemoteException;
}
主接口类一般命名为xxxHome
public interface HelloHome extends EJBHome {
public Hello create() throws RemoteException,CreateException;
}
实现类一般命名为xxxEJB
public class HelloEJB implements SessionBean {
public void ejbActivate() throws EJBException, RemoteException {
}
public void ejbPassivate() throws EJBException, RemoteException {
}
public void ejbRemove() throws EJBException, RemoteException {
}
public void setSessionContext(SessionContext arg0) throws EJBException,
RemoteException {
}
public void ejbCreate() throws javax.ejb.CreateException {
}
public String getHello() {
return "Hello!EJB ";
}
}
ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd"
version="2.1">
<description>
<![CDATA[No Description.]]>
</description>
<display-name>Generated by XDoclet</display-name>
<enterprise-beans>
<!-- Session Beans -->
<session>
<description>
<![CDATA[Description for Hello]]>
</description>
<display-name>Name for Hello</display-name>
<ejb-name>Hello</ejb-name>
<!-- 主接口类 HelloHome 继承EJBHome 管理Bean的生命周期 -->
<home>
com.eric.interfaces.HelloHome
</home>
<!-- 组件接口类 Hello 继承EJBObject 定义自己的业务处理方法-->
<remote>
com.eric.interfaces.Hello
</remote>
<!-- Bean的实现类 HelloEJB实现SessionBean接口 并实现Hello接口中的业务处理方法-->
<ejb-class>
com.eric.ejb.HelloEJB
</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
<assembly-descriptor>
</assembly-descriptor>
</ejb-jar>
jboss.xml
<?xml version="1.0" encoding="gb2312"?>
<jboss>
<enterprise-beans>
<session>
<ejb-name>Hello</ejb-name>
<jndi-name>ejb/Hello</jndi-name>
</session>
</enterprise-beans>
</jboss>
HelloHome home=null;
String result=null;
Properties props = new Properties();
props.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.provider.url", "localhost:1099");
props.setProperty("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
InitialContext ctx = new InitialContext(props);
home=(HelloHome)ctx.lookup("ejb/Hello");
Hello hello = home.create();
result = hello.getHello();