欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

部署Java在服务器端的EJB组件的方法

程序员文章站 2024-03-07 08:36:32
什么是ejb? ejb 是 java 企业bean, 是javaee服务端 企业组件模型,它的设计目标与核心应用是部署分布式应用程序。话不多说,直接看如何在本机部署ejb...

什么是ejb?
ejb 是 java 企业bean, 是javaee服务端 企业组件模型,它的设计目标与核心应用是部署分布式应用程序。话不多说,直接看如何在本机部署ejb3。
 
部署环境:
操作系统:windows 8.1
ejb容器:jboss 7.1
db: mysql 5.6.10
ide: myeclipse 10
jdk: 1.6
 
1、创建数据库、表
由于在此过程中,需要和数据库通信,需要首先创建数据库表。
创建数据库:

create database student; //创建数据库 student

创建表:

create table student( //创建表student 和 数据库同名
`id` integer(11) not null, 
`name` varchar2(20) default null,
primary key (`id`)
)engine=innodb default charset=latin1

插入一条数据:
insert into student values(1,'easynoder');
commit;
给本地的root用户(生产环境请勿这样做)指定访问权限。
grant all privileges on *.* to root@localhost indentified by "1234"
 
通过以上步骤,需要的数据库表已建立好。可通过root用户访问所有数据。
 
2、编写实体bean、用户操作接口和会话bean
 
建立ejb工程,名为myejbproject。该工程meta-info目录下包含一个文件persistence.xml文件。该文件用来配置数据源,稍后进行配置。
接着建立实体bean
@entity //表明这是一个实体bean
@table (name = "student" ) //和数据库表student 建立映射
public class studententity implements serializable {
  private static final long serialversionuid = 4002145187978562529l;

  @id // 表明是该实体的id
  @generatedvalue(strategy = generationtype. auto ) //id生成策略
  @column(name = "id" )//对应student表id字段
  private int id ; 

  @column(name = "name" ) // 对应student表name字段
  private string name;

  public int getid() {
    return id ;
  }

  public string getname() {
    return name ;
  }

  public void setid(int id) {
    this .id = id;
  }

  public void setname(string name) {
    this .name = name;
  }

}

建立操作接口:

 public interface baseoperation {
 
   public list<?> findall();
 }

该接口只有一个方法,获取所有的学生
 
建立会话bean

@stateless //这是一个无状态bean
@remote (baseoperation. class) //指明bean的remote接口
public class studentdaobean implements baseoperation {
   
   // entitymanager是由ejb容器自动配置和管理的,unitname属性的值对应
persistence.xml 中< persistence-unit name = "myejbproject" transaction-type = "jta" ></ persistence-unit > name的配置
  @persistencecontext(unitname = "myejbproject" )
  private entitymanager em;

  @suppresswarnings( "unchecked" )
  public list<?> findall() {
    system. out .println("查询开始..." );
    list<studententity> list = em.createquery( " from studententity ").getresultlist();
    if (list != null) {
      iterator<studententity> it = list.iterator();
      while (it.hasnext()) {
        studententity student = it.next();
        system. out .println("学生id:" + student.getid());
        system. out .println("学生名称:" + student.getname());
      }
    }
    system. out .println("查询完毕...." );
    return list;
  }

}

3、数据源配置
这里需要注意下,在jboss6 和jboss7的配置是不同的。
jboss6和以前版本都是在deploy目录下 添加**-ds.xml,这里**表示任意一种数据库名称,如果是mysql,则是mysql-ds.xml文件。而在jboss7中,是不一样的。
将目录切换至jboss 的安装目录,即%jboss_home%下,进入modules/com目录,建立文件夹mysqldatabase,进入,建立mysql文件夹,进入,建立main文件夹。
在main目录下,建立module.xml文件,该配置文件内容为:

<?xml version="1.0" encoding="utf-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.mysqldatabase.mysql">
   <resources>
     <resource-root path="mysql-connector-java-5.**-bin.jar"/>
   </resources>
   <dependencies>
     <module name="javax.api"/>
     <module name="javax.transaction.api"/>
     <module name="javax.servlet.api" optional="true"/>
   </dependencies>
</module>

尤其这里需要注意的是,module 节点属性name的值,就是刚才咱们建立的文件夹的路径。resources表示mysql驱动的路径。意味着,需要将mysql的驱动放在main目录下。即main目录下包含两个文件,module.xml和数据库驱动文件。
 
在做完上一步后,切换到%jboss_home%\standalone\configuration目录下,
打开standalone.xml,搜索datasources,进行如下配置

<datasources>
        <datasource jndi-name="java:jboss/koumysqlds" pool-name="mysqlds" enabled="true" use-java-context="true">
          <connection-url>jdbc:mysql://localhost:3306/student</connection-url>
          <driver>mysql</driver>
          <security>
            <user-name>root</user-name>
            <password>1234</password>
          </security>
        </datasource>
        <drivers>
          <driver name="mysql" module="com.mysqldatabase.mysql">
            <driver-class>com.mysql.jdbc.driver</driver-class>
            <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.mysqlxadatasource</xa-datasource-class>
          </driver>
        </drivers>
      </datasources>

jndi-name表示数据源jndi名称,connection-url表示连接的url字符串;这里默认使用3306端口,使用student库,用户名和密码即第一步配置的。module配置的即刚刚配置的module的路径。
 
jboss的相关配置已经完成,接着切换到刚新建的工程,其中有一个persistence.xml配置文件,该文件做如下配置,其中jta-data-source 就是上面配置的jndi-name.
 

< jta-data-source> java:jboss/koumysqlds </jta-data-source >
      < properties>
        < property name= "hibernate.hbm2ddl.auto" value ="validate" />
        < property name= "hibernate.jdbc.fetch_size" value ="15" />
        < property name= "hibernate.jdbc.batch_size" value ="10" />
        < property name= "hibernate.show_sql" value ="true" />
        < property name= "hibernate.format_sql" value ="true" ></ property>
      </ properties>

到此为止,服务端代码和数据源配置已经完成。接下来需要做的就是如何部署代码以及如何在客户端调用该ejb服务。
 
4、部署ejb服务。
将之前在工程中写的所有代码打成jar包,命名为ejbservice.jar。同时,只将实体bean和接口打包成jar包,命名为ebjinterface.jar,这个jar将来用于客户端调用使用。
将ejbservice.jar放入%jboss_home%\standalone\deployments目录下。在jboss启动时,会自动扫描该目录。然后部署该jar。
 
ok,我们将jboss配置到myeclipse下,在myeclipse中启动jboss,观察控制台的输出。
如果出现了 deployed "ejbservice.jar" 这个日志,说明ejb就部署成功了。
 
5、客户端如何调用呢?
客户端调用需要两个必备条件:
引入jboss-ejb-client.properties配置、 jboss-client.jar和ejbinterface.jar。其中jboss-client.jar 位于jboss bin/client目录下。ejbinterface.jar是我们刚刚创建的客户端需要使用的接口jar包。
jboss-ejb-client.properties配置如下:

endpoint.name= client-endpoint
remote.connectionprovider.create.options.org.xnio.options.ssl_enabled= false
remote.connections= default
remote.connection.default.host= localhost
remote.connection.default.port= 4447
remote.connection.default.connect.options.org.xnio.options.sasl_policy_noanonymous= false
remote.connection.default.username= yourusername
remote.connection.default.password= yourpassword

 
有了这两个条件,就可以安心的建立个测试类ejbtest.java,编写客户端方法了。

public static void main(string[] args) {

    properties props = new properties();
    props.setproperty(context. url_pkg_prefixes,"org.jboss.ejb.client.naming" );
    try {
      context context = new initialcontext(props);
      // 这里需要注意字符串的写法:ejbservice 表示ejb的包名,studentdaobean表示咱们实际调用的会话bean,org.easynoder.ejb2.dao.baseoperation表示 对应的接口
      baseoperation op = (baseoperation) context
          .lookup("ejb:/ejbservice//studentdaobean!org.easynoder.ejb2.dao.baseoperation" );
      op.findall();
    } catch (namingexception e) {
      e.printstacktrace();
    }
  }

运行这段代码,可以成功的查询到数据库的数据啦。
 
至此,ejb就部署成功啦。