Unable to schedule a TimerTask a long time in advance - java

Heavily edited/updated:
I am trying to schedule a task a long time in advance. I believe it is doing something to the process because it thinks it is not going to do anything else.
I should have used HOUR_OF_DAY for 24 hour time instead of hour, but still doesn't work correctly.
New code:
public class OtherMainClass {
private static Timer timer;
private static Calendar cal;
public static void main(String[] args) throws InterruptedException {
cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
cal.set(Calendar.HOUR_OF_DAY, 8);
cal.set(Calendar.MINUTE, 0);
System.out.println("Changed to: " + cal.getTime());
timer = new Timer();
System.out.println("current time is: " + Calendar.getInstance().getTime());
System.out.println("Scheduled for: " + cal.getTime());
timer.schedule(new MyTimerTask(), cal.getTime());
timer.schedule(new MyTimerCheckTask(), 0, 1000*60*10);
}
public static class MyTimerTask extends TimerTask {
#Override
public void run() {
System.out.println("in run at: " + Calendar.getInstance().getTime());
}
}
public static class MyTimerCheckTask extends TimerTask {
#Override
public void run() {
System.out.println("cal at: " + Calendar.getInstance().getTime() + " is: " + OtherMainClass.cal.getTime());
}
}
}
Commandline output (following "is:" is the calendar object passed into the schedule.):
Changed to: Fri Feb 14 08:00:41 GMT 2020
current time is: Thu Feb 13 23:25:41 GMT 2020
Scheduled for: Fri Feb 14 08:00:41 GMT 2020
cal at: Thu Feb 13 23:25:41 GMT 2020 is: Fri Feb 14 08:00:41 GMT 2020
cal at: Thu Feb 13 23:35:41 GMT 2020 is: Fri Feb 14 08:00:41 GMT 2020
cal at: Thu Feb 13 23:45:41 GMT 2020 is: Fri Feb 14 08:00:41 GMT 2020
cal at: Thu Feb 13 23:55:41 GMT 2020 is: Fri Feb 14 08:00:41 GMT 2020
cal at: Fri Feb 14 09:39:27 GMT 2020 is: Fri Feb 14 08:00:41 GMT 2020
in run at: Fri Feb 14 09:39:27 GMT 2020
Note the very interesting run at the end, as well as the other task running. I clicked on commandline, which turned it into select mode, then control-c to exit that, then a few seconds later, it did both Tasks. (It should have run long before I clicked on commandline.
Java version from java -version
openjdk version "1.8.0_242"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_242-b08)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.242-b08, mixed mode)
OS: Windows 10

I ran a test stating Friday around 11:30. I changed the windows sleep settings set to 1 hour (Start -> Settings -> System -> Power & sleep). Since Friday 8:00 had passed, I changed the day to Saturday. I also changed the repeated interval to every half hour 3*1000*60*10, since I wanted to limit the lines of output. I ran the code in 5 different Command Prompts and started them at around 11:30 Friday:
No QuickEdit Mode. The program was running and I had to Crtl-c to stop it. No printout to the screen after sleep initiated. (Admittedly I didn't wait to see if it would print the 8 o'clock message after some time, because it seemed pointless.)
QuickEdit Mode. Did not click inside the screen. Same as 1.
QuickEdit Mode. Clicked inside the screen and immediately cancelled with Ctrl^c. The prompt top bar changed to "Select Command Prompt.." and went back to "Command Prompt..". Same as 1.
QuickEdit Mode. Clicked inside the screen and highlighted some test. The prompt top bar changed to "Select Command Prompt". There was absolutely no printout after that until I pressed Ctrl^c and the printouts were as if I had just started the program and it just printed the below. However I did not just exit the program, and it resumes the 30 min interval printout, as you can see from the below:
QuickEdit Mode. Clicked inside the screen (that's all). The prompt top bar changed to "Select Command Prompt". Same as 4.
This is the result of 4 and 5, that only started printing after i exited "Select Command Prompt"-mode at 9:43 on Saturday:
Changed to: Sat Feb 15 08:00:16 CET 2020
current time is: Sat Feb 15 09:49:13 CET 2020
Scheduled for: Sat Feb 15 08:00:16 CET 2020
in run at: Sat Feb 15 09:49:13 CET 2020
cal at: Sat Feb 15 09:49:13 CET 2020 is: Sat Feb 15 08:00:16 CET 2020
cal at: Sat Feb 15 10:19:13 CET 2020 is: Sat Feb 15 08:00:16 CET 2020
cal at: Sat Feb 15 10:49:13 CET 2020 is: Sat Feb 15 08:00:16 CET 2020
cal at: Sat Feb 15 11:19:13 CET 2020 is: Sat Feb 15 08:00:16 CET 2020
...
I ran another test with the windows sleep setting set to "Never". Since Saturday 8:00 had passed, I changed the day to Sunday. I again ran the code in 5 different Command Prompts and started them at around 15:30 Saturday:
No QuickEdit Mode. Worked without issues. See below.
QuickEdit Mode. Did not click inside the screen. Same as 1.
QuickEdit Mode. Clicked inside the screen and immediately cancelled with Ctrl^c. The prompt top bar changed to "Select Command Prompt.." and went back to "Command Prompt..". Same as 1.
QuickEdit Mode. Clicked inside the screen and highlighted some test. The prompt top bar changed to "Select Command Prompt". There was absolutely no printout after that. Until I pressed Ctrl^c and the printout was identical to the test above.
QuickEdit Mode. Clicked inside the screen (that's all). The prompt top bar changed to "Select Command Prompt". There was absolutely no printout after that.
Result:
Changed to: Sun Feb 16 08:00:31 CET 2020
current time is: Sat Feb 15 15:35:31 CET 2020
Scheduled for: Sun Feb 16 08:00:31 CET 2020
cal at: Sat Feb 15 15:35:31 CET 2020 is: Sun Feb 16 08:00:31 CET 2020
cal at: Sat Feb 15 16:05:31 CET 2020 is: Sun Feb 16 08:00:31 CET 2020
..snip..
cal at: Sun Feb 16 07:05:31 CET 2020 is: Sun Feb 16 08:00:31 CET 2020
cal at: Sun Feb 16 07:35:31 CET 2020 is: Sun Feb 16 08:00:31 CET 2020
in run at: Sun Feb 16 08:00:31 CET 2020
cal at: Sun Feb 16 08:05:31 CET 2020 is: Sun Feb 16 08:00:31 CET 2020
The answer by Shaun Rowan to Why is my command prompt freezing on Windows 10? you provided mentions:
whenever you click on a command window in windows 10, it immediately halts the application process when it attempts to write to the console
So it doesn't seem to be a bug in the Command Prompt either, rather it's a feature.
As per Will my Window 10 apps continue to update or download on sleep or hibernation mode? by A. User:
Sleep is a power-saving state that allows a computer to quickly resume full-power operation (typically within several seconds) when you want to start working again. Putting your computer into the sleep state is like pausing a DVD player—the computer immediately stops what it’s doing and is ready to start again when you want to resume working.
If you're very interested in Windows sleep modes TechRepublic has an article on Investigating Sleep states in Windows 10 describes the 6 stages of sleep, from when the (processor clocks are stopped) -> (the processor shuts off to save power) -> (other chips on the motherboard may shut off)
Conclusion:
If you want your java program to keep executing, ensure your Command Prompt window is not in "Select Command Prompt.."-mode and also ensure that your computer does not enter sleep mode, as either of these will halt execution of the program.

Related

Spring Crontab pattern: every weekday at specific times

Here is the documentation for the Spring Crontab syntax.
I want to run a task every weekday at 14:40, 14:45, 14:50, 14:55, and 15:00, but I can't figure out how to express this with a Crontab pattern. The closest I've come up with so far is:
0 40/5 14 * * MON-FRI
But this doesn't run at 15:00.
Is this possible to express with a Crontab pattern at all?
You need to split it into two expressions :
0 40-55/5 14 * * MON-FRI
0 0 15 * * MON-FRI
If you only want to have one expression and you are okay with the last trigger time in a day run one minute before, you can use :
0 40-55/5,59 14 * * MON-FRI
Spring internally use CronSequenceGenerator to generate the next trigger time for a cron expression. You can use it to verify a cron expression.
For example , to verify the next several trigger times of 0 40-55/5,59 14 * * MON-FRI:
CronSequenceGenerator generator = new CronSequenceGenerator("0 40-55/5,59 14 * * MON-FRI");
Date d = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
for (int i = 0; i < 10; i++) {
d = generator.next(d);
System.out.println(d);
}
which will print out :
Tue Jun 02 14:40:00 HKT 2020
Tue Jun 02 14:45:00 HKT 2020
Tue Jun 02 14:50:00 HKT 2020
Tue Jun 02 14:55:00 HKT 2020
Tue Jun 02 14:59:00 HKT 2020
Wed Jun 03 14:40:00 HKT 2020
Wed Jun 03 14:45:00 HKT 2020
Wed Jun 03 14:50:00 HKT 2020
Wed Jun 03 14:55:00 HKT 2020
Wed Jun 03 14:59:00 HKT 2020

Trying to list strings in a config sectiong

Okay I don't know how to explain this but if you look a
Players:
_Brand0n_:
'Fourm Link:': 'Null'
'Registered:': 'No'
'GamePoints:': '0'
'UUID:': 94bad256-7fa0-49c6-add1-50b61a854dc3
Reports:
'#R:': '0'
'TheeLux:': Hacking Time: Fri Jul 22 12:21:35 CDT 2016'
'Adam_Hall:': Hacking Time: Fri Jul 22 12:21:35 CDT 2016'
I need to be able to take the reports from TheeLux and Adam_Hall and print an msg saying
Adam_Hall: Hacking - Time: Fri Jul 22 12:21:35 CDT 2016
But I don't know how to take however many configuration sections and make them into strings ext. If this is confusing please post a comment and I'll try and clarify.

Java Calendar bug?

After a lot of debugging I narrowed my problem to the following snippet:
public static void calendarBug() {
for (int i=0 ; i<6 ; i++) {
Calendar c = Calendar.getInstance();
c.clear();
c.set(2015, 2, 27, i, 0);
System.out.println(c.getTime());
}
}
Running this gives the following output:
Fri Mar 27 00:00:00 IST 2015
Fri Mar 27 01:00:00 IST 2015
Fri Mar 27 03:00:00 IDT 2015
Fri Mar 27 03:00:00 IDT 2015
Fri Mar 27 04:00:00 IDT 2015
Fri Mar 27 05:00:00 IDT 2015
Does anyone know why does c.set(2015,2,27,2,0) returns 3AM instead of 2AM?
Think about that your are at time just at DST time, It moves forward one hour or moves back one hour without living this time portion.
So It is not bug, it is feature.
When you change the timezone which does not use DST (lets say Canada/East-Saskatchewan), you will see what you have expected.
Here is an example.
public static void calendarBug() {
for (int i=0 ; i<6 ; i++) {
Calendar c = Calendar.getInstance();
c.setTimeZone(TimeZone.getTimeZone("Canada/East-Saskatchewan"));
c.clear();
c.set(2015, 2, 27, i, 0);
System.out.println(c.getTime());
}
}
Fri Mar 27 08:00:00 EET 2015
Fri Mar 27 09:00:00 EET 2015
Fri Mar 27 10:00:00 EET 2015
Fri Mar 27 11:00:00 EET 2015
Fri Mar 27 12:00:00 EET 2015
Fri Mar 27 13:00:00 EET 2015
That is most likely a DST switchover for your timezone.
Mar 27 is the last Friday of March in 2015. This is the day when the DST switchover takes place in the Israel, Jordan, Syria, the West Bank etc.
See
Daylight Savings Time
Daylight Savings Time by Country
Daylight Saving Time Dates - 2014/2015

Export .jar with .txt as resource won't work

I have a problem concerning the .jar Export. I have to include a .txt file into my jar from where I read words into a list. I used this to realize(in eclipse Run + Debug mode it works well):
File file = new File(Worte.class.getClassLoader().getResource("resource/wortliste.txt").getFile());
Now, if I export it into a JAR file, the JAR file is executable as it should, and it contains the list.txt and .java files in resource (as it should because it's homework ;-) ), but the program behaves like there was no file. I have the same problem when i use getResourceAsStream(). Does anybody know why this won't work? I don't understand that, because i did this 2 weeks ago with another code, and it worked o_O.
What i have tried:
deleting the Project and import into a new one
like 1, but into a new workspace
My System is a Windows 7 x64 PC, Eclipse Juno and JRE7.
Options I use for export:
[] Export generated class files and resources
[x] export all output folders...
[x] export java source files...
[] export refactorings
[x] compress the contents...
[x] add directory entries
[x] overwrite existing files without warning
jar tvf ...
39 Wed Jan 30 16:19:14 CET 2013 META-INF/MANIFEST.MF
0 Wed Jan 30 00:34:16 CET 2013 resource/
100250 Wed Jan 30 00:37:24 CET 2013 resource/wortliste.txt
0 Wed Jan 30 00:34:16 CET 2013 wortspiel/
1291 Wed Jan 30 01:19:14 CET 2013 wortspiel/BuchstabenKollektion.java
2251 Sun Jan 27 16:24:42 CET 2013 wortspiel/TestBuchstabenKollektion.java
506 Sun Jan 27 17:38:48 CET 2013 wortspiel/UI.java
1187 Sun Jan 27 16:24:42 CET 2013 wortspiel/TestWorte.java
2932 Wed Jan 30 01:25:00 CET 2013 wortspiel/WortspielGUI.java
4384 Wed Jan 30 01:50:40 CET 2013 wortspiel/Worte.java
310 Sun Jan 27 16:25:08 CET 2013 .classpath
383 Sun Jan 27 16:22:32 CET 2013 .project
1992 Wed Jan 30 16:02:12 CET 2013 wortspiel/BuchstabenKollektion.class
2075 Wed Jan 30 16:02:12 CET 2013 wortspiel/TestBuchstabenKollektion.class
1104 Wed Jan 30 16:02:12 CET 2013 wortspiel/TestWorte.class
942 Wed Jan 30 16:02:12 CET 2013 wortspiel/UI.class
4701 Wed Jan 30 16:02:12 CET 2013 wortspiel/Worte.class
688 Wed Jan 30 16:02:12 CET 2013 wortspiel/WortspielGUI$1.class
688 Wed Jan 30 16:02:12 CET 2013 wortspiel/WortspielGUI$2.class
3475 Wed Jan 30 16:02:12 CET 2013 wortspiel/WortspielGUI.class
You can't use java.io.File to get the contents of a file inside of a jar. Try running the following snippet,
public class TestGetResource
{
public static void main(String[] args) throws Exception
{
URL url = TestGetResource.class.getResource("/resource/wortliste.txt");
System.out.println("URL: " + url);
File f = new File(url.getFile());
System.out.println("File: " + f);
System.out.println("File exists: " + f.exists());
try {
FileReader reader = new FileReader(f);
reader.read();
} catch (FileNotFoundException notFoundEx) {
System.out.println("Caught FileNotFoundException: " + notFoundEx.getMessage());
}
}
}
You should see something similar to this,
URL: jar:file:/C:/some/path/my.jar!/resource/wortliste.txt
File: file:\C:\some\path\my.jar!\resource\wortliste.txt
File exists: false
Caught FileNotFoundException: file:\C:\some\path\my.jar!\resource\wortliste.txt (The filename, directory name, or volume label syntax is incorrect)
Instead, use Class.getClassLoader().getResourceAsStream(String) to get an InputStream for reading the file. If you still need to read the contents as a File, you could stream the contents out to a temp file.
Try this:
URL url = Thread.currentThread().getContextClassLoader().getResource("resource/list.txt");
File file = new File(url.getFile());

Java Calendar, acts differently OSX Windows

Basically i have built an appointment scheduler webapp using java servlets.
It relies heavily on javas Calendar.
The whole thing was developed on my macbook running mountain lion with jdk 1.6.
Now when testing it on my pc i have had some strange results.
Running:
System.out.println("selected = "+selected);
Calendar now = Calendar.getInstance();
System.out.println("a "+now.getTime());
now.setTimeInMillis(selected);
System.out.println("b "+now.getTime());
now.set(Calendar.MILLISECOND,0);
now.set(Calendar.SECOND,0);
now.set(Calendar.MINUTE,0);
now.set(Calendar.HOUR_OF_DAY,6);//start from 6am
System.out.println("d "+now.getTime());
now.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
System.out.println("e "+now.getTime());
on the PC will output:
selected = 1355835600000
a Wed Dec 19 19:35:36 EST 2012
b Wed Dec 19 00:00:00 EST 2012
d Wed Dec 19 06:00:00 EST 2012
e Sun Dec 23 06:00:00 EST 2012
Yet on the mac it will output:
selected = 1355835600000
a Wed Dec 19 19:33:57 EST 2012
b Wed Dec 19 00:00:00 EST 2012
d Wed Dec 19 06:00:00 EST 2012
e Sun Dec 16 06:00:00 EST 2012
As we can see here, if i build a table representing a weekly schedule off these values, the mac will start at 6am on the sunday contained in the current week.
But the pc will start at 6am on the sunday contained in the next week.
Which means any appointments i create are out of sync on the pc (different days to whats expected)
note: the pc has been tested with both jdk1.6 and jdk1.7
Does anyone know any solutions or reasons behind this?
Thanks
Possibly, this is a locale/timezone problem which might differ on both machines.
Make sure that both instances use the same locale by hard coding it for example:
Calendar.getInstance(Locale.US);
else getInstance() would use the default locale of your system.

Categories

Resources