Skip to content

Instantly share code, notes, and snippets.

View rponte's full-sized avatar
🏠
Working from home

Rafael Ponte rponte

🏠
Working from home
View GitHub Profile
@rponte
rponte / 1-CustomerCreatedEventSqsConsumer.java
Last active April 15, 2024 17:21
Spring Boot: Testing a @SqsListener with TestContainers and LocalStack
package br.com.zup.edu.app2.xxx.samples.aws.sqs;
import br.com.zup.edu.app2.xxx.samples.aws.sqs.model.Customer;
import br.com.zup.edu.app2.xxx.samples.aws.sqs.model.CustomerRepository;
import io.awspring.cloud.messaging.listener.SqsMessageDeletionPolicy;
import io.awspring.cloud.messaging.listener.annotation.SqsListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.messaging.handler.annotation.Header;
@rponte
rponte / 1a-application-test.yaml
Last active August 10, 2023 10:59
Spring Boot: how to test @SqsListener via integration tests
##
# Application configuration
##
samples.aws.sqs.consumer-queue: customersCreatedQueue
##
# Spring Cloud AWS
##
cloud:
aws:
@rponte
rponte / LockManager.kt
Created October 4, 2022 20:25 — forked from rafaelpontezup/LockManager.kt
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
}
@rponte
rponte / NaiveJavaSynchronizedATMService.java
Last active December 4, 2023 15:07
Spring: You should not mix @transactional with synchronized
@Service
public class NaiveJavaSynchronizedATMService {
@Autowired
private AccountRepository repository;
@Transactional
public synchronized void withdraw(Long accountId, double amount) {
Account account = repository.findById(accountId).orElseThrow(() -> {
@rponte
rponte / reset_and_clean_rep.sh
Created September 27, 2022 18:45
Git: reseting and cleaning a git repository
git reset --hard HEAD ; git clean -fd
@rponte
rponte / buying_new_tickets.sql
Last active August 5, 2022 16:45
PostgreSQL: Playing a little bit with race conditions, SQL and isolation levels
--
-- Trying to buy a new ticket for an event
--
BEGIN;
UPDATE events e
SET updated_at = now()
WHERE e.id = :event_id
AND e.max_tickets > (SELECT count(*) -- (does this logic work with READ_COMMITTED??)
FROM tickets t
WHERE t.event_id = e.id);
@rponte
rponte / AddsAccessTokenHeaderMockMvcConfigurer.java
Created May 20, 2022 13:03
Spring Security: Adding Access Token in all requests via MockMvcBuilderCustomizer
package br.com.zup.edu.livraria;
import org.springframework.boot.test.autoconfigure.web.servlet.MockMvcBuilderCustomizer;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.ConfigurableMockMvcBuilder;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.jwt;
@rponte
rponte / ResourceServerConfig.java
Last active February 7, 2024 02:39
Spring Security: example of OAuth2 Resource Server configuration (Spring Boot v2.6.7)
package br.com.zup.edu.minhasfigurinhas;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import static org.springframework.http.HttpMethod.GET;
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.security.config.http.SessionCreationPolicy.STATELESS;
@rponte
rponte / Album.java
Created April 12, 2022 21:10
JPA and Hibernate: Bean validation error when merging a transient entity
@Entity
public class Album {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Size(min = 1)
@OneToMany(cascade = {
@rponte
rponte / reset-network.sh
Last active February 8, 2022 19:08
WSL2: resetting network (WSL2 DNS issue)
##
# 1. run theses commands on PowerShell as admin
# 2. restart windows
##
wsl --shutdown
netsh winsock reset
netsh int ip reset all
netsh winhttp reset proxy
ipconfig /flushdns