initialDelay value take at runtime - spring-boot

I have my task to be scheduled to start after 1 min and then every 15 sec it will again run after completing my task execution. so I have my scheduler like:-
#Scheduled(initialDelay=60000, fixedDelay=15000)
public void runTask(){
// some code
}
Now I need to take initialDelay at runtime means if JVM start then I have a method "initialDelayCalculate" which calculate that, after how many hours or minute or sec my scheduler will invoke.
The time calculation method like:-
public long initialDelayCalculate(){
// time calculation
}
So it is any kind of solution to take initialDelay value from my calculation method ,like :-
#Scheduled(initialDelay={initialDelayCalculate()}, fixedDelay=15000)
public void runTask(){
// some code
}
I need a small piece of code which will solve my problem. I don't want to go for any new implementation. So please anyone can explain for any solution that how I can take value on initialDelay at runtime?

I got my solution by just calling method name as below:-
#Scheduled(initialDelayString="#{initialDelayCalculate}", fixedDelay=15000)
public void runTask(){
// some code
}
And for initialDelayCalculate method I created as bean so it will call at first when JVM up. Like:-
#Bean
public long initialDelayCalculate(){
// time calculation
}
Now it working as per my requirement. :)

Related

What to use instead of ScheduledLockConfiguration Bean , in shedlock-spring 3.0?

I have a ScheduledLockConfiguration bean configuration.
#Bean
public ScheduledLockConfiguration taskScheduler(LockProvider lockProvider) {
return ScheduledLockConfigurationBuilder
.withLockProvider(lockProvider)
.withPoolSize(5)
.withDefaultLockAtMostFor(Duration.ofMinutes(5))
.build();
}
I just upgraded to shedlock-spring 3.0, and I don't know what to use instead of this Bean?
We can configure like below.
#Component
class TaskScheduler {
#Scheduled(cron = "0 0 10 * * ?")
#SchedulerLock(name = "TaskScheduler_scheduledTask", lockAtLeastForString = "PT5M", lockAtMostForString = "PT14M")
public void scheduledTask() {
// ...
}
}
#Scheduled will support corn format.
#SchedulerLock, the name parameter has to be unique and ClassName_methodName is typically enough to achieve that. We don't want more than one run of this method happening at the same time, and ShedLock uses the unique name to achieve that.
First, we've added lockAtLeastForString so that we can put some distance between method invocations. Using “PT5M” means that this method will hold the lock for 5 minutes, at a minimum. In other words, that means that this method can be run by ShedLock no more often than every five minutes.
Next, we added lockAtMostForString to specify how long the lock should be kept in case the executing node dies. Using “PT14M” means that it will be locked for no longer than 14 minutes.
In normal situations, ShedLock releases the lock directly after the task finishes. Now, really, we didn't have to do that because there is a default provided in #EnableSchedulerLock, but we've chosen to override that here.

What determines which event will run first?

I've created a test plugin as a science project to try and determine if 2 of the same event are called at the same time, which will be executed first.
public class TestPlugin extends JavaPlugin implements Listener {
public void onEnable() {
Bukkit.getPluginManager().registerEvents(this, this);
}
#EventHandler(priority = EventPriority.HIGHEST)
public void event1(PlayerInteractEvent e) {
System.out.println("event 1");
}
#EventHandler(priority = EventPriority.HIGHEST)
public void event2(PlayerInteractEvent e) {
System.out.println("event 2");
}
}
the output that the plugin produced are
[17:01:51 INFO]: event 2
[17:01:51 INFO]: event 1
if event1 is listed first in the class file, why is it that event2 is fired first?
This is very hard to determine. Since registerEvents leads to the function createRegisteredListeners of https://github.com/Bukkit/Bukkit/blob/master/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java
In there they use a HashSet to store the Methods of your Listener-class. So it is the pseudo random hash they give each method to store it that determines which event gets registered first.
I'm not sure but I guess that each and every time you register the events it's kind of random which gets registered first. This is why you should use different eventPriorities in order to determine which is called first.
If you really want to get deeper you have to tear apart the function createRegisteredListeners of the github-link I posted earlier in this reply. But I guess it woll never be totally certain because of the HashSet used in line 235 of the JavaPluginLoader.java:
methods = new HashSet<Method>(publicMethods.length, Float.MAX_VALUE);
As Methods are added to the set it never is certain in which position in the RAM they end up.
I hope I could help you with this post even though it doesn't really answer your original question.

Prometheus + Micrometer: how to record time intervals and success/failure rates

I am sending from a front-end client to a metrics-microservice a JSON with the following data:
{
totalTimeOnTheNetwork: number;
timeElasticsearch: number;
isSuccessful: boolean;
}
The metrics-microservice currently handles the data like this:
#AllArgsConstructor
#Service
public class ClientMetricsService {
#Autowired
MeterRegistry registry; // abstract class, SimpleMeterRegistry gets injected
public void metrics(final MetricsProperty metrics) {
final long networkTime = metrics.getTotalTime() - metrics.getElasticTime();
registry.timer(ELASTIC_TIME_LABEL).record(metrics.getElasticTime(), TimeUnit.MILLISECONDS);
registry.timer(TOTAL_TIME_LABEL).record(metrics.getTotalTime(), TimeUnit.MILLISECONDS);
registry.timer(NETWORK_TIME_LABEL).record(networkTime, TimeUnit.MILLISECONDS);
}
}
As you can see I make a new metric for each of the time intervals. I was wondering if I can put all the intervals into one metric? It would be great if I did not have to calculate network-time on the metrics-microservice but rather in Grafana.
Also, could I put a success/failure tag inside the registry.timer? I assume I need to use a timer.builder on every request then like this:
Timer timer = Timer
.builder("my.timer")
.description("a description of what this timer does") // optional
.tags("region", "test") // optional
.register(registry);
Is that a typical way to do it (eg create a new timer on every HTTP request and link it to the registry) or should the timer be derived from the MeterRegistry like in my current version?
Or would you use another metric for logging success/failure? In the future instead of a boolean, the metric might change to a http-error-code for example, so I am not sure how to implement it in a maintainable way
Timer timer = Timer
.builder("your-timer-name-here")
.tags("ResponseStatus", isSuccessful.toString, "ResponseCode", http-error-code.toString)
.register(registry);
timer.record(metrics.getTotalTime);
Should be working code that responds to your question but I have a feeling there is a misunderstanding. Why do you want everything in one metric?
Either way you can probably sort that out with tags. I do not know the capabilities on the Grafana end but it might be as simple as throwing the .getElasticTime info into another tag and sending it through.

Tracking response times of API calls in springboot

I'm looking to track the response times of API calls.
I then want to plot the response times of the calls( GET, PUT, POST DELETE) on a graph afterwards to compare the time differences.
This is what I'm currently doing to find the response time of a GET call but I'm not quite sure if it's right.
#RequestMapping(value="/Students", method = RequestMethod.GET)
public ResponseEntity<List<Students>> getStudents()
{
long beginTime = System.currentTimeMillis();
List<Students> students = (List<Students>) repository.findAll();
if(students.isEmpty())
{
return new ResponseEntity(HttpStatus.NO_CONTENT);
}
long responseTime = System.currentTimeMillis() - beginTime;
logger.info("Response time for the call was "+responseTime);
return new ResponseEntity(students, HttpStatus.OK);
}
I believe I am returning the response time before I actually return the data to the client which is the whole point of this but I wouldn't be able to put it after the return statement as it would be unreachable code.
Are there any better ways of trying to track the times of the calls?
You can use Around Advice of springboot and in the advice you can log the time. The way it works is once a call is made to the controller, the Around Advice intercepts it and starts a Timer(to record the time taken). From the advice we proceed to the main controller using jointPoint.proceed() method. Once the controller returns the value you can log the timer value. return the Object.
Here is the sample code:
in build.grale include
compile("org.aspectj:aspectjweaver:1.8.8")
Create a Component Class and put around #Aspect
#Component
#Aspect
public class advice{
#Around(("#annotation(logger)")
public Object loggerAspect(ProceedingJoinPoint joinPoint){
// start the Timer
Object object = jointPoint.proceed();
// log the timer value
return object;
}
}
Add annotation #logger in the controller method. you are good to go.
Hope this helps.
you can refer the link for full explanation.

springs scheduing jobs for a given

Spring MVC:
how to schedule jobs at a specific time in a day. schedule time differs every day. time when these jobs needs to be run are available in a database table. I was able to read the data from table but not sure how to schedule them in spring mvc. can someone help.
The Spring scheduler requires that you know the time of day at compile time so this is going to get a little weird. But if you want to be creative you can schedule a job at midnight to query the database for the exact time the task should run, sleep until that time, and then execute the task. Something like this:
public abstract class DailyTaskRunner {
// Execute the specific task here
protected abstract void executeTask();
// Query the database here
// Return the number of milliseconds from midnight til the task should start
protected abstract long getMillisTilTaskStart();
// Run at midnight every day
#Scheduled(cron="0 0 * * *")
public void scheduledTask() {
long sleepMillis = getMillisTilTaskStart();
try {
Thread.sleep(sleepMillis);
} catch(InterruptedException ex) {
// Handle error
}
executeTask();
}
}
You can extend this class once for every job.

Resources