在本文主要介紹如何來開發一個Stateless Session Bean,並在未安裝WebLogic10的機器上訪問Session Bean。開發EJB3 Stateless Session Bean要比開發EJB2 Stateless Session Bean容易得多,只需要幾個註釋就可以搞定。讀者可按如下的步驟來開發和調用EJB3
Stateless Session Bean:
第1步:編寫遠程接口
每一個Session Bean需要一個遠程接口,該接口的代碼如下:
import javax.ejb.Remote;
// 此處必須使用@Remote註釋指定該接口爲遠程接口
@Remote
public interface CompanyRemote
{
public String getName();
public Employee[] getEmployees();
}
在CompanyRemote接口中定義了兩個方法,分別返回一個字符串和一個Employee類型的數組。
第2步:編寫Employee類
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 */ @font-face {font-family:宋體; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋體"; 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 */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->
第3步:編寫Stateless
Session Bean
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 */ @font-face {font-family:宋體; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋體"; 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 */ @page {} @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 Bean的JNDI名的前一部分,如果mappedName屬性的值是Company,則該Session Bean的JNDI名是Company#com.earth.CompanyRemote。
第4步:編譯.java文件,並生成.jar文件
將上面三個.java文件放到同一個目錄下,並使用如下的命令生成相應的.class文件:
<!-- /* Font Definitions */ @font-face {font-family:宋體; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋體"; 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 */ @page {} @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文件:
<!--
/* Font Definitions */
@font-face
{font-family:宋體;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:""@宋體";
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 */
@page
{}
@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 */ @font-face {font-family:宋體; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋體"; 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 */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->
安裝EJB後,本機註冊的JNDI如下圖所示。
第6步:編寫客戶端程序
調用EJB的客戶端代碼如下:
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 */
@font-face
{font-family:宋體;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:""@宋體";
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 */
@page
{}
@page Section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.Section1
{page:Section1;}
-->
使用如下的命令編譯TestCompany.java:
<!-- /* Font Definitions */ @font-face {font-family:宋體; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋體"; 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 */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->
注意:TestCompany.java與company.jar文件需要在同一目錄下。
由於調用客戶端的代碼並不一定在安裝WebLogic的機器上運行,因此,在將TestCompany.class和company.jar(發佈到客戶端的這個jar文件只需要Employee.class和CompanyRemote.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包:
<!-- /* Font Definitions */ @font-face {font-family:宋體; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋體"; 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 */ @page {} @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.class、company.jar一同複製到客戶端的機器上即可。並通過如下的命令來執行TestCompany.class:
<!--
/* Font Definitions */
@font-face
{font-family:宋體;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:""@宋體";
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 */
@page
{}
@page Section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.Section1
{page:Section1;}
-->
執行上面的命令後的輸出結果如下面兩個圖所示:
Windows下的執行結果
Linux下的執行結果