亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何只加載一次屬性文件?

如何只加載一次屬性文件?

墨色風雨 2023-08-09 16:59:51
我正在通過從屬性文件獲取數據來讀取電子郵件消息。我正在使用計時器計劃在一段時間后定期讀取新消息。我該如何執行此操作?TimerSchedule.javapublic class TimeScheduler{    public static void main(String[] args)    {        Timer timer = new Timer();        GmailConfiguration gmailConfiguration = new GmailConfiguration();        TimerTask timerTask = new TimerTask()        {            @Override            public void run()            {                gmailConfiguration.configure();            }        };        timer.scheduleAtFixedRate(timerTask, 500, 30000);    }}我正在從 GmailConfiguration.java 中的屬性文件獲取數據這是我的 GmailConfiguration.javapublic class GmailConfiguration{    private static final Logger LOGGER = LoggerFactory.getLogger(GmailConfiguration.class);    public void configure()    {        JSONParser parser = new JSONParser();        try        {            String propertyFileName = "emailServer.properties";            InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propertyFileName);            JSONObject jsonObject = (JSONObject) parser.parse(new InputStreamReader(inputStream, StandardCharsets.UTF_8));            JSONArray jadata = (JSONArray) jsonObject.get("Servers");            int len = jadata.size();            AccessMailMessages readGmail = new AccessMailMessages();            JSONObject server;            String name;            String host;            String username;            String password;            int port;            String folderName;            for (int i = 0; i < len; i++)
查看完整描述

1 回答

?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

Configuration將類與類分開EmailReceiver:


// Utilizes "Singleton" pattern

class GmailConfiguration {

  private static final GmailConfiguration INSTANCE = new GmailConfiguration();


  boolean isConfigured;

  String host;

  String port;

  //etc.


  public void configure() {

    if (!isConfigured) {

      // read in the properties, populate host/port etc.

      isConfigured = true;

    }

    // when called for the second time, reading won't happen

  }

}

然后,至于接收電子郵件:


class GmailReceiver {

  public void receive() {

    AccessMailMessages readGmail = new AccessMailMessages();

    GmailConfiguration config = GmailConfiguration.INSTANCE;

    config.configure();

    readGmail.recieveGmail(config.getName(),

        config.getHost(), 

        config.getPort() /* etc */);


  }

}

并確保只安排GmailReceiver


查看完整回答
反對 回復 2023-08-09
  • 1 回答
  • 0 關注
  • 102 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號