日誌列印兩次(問題)

來源:互聯網
上載者:User

今天碰到了一個日誌被列印兩次的問題

Configuration類

/**
 * 設定檔資訊
 * @author Administrator
 *
 */
public class Configuration {

private static Logger LOG = Logger.getLogger(Configuration.class);

/**
* 檔案路徑(備用 當前資訊重新整理到本地)
*/
public static String filePath = null;
/**
* 索引值對
*/
private static Properties prop = null;
/**
* 靜態建構函式
*/
static{
if(prop==null){
readConfiguration();
}
}
/**
* 預設建構函式
*
*/
public Configuration(){
if(prop==null){
readConfiguration();
}
}
/**
* prop
* @return
*/
public Properties getProperties(){
return prop;
}
/**
* 擷取值
* @param key
* @return
*/
public String getValue(String key){
if(key!=null&&!key.equals("")){
return prop.getProperty(key);
}
return null;
}
/**
* 擷取值
* @param key
* @return
*/
public String getValue(String key,String defaultVaule){
if(key!=null&&!key.equals("")){
return prop.getProperty(key,defaultVaule);
}
return null;
}
/**
* 讀取配置資訊

* @return
*/
private static synchronized void readConfiguration() {
/**
* 讀取dataexchange.properties檔案
*/
InputStream in = null;
Properties p = null;
try {
URL url = DEUtil.class.getResource(Constants.CONFIG_FILE_NAME);
if(url!=null){
filePath = java.net.URLDecoder.decode(url.getPath(),"utf-8");
LOG.info(filePath);
in = url.openStream();
}else{
in = DEUtil.class.getResourceAsStream(Constants.CONFIG_FILE_NAME);
}
p = new Properties();
if(p!=null){
p.load(in);
}else{
throw new NullPointerException();
}
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
if(in!=null){
in.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (p != null) {
prop = p;
}else{
throw new NullPointerException();
}
}
/**
* 重新整理索引值對到本地檔案
*
*/
public synchronized void porpertiesFlushBack(){
if(prop!=null){
//檔案輸出資料流 
OutputStream fos = null;
try {
fos = new FileOutputStream(Configuration.filePath);
//將Properties集合儲存到流中
prop.store(fos, "Copyright (c) dlmu 233"); 
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
if(fos!=null){
fos.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}else{
throw new NullPointerException();
}
}
}

還有一個工具類:DEUtil(貼部分代碼)

/**
 * 資料交換工具類
 * 
 * @author Administrator
 * 
 */
public class DEUtil {
/**
* 配置資訊
*/
private static Configuration conf = new Configuration();

。。。。。省略部分代碼

}

還有一個入口:DataExchangeClient

/**
 * 資料交換用戶端
 * @author Administrator
 *
 */
public class Client {

private static Logger LOG = Logger.getLogger(Client .class);
/**
* 配置資訊
*/
private Configuration conf = null;
/**
* @param args
* @author Administrator
*/
public static void main(String[] args){
/** 建立用戶端執行個體 */
Client client = new Client ();
client.run();
}
/**
* 初始化函數

*/
private void initial(){
this.conf = new Configuration();

}
/**
* 預設建構函式
*
*/
public Client () {
initial();
}

}

整個執行下來會出現列印兩次日誌 就是紅色的地方執行了兩次

整個過程都是單線程

暫時還不知道怎麼解釋(望有人能指點下 謝謝)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.