Execute MySQL statement in the background automatically - java

I want to delete records from MySQL table which were not updated for longer than 3 minutes. How can I set the timer in the background to manage it without being invoked by events or methods in java? Is that possible?
DELETE FROM bus WHERE created_at < (NOW() - INTERVAL 5 MINUTE)

As #abhishek-ghosh correctly pointed out, you can use CREATE EVENT.
CREATE EVENT event_bus_delete
EVERY 5 MINUTE
DO
DELETE FROM bus WHERE created_at < (NOW() - INTERVAL 5 MINUTE);
Event support was added in MySQL 5.1.6. However MySQL Event Scheduler is not running by default and needs to be enabled in order for events to work.
See this StackOverflow answer or How to Configure MySQL Event Scheduler article on how to enable MySQL Event Scheduler and make sure it's running.
You can ensure the scheduler starts when MySQL is launched with the command-line option --event-scheduler=ON or setting event_scheduler=ON in your MySQL configuration file (my.cnf or my.ini on Windows).
Alternatively, you can start the scheduler from the MySQL command line:
SET GLOBAL event_scheduler = ON;

Edit
Misread your question - the following describes how you can use Java to schedule the task rather than doing it independently of the Java layer
You can achieve this by using a combination of Timer and TimerTask. Together they form quite a simple scheduling facility. The Timer schedules TimerTasks that occur periodically.
There are a number of good tutorials on the web : for example

Related

Dynamic Job Scheduling with Quartz or any other java api

I have an UI Interface where user can define Job name, interval, active/Inactive etc.
How we can achieve this with Quartz Scheduler or any java/Spring api ?
Ex. Suppose any Quartz job is started and interval is set as 10 min, So in ideal case job will run in next 10 min interval. But every time job runs we want to fetch the latest interval from database and schedule it.
10:00 Job runs and in the database interval is set to 10 min
10:10 Job runs and in the database interval is set to 20 min
So next time job should run at 10:30
If you use Quartz, you can implement a custom Trigger. Your implementation would lookup the value in the database and return when the next time the run should happen in the getFireTimeAfter.
Another option is to use Spring Scheduling APIs and implement the Trigger interface. Same here, the nextExecutionTime method would decide when the next run should happen.
The advantage of using a custom implementation is that you have full control over the triggering logic (like in your case, do a lookup in the database and dynamically set the next run time).

Spring + Quartz: programmatic schedule of one-time, misfire proof jobs

I am working on an enterprise application that uses Quartz 2.2.1.
Trigger beans are configured in their XML, which included the relative cron expression.
Now, I have the need of programmatically creating triggers with custom fire times; these triggers also have to handle misfiring (I have to be sure that they get executed, even if the server is down at fire time). Is there a way to make quartz do the 'dirty' work without handling trigger persistency manually?
Thank you.
Easier than I thought, solution lies within the SimpleTrigger class.
// Update trigger infos
SimpleTrigger trigger = (SimpleTrigger)builder
.startAt( whenever you want it to start )
.withSchedule(simpleSchedule()
.withIntervalInMinutes( or any interval ).repeatForever() )
.endAt( whenever you want it to end)
.build()
This creates a quartz trigger living between start and end, and executing following the specified interval.
Misfire strategy can be specified as well.

How to change quartz job scheduled time manually using sql?

I am using Quartz job scheduler for running some jobs. Using java api I am managing the job.
When I reschedule a job to different time using java,
scheduler.deleteJob(jobName, jobGroupName);
addJobsInScheduler(jobName, jobGroupName, triggerName,
triggerGroup, newTime, schoolName);
The job is getting rescheduled and triggered at the specified time.
Now have a requirement in which I have to reschedule the job without UI(without Java api).
From my understanding when I reschedule a job the following table entries getting updated.
QRTZ_JOB_DETAILS
QRTZ_TRIGGERS
QRTZ_CRON_TRIGGERS
So I manually updated the fire time in QRTZ_TRIGGERS,QRTZ_CRON_TRIGGERS. But the job is not triggered at the updated time.
Is that possible to achieve my requirement?
Note: I am using sql server 2008 for my backend.
I believe you shouldn't update the Quartz tables directly with SQL. There are no right way with SQL in Quartz documents so that, no one guarantee not to have any changes for the interface between Quartz and database in the future.
According to the documents Lesson 9: Job Stores
Never use a JobStore instance directly in your code. For some reason
many people attempt to do this. The JobStore is for behind-the-scenes
use of Quartz itself. You have to tell Quartz (through configuration)
which JobStore to use, but then you should only work with the
Scheduler interface in your code.
You should follow the below document for updating your triggers.
How-To: Updating a trigger
If you have no UI, You need to create new interface for updating your schedules somehow.
From experienced, we just update the NEXT_FIRE_TIME column like this:
`UPDATE dbo.QRTZ_TRIGGERS SET NEXT_FIRE_TIME =1491004800 `
where 1491004800 is 1st of April 2017 00:00:00 GMT
Use the T-SQL below to get the NEXT_FIRE_TIME you desire. You may need to change the GETUTCDATE().
SELECT DATEDIFF(s, '1970-01-01 00:00:00', GETUTCDATE())
Then update the QRTZ_CRON_TRIGGERS table
UPDATE dbo.QRTZ_CRON_TRIGGERS set CRON_EXPRESSION='00 10 11 ? * *'
where '00 10 11 ? * *' is run 11:10AM daily. You can get the cron expression from CronMaker
This may not be the best practice but from experience it works for us without issues. I would highly recommend to TEST it first in your lower environment. Wait for the job to trigger and see if you see any changes on the QRTZ_TRIGGERS table.

Designing a server that performs an action every X minutes

Good Day,
I am required to write a java server that performs an action every X minutes. The action is to check a database to see if the current/system time matches any of the times in a database, and to pull out those items, and send a TCP message to them.
Hencen, the database call is local on the machine, so that is no problem. However, at least 10 TCP calls need to be sent out simultaneously. Hence, the tick may actually need to occur on it's own thread. Can I have some suggestions?
Do I need a thread pool?
one thing you can do is create a schedular job and run that job every x minutes. so that job will be perform every x minutes and you need to define your task in job to perform for more info click here
I would use a Timer or else I would use the Quartz Scheduler - the former is more lightweight, while the latter is (optionally) durable (meaning that scheduled tasks will be saved to a database and reloaded when your program restarts).
Either TimerTask (or) ScheduledExecutorServices implementations would be best options for this task. Yes, I think thread pool would be best option because you don't need to create 10 threads every X minutes.

Oracle database to send notification / Invoke a java method on reaching Time specified in a column

I have a table which has event-details. New records will be inserted to this table by another process continuously. The table has a column Event-end-time.
I have to design a mechanism to invoke a java method to process the events upon reaching the event end time. Some of the option i have are :
Continuously query the database (every 1 min) and find the next latest report to be processed and invoke it on reaching the end time.
Figure out if there is any built in mechanism in ORACLE database to send the java application a notification / Oracle itslef invoking the java method upon reaching the end time of the event.
I have seen options for notification if an record is Inserted/updated/deleted (trigger based) , but till now i have had no luck if the Notification condition to be made custom(if time specified in a column of teh record is reached).
Any advice regarding this will be much appreciated.
Regards,
Deepak
The best way that I know of to do this is via Oracle AQ ( a JMS implementation )
There is also database change notification, which I don't have any experience in, but could be what you are after, I believe it actually builds upon AQ.
/* removed trigger idea because the event end data is already in the table and the action
* should be fired when the event date/time is reached
*/
Since you want to fire your event when a certain time is passed, it could simpley be adding a Oracle Scheduler job with a start date identical to the event end time. See CREATE_JOB Procedure on Oracle Documentation site. Oracle Scheduler is very powerfull and can start all kinds of actions like pl/sql code, procdures and shell scripts, that can be local and remote using credentials.

Categories

Resources