![JAR search and dependency download from the Maven repository](/logo.png)
tech.simter.Context Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of simter-context Show documentation
Show all versions of simter-context Show documentation
System Context Interface and holder
package tech.simter;
import java.util.HashMap;
import java.util.Map;
/**
* A simple and powerful utils class for share data during the same thread lifecycle
* This tools use thread-local variables to make each thread has its own initialized share data.
* You don't need to transfer context data through method arguments, just use `Context.get(key)` to get
* its value inside the method.
*
For example:
*
* // set share data anywhere
* Context.set("userId", new Long(0));
* Context.set("userName", "RJ");
*
* void someMethodInOtherClass(){
* // get shared data by key anywhere
* Long userId = Context.get("userId");
* String userName = Context.get("userName");
*
* // or get all shared data
* Map<String, Object> all = Context.get();
* ...
* }
*
* // delete shared data anywhere
* Context.remove("userId");
*
* You can see that it just like to get a static constant value. But you need to know the difference.
* A static constant always has the same value event in different thread.
* The context data is isolated between each thread.
*
* @author RJ
*/
public class Context {
// containing the thread-local share data
private static ThreadLocal
© 2015 - 2025 Weber Informatics LLC | Privacy Policy