Can I access Http.Context.current () from an arbitrary asynchronous task?

I am working on a backend for a mobile application that currently runs on Play 2.1.1. In the process of processing some requests, we send push notifications. The subsequent request to send push notifications must be completely asynchronous and separated from the original request-response for the mobile client.

I want to have access to Http.Context.current() when sending a downstream request in order to access some trace data that we pass in the request headers.

Initially, the code looked like this following the advice of Play! documentation :

 PushNotificationRunnable sendNotificationTask = new ... Akka.system().scheduler().scheduleOnce(Duration.apply(0, TimeUnit.MICROSECONDS), sendNotificationTask, Akka.system().dispatcher()); 

Studying the play.libs.Akka will lead me to a future method that accepts the called and returns Promise. The promise allows me to cling to further code. Here I bound Callback , which has access to Http.Context.current () , thanks to setting up the code in the Play PromiseActor class . This allows me to log a string, including the trace ID, when the task is complete, but my log lines, while the task is running, still cannot access the trace information.

 PushNotificationCallable sendNotificationTask = new ... Akka.future(sendNotificationTask).onRedeem(new F.Callback<Void>() { @Override public void invoke(Void aVoid) throws Throwable { Logger.info("Completed sendNotificationTask from the service"); } }); 

Here are some abbreviated application logs that show where I am now and what is missing, the trace identifier in the 5th column:

 2013-07-26 11:31:06,885 DEBUG play-akka.actor.default-dispatcher-10 -2454018518484259555 [application] : Processing request for mobile app 2013-07-26 11:31:06,907 DEBUG play-akka.actor.default-dispatcher-10 -2454018518484259555 [application] : About to schedule push notification message send 2013-07-26 11:31:06,907 INFO application-akka.actor.default-dispatcher-8 n/a [services.PushMessageSenderTask] : Sending message in akka background task 2013-07-26 11:31:06,924 INFO application-akka.actor.default-dispatcher-8 n/a [services.PushMessageSenderTask] : Sent message in akka background task 2013-07-26 11:31:06,925 INFO play-akka.actor.default-dispatcher-16 -2454018518484259555 [application] : Completed sendNotificationTask 

Fields are the date, time, level, stream, and trace identifier. With this log configuration, if that helps:

 %d{ISO8601} %-5level %thread %traceId [%logger] : %msg%n 

As you can see, lines 3 and 4 are from the Akka stream and do not have access to TraceId instead of printing n/a . Lines 1 and 2 are in the initial request processing thread and have access. Finally, line 5 is in another request processing thread and also has access.

Is there any other way that I could schedule a task that would give it access to Http.Context.request (), but still execute it β€œoutside” the request-response cycle in the browser?

+4
source share

Source: https://habr.com/ru/post/1493719/


All Articles