在这种情况下,我是否每次都需要创建DocumentBuilderFactory?

时间:2015-11-01 08:23:58

标签: java

我需要每7分钟更新一次来自RSS Feed的新闻Feed。

为此,我写了一个TimerTask,如下所示

public class TimerTaskForAllNews 
{
    public static void main( String[] args )
    {
        TimerTask task = new AllNewsUpdatrUtility();
        Timer timer = new Timer();
        timer.schedule(task, 1000,60000);
    }
}

这是我的TimerTask实现类

package com.util;
import java.net.URL;
public class AllNewsUpdatrUtility extends TimerTask {
      private static AllNewsUpdatrUtility instance = null;
       public AllNewsUpdatrUtility() {}
       public static AllNewsUpdatrUtility getInstance() {
          if (instance == null)
             instance = new AllNewsUpdatrUtility();
          return instance;
       }
    @Override
    public void run() {
         try {
             JSONArray latestnews = new JSONArray();
             JSONObject jsonobj_allnews = new JSONObject();
             DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
             URL url = new URL("http://www.rssmix.com/u/8160628/rss.xml");
             Document doc = builder.parse(url.openStream());
             NodeList items = doc.getElementsByTagName("item");
            for (int i = 0; i < items.getLength(); i++) {
                Element item = (Element) items.item(i);
                String title = getValue(item, "title");
                String link = getValue(item, "link");
                String pub_date = getValue(item, "pubDate");

            } // for loop ends here 

          } catch (Exception e) {
             e.printStackTrace();
          }
    }


}

请你告诉我,无论如何我都可以改进这个节目吗?

1 个答案:

答案 0 :(得分:2)

规范JSR 206 Java™ API for XML Processing (JAXP) 1.4

  

预计SAXParserFactory实现的newSAXParser方法,DocumentBuilderFactory的newDocumentBuilder方法和TransformerFactory的newTransformer方法将是线程安全的,没有副作用。

如评论中所述,您可以缓存DocumentBuilderFactory实例:

package com.util;
import java.net.URL;
public class AllNewsUpdatrUtility extends TimerTask {
       private static AllNewsUpdatrUtility instance;
       private final DocumentBuilderFactory dbf;
       private AllNewsUpdatrUtility() {}
       public synchronized static AllNewsUpdatrUtility getInstance() {
          if (instance == null)
             instance = new AllNewsUpdatrUtility();
             dbf = DocumentBuilderFactory.newInstance();
          return instance;
       }
    @Override
    public void run() {
         try {
             JSONArray latestnews = new JSONArray();
             JSONObject jsonobj_allnews = new JSONObject();
             DocumentBuilder builder = dbf.newDocumentBuilder();
             URL url = new URL("http://www.rssmix.com/u/8160628/rss.xml");
             Document doc = builder.parse(url.openStream());
             NodeList items = doc.getElementsByTagName("item");
            for (int i = 0; i < items.getLength(); i++) {
                Element item = (Element) items.item(i);
                String title = getValue(item, "title");
                String link = getValue(item, "link");
                String pub_date = getValue(item, "pubDate");

            } // for loop ends here 

          } catch (Exception e) {
             e.printStackTrace();
          }
    }


}