This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SpringBootTest | |
@EmbeddedKafka(ports = [9092]) | |
class KafkaExampleApplicationTests( | |
@Autowired val embeddedKafkaBroker: EmbeddedKafkaBroker, | |
@Autowired val adminClient: AdminClient, | |
@Autowired val topicPurger: TopicPurger | |
) { | |
private val topicName = "test.topic" | |
@Test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component | |
class TopicPurger(private val adminClient: AdminClient) { | |
fun purge(topicName: String) { | |
val topicDescription = adminClient.describeTopics(listOf(topicName)).all().get()[topicName] | |
val partitionSize = topicDescription?.partitions()?.size | |
val recordsToDelete = (0..partitionSize!!).associate { partitionIndex -> | |
TopicPartition(topicName, partitionIndex) to RecordsToDelete.beforeOffset(-1) | |
} | |
adminClient.deleteRecords(recordsToDelete) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
class AppConfig { | |
@Value(value = "\${kafka.bootstrapAddress}") | |
lateinit var bootstrapAddress: String | |
@Bean | |
fun adminClient(): AdminClient { | |
return AdminClient.create(mapOf( | |
AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG to bootstrapAddress) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implementation("org.springframework.boot:spring-boot-starter") | |
implementation("org.jetbrains.kotlin:kotlin-reflect") | |
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") | |
implementation("org.springframework.kafka:spring-kafka") | |
testImplementation("org.springframework.boot:spring-boot-starter-test") | |
testImplementation("org.springframework.kafka:spring-kafka-test") | |
testImplementation("org.awaitility:awaitility-kotlin:4.1.1") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CityRetrieverWithOptional { | |
private final PersonService personService; | |
public Optional<String> retrieveCity(String name) { | |
return Optional.ofNullable(personService.getByName(name)) | |
.map(Person::getAddress) | |
.map(Address::getCity); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CityRetriever { | |
private final PersonService personService; | |
public String retrieveCity(String name) { | |
Person person = personService.getByName(name); | |
if (person != null) { | |
Address address = person.getAddress(); | |
if (address != null) { | |
if (address.getCity() != null) { | |
return address.getCity(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface PersonService { | |
Person getByName(String name); | |
} | |
public class Person { | |
private final String name; | |
private final int age; | |
private final Address address; | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class App { | |
private static final Logger logger = LoggerFactory.getLogger(App.class.getName()); | |
public static void main(String[] args) { | |
invoke(new DefaultService()); | |
logger.debug("----------------------\n"); | |
invoke(new LazyInitService()); | |
logger.debug("----------------------\n"); | |
invoke(LazyInitWithProxyServiceFactory.createService()); | |
logger.debug("----------------------\n"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LazyInitWithProxyThreadSafeServiceFactory { | |
public static Service createService() { | |
return Reflection.newProxy(Service.class, new ThreadSafeLazyInitInvocationHandler()); | |
} | |
private static class ThreadSafeLazyInitInvocationHandler extends AbstractInvocationHandler { | |
private static final LazyInitializer<Service> initializer = new LazyInitializer<Service>() { | |
@Override | |
protected Service initialize() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LazyInitWithProxyServiceFactory { | |
public static Service createService() { | |
return Reflection.newProxy(Service.class, new LazyInitInvocationHandler()); | |
} | |
private static class LazyInitInvocationHandler extends AbstractInvocationHandler { | |
private Service service; | |
@Override |
NewerOlder