Skip to content

Create custom Vert.x contextual data storage #2177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* Hibernate, Relational Persistence for Idiomatic Java
*
* SPDX-License-Identifier: Apache-2.0
* Copyright: Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.reactive.context.impl;

import java.util.concurrent.ConcurrentMap;

import io.vertx.core.impl.VertxBuilder;
import io.vertx.core.spi.VertxServiceProvider;
import io.vertx.core.spi.context.storage.ContextLocal;

/**
* SPI Implementation for {@link ContextLocal} storage.
*/
public class ContextualDataStorage implements VertxServiceProvider {

@SuppressWarnings("rawtypes")
static ContextLocal<ConcurrentMap> CONTEXTUAL_DATA_KEY = ContextLocal.registerLocal( ConcurrentMap.class );

@Override
public void init(VertxBuilder vertxBuilder) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
package org.hibernate.reactive.context.impl;

import java.lang.invoke.MethodHandles;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import io.vertx.core.Vertx;
import io.vertx.core.impl.ContextInternal;
import io.vertx.core.spi.context.storage.AccessMode;

import org.hibernate.reactive.context.Context;
import org.hibernate.reactive.logging.impl.Log;
Expand Down Expand Up @@ -39,7 +42,7 @@ public <T> void put(Key<T> key, T instance) {
final ContextInternal context = ContextInternal.current();
if ( context != null ) {
if ( trace ) LOG.tracef( "Putting key,value in context: [%1$s, %2$s]", key, instance );
context.localContextData().put( key, instance );
VertxContext.<T>contextualDataMap( context ).put( key, instance );
}
else {
if ( trace ) LOG.tracef( "Context is null for key,value: [%1$s, %2$s]", key, instance );
Expand All @@ -51,8 +54,7 @@ public <T> void put(Key<T> key, T instance) {
public <T> T get(Key<T> key) {
final ContextInternal context = ContextInternal.current();
if ( context != null ) {
@SuppressWarnings("unchecked")
T local = (T) context.localContextData().get( key );
T local = VertxContext.<T>contextualDataMap( context ).get( key );
if ( trace ) LOG.tracef( "Getting value %2$s from context for key %1$s", key, local );
return local;
}
Expand All @@ -66,7 +68,7 @@ public <T> T get(Key<T> key) {
public void remove(Key<?> key) {
final ContextInternal context = ContextInternal.current();
if ( context != null ) {
boolean removed = context.localContextData().remove( key ) != null;
boolean removed = contextualDataMap( context ).remove( key ) != null;
if ( trace ) LOG.tracef( "Key %s removed from context: %s", key, removed );
}
else {
Expand All @@ -93,4 +95,12 @@ public void execute(Runnable runnable) {
}
}

@SuppressWarnings({ "unchecked" })
private static <T> ConcurrentMap<Key<T>, T> contextualDataMap(ContextInternal vertxContext) {
return vertxContext.getLocal(
ContextualDataStorage.CONTEXTUAL_DATA_KEY,
AccessMode.CONCURRENT,
ConcurrentHashMap::new
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.hibernate.reactive.context.impl.ContextualDataStorage
Loading