简体   繁体   中英

How to load properties file only once?

I am reading the email message by getting the data from properties file.I am using timer schedule to read the new message after some time at regular interval.How can i do this?

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);
    }
}

I am getting data from properties file in GmailConfiguration.java

Here is my 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);
        }
    }

}

Separate the Configuration class from the EmailReceiver class:

// 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
  }
}

then, as for receiving the email:

class GmailReceiver {
  public void receive() {
    AccessMailMessages readGmail = new AccessMailMessages();
    GmailConfiguration config = GmailConfiguration.INSTANCE;
    config.configure();
    readGmail.recieveGmail(config.getName(),
        config.getHost(), 
        config.getPort() /* etc */);

  }
}

and make sure to only schedule GmailReceiver

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM