我有一個簡單的web服務,使用Netbeans 6.5創建並部署到2個glassfish服務器V2.1和V3。 ws有一個基本方法GetInstanceID,我從客戶端調用3次。可以在glassfish V3上擁有單例生命週期web服務嗎?
@WebService()
public class FirstWS {
private long m_instanceID = 0; //instance id
//Log
private void WriteLog(String cadena){
String msg = "";
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss:SSS");
Date fecha = new Date();
msg = dateFormat.format(fecha) + " ***** " + this.getClass().getName() + " (m_instanceID=" + m_instanceID + "): " + cadena +
" " + "ThreadID=" + Thread.currentThread().getId() + " ThreadName=" + Thread.currentThread().getName();
Logger.getLogger(this.getClass().getName()).log(Level.INFO,msg);
}
//Constructor
public FirstWS() {
Random rnd = new Random();
m_instanceID = rnd.nextLong();
WriteLog("Executing Constructor");
}
//PostConstruct
@PostConstruct
public void ThisIsThePostConstruct() {
WriteLog("Executing PostConstruct");
}
//PreDestroy
@PreDestroy
public void ThisIsThePreDestroy() {
WriteLog("Executing PreDestroy");
}
//Method
@WebMethod(operationName = "GetInstanceID")
@WebResult(name="InstanceID")
public long GetInstanceID() {
//TODO write your implementation code here:
WriteLog("Executing GetInstanceID");
return m_instanceID;
}
}
我總是通過默認的web服務是singleton。
在的GlassFish V2.1服務器的WS工作正常,具有單週期:創建
- 只有一個實例。
- 只有一個構造方法的執行。
- 只有一個執行if postconstructor方法。
- 當ws被取消部署時,只執行一次predestroy方法。
客戶端日誌
實例id = -4747957096764272596
實例id = -4747957096764272596
實例id = -4747957096764272596
Server日誌
26/08/2010 13:08:15:146 ***** first.test.FirstWS(m_instanceID = -4747957096764272596):執行構造函數ThreadID = 68 ThreadName = httpSSLWorkerThread -8080-0
26/08/2010 13:08:15:161 ***** first.test.FirstWS(m_instanceID = -4747957096764272596):執行PostConstruct ThreadID = 68 ThreadName = httpSSLWorkerThread -8080-0
26/08/2010 13:08: 15:364 ***** first.test.FirstWS(m_instanceID = -4747957096764272596):執行GetInstanceID ThreadID = 69 ThreadName = httpSSLWorkerThread -8080-1
26/08/2010 13:08:15:380 ** *** first.test.FirstWS(m_instanceID = -4747957096764272596):執行GetInstanceID ThreadID = 69 ThreadNa me = httpSSLWorkerThread-8080-1
26/08/2010 13:08:15:396 ***** first.test.FirstWS(m_instanceID = -4747957096764272596):執行GetInstanceID ThreadID = 69 ThreadName = httpSSLWorkerThread-8080 -1
26/08/2010 13:08:38:849個***** first.test.FirstWS(m_instanceID = -4747957096764272596):執行PreDestroy線程ID = 626 ThreadName =線程540
但在glassfish V3服務器上,ws未按預期工作,因爲每次調用GetInstanceID都會創建ws的新實例:
- 爲每個呼叫創建一個實例。
- 每次調用一次執行構造函數方法。
- 兩次(!?!?!)執行每次調用的postconstructor方法。
- 當ws被取消部署時,不會調用predestroy方法。
Client日誌
INSTANCEID = 7754248300017958713
INSTANCEID = -1714184485890589231
INSTANCEID = -4156829683887899017
服務器日誌
信息:26/08/2010 15:16:11:429 ***** first.test.FirstWS(m_instanceID = 7754248300017958713):執行構造函數ThreadID = 103 ThreadName = http-thread-pool-8080-(2)
信息:26/08/2010 15:16:11:429 ***** first.test.FirstWS(m_instanceID = 7754248300017958713):執行PostConstruct ThreadID = 103 ThreadName = http-thread-pool-8080-(2 )
信息:26/08/2010 15:16:11:429 ***** first.test.FirstWS(m_instanceID = 7754248300017958713):執行PostConstruct ThreadID = 103 ThreadName = http-thread-pool-8080- (2)
信息:26/08/2010 15:16:12:429 ***** first.test.FirstWS(m_instanceID = 7754248300017958713):執行GetInstanceID T hreadID = 103 ThreadName = http-thread-pool-8080-(2)
INFO:26/08/2010 15:16:12:460 ***** first.test.FirstWS(m_instanceID = -1714184485890589231) :執行構造函數ThreadID = 102 ThreadName = http-thread-pool-8080-(1)
INFO:26/08/2010 15:16:13:429 ***** first.test.FirstWS(m_instanceID = -1714184485890589231):執行PostConstruct ThreadID = 102 ThreadName = http-thread-pool-8080-(1)
信息:26/08/2010 15:16:13:429 ***** first.test.FirstWS (m_instanceID = -1714184485890589231):執行PostConstruct ThreadID = 102 ThreadName = http-thread-pool-8080-(1)
INFO:26/08/2010 15:16:14:429 ***** first.test.FirstWS(m_instanceID = -1714184485890589231):執行GetInstanceID ThreadID = 102 ThreadName = http-thread-pool-8080-(1 )
INFO:26/08/2010 15:16:14:445 ***** first.test.FirstWS(m_instanceID = -4156829683887899017):執行構造函數ThreadID = 103 ThreadName = http-thread-pool-8080 - (2)
信息:26/08/2010 15:16:15:429 ***** first.test.FirstWS(m_instanceID = -4156829683887899017):執行PostConstruct ThreadID = 103 ThreadName = http-thread-池-8080-(2)
INFO:26/08/2010 15:16:15:429個***** first.test.FirstWS(m_instanceID = -4156829683887899017):執行名次tConstruct ThreadID = 103 ThreadName = http-thread-pool-8080-(2)
信息:26/08/2010 15:16:15:429 ***** first.test.FirstWS(m_instanceID = -4156829683887899017 ):執行GetInstanceID ThreadID = 103 ThreadName = http-thread-pool-8080-(2)
INFO:關閉Metro監視根:amx:pp =/mon/server-mon [server],type = WSEndpoint,name =/FirstWebApplication-FirstWSService-FirstWSPort
那麼,爲什麼在glassfish V3上這種行爲?我怎麼能在glassfish V3上有一個單獨的Web服務?
你可以上傳你的項目的地方,所以我們也可以運行它? – 2010-08-30 13:21:41
謝謝,但我發現這個問題,看到我的答案。顯然,我是一個java和web服務的新手:-) – user434969 2010-08-31 13:39:43