Solved

java timer and scheduling

Posted on 2012-03-23
7
441 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
diffSum example 4 51
tomcat startup error 5 106
mysql jsp example issue 32 51
Java: The Public Class Main 4 32
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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.

828 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