[Spring] Schedule tasks

Scheduling execution of tasks in Spring is quite easy and requires just a couple annotations:

  • Annotate the application class with @EnableScheduling
  • Provide an @Component annotated class which includes the methods to be scheduled, each annotated with @Schedule 
The schedule itself can be passed as a property. 

[Maven] Generate Avro sources at compile time

If you are using Apache Avro in your project, you might want to automatize the sources generation step so that at every compile, the target classes are built from the Avro schema.

In Maven, this can be done with the avro-maven-plugin with a configuration as such:

     Use this for testing purposes only if you wish to manually generate the classes from Avro sources:  
     java -jar avro-tools.jar compile schema SCHEMA_FILE.avsc TARGET_DIRECTORY  
     See: https://avro.apache.org/docs/current/gettingstartedjava.html  

Remember that it is also possible to use avro-tools to manually generate sources from a schema.

[Java] Date and Time utilities

Since java 8 introduced the java.time package, a lot of useful objects and methods are now available without the need to import external libraries.

Here are some operations that might come in handy when dealing with LocalDate and LocalDateTime objects:

  • Convert String timezone (in format {Region}/{City} eg: "Europe/Rome") to ZoneOffset:
 public static ZoneOffset stringToZoneOffset(final String timezone){  
   final ZoneId zone = ZoneId.of(timezone);  
   return zone.getRules().getOffset(Instant.now());  

  • Retrieve the hours offset between UTC and a given timezone:
 public static int hoursOffsetFromUTC(final String timezone){  
   return ZoneId.of(timezone)  

  • Convert given time (in format HH:mm:ss) with specific timezone to UTC time:
 public static OffsetTime timeAndTimezoneAtUTC(final String time, final String timezone){  
   return LocalTime.parse(time, DateTimeFormatter.ISO_LOCAL_TIME)  

  • Convert date to epoch seconds (if we had a LocalDateTime, we wouldn't need to use atStartOfDay):
 public static Long localDateToEpoch(final LocalDate localDate){  
   return localDate.atStartOfDay(ZoneOffset.UTC).toEpochSecond();  


[Linux] Text handling for counting and replacing words

Here is a list of handy commands for VI and SED to count and replace text in a file. For both editors the escape character is /:


- count total words:


- count occurrences of particular word:



- copy all text from marker until end of file:

 sed -n -n '/MARKER/,$p' in >> out  

- replace all occurrences of SOURCE to TARGET with optional characters before SOURCE:

 sed -E 's/(OPTIONAL)?SOURCE/\1TARGET/g' in >> out