0

I have a java program where I wrote a scheduler, or a timer where once it starts running. It will call the function every N minutes interval indefinitely. For now, I set N to 5 minutes.

I want to sent out this program in a form of jar file along with some sort of properties file where the tester or anyone else can configure N; so it's much more convenient and I do not have to change in the code itself.

This is an example:

public void schedule() throws Exception {

    Timer t=new Timer();
    t.scheduleAtFixedRate(
        new TimerTask() {
            @Override
            public void run() {
              // System.out.println("HELEOELE");

                try {
                    test.index();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        },
      0,
        300000);
}

In this case, N = 300000.

I am not too familiar with jar file but I do have a properties file with db connection created.

This is how I called the properties:

 Properties props=new Properties();


        InputStream in = getClass().getResourceAsStream("/config.properties");

        props.load(in);
        in.close();
        int value=Integer.parseInt(props.getProperty("value"));

So I would appreciate is there any way to do this?

Daredevil
  • 1,672
  • 3
  • 18
  • 47
  • Have you done any research into this topic? As in, look up how to open or read from a file with java. – Minn Dec 03 '18 at 08:18
  • I think I didn't quite rephrase my question clearly. But even then, when I compiled to jar file, the properties is embedded inside the jar file. – Daredevil Dec 03 '18 at 08:22
  • @Daredevil You can read the properties file from the classpath in that case. https://stackoverflow.com/questions/2815404/load-properties-file-in-jar – A_C Dec 03 '18 at 08:50
  • @Daredevil If this is only for testing purpose; why not get the value as a parameter, from the tester, when the jar file is run? – Pranjal Gore Dec 03 '18 at 09:26
  • @PranjalGore What do you mean? – Daredevil Dec 03 '18 at 09:28
  • @Daredevil You said the jar will be run on command line. So I am just curious why not get the timer value as a command line argument? – Pranjal Gore Dec 03 '18 at 09:36
  • Well there are some other function that requires some values to be configured in the config file so I rather have those parameters in a config file and pass it along. – Daredevil Dec 03 '18 at 09:37

1 Answers1

1

You can read the properties file from the classpath in case it is present in your jar only.

Refer this: Load properties file in JAR?

But, if you wan't to store the properties file out of the jar and be able to make changes in it, then there are multiple options :

  • store it on the file system at a specified location from where your code reads it.

  • instead of a properties file, you can also have a system property defined, which stores the required value. But, you would have to restart your container every time you change the value.

  • you could also consider reading this value directly from the database (optionally loading it into an in-memory cache). But, here, another API would needed to make changes in the database.

  • Store it outside the jar. Refer the following link Read properties file outside JAR file

A_C
  • 905
  • 6
  • 18
  • I've looked into this link you gave before. However, once I compiled, the properties file is embedded inside the jar file as well – Daredevil Dec 03 '18 at 08:53
  • But, in general, that wouldn't be the right solution for you in case you want to make edits in the file. I will update the answer. – A_C Dec 03 '18 at 08:57
  • My issue is I want to send the jar file to someone, say a tester. Then maybe that person wants to edit the value N ,rather than changing it inside code. – Daredevil Dec 03 '18 at 08:57
  • But there is a way which is not advisable, read the properties file by specifying the directory which is hardcoded. Then it would work. But if you send to someone, that someone will not store the config properties in the same directory. It's a mess – Daredevil Dec 03 '18 at 09:01
  • Is there a specific operating environment in which this jar will be run always? What is this environment made of? Which OS, which container, etc? – A_C Dec 03 '18 at 09:03
  • windows os. No specific environment. It will be run on command prompt. just wanted to make it convenient to edit the config file without touching the code. – Daredevil Dec 03 '18 at 09:04
  • @Daredevil, updated the answer with a reference on how to read it from outside the jar. See if that helps – A_C Dec 03 '18 at 09:10
  • What if I can put the jar file in same folder as the config file and read from there, would it work ,? so regardless where you save the folder, it will always read the config file correctly – Daredevil Dec 03 '18 at 09:10
  • Yes, please refer the last option in the answer, it talks about the same, with various different strategies. – A_C Dec 03 '18 at 09:12
  • Would you recommend reading from sql table? So just change values in the sql table to get the change – Daredevil Dec 03 '18 at 09:13
  • Based on what you have described till now, I wouldn't recommend that, because you would need another API to be able to make changes to the value. Also, you need some sort of in-memory cache where you could store its value so that performance is better. It seems your functionality does not need such an elaborate mechanism. Your functionality seems to be very simple. – A_C Dec 03 '18 at 09:15
  • but i do not understand the explanation – Daredevil Dec 03 '18 at 09:19
  • I mean you would have to read the property from the database, and if someone wants to change the property value, he would need either an access to the database (a user-friendly one), and be able to make modifications. Also, making a call to the database every time in your task/thread would hamper performance. So, it would be ideal to read the value once, store it in memory and each task/thread shall read from the memory. Once the value is changed via an API or database directly, there should be a mechanism to update the in-memory cache value. For a simple application, I wouldn't do all this. – A_C Dec 03 '18 at 09:23
  • I meant i do not understand this explanation on the link u posted – Daredevil Dec 03 '18 at 09:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/184612/discussion-between-daredevil-and-ankur-chrungoo). – Daredevil Dec 03 '18 at 09:27