2011-10-04 89 views
1

下面的鏈接介紹如何使用服務接口啓動(以處理吉斯模塊的初始化和銷燬​​)和stop()方法:Guice中的模塊初始化和銷燬​​處理程序?

http://code.google.com/p/google-guice/wiki/ModulesShouldBeFastAndSideEffectFree

文檔解釋說,服務的創建看起來像這在客戶端代碼:

public static void main(String[] args) throws Exception { 
    Injector injector = Guice.createInjector(
     new DatabaseModule(), 
     new WebserverModule(), 
     ... 
    ); 

    Service databaseConnectionPool = injector.getInstance(
     Key.get(Service.class, DatabaseService.class)); 
    databaseConnectionPool.start(); 
    addShutdownHook(databaseConnectionPool); 

    Service webserver = injector.getInstance(
     Key.get(Service.class, WebserverService.class)); 
    webserver.start(); 
    addShutdownHook(webserver); 
} 

但沒有列出具體服務類的任何示例實現。任何人都可以提供給我一個嗎?至少包含start()和stop()所包含內容的示例實現。

回答

2

查看Guava中的Service接口及其抽象實現。我很確定界面(和其他類似的界面)通常是文檔所指的。無論如何,這是基礎設施。

只要你的服務實際需要當它啓動或關閉,這取決於服務本身。在該示例中,webserver服務可能會在啓動時在端口上偵聽,並在停止時停止偵聽。連接池在啓動時可能會獲取一些連接,並且在連接池停止時需要釋放它保留的任何連接。

+0

詳細說明可以在這裏找到:http://hellotojavaworld.blogspot.co.il/2010/11/runtimeaddshutdownhook.html – forhas

相關問題