views:

30

answers:

2

Is there any tidy way to keep the same thread name when making an RMI call? At the moment if I have a named thread that makes an RMI call, on the server side of the RMI call, Thread.currentThread().getName() returns something un-illuminating like "RMI TCP Connection(4)-10.0.0.2".

Of course, I could go and add to all my RMI methods a parameter String callingThreadName and make the first line of each RMI method implementation Thread.currentThread().setName(callingThreadName), but that's hardly the neatest way of doing so. Is there any way to get at least some of the meaning behind the thread name transferred over the RMI connection?

+1  A: 

What you are tryig to do is correlate the actions in different processes while looking at log files. The best way to do this is to add a unique transaction_id to your RPC that is used just for that purpose. This allows you to track the flow through the system.

Romain Hippeau
There is a unique transaction ID, but it's based in part on the thread name (and generated on the server). Even if a separate one was generated and added to the things that were passed, how would this be an improvement on the solution suggested in the original question (if anything, it'd be an extra parameter to drag into subsidiary methods whereas a thread name comes "for free").
Scott
@Scott Threads are usually pooled and in addition if you are using RMI you are working across multiple processes. There is no way to keep the ThreadIDs in sync unless you pass in a value to the callee to change the Thread name to. In addition since your are operating in a ThreadPool you will need to set the name for each invocation. There is no benefit to trying to do this anyways. Pass a transaction ID with your calls and in your callee then set a ThreadLocal variable if you want and use that. forget about the Thread ID or name.
Romain Hippeau
A: 

The Java RMI Specification specifically states that there are no guarantees about how client calls are mapped to server threads. So you can make no assumptions about it. Spacifically, you shouldn't be trying to use a thread identifier to correlate clients.

EJP