Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ThreadLocal on Google App Engine (GAE)

I would like to make some request-wide data available in my app engine application.

Examples:

  • The URL for which the request was made.
  • Authentication information.

I see that ThreadLocal is on GAE's JRE whitelist.

Is ThreadLocala good and safe way to make this information available? Are there alternative / better / more accepted ways?

like image 728
Chris Dutrow Avatar asked Jul 04 '10 20:07

Chris Dutrow


People also ask

What is ThreadLocal class how and why you should use it?

The ThreadLocal class is used to create thread local variables which can only be read and written by the same thread. For example, if two threads are accessing code having reference to same threadLocal variable then each thread will not see any modification to threadLocal variable done by other thread.

What is valid about ThreadLocal?

Java ThreadLocal class provides thread-local variables. It enables you to create variables that can only be read and write by the same thread. If two threads are executing the same code and that code has a reference to a ThreadLocal variable then the two threads can't see the local variable of each other.

What is the difference between APP engine standard and flexible?

The standard environment can scale from zero instances up to thousands very quickly. In contrast, the flexible environment must have at least one instance running for each active version and can take longer to scale up in response to traffic. Standard environment uses a custom-designed autoscaling algorithm.

What is App Engine flexible environment?

App Engine allows developers to focus on what they do best: writing code. Based on Compute Engine, the App Engine flexible environment automatically scales your app up and down while also balancing the load.


1 Answers

Yes, it is an accepted practice to store these things in a ThreadLocal. However, a more preferable approach is to pass these values around (as method arguments) wherever they are needed, instead of reaching for them. It's more preferable, because it's more testable at least.

like image 144
Bozho Avatar answered Sep 23 '22 06:09

Bozho