Solved

java timer and scheduling

Posted on 2012-03-23
7
437 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
Comment Utility
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
Comment Utility
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
Comment Utility
also useful info about windows scheduler waking up here:

http://superuser.com/questions/51967/task-scheduler-wont-wake-the-computer
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:onyourmark
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now