Skip to content

Instantly share code, notes, and snippets.

View soudmaijer's full-sized avatar

Stephan Oudmaijer soudmaijer

View GitHub Profile
public class CallFoo {
public static void main(String[] args) {
Utils.foo();
}
}
public class Person {
private final String name;
private final int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
import brave.Tracer
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
@Aspect
@Component
class TracingAspect {
@soudmaijer
soudmaijer / TestProducer.kt
Created January 31, 2020 14:28
Kotlin Spring Boot Kafka producer example
package nl.sourcelabs.kafka.producer
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.CommandLineRunner
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.kafka.core.KafkaTemplate
@SpringBootApplication
@soudmaijer
soudmaijer / pom.xml
Created December 12, 2019 07:29
Maven Kotlin Compiler
<project>
<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
@soudmaijer
soudmaijer / pom.xml
Last active December 12, 2019 07:29
Maven Java Compiler
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>9</source>
@soudmaijer
soudmaijer / LockManager.kt
Created April 17, 2019 07:27
Postgres transaction-level advisory lock implementation that uses Spring JDBC
import org.slf4j.LoggerFactory
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Propagation
import org.springframework.transaction.annotation.Transactional
import java.time.Duration
interface LockManager {
fun <T> tryWithLock(key: Long, timeout: Duration, function: () -> T): T
}
@soudmaijer
soudmaijer / GiftCardService.kt
Last active September 26, 2018 11:21
Using Kotlin Coroutines to async fetch data from an external service
import kotlinx.coroutines.experimental.GlobalScope
import kotlinx.coroutines.experimental.async
import kotlinx.coroutines.experimental.runBlocking
import java.util.*
data class GiftCard(val id: Int, val code: String)
class GiftCardService {
/**
val orderOrElse: Order = orderById?.apply {
// do stuff with order
} ?: Order() // orElse with default
val orderOrElseThrow: Order = orderById?.apply {
// do stuff with order
} ?: throw RuntimeException("orElseThrow") // orElseThrow
val orderId: Long? = orderById?.let {
// do stuff with order
fun doStuffWithOrder(id: Long) {
val orderById: Order? = orderRepository.findOrderById(id)
orderById?.run {
// do stuff with order
}
orderById.id // Compiler error: Only safe ?. or non-null asserted calls are allowed on a nullable receiver of type Order?
}