以编程方式关闭码头客户端

时间:2014-06-25 09:20:44

标签: java oauth-2.0 jetty google-oauth

我正在设置码头客户端,该客户端将用于处理Google的 oauth2 授权流程

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

如果之前的授权流失败并且让Jetty客户端打开,这将失败

INFO [log:131] jetty-6.1.26
WARN [log:265] failed SocketConnector@180.200.105.100:8085: java.net.BindException: Address already in use
WARN [log:265] failed Server@32c65328: java.net.BindException: Address already in use

请注意,我使用固定网址和端口

是否有某种方法可以以编程方式检查 jetty客户端是否已经打开,并处理这种情况,例如关闭之前的码头连接?

1 个答案:

答案 0 :(得分:1)

我遇到了类似的情况,使用google api连接youtube服务,连接也基于jetty客户端。

我解决了将localServerReceiver对象放在一个静态类(说实话,是一个单独的Spring Bean)中,默认值为" null",在第一次尝试连接时,它创建了一个新实例,将它存储在静态类属性中。

在此之后,在任何新尝试连接之前,我调用方法localServerReceiver.stop()来取消任何先前的连接。

像这样的东西:

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();"需要的地方。

它对我有用。 祝你好运