Dear All,
I am using an adapted code from the web for sending sms via gsm modem. Now I would like to modify to read sms message from database table and send it.So I would like to programme it to read on every 30 second from the db process and send the message. What will be best method is it timer or cronjob? Thank you.

Recommended Answers

All 12 Replies

cronjob is Unix related
java System.currentTimeMillis(); will give you the actual time in millis

Dear Pbl,
Any good example how to start on java time? Thank you.

Dear James,
I saw the example. I dont know how to adapt because it requires a run function. Below is my main function so what must I edit to apply the timer functionality.

public class callSMSClient{

  public static void main(String[] args)
  {
    SMSClient t1 = new SMSClient(0);
    String  phoneNumber, messageSend;
         //this is the function I want to call every 30 second.
    t1.sendMessage(phoneNumber,messageSend);    
    }


  }
}

public void run() {
t1.sendMessage(phoneNumber,messageSend);
}

Dear James,
I have managed to come out with this code below. Just to confirm with you the value 20000 is that every 20 second it will run the function to print am I right? So what the is delay value then? Why must we need it ? Thank you.

public class test1
{
    
    public static void main(String[] args) {
       int delay = 1000;
       Timer timer = new Timer();
       timer.schedule(new TimerTask(){
        public void run(){
          System.out.println("This line is printed only once.");
        }
      },delay,20000);
    
    }

}

am I right

Don't ask me - ask Java! Did you run it? What did it do?

20,000 mSec is 20 secs, yes.
The delay is the time before the first call to your run. so for example
0,20000 runs immediately, then every 20 secs after
60000,20000 runs after 1 minute, then every 20 secs after
Its there because the Timer class covers many uses, eg if you just want to wait 10 secs before doing something once... 10000,0 (delay 10 secs, no repeats)

Dear James,
Yes the code runs. I am kind of confuse. So for instance I want it to run every 30 seconds. Is this how the time should be set (0,30000). Actually how the delay play its part? Thank you.

Instead of Timer, use ScheduledThreadPoolExecutor, it removes several ambiguities and risks from Timer .In Timer if the thread throws Exception,it ends and other tasks are not executed. In ScheduledThreadPoolExecutor, this is taken care of. And also if the delay period of waiting tasks is near to over, it makes new threads and executes it unlike Timer where it will still have to wait for the current task to get over.

Also adding to it, it performs much better in an concurrent atmosphere.

Dear James,
Yes the code runs. I am kind of confuse. So for instance I want it to run every 30 seconds. Is this how the time should be set (0,30000). Actually how the delay play its part? Thank you.

I guess you want your code to start running right away, so set the delay to 0.

ps: I'm not familiar with ScheduledThreadPoolExecutor, so I can't comment on purijatin's suggestion.

Dear Purijatin,
Can you guide me how to use and apply ScheduledThreadPoolExecutor? I would like to replace it with my existing methond then. Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.