Solved

java timer and scheduling

Posted on 2012-03-23
7
447 Views
Last Modified: 2012-08-14
Hello. I am wondering, if I am using Timer or TimeTask classes or ExctutoThreadPool to have code run and write to a remote database and I am running this on Windows 7, what happens if the computer goes into sleep mode? Does the code not run and the remote writes to a database not happen?

And how about if I use the Windows scheduler, does the code run if the computer is in sleep mode?

Thank you.
0
Comment
Question by:onyourmark
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37759965
I think it should work nevertheless.
It is like any service running on computer, as long as you didn't switch it of , all running services should be running.
With windows schedyuler it is very easy to try
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37759968
in windows scheduler there is acheck box to wake up computer from sleeping mode as I see from responses to this question:

http://stackoverflow.com/questions/3960391/would-a-scheduled-java-app-run-when-windows-is-in-sleep-mode
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37759970
also useful info about windows scheduler waking up here:

http://superuser.com/questions/51967/task-scheduler-wont-wake-the-computer
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:onyourmark
ID: 37759977
Thanks. What is your opinion about "windows service applications" that run in the background? I found the following:

For an application that needs to complete some action on a schedule, I would consider writing a windows service application rather than a normal executable that is scheduled by the windows scheduler.

Starting the service can be automatic with windows startup.

To create a service, select File | New | Other and select Service Application.

In the Object Inspector Double Click OnExecute.

Code:
procedure TService1.ServiceExecute(Sender: TService);
const
   ExecuteTime = 900;
var
   CurrTime:  Integer;
begin
   CurrTime := 0;
   While Not Terminated do
   begin
      Inc(CurrTime);
      if CurrTime >= ExecuteTime then
      begin
         //to catch exceptions enclose this section in a try/except block
         try
            //Executes this code every 15 Minutes
         except
            //write out to a log file your error
         end;
         CurrTime := 0;
      end;
      Sleep(1000);
      //sleep one second...
      //If you set the service to sleep for 15 minutes then
      //if a user wants to terminate the process, they'd have to
      //wait 15 minutes for it to terminate.  This way the
      //service stays responsive, but with the use of the
      //currTime vs executetime you can limit the execution to
      //every 15 minutes.
      ServiceThread.ProcessRequests(False);
   end;
end;


to have the service startup automatically when loaded add this to the end of the service...

Code:
initialization
   if (ParamCount >= 1) and (CompareText('/uninstall', ParamStr(1)) = 0) then
   begin
      WinExec('cmd.exe /c net stop <YourServiceName>', SW_HIDE);
      Sleep(300);
   end;

finalization
   if (ParamCount >= 1) and (CompareText('/install', ParamStr(1)) = 0) then
      WinExec('cmd.exe /c net start <YourServiceName> /silent', SW_HIDE);
end.


Then to install/start your service from the command line:

BTW: You must have admin privileges if in Vista or Win7 to install/uninstall

From the directory where your service application resides:

<ServiceAppName> /install /silent

To uninstall/stop your service from the command line

Also from the directory your service application resides:

<ServiceAppName> /uninstall /silent

Also, here's a link on how to do this:

http://www.tolderlund.eu/delphi/service/service.htm
0
 

Author Comment

by:onyourmark
ID: 37759981
And can I ask, what about timers? Will they continue to run if the machine is in sleep mode? And if I use them does that mean that the code is always running and using resources? And are there other pros or cons for the timer?
Thanks.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37759982
I think if you write it in Java with TimeTask or Quratz then wrapping it into windows service is a way to go.

Still, I'd rather try as much as possible use Windows scheduler.
Why to add one more service, if you already have service which does it for you?
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 37759984
I think thesuse of resoourec with timers in between the runs is minimal, but of course there will be some use.
That's why I frankly don't see advantages to have it injava - I'd rather use Windows schedurler.

I'd rather use java timers if in the course of, say 30 miniutes you need to run some process every 20 seconds.

If you want to run for months and years the process ever 30 minutes - I'd rather use scheduler.
And I think scheduler will interact better with this sleeping, etc. as it is part of the same system - it will have more useful provisions
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question