IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    jdk的HttpURLConnection提供了日志开关

    hongjiang发表于 2015-07-25 09:28:56
    love 0

    下午运行一个junit程序,时不时启动时就“卡住”,没有输出任何日志,通过jstack发现是spring初始化时调用了sun.net.www.protocol.http.HttpURLConnection访问网络时阻塞住了。按说依赖的spring的jar里都应该包含了这些dtd/xsd之类的文件,怎么仍会访问远程网络呢?

    jdk自带的HttpURLConnection提供了日志(JUL)开关,可以查看有哪些URL请求,在运行时指定

    -Djava.util.logging.config.file=/tmp/logging.properties
    

    不指定的话,默认从 jre/lib/logging.properties 加载。在配置文件里:

    # 增加一条针对HttpURLConnection的配置
    sun.net.www.protocol.http.HttpURLConnection.level = ALL
    
    # 并修改终端的输出级别
    java.util.logging.ConsoleHandler.level = ALL
    

    启动后看到如下日志:

    FINEST: ProxySelector Request for http://www.springframework.org/dtd/spring-beans.dtd
    Jul 25, 2015 4:49:45 PM sun.net.www.protocol.http.HttpURLConnection plainConnect0
    
    FINEST: Proxy used: DIRECT
    Jul 25, 2015 4:49:45 PM sun.net.www.protocol.http.HttpURLConnection writeRequests
    
    FINE: sun.net.www.MessageHeader@531be3c55 pairs: 
    {GET /dtd/spring-beans.dtd HTTP/1.1: null}
    {User-Agent: Java/1.8.0_20}
    {Host: www.springframework.org}
    {Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2}
    {Connection: keep-alive}
    Jul 25, 2015 4:49:45 PM sun.net.www.protocol.http.HttpURLConnection getInputStream0
    
    FINE: sun.net.www.MessageHeader@52af6cff16 pairs: {null: HTTP/1.1 200 OK}...
    

    日志里看到确实是去访问了spring网站,根据url信息找到包含spring-beans.dtd内容的xml配置;这个工程是一个很老的工程,里的写法是:

    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    

    要防止springframework.org或者GFW引发的不稳定,采用新的写法来避免:

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:p="http://www.springframework.org/schema/p" 
        xmlns:c="http://www.springframework.org/schema/c"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.1.xsd ">
    


沪ICP备19023445号-2号
友情链接