Is there a way to make a #Scheduled function run on initialisation? - spring

I want my program to download from an API every day at 5 AM and at launch. Is there a way to achieve this?
#Scheduled(cron = "0 0 5 * * *", zone = "Europe/Oslo")
fun scheduledDL(link: String) {
val xmlString = downloader.download(link)
}

Related

Do not run scheduled tasks before application is ready

Is it possible to postpone scheduled task (or not execute it at all) until application is ready (i.e. until after ApplicationReadyEvent)?
#Scheduled(cron = "0 0 */4 * * *")
fun foo() {
...
}

Is it possible to generate UUID v1 with JMeter?

I read JMeter's manual and saw that there is __uuid() function for JMeter. It allows to generate UUID type 4 for JMeter tests. Is it possible to generate UUIDv1 in JMeter or maybe some plugin exists.
I would recommend taking the following steps:
Download Jug library (for example from here) and drop the .jar somewhere to JMeter Classpath
Restart JMeter to pick the .jar up
Once done you should be able to generate UUIDv1 using JSR223 Test Elements and Groovy language like:
import com.fasterxml.uuid.EthernetAddress
import com.fasterxml.uuid.Generators
import com.fasterxml.uuid.impl.TimeBasedGenerator
def addr = EthernetAddress.fromInterface()
def gen = Generators.timeBasedGenerator(addr)
def v1uuid = gen.generate()
log.info(v1uuid.toString())
Demo:
References:
Generating version 1 UUIDs
Groovy is the New Black
In jmeter you can add JSR 223 Sampler choose Java language and execute java code for UUID version 1:
String timeuuid = com.datastax.driver.core.utils.UUIDs.timeBased().toString();
And then add it to Jmeter variable:
vars.put("myUUID", timeuuid);
First, we'll generate the 64 least and most significant bits as long values:
private static long get64LeastSignificantBitsForVersion1() {
Random random = new Random();
long random63BitLong = random.nextLong() & 0x3FFFFFFFFFFFFFFFL;
long variant3BitFlag = 0x8000000000000000L;
return random63BitLong + variant3BitFlag;
}
private static long get64MostSignificantBitsForVersion1() {
LocalDateTime start = LocalDateTime.of(1582, 10, 15, 0, 0, 0);
Duration duration = Duration.between(start, LocalDateTime.now());
long seconds = duration.getSeconds();
long nanos = duration.getNano();
long timeForUuidIn100Nanos = seconds * 10000000 + nanos * 100;
long least12SignificatBitOfTime = (timeForUuidIn100Nanos & 0x000000000000FFFFL) >> 4;
long version = 1 << 12;
return
(timeForUuidIn100Nanos & 0xFFFFFFFFFFFF0000L) + version + least12SignificatBitOfTime;
}
We can then pass these two values to the constructor of the UUID:
public static UUID generateType1UUID() {
long most64SigBits = get64MostSignificantBitsForVersion1();
long least64SigBits = get64LeastSignificantBitsForVersion1();
return new UUID(most64SigBits, least64SigBits);
}

Java task scheduler run daily from start to end date

I've got a spring boot application in java with some scheduled tasks I need to run at midnight every day, from 15th June to 15th August
#Scheduled(cron = "0 0 0 15-30 5 ?") // Midnight every day from 15th June until end of month
public void sendReminderEmailsJune() {
doStuff();
}
#Scheduled(cron = "0 0 0 * 6 ?") // Every day in July
public void sendReminderEmailsJuly() {
doStuff();
}
#Scheduled(cron = "0 0 0 1-15 7 ?") // The first day in August to 15th August
public void sendRemindersEmailsAugust() {
doStuff();
}
Is there a better way to do this so I don't need 3 separate #Scheduled functions?
You could simply repeat these annotations, if you are on
Spring 4 / JDK 8
#Scheduled(cron = "0 0 12 * * ?")
#Scheduled(cron = "0 0 18 * * ?")
public void sendReminderEmails() {...}
else, JDK 6+
#Schedules({
#Scheduled(cron = "0 0 12 * * ?"),
#Scheduled(cron = "0 0 18 * * ?")})
public void sendReminderEmails() {...}

Why is my code throwing a binary operator error on the second + sign for the counterLabel.text variable?

This code throws the error "Binary Operator '+' cannot be applied to operands of type 'String' and 'Double'" on the second + sign of the counter Label.text variable. It only places the error here if I delete everything after the minutesLabel in counter Label.text the error goes away (This is written in Swift). Also sorry for any formatting confusion I'm a first time user.
func updateCounter(timer: NSTimer) {
let hours = floor(stopWatchTime / pow(60, 2))
let hoursInSeconds = hours * pow(60, 2)
let minutes = floor((stopWatchTime - hoursInSeconds) / 60)
let minutesInSeconds = minutes * 60
let seconds = floor((stopWatchTime - hoursInSeconds - minutesInSeconds) / 60)
let secondsInCentiseconds = seconds * 100
let centiseconds = stopWatchTime - hoursInSeconds - minutesInSeconds - secondsInCentiseconds
let hoursLabel = String(format: "%02.0f:", hours)
let minutesLabel = String(format: "%02.0f:", minutes)
let secondsLabel = String(format: "%02.0f:", seconds)
let centisecondsLabel = String(format: "%02.0f", centiseconds)
counterLabel.text = hoursLabel + minutesLabel + secondsLabel + centiseconds
stopWatchTime = stopWatchTime + 1
You forgot to add Label to the variable centiseconds,as it is not string and centisecondsLabel is a String.That is why you got the binary operator error.
Use the below code:
counterLabel.text = hoursLabel + minutesLabel + secondsLabel + centisecondsLabel
that will fix the problem!

How do I get the my timer to display the right number format using NSDateComponentFormatter

I've been trying to get my StopWatch to display the time in the following format
00h:00m:00s (suffixes are optional) using the NSDateComponentFormatter()
But I get is a continuous column of 0s.... any suggestions would be greatly appreciated.
func timerResults(){
++stopWatchCounter
let formatter = NSDateComponentsFormatter()
formatter.unitsStyle = .Abbreviated
formatter.allowedUnits = .CalendarUnitSecond | .CalendarUnitMinute | .CalendarUnitHour
let counterComponents = NSDateComponents()
counterComponents.hour = 00
counterComponents.minute = 00
counterComponents.second = 00
if let stopwatch = formatter.stringFromDateComponents(counterComponents){
// stopWatch.text = "\(stopwatch)"
stopWatch.text = "\(stopWatchCounter)"
println(stopwatch)
}}
Thats not possible using NSDateComponentsFormatter(). If you use .Positional you would get something like this 1:01:01. If you use .Short would be like this "1 hr, 1 min, 1 sec". You can use String(format:) and create your own format as a NStimeInterval extension as follow:
extension NSTimeInterval {
var time:String {
return String(format:"%02dh:%02dm:%02ds", Int((self/3600.0)%100),Int((self/60.0)%60), Int((self) % 60 ))
}
}
(359999.0).time // "99h:59m:59s"

Resources