开发环境

  • IntelliJ IDEA 14.1.4
  • JDK1.7
  • Jboss4.0
  • EJB2.1

依赖库

依赖库

描述文件

描述文件

EJB容器

EJB容器

项目结构

项目结构

EJB的jar包

EJB的jar包

ejb-jar.xml内容

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar id="ejb-jar_ID" version="2.1"
         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">
    <display-name>EJB-demo2.x</display-name>
    <enterprise-beans>
        <session>
            <display-name>helloWorldEJB</display-name>
            <ejb-name>helloWorldEJB</ejb-name>
            <home>HelloHome</home>
            <remote>HelloRemote</remote>
            <ejb-class>HelloBean</ejb-class>
            <session-type>Stateless</session-type>
            <transaction-type>Container</transaction-type>
        </session>
    </enterprise-beans>
</ejb-jar>

jboss.xml内容

<?xml version="1.0" encoding="UTF-8"?>
<jboss>
        <enterprise-beans>
                <session>
                        <ejb-name>helloWorldEJB</ejb-name>
                        <jndi-name>ejb/helloEJB</jndi-name>
                </session>
        </enterprise-beans>
</jboss>

HelloBean.java内容

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;
import java.nio.channels.Channel;
import java.rmi.RemoteException;

/**
 * Created by Admin on 2015/10/5.
 */
public class HelloBean implements SessionBean {

    public String sayHello(String name){
        System.out.print(name);
        return name;
    }

    public void create(){

    }

    public void setSessionContext(SessionContext sessionContext) throws EJBException {

    }

    public void ejbRemove() throws EJBException {

    }

    public void ejbActivate() throws EJBException {

    }

    public void ejbPassivate() throws EJBException {

    }

    public void ejbCreate() throws CreateException {

    }
}

HelloHome.java内容

import javax.ejb.CreateException;
import javax.ejb.EJBHome;
import java.rmi.RemoteException;

/**
 * Created by Admin on 2015/10/5.
 */
public interface HelloHome extends EJBHome {
    HelloRemote create() throws RemoteException, CreateException;
}

HelloRemote.java内容

import javax.ejb.EJBObject;
import java.rmi.RemoteException;

/**
 * Created by Admin on 2015/10/5.
 */
public interface HelloRemote extends EJBObject {
    public String sayHello(String name) throws RemoteException;
}

TestClient.java内容

import javax.ejb.CreateException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;
import java.util.Properties;

/**
 * Created by Admin on 2015/10/5.
 */
public class TestClient {
    public static void main(String[] args) throws NamingException, RemoteException, CreateException {
        Properties props = new Properties();
        props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
        props.setProperty("java.naming.provider.url", "localhost:1099");
        //初始化JNDI上下文环境,因为客户端不知道JBOSS的环境
        InitialContext ctx = new InitialContext(props);
        //检索指定的对象
        Object objref = ctx.lookup("ejb/helloEJB");
        //强制转换为所需类型的对象
        HelloHome home = (HelloHome) PortableRemoteObject.narrow(objref, HelloHome.class);
        //通过home对象创建一个组件接口对象
        HelloRemote hello = home.create();
        //通过组件接口对象调用业务方法
        String msg = hello.sayHello("ejb");
        System.out.println(msg);
    }
}

(完)