How to perform arithmentic operation in delayExpression in #Backoff (Spring-retry) - java

I want to take delay time in minutes from ENV variable. How to convert minutes into ms in expression string?
#Backoff(delayExpression = "${delay_in_minutes:2} * 60 * 1000",
maxDelayExpression = "${max_delay_in_minutes:10} * 60 * 1000",
multiplierExpression = "${multiplier:2.0}")

Use SEPL(Spring expression language) like below:
#Backoff(delayExpression = "#{${delay_in_minutes:2} * 60 * 1000}",
maxDelayExpression = "#{${max_delay_in_minutes:10} * 60 * 1000}",
multiplierExpression = "${multiplier:2.0}")

Related

How parametrize delay #BackOff spring boot, #Retry

I can't use my propertie for asign value to delay:
#Value("${delayReintentos}")
private long delay;
#Retryable(value = { SQLException.class }, maxAttempts = 3, backoff = #Backoff(delay = delay))
public String simpleRetry() throws SQLException {
counter++;
LOGGER.info("Billing Service Failed "+ counter);
throw new SQLException();
}
Java11, Spring boot
Use delayExpression instead
/**
* An expression evaluating to the canonical backoff period. Used as an initial value
* in the exponential case, and as a minimum value in the uniform case. Overrides
* {#link #delay()}.
* #return the initial or canonical backoff period in milliseconds.
* #since 1.2
*/
String delayExpression() default "";
See the readme: https://github.com/spring-projects/spring-retry#readme
You can use SpEL or property placeholders
#Backoff(delayExpression = "${my.delay}",
maxDelayExpression = "#integerFiveBean", multiplierExpression = "${onePointOne}")
Try this by wrapping the property name in braces.
#Retryable(value = SQLException.class, maxAttempts = 3, backoff = #Backoff(delayExpression = "${delay}")

Why is my Scheduled Task not running

I have a scheduled task which takes the hostname and other Calendar information from a JSP page. For the "HOUR_of_Day" input, I always add 12 and pass the total to Calender since it uses a 24 - hour format. What I did worked fine yesterday evening but it is not working this morning. It does not run anymore at the scheduled time. I certainly have done something wrong and need some direction. Below is my code:
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
public class ScheduledTask extends TimerTask {
String hostname = null;
//The default no - arg constructor
//=============================
public ScheduledTask(){
}
/**
* Another overloaded constructor
* that accepts an object
* #param scanHostObj
public ScheduledTask(ScanHost scanHostObj){
hostname = scanHostObj.getHostname();
}
/**
* Another overloaded constructor
* that accepts a string parameter
* #param nodename
*/
public ScheduledTask(String nodename){
hostname = nodename;
}
/**
* The run method that executes
* the scheduled task
*/
public void run() {
new ScanUtility().performHostScan(hostname);
}
public void executeScheduledScanJob(ScanHost hostObj, Scheduler schedulerObj){
/**
* Get the various schedule
* data, convert to appropriate data type
* and feed to calender class
*/
String nodename = hostObj.getHostname();
String dayOfScan = schedulerObj.getDayOfScan();
String hourOfScan = schedulerObj.getHourOfScan();
String minuteOfScan = schedulerObj.getMinuteofScan();
//Convert String values to integers
//===================================
final int THE_DAY = Integer.parseInt(dayOfScan);
final int THE_HOUR = Integer.parseInt(hourOfScan);
final int REAL_HOUR = THE_HOUR + 12;
final int THE_MINUTE = Integer.parseInt(minuteOfScan);
/**
* Feed these time values to the Calendar class.
* Since Calendar takes a 24 - hour format for hours
* it is better to add 12 to any integer value for
* hours to bring the real hourly format to the 24
* format required by the Calendar class
*/
Calendar scheduleCalendar = Calendar.getInstance();
scheduleCalendar.set(Calendar.DAY_OF_WEEK, THE_DAY);
scheduleCalendar.set(Calendar.HOUR_OF_DAY, REAL_HOUR);
scheduleCalendar.set(Calendar.MINUTE, THE_MINUTE);
/**
* Then Initialize the timer Object
* and pass in parameters to it method
* cancel out all tasks/jobs after running them
*/
Timer scheduledTimeObj = new Timer();
ScheduledTask scheduledTaskObj = new ScheduledTask(nodename);
scheduledTimeObj.schedule(scheduledTaskObj, scheduleCalendar.getTime());
scheduledTimeObj.cancel();
scheduledTaskObj.cancel();
}
}
You could try to replace
scheduleCalendar.set(Calendar.HOUR_OF_DAY, REAL_HOUR);
With this:
scheduleCalendar.set(Calendar.HOUR, THE_HOUR);
This one will take 12 hour format.
Additionally, you can set AM_PM to AM or PM. If you try to parse a PM date, use PM here.
scheduleCalendar.set(Calendar.AM_PM, Calendar.PM);

How to fire a trigger every 30 seconds from 2 pm to 11 pm in quartz using Java?

I am using this statement -
trigger2 = TriggerBuilder.newTrigger()
.withIdentity("abc", "group1")
.withSchedule(CronScheduleBuilder
.cronSchedule("0/30 0 14-23 * * ?"))
.build();
Somehow the trigger is fired at 2 pm, 2:00:30 pm and no more.
What is the problem?
The problem is that you have put 0 in the minute field.So it fires only at 2:00.
Try
trigger2 = TriggerBuilder
.newTrigger()
.withIdentity("abc", "group1")
.withSchedule(
CronScheduleBuilder.cronSchedule("0/30 * 14-23 * * ?"))
.build();
From quartz scheduler documentation I extracted the following example:
Job #1 is scheduled to run every 20 seconds
JobDetail job = new JobDetail("job1", "group1", SimpleJob.class);
CronTrigger trigger = new CronTrigger("trigger1", "group1", "job1", "group1", "0/20 * * * * ?");
sched.addJob(job, true);
Adapting to your situation it should go like this:
CronTrigger trigger = new CronTrigger("trigger1", "group1", "job1", "group1", "0/30 * 14-23 * * ?");

Frame to Timecode Conversion HTML5

I'm looking to convert a frame number (for a given sample rate) into time in HTML5.
eg:
sample_rate = 24fps
frame 226 => ? time
Please advise.
24 fps = 1 second (your sample rate = 1 second)
226 frames / 24 fps = 9,41 seconds
total / frame_rate = time in seconds
Example:
video.currentTime = 9.41;

Formatting file sizes in Java/JSTL [duplicate]

This question already has answers here:
How can I convert byte size into a human-readable format in Java?
(31 answers)
Closed 8 years ago.
I was wondering if anyone knew of a good way to format files sizes in Java/JSP/JSTL pages.
Is there a util class that with do this?
I've searched commons but found nothing. Any custom tags?
Does a library already exist for this?
Ideally I'd like it to behave like the -h switch on Unix's ls command
34 -> 34
795 -> 795
2646 -> 2.6K
2705 -> 2.7K
4096 -> 4.0K
13588 -> 14K
28282471 -> 27M
28533748 -> 28M
A quick google search returned me this from Appache hadoop project. Copying from there:
(Apache License, Version 2.0):
private static DecimalFormat oneDecimal = new DecimalFormat("0.0");
/**
* Given an integer, return a string that is in an approximate, but human
* readable format.
* It uses the bases 'k', 'm', and 'g' for 1024, 1024**2, and 1024**3.
* #param number the number to format
* #return a human readable form of the integer
*/
public static String humanReadableInt(long number) {
long absNumber = Math.abs(number);
double result = number;
String suffix = "";
if (absNumber < 1024) {
// nothing
} else if (absNumber < 1024 * 1024) {
result = number / 1024.0;
suffix = "k";
} else if (absNumber < 1024 * 1024 * 1024) {
result = number / (1024.0 * 1024);
suffix = "m";
} else {
result = number / (1024.0 * 1024 * 1024);
suffix = "g";
}
return oneDecimal.format(result) + suffix;
}
It uses 1K = 1024, but you can adapt this if you prefer. You also need to handle the <1024 case with a different DecimalFormat.
You can use the commons-io FileUtils.byteCountToDisplaySize methods. For a JSTL implementation you can add the following taglib function while having commons-io on your classpath:
<function>
<name>fileSize</name>
<function-class>org.apache.commons.io.FileUtils</function-class>
<function-signature>String byteCountToDisplaySize(long)</function-signature>
</function>
Now in your JSP you can do:
<%# taglib uri="/WEB-INF/FileSizeFormatter.tld" prefix="sz"%>
Some Size: ${sz:fileSize(1024)} <!-- 1 K -->
Some Size: ${sz:fileSize(10485760)} <!-- 10 MB -->

Categories