Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

java timer and scheduling

Posted on 2012-03-23
7
Medium Priority
?
449 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 2000 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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

730 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