weblogic10+ejb3

来源:互联网 发布:软件开发个人工作总结 编辑:程序博客网 时间:2024/05/21 15:50


    在本文主要介绍如何来开发一个Stateless Session Bean,并在未安装WebLogic10的机器上访问Session Bean。开发EJB3 Stateless Session Bean要比开发EJB2 Stateless Session Bean容易得多,只需要几个注释就可以搞定。读者可按如下的步骤来开发和调用EJB3Stateless Session Bean

1步:编写远程接口

    每一个Session Bean需要一个远程接口,该接口的代码如下:

package com.earth;

import javax.ejb.Remote;

//  此处必须使用@Remote注释指定该接口为远程接口
@Remote
public interface CompanyRemote
{
    
public String getName();
    
public Employee[] getEmployees();
}

 

<!--/* Font Definitions */&#64;font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;}&#64;font-face{font-family:""&#64;宋体";panose-1:2 1 6 0 3 1 1 1 1 1;}/* Style Definitions */p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;font-size:10.5pt;font-family:"Times New Roman";}/* Page Definitions */&#64;page{}&#64;page Section1{size:612.0pt 792.0pt;margin:72.0pt 90.0pt 72.0pt 90.0pt;}div.Section1{page:Section1;}-->

    CompanyRemote接口中定义了两个方法,分别返回一个字符串和一个Employee类型的数组。

2步:编写Employee

    Employee类是一个实现implements java.io.Serializable接口的JavaBean,代码如下:

package com.earth;

public class Employee implements java.io.Serializable
{
    
private String name;
    
private String job;
    
private int age;
    
public String getName()
    {
        
return name;
    }
    
public void setName(String name)
    {
        
this.name = name;
    }
    
public String getJob()
    {
        
return job;
    }
    
public void setJob(String job)
    {
        
this.job = job;
    }
    
public int getAge()
    {
        
return age;
    }
    
public void setAge(int age)
    {
        
this.age = age;
    }
}

<!--/* Font Definitions */&#64;font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;}&#64;font-face{font-family:""&#64;宋体";panose-1:2 1 6 0 3 1 1 1 1 1;}/* Style Definitions */p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;font-size:10.5pt;font-family:"Times New Roman";}/* Page Definitions */&#64;page{}&#64;page Section1{size:612.0pt 792.0pt;margin:72.0pt 90.0pt 72.0pt 90.0pt;}div.Section1{page:Section1;}-->

3步:编写StatelessSession Bean

    Session Bean需要实现CompanyRemote接口,代码如下:

package com.earth;

import java.util.List;
import java.util.ArrayList;
import javax.ejb.Remote;
import javax.ejb.Stateless;

@Stateless(mappedName 
= "Company")
public class Company implements CompanyRemote
{
    
public Employee[] getEmployees()
    {
        Employee[] employees 
= new Employee[2];
        employees[
0= new Employee();
        employees[
0].setName("superman");
        employees[
0].setJob("CEO");
        employees[
0].setAge(1234);
        
        employees[
1= new Employee();
        employees[
1].setName("擎天柱");
        employees[
1].setJob("CTO");
        employees[
1].setAge(4321);
        
        
return employees;
    }
    
public String getName()
    {        
        
return "地球软件有限公司";
    }
}

<!--/* Font Definitions */&#64;font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;}&#64;font-face{font-family:""&#64;宋体";panose-1:2 1 6 0 3 1 1 1 1 1;}/* Style Definitions */p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;font-size:10.5pt;font-family:"Times New Roman";}/* Page Definitions */&#64;page{}&#64;page Section1{size:612.0pt 792.0pt;margin:72.0pt 90.0pt 72.0pt 90.0pt;}div.Section1{page:Section1;}-->

    这个Session Bean使用了@Stateless注释指定了该Session Bean是无状态的,并使用了mappedName属性指定该Session BeanJNDI名的前一部分,如果mappedName属性的值是Company,则该Session BeanJNDI名是Company#com.earth.CompanyRemote

4步:编译.java文件,并生成.jar文件


    将上面三个.java文件放到同一个目录下,并使用如下的命令生成相应的.class文件:

javac -d . -classpath .;C:/bea/wlserver_10.3/server/lib/weblogic.jar *.java

<!--/* Font Definitions */&#64;font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;}&#64;font-face{font-family:""&#64;宋体";panose-1:2 1 6 0 3 1 1 1 1 1;}/* Style Definitions */p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;font-size:10.5pt;font-family:"Times New Roman";}/* Page Definitions */&#64;page{}&#64;page Section1{size:612.0pt 792.0pt;margin:72.0pt 90.0pt 72.0pt 90.0pt;}div.Section1{page:Section1;}-->

    注意:在编译.java文件时要将在classpath环境变量或javac-classpath参数中指定weblogic.jar文件,假设WebLogic10安装在了C盘的bea目录,则weblogic.jar文件位于C:"bea"wlserver_10.3"server"lib目录中,本例使用了weblogic10.3
    在生成.class文件后,使用如下的命令生成company.jar文件:

jar cvf company.jar com


<!--/* Font Definitions */&#64;font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;}&#64;font-face{font-family:""&#64;宋体";panose-1:2 1 6 0 3 1 1 1 1 1;}/* Style Definitions */p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;font-size:10.5pt;font-family:"Times New Roman";}/* Page Definitions */&#64;page{}&#64;page Section1{size:612.0pt 792.0pt;margin:72.0pt 90.0pt 72.0pt 90.0pt;}div.Section1{page:Section1;}-->

5步:发布EJB

    发布EJB的方法很多,然而最简单的是直接将company.jar文件复制到如下的目录:

C:/bea/user_projects/domains/base_domain_new/autodeploy

    其中base_domain_new是域名,读者也可将company.jar文件复制到其他域的autodeploy目录中。
    启动Weblogic,该EJB自动发布。读者可以使用如下的URL来查看在当前Weblogic服务器中注册的JNDI


http://localhost:7001/console/consolejndi.portal?_nfpb=true&_pageLabel=JNDIHomePage&server=AdminServer

    其中AdminServer为Weblogic的服务名,可能在读者的机器上是其他的服务名,请使用如下的URL进行Weblogic Console进行查看:

http://localhost:7001/console

<!--/* Font Definitions */&#64;font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;}&#64;font-face{font-family:""&#64;宋体";panose-1:2 1 6 0 3 1 1 1 1 1;}/* Style Definitions */p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;font-size:10.5pt;font-family:"Times New Roman";}/* Page Definitions */&#64;page{}&#64;page Section1{size:612.0pt 792.0pt;margin:72.0pt 90.0pt 72.0pt 90.0pt;}div.Section1{page:Section1;}-->

    安装EJB后,本机注册的JNDI如下图所示。

<!--/* Font Definitions */&#64;font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;}&#64;font-face{font-family:""&#64;宋体";panose-1:2 1 6 0 3 1 1 1 1 1;}/* Style Definitions */p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;font-size:10.5pt;font-family:"Times New Roman";}/* Page Definitions */&#64;page{}&#64;page Section1{size:612.0pt 792.0pt;margin:72.0pt 90.0pt 72.0pt 90.0pt;}div.Section1{page:Section1;}-->
6
步:编写客户端程序

    调用EJB的客户端代码如下:

import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import com.earth.*;

public class TestCompany
{
    
public static void main(String[] args) throws Exception
    {

        Properties props 
= new Properties();
        props.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                
"weblogic.jndi.WLInitialContextFactory");
        props.setProperty(Context.PROVIDER_URL, 
"t3://192.168.17.127:7001");
        InitialContext ctx 
= new InitialContext(props);
        CompanyRemote companyRemote 
= (CompanyRemote) ctx
                .lookup(
"Company#com.earth.CompanyRemote");
        System.out.println(companyRemote.getName());
        Employee[] employees 
= companyRemote.getEmployees();
        
for (Employee employee : employees)
        {
            System.out.println(
"name:" + employee.getName());
            System.out.println(
"job:" + employee.getJob());
            System.out.println(
"age:" + employee.getAge());
            System.out.println(
"-------------------------");            
        }
    }
}

<!--/* Font Definitions */&#64;font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;}&#64;font-face{font-family:""&#64;宋体";panose-1:2 1 6 0 3 1 1 1 1 1;}/* Style Definitions */p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;font-size:10.5pt;font-family:"Times New Roman";}/* Page Definitions */&#64;page{}&#64;page Section1{size:612.0pt 792.0pt;margin:72.0pt 90.0pt 72.0pt 90.0pt;}div.Section1{page:Section1;}-->    使用如下的命令编译TestCompany.java

javac  -classpath .;company.jar;E:/bea/wlserver_10.3/server/lib/weblogic.jar TestCompany.java

<!--/* Font Definitions */&#64;font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;}&#64;font-face{font-family:""&#64;宋体";panose-1:2 1 6 0 3 1 1 1 1 1;}/* Style Definitions */p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;font-size:10.5pt;font-family:"Times New Roman";}/* Page Definitions */&#64;page{}&#64;page Section1{size:612.0pt 792.0pt;margin:72.0pt 90.0pt 72.0pt 90.0pt;}div.Section1{page:Section1;}-->

    注意:TestCompany.javacompany.jar文件需要在同一目录下。

   
由于调用客户端的代码并不一定在安装WebLogic的机器上运行,因此,在将TestCompany.classcompany.jar(发布到客户端的这个jar文件只需要Employee.classCompanyRemote.class文件即可,Company.class可以从该jar文件中删除)发布到客户端时还需要带一些WebLogic自身的jar文件。虽然Weblogic中有一个wlclient.jar文件,但光有这个文件还不够,不了方便提取客户端需要的jar文件,Weblogic提供了一个jar包,通过该包,可以将客户端需要的所有.class文件打成一个jar包。
    这个jar文件是C:/bea/modules/com.bea.core.jarbuilder_1.2.0.0.jar,可通过如下的命令行来生成客户端需要的jar包:

    java -jar ../../../modules/com.bea.core.jarbuilder_1.2.0.0.jar

 

<!--/* Font Definitions */&#64;font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;}&#64;font-face{font-family:""&#64;宋体";panose-1:2 1 6 0 3 1 1 1 1 1;}/* Style Definitions */p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;font-size:10.5pt;font-family:"Times New Roman";}/* Page Definitions */&#64;page{}&#64;page Section1{size:612.0pt 792.0pt;margin:72.0pt 90.0pt 72.0pt 90.0pt;}div.Section1{page:Section1;}-->    注意:上面的命令必须在C:/bea/wlserver_10.3/server/lib目录中执行。

    在执行上面的命令后,将在E:/bea/wlserver_10.3/server/lib目录生成一个wlfullclient.jar文件,将该文件与TestCompany.classcompany.jar一同复制到客户端的机器上即可。并通过如下的命令来执行TestCompany.class

    java -classpath .;company.jar;wlfullclient.jar TestCom

 

 

<!--/* Font Definitions */&#64;font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;}&#64;font-face{font-family:""&#64;宋体";panose-1:2 1 6 0 3 1 1 1 1 1;}/* Style Definitions */p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;font-size:10.5pt;font-family:"Times New Roman";}/* Page Definitions */&#64;page{}&#64;page Section1{size:612.0pt 792.0pt;margin:72.0pt 90.0pt 72.0pt 90.0pt;}div.Section1{page:Section1;}-->    执行上面的命令后的输出结果如下面两个图所示:



Windows下的执行结果



Linux下的执行结果

原创粉丝点击