简体   繁体   English

Spring SmartLifeCycle 顺序行为?

[英]Spring SmartLifeCycle sequential behavior?

I have an application that is broken down into several modules.我有一个分为几个模块的应用程序。 I created a class whose job is to start them in the right order and give each module enough time to start.我创建了一个 class,它的工作是以正确的顺序启动它们并给每个模块足够的时间来启动。 I have messed with SmartLifeCycle quite a bit in the past but i would like to know more about how it would behave if i was to use it for my modules instead of needing a separate class that handles each modules's startups and shutdowns.过去我对 SmartLifeCycle 搞砸了很多,但我想更多地了解如果我将它用于我的模块而不是需要一个单独的 class 来处理每个模块的启动和关闭,它会如何表现。

For example, module A takes quite a bit of time to start as it needs to initialize several TCP connections to other systems.例如,模块 A 需要相当长的时间才能启动,因为它需要初始化几个 TCP 与其他系统的连接。 Module B is dependent on module A being fully initialized as it sends various messages to these clients.模块 B 依赖于模块 A 在向这些客户端发送各种消息时被完全初始化。 If i make each module implement SmartLifeCycle and give them the correct phase so they start in the right order, can i assume that Spring will fully initialize one before moving to the next?如果我让每个模块实现 SmartLifeCycle 并为它们提供正确的阶段,以便它们以正确的顺序开始,我可以假设 Spring 将在移动到下一个之前完全初始化一个吗? Do we have any control over this behavior?我们可以控制这种行为吗?

IMO it's better to use an event-driven way to control your "modules". IMO 最好使用事件驱动的方式来控制您的“模块”。 If I understand you correctly in each modules you have some services which should do some job depending on each other.如果我在每个模块中都正确理解了您,那么您有一些服务应该相互依赖。 So in this case, you can publish the necessary events and implement some listeners that react to these events and call the correspond methods of those servers, then publish other events that trigger the next part of listeners, etc.所以在这种情况下,您可以发布必要的事件并实现一些对这些事件做出反应的监听器并调用这些服务器的相应方法,然后发布触发下一部分监听器的其他事件等。

For example:例如:

@Component
public class FirstHandler {
    @Autoware private FirstService service;

    @EventListener(ApplicationReadyEvent.class) // start when App is ready
    public FirstWorkCompleted onAppReady() {
        service.doWork();
        return new FirstWorkCompleted(); // send FirstWorkCompleted event
    }
}
@Component
public class SecondHandler {
    @Autoware private SecondService service;

    @EventListener(FirstWorkCompleted.class) // start when First work is completed
    public SecondWorkCompleted onFirstWorkCompleted() {
        service.doWork();
        return new SecondWorkCompleted(); // send SecondWorkCompleted event
    }
}

You can also send events with ApplicationEventPublisher - just inject it in your component.您还可以使用ApplicationEventPublisher发送事件 - 只需将其注入您的组件中。

@EventListener methods can work asynchronously - just add @Async annotation to such methods (don't forget to apply @EnableAsync annotation to your config or application class as well). @EventListener方法可以异步工作 - 只需将@Async注释添加到此类方法(不要忘记将@EnableAsync注释应用于您的配置或应用程序 class )。

@EventListener methods can be ordered - just add @Order annotation to them with desired order.可以对@EventListener方法进行排序 - 只需按所需顺序向它们添加@Order注释。

More info about application event:有关应用程序事件的更多信息:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM