Solved

java timer and scheduling

Posted on 2012-03-23
7
440 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mockito example issue 8 73
xampp tool 12 51
java stored proc example 9 32
eclipse console opening separately 2 21
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

821 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