Skip to content

Instantly share code, notes, and snippets.

View MichalAdorno's full-sized avatar
🎯
Synchronizing

Michal Z. MichalAdorno

🎯
Synchronizing
  • Distributed
View GitHub Profile
@MichalAdorno
MichalAdorno / example.go
Created May 1, 2020 21:42 — forked from yanmhlv/example.go
JSONB in gorm
package main
import (
"database/sql/driver"
"encoding/json"
"github.com/jinzhu/gorm"
_ "github.com/lib/pq"
)
@MichalAdorno
MichalAdorno / CORS Filter
Created November 15, 2019 21:04 — forked from wildoctopus/CORS Filter
How to fix the CORS issue on backend side , java Springboot App (Handling Simple CORS requests)
//Contents from https://spring.io/understanding/CORS
In the simplest scenario, cross-origin communications starts with a client making a GET, POST, or HEAD request against a resource on the server.
In this scenario, the content type of a POST request is limited to application/x-www-form-urlencoded, multipart/form-data, or text/plain. The request includes an Origin header that indicates the origin of the client code.
The server will consider the request's Origin and either allow or disallow the request. If the server allows the request, then it will respond with the requested resource and an Access-Control-Allow-Origin header in the response. This header will indicate to the client which client origins will be allowed to access the resource. Assuming that the Access-Control-Allow-Origin header matches the request's Origin, the browser will allow the request.
On the other hand, if Access-Control-Allow-Origin is missing in the response or if it doesn't match the request's Origin, the browser will disallow th
@MichalAdorno
MichalAdorno / docker-compose.yml
Created November 1, 2019 15:31 — forked from zhunik/docker-compose.yml
docker-compose Postgres health-check
version: "3"
services:
postgress:
....
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
timeout: 5s
retries: 5
app:
@MichalAdorno
MichalAdorno / redis_cheatsheet.bash
Created May 5, 2019 09:10 — forked from LeCoupa/redis_cheatsheet.bash
Redis Cheatsheet - Basic Commands You Must Know --> UPDATED VERSION --> https://github.com/LeCoupa/awesome-cheatsheets
# Redis Cheatsheet
# All the commands you need to know
redis-server /path/redis.conf # start redis with the related configuration file
redis-cli # opens a redis prompt
# Strings.
@MichalAdorno
MichalAdorno / MockAndSpy.java
Created January 3, 2019 14:04
@SpyBean vs @MockBean /spies vs mocks in programmatic testing
/*
@MockBean //or Mockito's @Mock
- it mocks the object and all its methods with do nothing and their result value will be null,
- use for example: when(...) methods to create mocked method behaviour
- use when you want to completely get rid of the object's normal behaviour
- caution: unmocked methods won't work -> can result in a RuntimeException during tests (@SpyBean is a remedy here apart from wider mocking)
@SpyBean //or Mockito's @Spy
- an object will behave like an @Autowired object
- all its methods will actually works, but we can define some custom behavior for its methods
- use doReturn(...) / doNothing(...) to add custom (mocked) method behaviour
@MichalAdorno
MichalAdorno / DeferredResultAndCompletableFuture.java
Last active January 17, 2024 18:59
Combining CompletableFuture API with Spring's DeferredResult
//--------------------------------------------------------
//Service that we want to execute asynchroneously - consists of the following method:
public String execute() {
try {
Thread.sleep(5000);
logger.info("Slow task executed");
return "Task finished";
} catch (InterruptedException e) {
throw new RuntimeException();
}
@MichalAdorno
MichalAdorno / DeferredResultInSpring.java
Last active July 17, 2020 06:21
Using the DeferredResult Class in Spring (Non-blocking Java Servlet Controller Methods in Spring)
GetMapping("/async-deferredresult")
public DeferredResult<ResponseEntity<?>> handleReqDefResult(Model model) {
LOG.info("Received async-deferredresult request");
DeferredResult<ResponseEntity<?>> output = new DeferredResult<>(); //just a declaration
ForkJoinPool.commonPool().submit(() -> { //we pass a service method to an ExecutorService / thread pool
LOG.info("Processing in separate thread");
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
@MichalAdorno
MichalAdorno / ClassLoaderLeakExample.java
Created November 9, 2018 19:47 — forked from dpryden/ClassLoaderLeakExample.java
Example of a ClassLoader leak in Java
import java.io.IOException;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
/**
* Example demonstrating a ClassLoader leak.
*
* <p>To see it in action, copy this file to a temp directory somewhere,
@MichalAdorno
MichalAdorno / pom.xml
Created November 9, 2018 10:04 — forked from Rocketeer007/pom.xml
Surefire, Failsafe and Jacoco configuration for build
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>default-prepare-agent</id>