In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to pass the value of MDC in the web application, the article is very detailed, has a certain reference value, interested friends must read it!
Pass the value of MDC
MDC (Mapped Diagnostic Context) is typically used to store specific values for a single task. For example, in a web application, it might store a request id and a user id for each request, making it easier for MDC to find log records related to a single request or the entire user activity.
2017-08-27 14 INFO [server-thread-0] [requestId=060d8c7f, userId=2928ea66] c.g.s.web.Controller-Message.
However, if some part of the code is executed in a dedicated thread pool, the MDC in the thread (the thread that submitted the task) will not continue to be passed. In the following example, the log on line 7 contains "requestId", while the log on line 9 does not:
@ GET@Path ("/ genre/ {name}") @ Produces (MediaType.APPLICATION_JSON) public void getGenre (@ PathParam ("name") String genreName, @ Suspended AsyncResponse response) {try (MDC.MDCCloseable ignored = MDC.putCloseable ("requestId", UUID.randomUUID (). ToString () {String genreId = getGenreIdbyName (genreName) / / Sync call logger.trace ("Submitting task to find genre with id'{}'.", genreId); / / 'requestId' is logged executorService.submit (()-> {logger.trace ("Starting task to find genre with id' {}'.", genreId) / / 'requestId' is not logged Response result = getGenre (genreId) / / Async call .map (artist-> Response.ok (artist) .build ()) .orElseGet (()-> Response.status (Response.Status.NOT_FOUND) .build ()); response.resume (result) );}}
This can be solved through the MDC#getCopyOfContextMap () method:
... public void getGenre (@ PathParam ("name") String genreName, @ Suspended AsyncResponse response) {try (MDC.MDCCloseable ignored = MDC.putCloseable ("requestId", UUID.randomUUID (). ToString () {. Logger.trace ("Submitting task to find genre with id'{}'.", genreId); / / 'requestId' is logged withCopyingMdc (executorService, ()-> {logger.trace ("Starting task to find genre with id' {}', genreId); / / 'requestId' is logged...});}} private void withCopyingMdc (ExecutorService executorService, Runnable function) {Map above is all the content of the article" how to pass MDC values in web applications ". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.