在第一个进程结束时,第二个进程进入并更改UUID值。
这不是一个Web应用程序,因此无法对跟踪类使用“request”作用域,我的应用程序是一个pub-sub模型。这是一个示例应用程序。
public class MainClass {
@Bean
@Autowired
public Receiver getEventHubReceiver(Trace trace, SomeOtherClass classex) {
return new Receiver(trace, classex);
}
}
@Component
class Trace{
String traceid;
public void setTraceid(String traceid) {
this.traceid = traceid;
}
public void trace(String msg){
System.out.println(traceid+" : Tracing : "+msg);
}
}
class Receiver{
private final SomeOtherClass classex;
private final Trace trace;
Receiver(Trace trace, SomeOtherClass classex){
this.trace= trace;
this.classex = classex;
}
//This is a callback method invoked by server internally.
public void receiver(){
trace.setTraceid(UUID.randomUUID().toString());
trace.trace("reached Receiver..started");
classex.invoke(); //==> I don't want to pass the Trace object here, same object should be used by multiple classes.
trace.trace("reached Receiver..completed");
}
}
class SomeOtherClass{
@Autowired
private Trace trace; // ==> ISSUE -- Behaving as a single ton.
public void invoke(){
trace.trace("Reached SomeOtherClass - started");
someLongRunProcess(); // it take almost 3-5 seconds to complete this.
trace.trace("Reached SomeOtherClass - completed");
}
private void someLongRunProcess() {
//DB invoke option
}
}