Skip to content

Commit 690a2c3

Browse files
artembilangaryrussell
authored andcommitted
Implement Lifecycle for IntegrationComponentSpec
Since an `IntegrationComponentSpec` is a `FactoryBean`, all it's target callbacks and lifecycle is controlled over a `FactoryBean`. * Add `SmartLifecycle` for the `IntegrationComponentSpec` to delegate lifecycle hooks to the `target` if necessary * Refactor `IntegrationComponentSpec` to be an `AbstractFactoryBean` which is a central place for the `FactoryBean`, `InitializingBean` and `DisposableBean` interfaces **Cherry-pick to 5.0.x**
1 parent 119db81 commit 690a2c3

File tree

2 files changed

+72
-17
lines changed

2 files changed

+72
-17
lines changed

spring-integration-core/src/main/java/org/springframework/integration/dsl/IntegrationComponentSpec.java

+63-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,8 +20,10 @@
2020
import org.apache.commons.logging.LogFactory;
2121

2222
import org.springframework.beans.factory.DisposableBean;
23-
import org.springframework.beans.factory.FactoryBean;
2423
import org.springframework.beans.factory.InitializingBean;
24+
import org.springframework.beans.factory.config.AbstractFactoryBean;
25+
import org.springframework.context.Lifecycle;
26+
import org.springframework.context.SmartLifecycle;
2527
import org.springframework.expression.spel.standard.SpelExpressionParser;
2628

2729
/**
@@ -35,7 +37,8 @@
3537
* @since 5.0
3638
*/
3739
public abstract class IntegrationComponentSpec<S extends IntegrationComponentSpec<S, T>, T>
38-
implements FactoryBean<T>, InitializingBean, DisposableBean {
40+
extends AbstractFactoryBean<T>
41+
implements SmartLifecycle {
3942

4043
protected static final SpelExpressionParser PARSER = new SpelExpressionParser();
4144

@@ -71,26 +74,73 @@ public T get() {
7174
}
7275

7376
@Override
74-
public T getObject() {
75-
return get();
77+
public Class<?> getObjectType() {
78+
return get().getClass();
7679
}
7780

7881
@Override
79-
public Class<?> getObjectType() {
80-
return get().getClass();
82+
protected T createInstance() throws Exception {
83+
T instance = get();
84+
if (instance instanceof InitializingBean) {
85+
((InitializingBean) instance).afterPropertiesSet();
86+
}
87+
return instance;
88+
}
89+
90+
@Override
91+
protected void destroyInstance(T instance) throws Exception {
92+
if (instance instanceof DisposableBean) {
93+
((DisposableBean) instance).destroy();
94+
}
95+
}
96+
97+
@Override
98+
public void start() {
99+
T instance = get();
100+
if (instance instanceof Lifecycle) {
101+
((Lifecycle) instance).start();
102+
}
81103
}
82104

83105
@Override
84-
public void afterPropertiesSet() throws Exception {
85-
if (this.target instanceof InitializingBean) {
86-
((InitializingBean) this.target).afterPropertiesSet();
106+
public void stop() {
107+
T instance = get();
108+
if (instance instanceof Lifecycle) {
109+
((Lifecycle) instance).stop();
87110
}
88111
}
89112

90113
@Override
91-
public void destroy() throws Exception {
92-
if (this.target instanceof DisposableBean) {
93-
((DisposableBean) this.target).destroy();
114+
public boolean isRunning() {
115+
T instance = get();
116+
return !(instance instanceof Lifecycle) || ((Lifecycle) instance).isRunning();
117+
}
118+
119+
@Override
120+
public boolean isAutoStartup() {
121+
T instance = get();
122+
return instance instanceof SmartLifecycle && ((SmartLifecycle) instance).isAutoStartup();
123+
}
124+
125+
@Override
126+
public void stop(Runnable callback) {
127+
T instance = get();
128+
if (instance instanceof SmartLifecycle) {
129+
((SmartLifecycle) instance).stop(callback);
130+
}
131+
else {
132+
callback.run();
133+
}
134+
}
135+
136+
@Override
137+
public int getPhase() {
138+
T instance = get();
139+
if (instance instanceof SmartLifecycle) {
140+
return ((SmartLifecycle) instance).getPhase();
141+
}
142+
else {
143+
return 0;
94144
}
95145
}
96146

spring-integration-http/src/test/java/org/springframework/integration/http/dsl/HttpDslTests.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -210,12 +210,17 @@ public IntegrationFlow httpInternalServiceFlow() {
210210
.get();
211211
}
212212

213+
@Bean
214+
public HttpRequestHandlerEndpointSpec httpService() {
215+
return Http.inboundGateway("/service")
216+
.requestMapping(r -> r.params("name"))
217+
.errorChannel("httpProxyErrorFlow.input");
218+
}
219+
213220
@Bean
214221
public IntegrationFlow httpProxyFlow() {
215222
return IntegrationFlows
216-
.from(Http.inboundGateway("/service")
217-
.requestMapping(r -> r.params("name"))
218-
.errorChannel("httpProxyErrorFlow.input"))
223+
.from(httpService())
219224
.handle(Http.outboundGateway("/service/internal?{params}")
220225
.uriVariable("params", "payload")
221226
.expectedResponseType(String.class)

0 commit comments

Comments
 (0)