Skip to content

Instantly share code, notes, and snippets.

@pavelfomin
pavelfomin / CloudStorageControllerSpec.java
Last active April 19, 2024 15:09
ControllerExceptionHandler that uses existing org.springframework.http.ProblemDetail for custom exception handling
import com.google.api.client.http.HttpHeaders
import com.google.api.client.http.HttpResponseException
import com.google.cloud.storage.StorageException
import org.spockframework.spring.SpringBean
import org.springframework.test.web.servlet.ResultActions
import static org.hamcrest.Matchers.is
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content

Things I don't like about java records

  • record is final so it
    • can't be used in inheritance
    • can't be mocked in unit tests
  • doesn't support builder pattern and as the number of fields grows Lombok's @Builder becomes preferable
    • although it seems to be possible to use Lombok's @Builder with java record

I don't see any major advantages with using record other than its immutable nature.

Java record

@pavelfomin
pavelfomin / git-merge-vs-squash.md
Last active February 14, 2024 16:50
Advantages of using merge instead of squash in Git

Advantages of using merge instead of squash for PRs:

  • if there is a chain of PRs, with PR1 (feature1 -> main) and PR2 (feature2 -> feature1) then when PR1 is merged, PR2's base is automatically adjusted to main (GitHub). I do not believe this will happen with a squash.
  • ability to check if the commits from other branches have been merged to main (using git branch -r --no-merge) which is especially useful for release/* deployment strategies if a file was first renamed and then modified in more than one commit (recommended approach for git to retain history), if these commits are merged then the history is retained. Otherwise, if squashed, git might consider the rename and update as old file deletion and new file addition (depending of the percentage of the changes).
  • If there are commits that logically address separate issues it's easier to understand such changes as individual commits in history rather than squashed result

However, there is definitely a case for squashing PRs when individual com

@pavelfomin
pavelfomin / Kenmore-control-board-8524212.md
Last active February 14, 2024 16:44
Kenmore electric range control board 8524212 replacement

How to replace Kenmore electric range control board 8524212 (range model 665.95142300)

It took me awhile to figure how to remove the control board to either replace or repair it. Someone else showed me how.

kenmore-range-control-board-8524212

To remove the control board:

  • disconnect the clock / buttons panel connector image
  • disconnect all of the wires from the control board (mark them before disconnecting)
  • if the board needs to be replaced there is no choice obviously
@pavelfomin
pavelfomin / youtube-dl-ffmpeg.md
Last active February 28, 2024 00:35
Download portion of video / audio with youtube-dl / ffmpeg

Download the audio only

  • utlink='https://www.youtube.com/watch?v=bn9F19Hi1Lk'
  • get all video and audio links for the video
    • youtube-dl -F "$utlink"
    • pick the best mp4a audio only option (e.g. 140)
  • get a download link
    • audio=$(youtube-dl -f 140 -g "$utlink")
  • use audio download link in ffmpeg to download portion of the audio
    • download first 5 mins of the audio
  • ffmpeg -i "$audio" -t 300 -c:a libmp3lame waves300.mp3
@pavelfomin
pavelfomin / producer.java
Last active January 11, 2024 21:03
Spring Kafka Producer using KafkaTemplate, CompletableFuture and KafkaTemplate.flush()
@Getter
private final KafkaTemplate<String, T> kafkaTemplate;
public CompletableFuture<SendResult<String, T>> sendMessage(String key, T message, Integer partition) {
try {
return getKafkaTemplate().send(getTopicName(), partition, key, message);
} catch (Exception e) {
throw new RuntimeException("Failed to send a message to topic: " + getTopicName(), e);
}
@pavelfomin
pavelfomin / ObjectMapperConfiguration.java
Created December 29, 2023 15:27
ObjectMapper MixIn configuration for Avro classes
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import org.apache.avro.generic.GenericContainer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
@Configuration
public class ObjectMapperConfiguration {
@pavelfomin
pavelfomin / AvroDataConsumer.java
Last active December 8, 2023 15:50
Spring Kafka configuration for consuming JSON and Avro formats using @KafkaListener.
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.messaging.MessageHeaders;
@RequiredArgsConstructor
@Slf4j
public class AvroDataConsumer {
@pavelfomin
pavelfomin / metrics.md
Last active January 16, 2024 23:12
How to use @timed annotation