-1

I want a function to take a name of database/schema as a input and check weather that database is active or not every 5 min and then log it in a different table. Sort of like monitoring the database

I have a function say repeat

function repeat(String database_name) {
     //check database_name can be connected or not
}

How to pass value inside the function repeat (if not parameter) when I will annotate it with @scheduled

Rupesh juyal
  • 21
  • 1
  • 3
  • 2
    You will need to clarify what you mean by "I need to pass parameters". Scheduled functions are just that; scheduled to run at specified time period without any user interaction. If you need to pass a parameter for invocation then it is really not a scheduled task. What is the use-case for this? – madteapot Mar 23 '20 at 12:58
  • How are you expecting to provide the parameters then? – Michael Mar 23 '20 at 13:00
  • From where have you taken these parameters in your initial solution? Are they from the database? Are they user input? Please clarify – Lino Mar 23 '20 at 13:04
  • yes the parameters are from database @Lino – Rupesh juyal Mar 23 '20 at 13:05
  • 1
    @Rupeshjuyal then you can simply fetch these values in the scheduled method itself – Lino Mar 23 '20 at 13:06
  • I get your point and thats where I got confused because till now it was same data(say same row to pass) but now data is changing what can I do? – Rupesh juyal Mar 23 '20 at 13:10
  • @Rupeshjuyal Check my answer. It should provide a solution but you should update your question with more information like what parameters types are needed, and if they're always the same or if they change dynamically. – Jason Mar 23 '20 at 13:13
  • @jason thnx I will try it and will sure change the question...Its my 1st question on stackoverflow will try to improve the question – Rupesh juyal Mar 23 '20 at 13:16
  • @Rupeshjuyal No problem, we all have to start somewhere. If it's not a valid solution let me know and i'll see if I can provide a more suitable answer upon the question being updated. – Jason Mar 23 '20 at 13:21

2 Answers2

1

One solution to this would be to instead create an object that represents the parameters and on that execution after 5 minutes, obtain that object and use the internal state as a representation of the parameters. You have to ensure that MyEventService contains a reference to Event before the execution every 5 minutes. Without more input on the original post it's impossible to know how/when you're doing this.

If the parameters never change then I suggest making the service immutable, and removing the event class, and just providing the values via properties using @Value.

class Event {

    private final String myString;

    // constructor, getter

}
@Service
public class MyEventService {

    private Event event;

    // constructor, setter, getter

}
class MyScheduledTask {

    @Autowired
    private MyEventService eventService;

    @Scheduled(fixedDelay = 300_000)
    public void execute() {
        Event event = eventService.getEvent();

        if (event == null) {
            throw new IllegalStateException("No event to process.");
        }
        // process event
    }
}
Jason
  • 5,154
  • 2
  • 12
  • 22
-1

Actually @Scheduled takes parameters. Use the parameter fixedRate for specify the interval in milliseconds:

@Scheduled(fixedRate = 300000‬)
public void youFunction() {
}

will execute it every 5 minutes.

If you need to specify in interval during runtime via the method parameter see my answer here: Is it possible to call a spring scheduled method manually

You can use a TaskScheduler and manually schedule tasks via

taskScheduler.schedule(task1, new PeriodicTrigger(5, TimeUnit.MINUTES));

where the 5 could also be a parameter.

das Keks
  • 3,723
  • 5
  • 35
  • 57
  • 2
    I think OP meant to pass *method* parameters – Lino Mar 23 '20 at 13:06
  • 1
    And technically, annotation "parameters" are called [*elements*](https://docs.oracle.com/javase/tutorial/java/annotations/basics.html) – Michael Mar 23 '20 at 13:07