您的位置:首页 > 编程语言 > Java开发

EJB学习二 我的第一个EJB项目(Eclipse下的EJB开发,与JBoss集成,Ant打包)

2013-12-10 23:17 507 查看
EJB项目的创建(使用开发工具eclipse)

1、打开eclipse,新建一个普通的Java Project(实际开发使用的是java Project而不是ejb的)



2、点击Next,添加外部的jar包



3、为了方便,可以将jboss-5.1\cliient下的所有jar包全部导入(入门...将就一下...)



4、点击finish完成

EJB项目的第一个程序HelloWorld(无状态的会话Bean)

创建接口

public interface HelloWorld {

public String sayHello(String name);

}


创建实现类(命名规则:一般为 接口名+Bean)

public class HelloWorldBean implements HelloWorld{

public String sayHello(String name) {
return name+"说:你好,世界!我的第一个ejb程序";
}

}


当前目录结构



声明为ejb程序(添加注解)



发布项目

1)首先需要将项目打成jar包
方式一:通过集成工具方式(eclipse自带到的)

点击File -->Export



点击Java,选中JAR file



选择需要进行打包的,点击finish完成



2)启动jboss
通过jboss-5.1.0.GA\bin目录下的run.bat启动jboss

将打好的jar包复制到jboss-5.1.0.GA\server\default\deploy(项目部署目录)

发布成功,你就可以在 jboss 的管理平台查看她们的 JNDI ,输入下面 URL http://localhost:8080/jmx-console/


点击 service=JNDIView



在出现的页面中,找到“List”栏。点击”Invoke”按钮,出现如下界面:



在上图中可以看见 HelloWorld 会话 Bean 的 JNDI , JNDI 名的组成规则是 “上层名称/下层名称 “, 每层之间以”/”
分隔。HelloWorld 会话 Bean 的 JNDI 是:HelloWorldBean/remote 。

3)编写客户端进行调用

import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.runtimebj.ejb3.HelloWorld;
public class EJBClient {
public static void main(String[] args) {
Properties props = new Properties();
//建立通信
props.setProperty("java.naming.factory.initial" ,"org.jnp.interfaces.NamingContextFactory");
//第一个值固定写法
props.setProperty("java.naming.provider.url", "localhost:1099");
try{
InitialContext ctx = new InitialContext(props);
HelloWorld helloworld = (HelloWorld) ctx.lookup("HelloWorldBean/remote");// (JNDI名称)
System.out.println(helloworld.getClass().getName()); //打印调用名称(证明是远程调用而不是本地接口)
System.out.println(helloworld.sayHello("我了个擦?莫名其妙"));

} catch(NamingException e) {
System.out.println(e.getMessage());
}
}
}


通过OutLine,执行



结果(原谅我我懒得改了)



这就是第一个HelloWorld程序,起始可以将对客户端进行简化,将try外的代码进行提取,在src下创建jndi.properties配置文件

配置文件jndi.properties

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=localhost:1099


测试类EJBClient

import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.runtimebj.ejb3.HelloWorld;

public class EJBClient {

public static void main(String[] args) {

try{
InitialContext ctx = new InitialContext();
HelloWorld helloworld = (HelloWorld) ctx.lookup("HelloWorldBean/remote");
System.out.println(helloworld.getClass().getName());
System.out.println(helloworld.sayHello("奥特曼"));

} catch(NamingException e) {
System.out.println(e.getMessage());
}
}
}


将JBoos集成到eclipse中(为了提高开发效率,JBoss一般会集成到Eclipse中)

进入woindows -->showView --> other --> 选择server

右键创建server



根据当前环境选择JBoss的版本



选择jdk与JBoss的安装路径



下一步,默认即可



点击完成!

通过

或者鼠标右键start启动

如果启动Eclipse报错,错误类似于

Server JBoss v5.0 2 at localhost was unable to start within 50 seconds.
If the server requires more time, try increasing the timeout in the server editor.


请参见Eclipse启动JBoss报错

ANT方式打包
企业开发为了方便打包,减少不必要的时间浪费一般会采用ant的打包方式
在项目下创建build.xml文件



文件中的内容如下

<?xml version="1.0"?>

<!-- ======================================================================= -->
<!-- EJB3 HelloWorld build file                                                       -->
<!-- ======================================================================= -->
<!-- name 表示项目名称(自定义) basedir指定项目所在路径:'.'表示项目所在路径就是在build.xml所在路径 '..'表示项目在build.xml的上级目录-->
<project name="HelloWorld"  basedir=".">
<!-- 指向操作系统中的环境变量 -->
<property environment="env" />

<!-- 用于指定项目的源文件所在目录  basedir:引用第一行中basedir所在的路径    -->
<property name="src.dir" value="${basedir}/src" />
<!-- 表示引用环境变量中的JBOSS_HOME -->
<property name="jboss.home" value="${env.JBOSS_HOME}" />
<!-- 指定目前jboss的配置项 -->
<property name="jboss.server.config" value="default" />
<!-- 方便后面使用,class类存放目录 -->
<property name="build.dir" value="${basedir}/build" />

<!-- Build classpath -->
<path id="build.classpath">
<!-- 引入jboss目录下client下的所有jar文件 -->
<fileset dir="${jboss.home}\client">
<include name="*.jar" />
</fileset>
<pathelement location="${build.dir}" />
</path>

<!-- =================================================================== -->
<!-- 定义了一个工作                                       -->
<!-- =================================================================== -->
<target name="prepare" >
<delete dir="${build.dir}" />
<mkdir dir="${build.dir}" />
</target>

<!-- =================================================================== -->
<!-- 编译                                           -->
<!-- =================================================================== -->
<!-- depends表示依赖,回先执行prepare-->
<target name="compile" depends="prepare" description="编绎">
<javac srcdir="${src.dir}" destdir="${build.dir}" >
<!--引用前面配置的jar路径-->
<classpath refid="build.classpath" />
</javac>
</target>

<!-- ejb的打包 -->
<target name="ejbjar" depends="compile" description="创建EJB发布包">
<!-- {ant.project.name}表示项目名称 -->
<jar jarfile="${basedir}/${ant.project.name}.jar">
<fileset dir="${build.dir}">
<include name="**/*.class" />
</fileset>
</jar>
</target>

<target name="deploy" depends="ejbjar" description="发布ejb" >
<!-- 发布实际就是一个拷贝的过程,将打包好的jar复制到deploy发布目录下 -->
<copy file="${basedir}\${ant.project.name}.jar" todir="${jboss.home}/server/${jboss.server.config}/deploy" />
</target>

<!-- =================================================================== -->
<!-- 卸载ejb                                          -->
<!-- =================================================================== -->
<target name="undeploy" description="卸载ejb" >
<delete file="${jboss.home}/server/${jboss.server.config}/deploy/${ant.project.name}.jar" />
</target>

</project>
双击build.xml文件,在outline中看到如下



因为deploy表示的是发布项目,而且它依赖于deploy,deploy依赖于ejbjar的,ejbjar依赖于prepare,所以我们直接运行deploy即可
方法:



建议启动流程:启动JBoss-->右键Ant Build deploy完成jar包的自动发布-->执行客户端测试程序
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐