Thread Local Context Pattern

aka Thread Local Global Variables


class RequestContextHolder {
	private static ThreadLocal threadLocal = new ThreadLocal();
	public static Map get() {
		return (Map)threadLocal.get();
	}
	public static void set(Map context) {
		threadLocal.set(context);
	}
	public static void clearContext() {
		threadLocal.set(null); }
	}
}

class FrontController
{
	public void handleReqeuest(HttpRequst request.....) {
		try {
			Map requestContext = buildRequestContext(request);
			RequestContextHolder.set(requestContext); .....
			Response response = handleRequest(request);
			processResponse(response);
		} finally {
			RequestContextHolder.clearContext();
		}
	}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s