簡體   English   中英

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

[英]How to load properties file only once?

我正在通過從屬性文件獲取數據來閱讀電子郵件。我正在使用計時器計划以固定的時間間隔一段時間后讀取新消息。我該怎么做?

TimerSchedule.java

public 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.java

public 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++)
            {
                server = (JSONObject) jadata.get(i);
                name = (String) server.get("Name");
                host = (String) server.get("Host");
                username = (String) server.get("UserName");
                password = (String) server.get("Password");
                port = ((Long) server.get("Port")).intValue();
                folderName = (String) server.get("FolderName");

                readGmail.recieveGmail(name, host, port, username, password, folderName);
            }

        }
        catch (Exception e)
        {
            LOGGER.error("Exception", e);
        }
    }

}

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM