forked from apache/dubbo
/
DubboLifecycleComponentApplicationListener.java
95 lines (79 loc) · 3.61 KB
/
DubboLifecycleComponentApplicationListener.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.config.spring.context;
import org.apache.dubbo.common.context.Lifecycle;
import com.alibaba.spring.context.OnceApplicationContextEventListener;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ApplicationContextEvent;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.SmartApplicationListener;
import java.util.LinkedList;
import java.util.List;
import static java.util.Collections.emptyList;
import static org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors;
/**
* A {@link ApplicationListener listener} for the {@link Lifecycle Dubbo Lifecycle} components
*
* @see {@link Lifecycle Dubbo Lifecycle}
* @see SmartApplicationListener
* @since 2.7.5
*/
public class DubboLifecycleComponentApplicationListener extends OnceApplicationContextEventListener {
/**
* The bean name of {@link DubboLifecycleComponentApplicationListener}
*
* @since 2.7.6
*/
public static final String BEAN_NAME = "dubboLifecycleComponentApplicationListener";
private List<Lifecycle> lifecycleComponents = emptyList();
public DubboLifecycleComponentApplicationListener(ApplicationContext applicationContext) {
super(applicationContext);
}
@Override
protected void onApplicationContextEvent(ApplicationContextEvent event) {
if (event instanceof ContextRefreshedEvent) {
onContextRefreshedEvent((ContextRefreshedEvent) event);
} else if (event instanceof ContextClosedEvent) {
onContextClosedEvent((ContextClosedEvent) event);
}
}
protected void onContextRefreshedEvent(ContextRefreshedEvent event) {
initLifecycleComponents(event);
startLifecycleComponents();
}
protected void onContextClosedEvent(ContextClosedEvent event) {
destroyLifecycleComponents();
}
private void initLifecycleComponents(ContextRefreshedEvent event) {
ApplicationContext context = event.getApplicationContext();
ClassLoader classLoader = context.getClassLoader();
lifecycleComponents = new LinkedList<>();
// load the Beans of Lifecycle from ApplicationContext
loadLifecycleComponents(lifecycleComponents, context);
}
private void loadLifecycleComponents(List<Lifecycle> lifecycleComponents, ApplicationContext context) {
lifecycleComponents.addAll(beansOfTypeIncludingAncestors(context, Lifecycle.class).values());
}
private void startLifecycleComponents() {
lifecycleComponents.forEach(Lifecycle::start);
}
private void destroyLifecycleComponents() {
lifecycleComponents.forEach(Lifecycle::destroy);
}
}