2010-08-30 46 views
2

我有一個簡單的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服務?

+0

你可以上傳你的項目的地方,所以我們也可以運行它? – 2010-08-30 13:21:41

+0

謝謝,但我發現這個問題,看到我的答案。顯然,我是一個java和web服務的新手:-) – user434969 2010-08-31 13:39:43

回答

0

最後我發現問題:項目的類型。在NetBeans中,我在「Web應用程序」項目中創建了Web服務,以便將其部署到Glassfish中的Servlet容器中,並且@Stateless和@Singleton註釋不起作用。您必須在「EJB Moule」項目中創建ws,並將其部署到EJB容器中,這樣@Stateless和@Singleton註釋才能正常工作。

正如Java EE 6所言:「單例會話bean提供與無狀態會話bean類似的功能,但與它們不同,因爲每個應用程序只有一個單例會話bean,而不是無狀態會話Bean池,其中任何一個都可能響應客戶端請求,像無狀態會話bean一樣,單例會話bean可以實現Web服務端點。「

0

我認爲這可能是一個規範的問題(Java EE 5的對Java EE 6的),你可能需要使用@Stateless annotation

package com.sun.tutorial.javaee.ejb; 

import javax.ejb.Stateless; 
import javax.jws.WebMethod; 
import javax.jws.WebService; 

    @Stateless 
    @WebService 
    public class HelloServiceBean { 
     private String message = "Hello, "; 

     public void HelloServiceBean() {} 

     @WebMethod 
     public String sayHello(String name) { 
      return message + name + "."; 
     } 
    } 

更正:此GlassFish v3中對我的作品。

+0

感謝指向我在正確的方向 – user434969 2010-08-31 13:34:39