Using thread local to determine who is calling a component?

28 views
Skip to first unread message

Kevin Burton

unread,
Oct 24, 2014, 11:26:38 PM10/24/14
to google...@googlegroups.com
I have a complex stats system to monitor the behavior of our app.  

One of the things I want to do is track the name of a thread/task calling a component.

So I have a component called a Caller which I inject so that when a component is created, I can see who called it.

The problem is, how do you change the caller at runtime?

It should be possible to do this with thread locals and a @Singleton.

So basically you have a Task, which has a Caller dependency... then that task just does

String defaultCaller = caller.get();

try {

    caller.set( "my-task" );

    mytask.run();

} finally {
    caller.set( defaultCaller );
}

... now I'm not super happy with thread locals, but this doesn't seem amazingly ugly.

Is there a better way to do this?

Tim Peierls

unread,
Oct 25, 2014, 8:21:04 AM10/25/14
to google...@googlegroups.com
It's easy to go overboard with custom scopes, but sometimes they're just what you need. Check out Tim Boudreau's custom scopes framework.



--tim

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/70f1dabe-40e0-4698-a451-6a1a07aabe28%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages