Posts Tagged kotlin

About patterns for handling errors in Kafka applications

I have just read an interesting article about patterns for handling errors in Kafka. The article presents four patterns for handling errors in Kafka applications:

  • Stop on error
  • Dead Letter Queue
  • Retry topic and retry application
  • Maintain order of redirected events

I have already used the spring-kafka @RetryableTopic annotation where it handles the creation of the retry and dead letter topics automatically for Java and Kotlin backend applications. It is very useful and saves a lot of time in these scenarios.

How do you handle errors in Kafka applications? Drop you comments below about what you do in these situations.

Tags: , , , , , ,

About JUnit 5 parameterized tests

Interesting article about JUnit 5 parameterized tests using BDD (Behaviour-Driven Development) style. It shows an example of using @CsvSource as arguments source and a specific implementation of an argument converter. Have you ever used parameterized tests in your unit tests?

Tags: , , , ,

About the Kotlin programming language

Kotlin is a statically typed language which is fully interoperable with Java.

Recently my friend Andre showed me Kotlin‘s nice syntax and I considered giving it a try.

In the meantime, my friend Leonnardo sent me this nice link which helps migrating from Java to Kotlin easily.

Let’s compare some syntax from Java and Kotlin and see the differences. Suppose we have some employees and we want to group them by their departments.

In Java we create an Employee class, build some employees and use the Streams API to group them by their departments:

public class Employee {

    private final String name;

    private final String department;

    public Employee(final String name, final String department) {
        this.name = name;
        this.department = department;
    }

    public String getName() {
        return name;
    }

    public String getDepartment() {
        return department;
    }

    @Override
    public String toString() {
        return "Employee(name=" + name + ", department=" + department + ")";
    }
}

final Employee mark = new Employee("Mark", "Accounting");
final Employee john = new Employee("John", "Management");
final Employee smith = new Employee("Smith", "Administrative");
final Employee paul = new Employee("Paul", "Accounting");

final List employees = Arrays.asList(mark, john, smith, paul);

final Map> employeesByDepartment = employees
                .stream()
                .collect(Collectors.groupingBy(Employee::getDepartment));

In Kotlin we create a data class Employee, build some employees and use the collection built-in groupBy method to group them by their departments:

data class Employee(val name: String, val department: String)

val mark = Employee("Mark", "Accounting")
val john = Employee("John", "Management")
val smith = Employee("Smith", "Administrative")
val paul = Employee("Paul", "Accounting")

val employees = listOf(mark, john, smith, paul)

val employeesByDepartment = employees.groupBy { it.department }

As you can see, Kotlin has some syntactic sugar that makes it less verbose than Java.

If you haven’t considered trying Kotlin yet, I think it is worth giving it a try.

Tags: , , , , , ,