Skip to content

Instantly share code, notes, and snippets.

@mike-seger
mike-seger / KafkaConfig.java
Last active May 26, 2024 00:38
Dual Broker
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
@mike-seger
mike-seger / Config.java
Last active May 25, 2024 10:39
getRandomValueForType
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;
import io.confluent.kafka.serializers.KafkaAvroSerializer;
import java.util.HashMap;
import java.util.Map;

In Azure, networking can be configured at various levels, including the service plan level and the subscription level. Here’s how it breaks down:

Subscription Level:

Networking configurations at the subscription level affect all resources within the subscription. This includes setting up Virtual Networks (VNets), Network Security Groups (NSGs), and configuring Azure Firewall rules. These configurations create a networking framework that individual resources and service plans can utilize.

Service Plan Level (e.g., App Service Plan):

  • While the overarching network infrastructure is defined at the subscription level, specific configurations related to how services interact with the network can be done at the service plan level.
@mike-seger
mike-seger / spring.md
Last active January 25, 2024 06:52
spring application yamls

In a Spring Boot application with a @KafkaListener, handling failures, especially when they are random and expected to happen in a certain percentage of cases (like 20%), requires a thoughtful approach to ensure system robustness and message processing reliability. Here are some strategies to handle such situations effectively:

  1. Acknowledgment and Retries: You can manage acknowledgments manually, giving you control over when a message is considered successfully processed. If processing fails, you can retry a certain number of times before giving up.
@KafkaListener(topics = "yourTopic", groupId = "yourGroup")
public void listen(ConsumerRecord<?, ?> record, Acknowledgment acknowledgment) {
    try {
 // Process the message
@mike-seger
mike-seger / TimestampConverter.java
Last active January 5, 2024 17:33
Fetch and log url contents
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class TimestampConverter {
public static void main(String[] args) {
// Sample input. Replace this with reading from a file or other input source
String input = "1609459200000\t1.4567\n1704475839002\t1.4578\n";
Scanner scanner = new Scanner(input);
@mike-seger
mike-seger / comparison.md
Created December 3, 2023 09:36
JMESPath vs JSON

Renaming keys

Renaming keys works with both solutions. Although for Jmespath you need to repeat all attributes the object should keep. If one is missing it won't be added to the output. If some objects have additional values they are set to null.

Data:

[
  {
    "key": 1,
    "name": "myname1",
@mike-seger
mike-seger / snippet.kt
Last active November 16, 2023 18:03
snippets
val restTemplate = RestTemplate()
val relativeUri = "/other-endpoint" // Replace with your relative URI
// Construct the full URI
val fullUri = "${request.scheme}://${request.serverName}:${request.serverPort}$relativeUri"
return restTemplate.getForObject(fullUri, String::class.java) ?: "No content"
@mike-seger
mike-seger / GenericSettings.kt
Last active September 21, 2023 07:01
snippets
class Setting(var key: Key, var value: String)
enum class Key(val initialValue: Any) {
Value1(123), Value2(123.0), Value3("ABC"), Value4(1)
}
inline fun <reified T> getSetting(settings: List<Setting>, key: Key): T {
val setting = settings.find { it.key == key }
val value = setting?.value ?: key.initialValue.toString()
@mike-seger
mike-seger / Interpolate.kt
Created September 20, 2023 16:49
Interpolate incomplete value list
package com.net128.test.entitysort.util
class Interpolate {
fun interpolate(list: List<Double?>): List<Double?> {
val nonNullIndices = list.withIndex().filter { it.value != null }.map { it.index }.toList()
val nonNullValues = list.filterNotNull()
// Only one non-null number case
if (nonNullIndices.size == 1) {
return List(list.size) { nonNullValues[0] + (it - nonNullIndices[0]) * 0.1 }
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import javax.persistence.Table
import javax.persistence.EntityManagerFactory
import javax.persistence.metamodel.EntityType
import kotlin.reflect.KClass
import kotlin.reflect.full.findAnnotation
@Component
class EntityScanner(@Autowired private val entityManagerFactory: EntityManagerFactory) {