Skip to content

ThreadLocalAccessor implementation for RequestAttributes and LocaleContext #32112

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

Closed
ttddyy opened this issue Jan 25, 2024 · 5 comments
Closed
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: superseded An issue that has been superseded by another type: enhancement A general enhancement

Comments

@ttddyy
Copy link
Contributor

ttddyy commented Jan 25, 2024

In Spring MVC, objects like RequestAttributes and LocaleContext are stored in threadlocal(RequestContextHolder, LocaleContextHolder).

To support propagation between threadlocal and reactor operation chain, e.g. using WebClient in MVC, I have custom ThreadLocalAccessor(from micrometer context-propagation) implementations for them.
However, it would be more useful if Spring Framework provided them.

Sample Implementations:

public class LocaleContextThreadLocalAccessor implements ThreadLocalAccessor<LocaleContext> {

	static final String KEY = "my-locale-context";

	@Override
	public Object key() {
		return KEY;
	}

	@Override
	public LocaleContext getValue() {
		return LocaleContextHolder.getLocaleContext();
	}

	@Override
	public void setValue(LocaleContext value) {
		LocaleContextHolder.setLocaleContext(value);
	}

	@Override
	public void setValue() {
		LocaleContextHolder.resetLocaleContext();
	}

}
public class RequestAttributesThreadLocalAccessor implements ThreadLocalAccessor<RequestAttributes> {

	static final String KEY = "my-request-attributes";

	@Override
	public Object key() {
		return KEY;
	}

	@Override
	public RequestAttributes getValue() {
		return RequestContextHolder.getRequestAttributes();
	}

	@Override
	public void setValue(RequestAttributes value) {
		RequestContextHolder.setRequestAttributes(value);
	}

	@Override
	public void setValue() {
		RequestContextHolder.resetRequestAttributes();
	}

}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jan 25, 2024
@dsyer
Copy link
Member

dsyer commented Jan 25, 2024

This looks useful to me. It won't work with an SSE controller method because the reactor context gets reset too early. Here an example: https://github.com/scratches/sse-render.

@rstoyanchev rstoyanchev self-assigned this Jan 25, 2024
@rstoyanchev rstoyanchev added in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 25, 2024
@rstoyanchev rstoyanchev added this to the 6.2.0-M1 milestone Jan 25, 2024
@rstoyanchev rstoyanchev changed the title ThreadLocalAccessor implementation for servlet request associated objects ThreadLocalAccessor implementation for RequestAttributes Jan 25, 2024
@rstoyanchev rstoyanchev changed the title ThreadLocalAccessor implementation for RequestAttributes ThreadLocalAccessor implementation for RequestAttributes and LocaleContext Jan 25, 2024
@snicoll
Copy link
Member

snicoll commented Jan 31, 2024

@ttddyy would you be interested to move that code snippet into an actual PR we can review (with the relevant doc and test). This targets main and 6.2. If not, no worries, we'll handle it.

@ttddyy
Copy link
Contributor Author

ttddyy commented Feb 1, 2024

Yup, no problem. I'll create a PR for the change.

@snicoll
Copy link
Member

snicoll commented Feb 12, 2024

Closing in favor of PR #32243, thanks @ttddyy!

@snicoll snicoll closed this as not planned Won't fix, can't repro, duplicate, stale Feb 12, 2024
@snicoll snicoll added the status: superseded An issue that has been superseded by another label Feb 12, 2024
@snicoll snicoll removed this from the 6.2.0-M1 milestone Feb 12, 2024
ttddyy added a commit to ttddyy/spring-framework that referenced this issue Feb 13, 2024
Add `ThreadLocalAccessor` implementations:
- `LocaleThreadLocalAccessor`
- `RequestAttributesThreadLocalAccessor`

Closes spring-projectsgh-32112

Signed-off-by: Tadaya Tsuyukubo <[email protected]>
@dsyer
Copy link
Member

dsyer commented Apr 8, 2024

See also #32296 for a related bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: superseded An issue that has been superseded by another type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants