New candidate JEP: 506: Scoped Values

Josiah Noel josiahnoel at gmail.com
Tue Apr 15 16:32:27 UTC 2025


Alright then,

The project is an avaje + helidon 4 API that retrieves credit card info
from a DocumentDB collection. It runs on AWS Fargate with 3-15 containers
depending on scaling. It's got 1024 CPU units/memory serving an average of
2-5k requests per second with a max of 14k.

I have a utility class like this

public class MDCUtil {


  static final ScopedValue<Map<String, String>> MDC_SV = ScopedValue.
newInstance();


  public static void createScopedMDC(Map<String, String> mdc, Runnable
runnable) {

    ScopedValue.where(MDC_SV, mdc).run(runnable);

  }

}

That I call inside a request filter

final Map<String, String> context = new HashMap<>();

// extract relevant data and place it in the context


// call controller handler

MDCUtil.createScopedMDC(context, chain::proceed);

Inside the custom log encoder, I have the following to write the values
into the log message:

if (MDCUtil.MDC_SV.isBound()) {

  MDCUtil.MDC_SV

    .get()

    .forEach(

      (k, v) -> {

        writer.name(k);

        writer.value(v);

      });

}

On Tue, Apr 15, 2025 at 11:33 AM David Alayachew <davidalayachew at gmail.com>
wrote:

> Folks, saying that you tried the feature in earnest on a non-trivial
> project, and everything went as expected is VALUABLE feedback. Therefore,
> don't hesitate to share your experience!
>
>
> Only thing I'd change is that you give a brief overview of your project,
> and an example of how specifically you used the JEP. That is the ideal form
> of feedback, even if the feedback boils down to "went as expected".
>
> On Tue, Apr 15, 2025, 11:10 AM Davor Hrg <hrgdavor at gmail.com> wrote:
>
>> That is exactly the use-case I was going for in my tests, but have not
>> been able to test it in a high traffic environment.
>>
>> Api itself is fine, nothing to report.
>>
>>
>>
>>
>> On Tue, Apr 15, 2025 at 4:51 PM Josiah Noel <josiahnoel at gmail.com> wrote:
>>
>>> I suppose I'm just not sure what feedback to give beyond it works like I
>>> thought it would. I've been using it in one of our high traffic APIs since
>>> java 21. Currently the service is on JDK 24 and I haven't noticed any
>>> problems with the preceding java versions either.
>>>
>>> I use it to store request data that would otherwise be put in an MDC
>>> context. I then have a custom logback encoder which extracts the values and
>>> put them into the json log statement.
>>>
>>> On Tue, Apr 15, 2025, 10:16 AM David Alayachew <davidalayachew at gmail.com>
>>> wrote:
>>>
>>>> Correct
>>>>
>>>> On Tue, Apr 15, 2025, 8:59 AM Davor Hrg <hrgdavor at gmail.com> wrote:
>>>>
>>>>> Am I reading this correctly?
>>>>>
>>>>> Does this mean Scoped Values will be fully available (not hidden
>>>>> behind the preview flag) in Java 25 ?
>>>>>
>>>>>
>>>>>
>>>>> On Mon, Apr 14, 2025 at 9:43 PM Mark Reinhold <
>>>>> mark.reinhold at oracle.com> wrote:
>>>>>
>>>>>> https://openjdk.org/jeps/506
>>>>>>
>>>>>>   Summary: Introduce scoped values, which enable a method to share
>>>>>>   immutable data both with its callees within a thread, and with child
>>>>>>   threads.  Scoped values are easier to reason about than thread-local
>>>>>>   variables.  They also have lower space and time costs, especially
>>>>>> when
>>>>>>   used together with virtual threads (JEP 444) and structured
>>>>>> concurrency
>>>>>>   (JEP 505).
>>>>>>
>>>>>> - Mark
>>>>>
>>>>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/loom-dev/attachments/20250415/7915cef0/attachment-0001.htm>


More information about the loom-dev mailing list