How to create Schedular on SpringBoot which invoke method - spring-boot

I have a table called JobSchedular
JobSchedular
JobCode
JobName
JobDescription
FunctionName
Status
RunInterval(CronEcpression)
Job001
Notify
To Send Notification
SendNotification
ACTIVE
0 * * * * ?
Job002
Appointment
To manage appointment
ManageAppointment
ACTIVE
0 * * * * ?
My Requirement is to Create Schedular on SPring Boot using #Scheduled annotation
which takes list of rows from tables and call the function dynamically based on corn expression

Now spring boot has built in support to quartz. Use this example to schedule job.
https://www.callicoder.com/spring-boot-quartz-scheduler-email-scheduling-example/

Related

Is there a way to route the emails (read via an imap idle adaptor) to different output channel based on the size of the email?

I have configured an Imap Idle adaptor which is listening to an email box, reading and parsing the email. The handling of these emails is done in a service activator class.
These emails are getting generated in bulk from an ERP system and have heavy attachments. Because of the heavy load, some emails are getting lost.
Is there a way in Spring integration to route these emails to different output channels based on the size and assign different handler classes to these output channels so that these emails can be processed in parallel and bulky emails don't block the processing?
Details of the configuration is in this ticket
The ImapIdleChannelAdapter produces a MimeMessage if you don't let it to convert the email into a simple content.
That MimeMessage has this method to check from your router:
/**
* Return the size of the content of this message in bytes.
* Return -1 if the size cannot be determined. <p>
*
* Note that this number may not be an exact measure of the
* content size and may or may not account for any transfer
* encoding of the content. <p>
*
* This implementation returns the size of the <code>content</code>
* array (if not null), or, if <code>contentStream</code> is not
* null, and the <code>available</code> method returns a positive
* number, it returns that number as the size. Otherwise, it returns
* -1.
*
* #return size of content in bytes
* #exception MessagingException for failures
*/
#Override
public int getSize() throws MessagingException {
See router for more info:
https://docs.spring.io/spring-integration/docs/current/reference/html/message-routing.html#messaging-routing-chapter
https://docs.spring.io/spring-integration/docs/current/reference/html/dsl.html#java-dsl-routers

Spring scheduler - getting time of next event

I've got a SpringBoot Scheduler that looks like this:
#Scheduled(cron = "*/10 * * * * * ") // Every 10 seconds
public void scheduleTaskUsingCronExpression() {
long now = System.currentTimeMillis() / 1000;
System.out.println("schedule tasks using cron jobs - " + now);
}
I'd like to get the time of the next event, so I can print it out at the end of each event. I've read about using CronExpression, which has a next() method, but not sure how I get the CronExpression. OR do I create a CronExpression and somehow pass it in instead of using the #Scheduled annotation?
I'm not sure I can get accurate interval time but I can get roughly correct next running time from following code.
#Value("${cron.schedule}") String schedule;
#Scheduled(cron = "${cron.schedule}") // Every 10 seconds
public void scheduleTaskUsingCronExpression() {
var expression = CronExpression.parse(schedule);
var result = expression.next(LocalDateTime.now());
System.out.println("schedule tasks using cron jobs - " + result);
}
You can define your cron string (that you use in the #Scheduled annotation) as a final field of your class, and then create CronExpression from it to find out the next trigger date.
Also, instead of using #Scheduled annotation, you can take a look at Spring's TaskScheduler (from the package org.springframework.scheduling). It has the method TaskScheduler#schedule with two arguments: a Runnable that will run in background and a CronTrigger to set the cron expression and and the timezone of executing background tasks.
UPD. One other way to reuse your cron is set it in your application.properties and use within #Value and #Scheduled annotations, e.g. #Scheduled(cron = ${property.from.file}). In this case you can also change the cron expression before running your application if needed.

Customize Passport Queries time of authenticating

For every request I found that 4 queries are fired to validate the user and the token. Among them one is to fetch the user (select * from user) based on the user id. This queries are fired by Passport/Laravel But what I want is to modify this query to add one status field check also to check if any user become invalid during the token validity period. If we only check with the id then if any user become inactive(By changing status then also we will not be able to stop the user as deleting the token for the user is not a good solution for me).
Queries Fired on every request by Passport Laravel:
select * from oauth_access_tokens where id = ?
select * from user where id = ? limit 1 ["2"]
select * from oauth_access_tokens where id = ?
select * from oauth_clients where id = ?
So, can anyone tell me how to change the 'select * from user where id' query in passport at time of Token validation.
You can add this method on your User model (or any model you're authenticating with passport)
...
public function findForPassport($username)
{
return $user = (new self)->where('email', $username)->where('is_active', 1)->first();
}
...
of course you can modify is_active by whichever column you are using (and/or any query constraint for that matter), as long as it returns Illuminate\Contracts\Auth\Authenticatable contract.
I wouldn't try and modify passports default behaviour as I have no idea what else it might impact both now and in future upgrades.
Your best bet might be to hook into the passport events and apply you business logic to a listener that is called when the events are fired

renaming custom operation in api-platform

I'm modeling one entity that has a prepare-release workflow. So in addition to the ordinary POST action to create the entity, I also have a second POST custom operation to set it active. This triggers considerable backend activity which is why I implement it as a custom operation and not as a simple update on a property (PUT).
So far so good, however in the API interface documentation, it still describes the operation as "create a xxx resource", which is false. I found no way to change this description. How can I put a different text there?
actually figured out from a totally unrelated post (https://stackoverflow.com/a/49534635/982364) that this works:
* collectionOperations={"post", "special"={
* "method"="PUT",
* "path"="/myentity/{id}/commit",
* "controller"=EntitySpecial::class,
* "denormalization_context"={"groups"={"myentity_commit"}},
* "swagger_context" = {
* "summary" = "commit to this action"
* },
* "defaults"={"_api_receive"=false}
* }},

JobExplorer.findRunningJobExecutions does not work correctly

In a project I use spring-batch, as a part of cleanup for processes that stuck I use
JobExplorer.findRunningJobExecutions(...)
but that function return empty set. After digging I found next problem: spring-batch use SQL like
SELECT E.JOB_EXECUTION_ID, ... from %PREFIX%JOB_EXECUTION E ... WHERE ... E.END_TIME is NULL .... After running long job, I got that from beginning 3 Dates in %PREFIX%JOB_EXECUTION (CREATE_TIME, START_TIME, END_TIME) have same value. This mean that SQL will always return empty set.
Question is, am I one who get this?
How to report bug to Spring team?
Here is the link to the Spring batch project home page
Jira's can be Entered here
Not sure what version you are running, but with version 2.1.1 (which is real old I know) but this code works.
jobOperator.getRunningExecutions(String jobName)
Here is the javadoc from that version
/**
* Get the id values of all the running {#link JobExecution JobExecutions}
* with the given job name.
*
* #param jobName the name of the job to search under
* #return the id values of the running {#link JobExecution} instances
* #throws NoSuchJobException if there are no {#link JobExecution
* JobExecutions} with that job name
*/
Set<Long> getRunningExecutions(String jobName) throws NoSuchJobException;
Link to the most update JobOperator
Hope this helps.

Resources