-2

I have a REST sending 2 parameters to database and want to have a timertask who shoud ask database at regular intervals.

And the problem is that TimerTask and his method run() doesnt accept any parameters so how to do this:

for example :

sending 2 Strings through TimerTask, get response from database and TimerTask ask the same query (with the same Strings at evry 2 minutes)

Throught the web it's only simple examples how to print "Hello" for regular intervals (know how to do that) but i can't any answears how to use method (who send query to database) in TimerTask

MajkelEight
  • 107
  • 1
  • 8
  • do you have the 2 params when you are creating the timerTask ? – Kishita Variya Oct 17 '19 at 13:14
  • Is your TimerTask subclass an anonymous class? – VGR Oct 17 '19 at 13:16
  • From what I'm gathering from your response, you seem to be struggling with more than one problem here: **1)** implementing a parameterized `TimerTask` (by the way, does it run forever? when it stops?), and **2)** how to actually query a database. Please ask a *specific* problem. And making a [mcve] and explaining why it doesn't work would be the best. – M. Prokhorov Oct 17 '19 at 14:13

2 Answers2

0

You can try to implement Producer/Consumer concept for dynamic parameter handling i guess.

Think producer as a parameter generator and consumer as TimerTask.

import java.util.TimerTask;
import java.util.Vector;

class Producer extends Thread {
    static final int MAXQUEUE = 5;
    private Vector messages = new Vector();

    @Override
    public void run() {
        try {
            while (true) {
                putMessage();
                sleep(1000);
            }
        } catch (InterruptedException e) {
        }
    }

    private synchronized void putMessage() throws InterruptedException {

        while (messages.size() == MAXQUEUE)
            wait();
        messages.addElement(new java.util.Date().toString());
        notify();
    }

    // Called by Consumer
    public synchronized String getMessage() throws InterruptedException {
        notify();
        while (messages.size() == 0)
            wait();
        String message = (String) messages.firstElement();
        messages.removeElement(message);
        return message;
    }
}

class Consumer extends TimerTask {
    Producer producer;

    Consumer(Producer p) {
        producer = p;
    }

    @Override
    public void run() {
        try {
            while (true) {
                String message = producer.getMessage();
                System.out.println("Got message: " + message);
                Thread.sleep(2000);
            }
        } catch (InterruptedException e) {
        }
    }

    public static void main(String args[]) {
        Producer producer = new Producer();
        producer.start();
        new Consumer(producer).run();
    }
}

Think new date as a dynamic query parameter to pass a consumer TimerTask

Alican Beydemir
  • 343
  • 2
  • 13
-1

If the values are available at the time of creation of timer task:

Timer timer = null;
TimerTask task = null;

    public void initTimer(final String param1, final String param2){
        if(timer == null)
            timer = new Timer();

        // creating an instance of task to be scheduled
        if(task == null)
          task  = new TimerTask() {
            @Override
            public void run() {
                // database call here
                System.out.println("My params "+param1+" and "+param2);
            }
          };

        // scheduling the timer instance
        timer.schedule(task, 1000, 3000);
    }
Kishita Variya
  • 810
  • 9
  • 19