EJB3下無狀態(tài)會話Bean發(fā)布為WebService
無狀態(tài)會話Bean發(fā)布為WebService是非常有意義的,因為如果我們將每一個EJB模塊按照SOA的思想做成一個一個獨立的服務模塊(這里不討論SCA,我認為Apache的SCA框架還不夠強大,文檔還不夠全面),同樣的代碼在JavaEE體系之內的應用程序可以直接使用EJB3直接訪問,JavaEE體系之外的應用程序可以直接使用WebService訪問,這一點是非常有意義的。
下面要考慮的就是如何只寫一份代碼,讓它能做兩樣事情。
網上有大量的使用EJB3發(fā)布WebService的教程,但是這些教程都是一個人寫的,基本上沒有太大的參考價值。最重要的是,他寫的元注釋都是寫在實現類上的,接口上沒有做任何描述。這種做法有兩個問題,第一如果你的會話Bean使用了EM,那么麻煩了,JAXB在做綁定的時候不認識接口;第二WSDL文件的描述實際上應該是基于接口的描述,應該和實現類沒有太大的關系。
看看網上流行的代碼:
1
@WebService(name = "PurchaseArrival", serviceName = "PurchaseArrivalService")
2
@SOAPBinding(style = SOAPBinding.Style.RPC)
3
public class PurchaseArrivalImpl implements IPurchaseArrival {

2

3

注意,基于RPC綁定模式下List,Set一類的接口都是不可傳輸的,這點會很麻煩,要用必須用實現類。
看看我的代碼:
接口:
1
@WebService(name = "ILogRemoteService",targetNamespace = "http://www.glnpu.com/dmp/xml")
2
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT)
3
public interface ILogRemoteService {

2

3

實現類:
1
@Stateless
2
@Remote(ILogRemoteService.class)
3
@WebService(endpointInterface = "com.glnpu.dmp.server.service.base.log.ILogRemoteService",serviceName = "LogRemoteService")
4
public class LogRemoteService extends BaseService implements ILogRemoteService {

2

3

4

這個WebService是針對接口發(fā)布出去,而且使用的DOCUMENT樣式,可以直接使用List、Set的接口。
這個WebService生成的WSDL文件很有意思:
1
<definitions name="LogRemoteService" targetNamespace="http://impl.log.base.service.server.dmp.glnpu.com/" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://www.glnpu.com/dmp/xml" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.log.base.service.server.dmp.glnpu.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
2
3
<import location="http://127.0.0.1:8080/LogRemoteServiceService/LogRemoteService?wsdl&resource=ILogRemoteService_PortType25057.wsdl" namespace="http://www.glnpu.com/dmp/xml" />
4
5
- <service name="LogRemoteService">
6
7
- <port binding="ns1:ILogRemoteServiceBinding" name="LogRemoteServicePort">
8
9
<soap:address location="http://127.0.0.1:8080/LogRemoteServiceService/LogRemoteService" />
10
11
</port>
12
13
</service>
14
15
</definitions>
16
17

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

里面有import來指引到真正的WSDL文件。這樣很容易將會話Bean變成WS~一樣的代碼兩樣的功能,超值!
客戶虐我千百遍,我待客戶如初戀!
posted on 2009-07-02 13:54 阿南 閱讀(1954) 評論(0) 編輯 收藏 所屬分類: EJB3 、個人原創(chuàng)