Maven2下第一个EJB2.1简单实例

        一开始还是得列出Maven2下EJB2.1的目录结构,详细介绍请参照我之前写的一篇文章 [ Maven2下第一个EJB3.0简单实例 ],之所以将EJB2.1的实例与EJB3.0的实例写到一起,是因为确实两个版本变化较大,也是因为很多人确实遇到要从EJB2.1跳转到EJB3.0的情况,就是说要把以前EJB2.1的改写成EJB3.0的,这样的话,肯定需要有一个开始,万事开头难,搞技术一点也例外不了,有些就是在一开始的时候做个简单实例都要花上好长一段时间,反而在做出简单实例后,之后的工作进展就快许多,所以我将这两篇文章摆到一起,有需要就看吧,没需要也不影响。

ejb2test

    |--pom.xml                                                 //Maven项目根目录必须有这文件,主配置文件来的

    |--src                                                         //项目资源文件目录,所有代码,项目相关的都在这

        |--main                                                  //主文件目录,与之相对应的是单元测试test目录

            |--java                                               //JAVA文件目录,使之与图片,配置文件等隔离开来

                |--mydomain                                 //这下面就是你自己的东西,不一一介绍了

                    |--ejb2test

                        |--HaiRemoteHome.java       //EJB2需要一个或两个Home文件,用来返回EJBObject

                        |--HaiRemote.java                //Remote接口,相对应的是Local接口,二选一或二全选

                        |--HaiBean.java                    //这个才是主角,SessionBean文件,里面才有实现方法

            |--resources                                      //资源文件,像一些配置文件全放这,图片也放这

                |--META-INF

                    |--ejb-jar.xml                            

                    |--jboss.xml

        |--test                                                    //单元测试目录,与主目录main相隔离,可不要这文件夹

            |--java                                               //下面这些目录与main里面的java目录一模一样,对应的

                |--mydomain

                    |--ejb2test

 

下面是整个pom文件,全贴出来了


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mydomain</groupId>
    <artifactId>ejb2.1</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>ejb2 Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

       <!--就只需这一个包-->
        <dependency>
            <groupId>jboss</groupId>
            <artifactId>jboss-j2ee</artifactId>
            <version>4.0.2</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>ejb2</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

 

下面是HaiRemoteHome,HaiRemote与HaiBean的源码


1、HaiRemoteHome.java

package com.mydomain;

 

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


public interface HaiRemoteHome extends EJBHome
{
    HaiRemote create() throws RemoteException, CreateException;
}

 

2、HaiRemote.java

package com.mydomain;

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


public interface HaiRemote extends EJBObject
{
    String sayHai() throws RemoteException;
}

3、HaiBean.java

package com.mydomain;

import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

public class HaiBean implements SessionBean
{

    public void setSessionContext(SessionContext arg0) throws EJBException, RemoteException
    {
        System.out.println("set session context");
    }
   
    public void ejbCreate() throws EJBException
    {
        System.out.println("ejb create");
    }

    public void ejbRemove() throws EJBException, RemoteException
    {
        System.out.println("ejb remove");
    }

    public void ejbActivate() throws EJBException, RemoteException
    {
        System.out.println("ejb activate");
    }

    public void ejbPassivate() throws EJBException, RemoteException
    {
        System.out.println("ejb passivate");
    }

    public String sayHai() throws RemoteException
    {
        return "Hai, EJB 2!";
    }

}

 

下面是ejb-jar.xml与jboss.xml的内容

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>
    <description>Hai EJB instance.</description>
    <display-name>Hai EJB</display-name>
    <enterprise-beans>
        <session>
            <ejb-name>HaiEJB</ejb-name>
            <home>com.mydomain.HaiHome</home>
            <remote>com.mydomain.HaiRemote</remote>
            <ejb-class>com.mydomain.HaiBean</ejb-class>
            <session-type>Stateful</session-type>
            <transaction-type>Bean</transaction-type>
        </session>
    </enterprise-beans>
</ejb-jar>

2、jboss.xml

<?xml version="1.0" encoding="UTF-8"?>

<jboss>
    <enterprise-beans>
        <session>
            <ejb-name>HaiEJB</ejb-name>
            <jndi-name>HaiEJB</jndi-name>
        </session>
    </enterprise-beans>
</jboss>

写完这些文件,就可以用mvn生成jar包了

mvn install

然后在项目根目录的target文件夹下,有一个ejb2.jar,将它复制到jboss文件夹下,server/default/deploy目录中,然后在命令行中运行jboss文件夹下的/bin/run.bat文件,这一过程通常称之为deploy

 

待deploy成功后,就可以写一个java application进行测试了

新建一个java application

导入jboss目录下client文件夹下的jboss-j2ee.jar,

然后新建一个类,写上以下代码

 

import com.mydomain.HaiHome;
import com.mydomain.HaiRemote;
import java.rmi.RemoteException;
import java.util.Properties;
import javax.ejb.CreateException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;


public class Main
{
    public static void main(String[] args)
    {
        System.out.println("===========================================================");
        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");
        try
        {
            InitialContext ctx = new InitialContext(props);
           
            Object objRef = ctx.lookup("HaiEJB");
           
            HaiHome home = (HaiHome) PortableRemoteObject.narrow(objRef, HaiHome.class);
           
            HaiRemote remote = home.create();
           
            System.out.println(remote.sayHai());
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
}

 如果顺利的话,你应该可以看到以下信息

Hai, EJB 2!

你可能感兴趣的:(apache,maven,xml,jboss,ejb)