代码之家  ›  专栏  ›  技术社区  ›  Molay

在Pub Sub model-issue中跟踪请求的开始和结束

  •  0
  • Molay  · 技术社区  · 5 年前

    在第一个进程结束时,第二个进程进入并更改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
        }
    }
    
    0 回复  |  直到 5 年前
        1
  •  0
  •   jolo    5 年前

    你试过 prototype 你的范围 Trace 组件?原型作用域应该允许为处理的每个请求创建一个新bean。有关详细信息,请参见 this answer on prototype scope instantiation .