• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to run a task every month using Java  RSS feed

 
Dennis Colossal
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I would to know how can I schedule a task to be executed every month, I was trying with a method I see on StackOverflow: "http://stackoverflow.com/questions/4107011/java-monthly-timer".
Thank You.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my advice: don't.

Java is not really the best tool for this sort of thing. On *nix systems, you'd use cron. on Windows, you'd use a scheduled task.

As my old boss used to say, "Use the right tool for the right job".
 
Dennis Colossal
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:my advice: don't.

Java is not really the best tool for this sort of thing. On *nix systems, you'd use cron. on Windows, you'd use a scheduled task.

As my old boss used to say, "Use the right tool for the right job".


But how can I do it on Windows?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Colossal wrote:
But how can I do it on Windows?


If you are using server side Windows, you can you the "at" command. If not, you can either contact Microsoft to see if it is available, or load something like Cygwin to get Unix services like cron... or you can use a schedule task (ie. schtasks) as fred mentioned, which I have never done, so can't help...

Henry
 
Dennis Colossal
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But btw, have no way to do it with Java? It have no support class to make it?
Thanks.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Colossal wrote:But btw, have no way to do it with Java? It have no support class to make it?


Sure.... Java can do this via a scheduled executor, but is it guaranteed to be running? What happens when the machine gets rebooted?

With OS services, those tasks will be running at the requested day and time, as long as the OS is running. The OS will deal with restarts, etc.... Remember that you are talking about months in terms of time frames. Is Java really the preferred option?

Henry
 
Dennis Colossal
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Dennis Colossal wrote:But btw, have no way to do it with Java? It have no support class to make it?


Sure.... Java can do this via a scheduled executor, but is it guaranteed to be running? What happens when the machine gets rebooted?

With OS services, those tasks will be running at the requested day and time, as long as the OS is running. The OS will deal with restarts, etc.... Remember that you are talking about months in terms of time frames. Is Java really the preferred option?

Henry



But, I need execute a method on my Java plugin, So isn't better I make an method on my Java plugin?
 
salvin francis
Bartender
Posts: 1653
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Colossal wrote:But btw, have no way to do it with Java? It have no support class to make it?

Java is a programming language. Using java, you can write programs to do any task. A program has to be 'executed'. It can be killed by a user or by the operating system. If a program is no longer running, it simply cannot do your task.

As suggested by others, there are daemons/services which are provided by the operating systems to do repeating tasks. These are special programs which run in the background. Usually, an operating system manages  it and ensures that it's always running even after rebooting.
 
Dennis Colossal
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:
Dennis Colossal wrote:But btw, have no way to do it with Java? It have no support class to make it?

Java is a programming language. Using java, you can write programs to do any task. A program has to be 'executed'. It can be killed by a user or by the operating system. If a program is no longer running, it simply cannot do your task.

As suggested by others, there are daemons/services which are provided by the operating systems to do repeating tasks. These are special programs which run in the background. Usually, an operating system manages  it and ensures that it's always running even after rebooting.



But look at this class: "http://stackoverflow.com/questions/4107011/java-monthly-timer" You put on main class, and it will always when the plugin start will remove the last timer, and create a new timer. But It doesn't wotk to me I don't know why.
 
salvin francis
Bartender
Posts: 1653
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While the SO link has suggested many ways, the discussion here was about feasibility rather than possibility.
Where are you going to run your program ?
What is the error you are facing ? ItDoesntWorkIsUseless
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this suggestion near the bottom of the page is the best:

the simplest solution might be to use cron or equivalent to schedule a stand-alone program execution...
 
Dennis Colossal
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:While the SO link has suggested many ways, the discussion here was about feasibility rather than possibility.
Where are you going to run your program ?
What is the error you are facing ? ItDoesntWorkIsUseless


I want to execute a Method, Let's say this method is a message so every first day of moth I want to send a message.

I change this class to it:


But it don't show nothing.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For that to work, the program must be running all the time, waiting until it's time to display the message. You can't just run the program once, let it end, maybe switch off the computer and switch it back on tomorrow, and still expect the timer to go off. It can only go off as long as the program is running. You could use this when you have a program that is running 24x7 on a server. But for a normal desktop or laptop, where you are not running your program all the time, this isn't a great way to do it.

That's why it is better to use the operating system's task scheduling mechanism to do this. The operating system is always running as long as the computer is switched on, and most operating systems have ways to schedule running a particular program at a specific time. The only thing you would have to do is write a program that displays a message, and then you use the operating system's facility to run that program at the time you want the message to be displayed.
 
Dennis Colossal
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I can implements: Quartz Scheduler on my code? This is better than my Class method?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!