2014-06-25 81 views
0

我的建立一個碼頭客戶端將被用來處理谷歌的的oauth2授權流編程方式關閉碼頭客戶

com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver receiver = 
       new LocalServerReceiver.Builder().setHost(redirect_host).setPort(redirect_port).build(); 

這將失敗如果先前的授權流失敗, 離開碼頭客戶端打開

INFO [log:131] jetty-6.1.26 
WARN [log:265] failed [email protected]:8085: java.net.BindException: Address already in use 
WARN [log:265] failed [email protected]: java.net.BindException: Address already in use 

請注意,我使用的是固定的URL和端口

有沒有什麼辦法可以檢查編程碼頭客戶是否是已經打開,並通過關閉以前的碼頭連接處理這種情況,e.g?

回答

1

我面臨着類似的情況,使用google api連接到youtube服務,連接也基於碼頭客戶端。

我解決了把我的localServerReceiver對象放在一個默認值爲「null」的靜態類(老實說,是一個singleton Spring Bean),在第一次嘗試連接時,它創建一個新實例並將其存儲在靜態類屬性。

之後,在任何新的嘗試連接之前,我調用localServerReceiver.stop()方法取消任何以前的連接。

Soomething這樣的:

public class LocaServerReceiverRetriever{ 

private static LocalServerReceiver localServerReceiver = null; 

public static LocalServerReceiver getLocalServerReceiver(){ 
    if(localServerReceiver == null){ 
     localServerReceiver = new LocalServerReceiver.Builder().setPort(9001).build(); 
    }else{ 
     localServerReceiver.stop(); 
     localServerReceiver = new LocalServerReceiver.Builder().setPort(9001).build(); 
    } 
    return localServerReceiver ; 
} 

}

然後調用 「LocaServerReceiverRetriever.getLocalServerReceiver();」需要的地方。

它適用於我。 好運

+0

感謝分享!您認爲這對於具有多個用戶/會話的WebApp以及多個授權請求可以正常工作嗎? – yannicuLar