Skip to content

Instantly share code, notes, and snippets.

View david-developer-play's full-sized avatar

David Bulant david-developer-play

View GitHub Profile
@david-developer-play
david-developer-play / gist:552ac9ce03dc8752464231e5eae95b59
Last active May 13, 2025 21:44
Envers auditing of changes in children
public List<ChildChangeAtRevision> getAllChildChangesWithMetadata(Long parentId) {
AuditReader reader = AuditReaderFactory.get(entityManager);
// 1. Get revisions from parent collection changes
List<Number> parentRevs = reader.createQuery()
.forRevisionsOfEntity(Parent.class, false, true)
.add(AuditEntity.id().eq(parentId))
.add(AuditEntity.property("children").hasChanged())
.getResultList();
In a scenario where an application is configured to receive notifications when a new file is uploaded to an Amazon S3 bucket, the notification typically comes in the form of an event message sent to a configured destination (SNS, SQS, Lambda, etc.).
1. S3 Event Notification Structure
When a new file is uploaded, S3 generates a JSON-formatted notification event. Here’s what a typical S3 event notification looks like:
{
"Records": [
{
"eventVersion": "2.1",
"eventSource": "aws:s3",
Notes about roles and rules:
Good morning! In PostgreSQL, a table can have only one owner. Ownership of a table (or any object) is always assigned to a single database user. PostgreSQL does not support multiple owners for a single table.
However, you can achieve similar results with permissions:
While there cannot be multiple owners, you can grant other users or roles permissions that closely mimic ownership capabilities by using privileges and roles.
Steps to Mimic Multi-Owner Access:
Create a Group Role: Create a group role and assign both users to this role. Then, you can grant full privileges on the table to this role.
1. Using the PostgreSQL Log (log_statement) Settings
PostgreSQL can log all queries executed by any user if logging is configured properly.
Step 1: Modify PostgreSQL configuration file (postgresql.conf)
To enable logging, you need to edit the postgresql.conf file and set the following parameters:
conf
1. Deployment (Kubernetes Native Object)
Origin: Native to Kubernetes.
Purpose: A Kubernetes resource for managing the deployment and scaling of applications. It helps ensure that the desired number of pods are running with the specified container images.
Features:
Declarative Updates: Supports declarative updates to application pods, meaning you can specify the desired state, and Kubernetes will gradually update the running pods to match that state.
Rolling Updates: By default, Kubernetes deployments support rolling updates, where the old pods are gradually replaced with new pods. This ensures minimal downtime.
Rollback: Supports automatic rollbacks to previous versions in case of a failure during the update process.
Testing asynchronous methods in JUnit can be done using several approaches, depending on the specific requirements of your test and the Java version or testing frameworks you're using. Here's a basic guide on how to test async methods in JUnit:
1. Using CompletableFuture and assertTrue
If your asynchronous method returns a CompletableFuture, you can use the get() method to block until the result is available and then assert the result.
java
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
In a Spring Boot application, if you're starting background tasks in a @Service method that's called from a REST controller, and the tasks are not executing, this is likely because the method is returning immediately, and the tasks may not have the chance to run. This usually happens if the background tasks are initiated in a non-blocking way, and the response is sent back to the client before the tasks actually start.
Here's how to correctly manage background tasks in a Spring Boot application to ensure they execute properly:
1. Using @Async with Spring Boot
Spring Boot provides a straightforward way to execute tasks asynchronously using the @Async annotation. When you annotate a method with @Async, it will run in a separate thread, allowing the main thread to return immediately while the task executes in the background.
Step 1: Enable Async Support
First, you need to enable async processing in your Spring Boot application by adding @EnableAsync to your main application class or any configuration class.
1. Spring Boot WebSocket Server
First, let's create the Spring Boot WebSocket server.
Step 1: Create a Spring Boot Project
Create a new Spring Boot project using Spring Initializr with the following dependencies:
Spring Web
Spring WebSocket
Lombok (optional, for cleaner code)
In Spring Boot, to process a web request asynchronously and send back the result once it is completed, you can leverage asynchronous request processing. This is achieved using @Async in combination with DeferredResult or CompletableFuture.
Here's a step-by-step guide on how to do it:
Using CompletableFuture with @Async
Step 1: Enable Async Support
Ensure that asynchronous support is enabled in your Spring Boot application.
java
First, you need to add the Hibernate Envers dependency to your project. If you are using Maven, you can add the following dependency in your pom.xml:
xml
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>5.6.14.Final</version> <!-- Replace with the latest version -->
</dependency>