封装HttpSolrServer设置超时时间

凡是要连接网络的,都要设置超时时间,这样防止网络卡住了没返回结果,例子可以参考 Spark Streaming调用http接口导致卡住了

/**
 * HttpSolrServer扩展对象服务 .<br>
 * 专门设置请求连接超时时间和socket超时时间,调用solr请求服务,防止网络断开时无法中断调用线程<br>
 * 
 */
public class HttpSolrExServer extends HttpSolrServer {

    /** serialVersionUID */
    private static final long serialVersionUID = 4068028650816903817L;

    /**
     * 连接超时值
     */
    private static Integer connectionTimeoutNum;

    /**
     * socket超时值
     */
    private static Integer socketTimeoutNum;



    /**
     * 有参构造函数
     * 
     * @param baseUrl url地址
     */
    public HttpSolrExServer(String baseUrl) {
        super(baseUrl);
        // 设置请求连接超时时间,以毫秒为单位
        this.setConnectionTimeout(getConnectionTimeout());
        // 设置socket超时时间,以毫秒为单位
        this.setSoTimeout(getSocketTimeout());
    }

    public HttpSolrExServer(String baseUrl, HttpClient client){
        super(baseUrl,client);
        // 设置请求连接超时时间,以毫秒为单位
        this.setConnectionTimeout(getConnectionTimeout());
        // 设置socket超时时间,以毫秒为单位
        this.setSoTimeout(getSocketTimeout());
    }

    /**
     * 得到solr的连接超时时间,以毫秒为单位,默认为60秒<br>
     * 
     * @return
     */
    private static int getConnectionTimeout() {
        if (connectionTimeoutNum == null || connectionTimeoutNum <= 0) {
            // solr的连接超时时间,以毫秒为单位,默认为60秒
            int defaultConnectionTimeout = 60000;
            // solr的连接超时时间字符串变量值

            String connectionTimeoutStr = ConfigUtil.getPropsValueByKey("ac.httpSolr.connectionTimeout");
            

            try {
                int configValue = Integer.parseInt(connectionTimeoutStr);

                if (configValue > 0) {
                    defaultConnectionTimeout = configValue;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            connectionTimeoutNum = defaultConnectionTimeout;
        }
        return connectionTimeoutNum.intValue();
    }

    /**
     * 得到solr的socket超时时间,以毫秒为单位,默认为60秒<br>
     * 
     * @return
     */
    private static int getSocketTimeout() {
        if (socketTimeoutNum == null || socketTimeoutNum <= 0) {
            // solr的socket超时时间,以毫秒为单位,默认为60秒
            int defaultSocketTimeout = 60000;
            // solr的socket超时时间字符串变量值
            String socketTimeoutStr = ConfigUtil.getPropsValueByKey("ac.httpSolr.socketTimeout");

            try {
                int configValue = Integer.parseInt(socketTimeoutStr);

                if (configValue > 0) {
                    defaultSocketTimeout = configValue;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            socketTimeoutNum = defaultSocketTimeout;
        }
        return socketTimeoutNum.intValue();
    }
}
/**
     * 根据属性key得到对应的属性值<br>
     * 配置文件按spring boot的约定的配置文件命名规则进行加载<br>
     * <font color=red>注意: 暂不支持yml文件属性读取</font>
     * 
     * @param key 键名称
     * @return
     */
    public static String getPropsValueByKey(String key) {
        // TODO 暂时不支持读取yml格式文件,yml文件支持map和list格式数据,需要另写方法支持
        if (!props.containsKey(CONFIG_FILENAME)) {
            Properties prop = new Properties();
            prop = getPropertiesByFileName(CONFIG_FILENAME);

            if (prop.get(SPRING_PROFILES_ACTIVE) != null) {
                // 依次读取指定的配置文件
                for (String partName : prop.get(SPRING_PROFILES_ACTIVE).toString().split(",")) {
                    prop.putAll(getPropertiesByFileName(SPRING_BOOT_PROFILE_TEMPLATE.replace("{profile}", partName)));
                }
            }
            props.put(CONFIG_FILENAME, prop);
        }
        Object obj = props.get(CONFIG_FILENAME).get(key);
        if (obj == null) {
            return null;
        } else {
            return obj.toString();
        }
    }

关注公众号“大模型全栈程序员”回复“小程序”获取1000个小程序打包源码。更多免费资源在http://www.gitweixin.com/?p=2627

发表评论

邮箱地址不会被公开。 必填项已用*标注