Reputation: 163
There is a system at a company I just started working for. The system allows clients to manage their subscriber notifications via SMS. Users log in via a web application and create messages (each message has a send date_time, destination phone_number etc). These messages are saved in a mysql database. They also have a java application implemented using jsvc. This application keeps reading the database 24/7 every 1 minute and sending the messages (via an external network provider API), that is for very message that should be sent at that particular time.
I've been brought in to rewrite the whole thing. Currently I can't change anything that deals with the web application and the database. I'm only trying to rewrite the jsvc using vertx.
I already have already written vertx service that communicates with the db and another service that communicates with the external API.
I have thought of creating a periodic function that runs every hour (it reads the database and gets all the messages that should be sent within the next one hour) and then schedules this messages to be sent to the service that communicates with external API.
My question is, how would I achieve such scheduling? The send date_time of each message is precise to the minute in the database. I'm looking at Chime and how to incorporate it but I haven't seen a good example to help me know the proper way to do it.
So, what is the proper way to use Chime to schedule eventbus messages to the external API service and is there another way I can structure this use case?
Upvotes: 4
Views: 8504
Reputation: 180
its a improvement/extension of Alexeys answer.
vertx.setPeriodic(1000 * 60 * 60, (l) ->
{
for(task t: tasks_in_that_hour)
{
long timerID = vertx.setTimer(t.timeDiff(), id ->
{
//your code here
});
}
});
ok if you have in that hour millions of tasks, then you could use a for-loop which dont fetch the tasks in the next our, but the next 1000 tasks. In that case you have to change that periodic timer to a singleTimer: vertx.setTimer(...) to the task 1001 (maybe to a time that points to a few msec before that task^^). The code in the for-loop would be the same as i posted.
Upvotes: 3
Reputation: 36
Look into Java timer for scheduling or RabbitMQ-client.
public void timerSchdule() {
TimerTask repeatedTask = new TimerTask() {
public void run() {
System.out.println("performing activity on " + new Date());
}
};
Timer timer = new Timer("Timer");
long delay = 1000L;
long period = 1000L * 60L * 60L * 24L;
timer.scheduleAtFixedRate(repeatedTask, delay, period);
}
Upvotes: 1
Reputation: 17701
Vert.x provides you with setPeriodic()
for that.
You can run a job every hour with something like:
vertx.setPeriodic(1000 * 60 * 60, (l) -> {
// Your code here
});
Upvotes: 3