diff --git a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/api/component/ComponentLifeCycle.java b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/api/component/ComponentLifeCycle.java new file mode 100644 index 000000000..c106da1ef --- /dev/null +++ b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/api/component/ComponentLifeCycle.java @@ -0,0 +1,41 @@ +/* + * 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 com.alipay.sofa.runtime.api.component; + +import com.alipay.sofa.runtime.api.ServiceRuntimeException; + +/** + * Interface used to implemented by components who wants to do something when certain lifecycle event of the component. + * + * @author khotyn + * @since 2.6.0 + */ +public interface ComponentLifeCycle { + /** + * Component lifecycle event occurred when component is activated. + * + * @throws ServiceRuntimeException + */ + void activate() throws ServiceRuntimeException; + + /** + * Component lifecycle event occurred when component is activated. + * + * @throws ServiceRuntimeException + */ + void deactivate() throws ServiceRuntimeException; +} diff --git a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spi/component/AbstractComponent.java b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spi/component/AbstractComponent.java index bc137ab21..0a3192093 100644 --- a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spi/component/AbstractComponent.java +++ b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spi/component/AbstractComponent.java @@ -17,6 +17,7 @@ package com.alipay.sofa.runtime.spi.component; import com.alipay.sofa.runtime.api.ServiceRuntimeException; +import com.alipay.sofa.runtime.api.component.ComponentLifeCycle; import com.alipay.sofa.runtime.api.component.ComponentName; import com.alipay.sofa.runtime.model.ComponentStatus; import com.alipay.sofa.runtime.spi.health.HealthResult; @@ -121,6 +122,13 @@ public void activate() throws ServiceRuntimeException { return; } + if (this.implementation != null) { + Object target = this.implementation.getTarget(); + if (!(target instanceof Component) && target instanceof ComponentLifeCycle) { + ((ComponentLifeCycle) target).activate(); + } + } + componentStatus = ComponentStatus.ACTIVATED; } @@ -134,6 +142,13 @@ public void deactivate() throws ServiceRuntimeException { return; } + if (this.implementation != null) { + Object target = this.implementation.getTarget(); + if (!(target instanceof Component) && target instanceof ComponentLifeCycle) { + ((ComponentLifeCycle) target).deactivate(); + } + } + componentStatus = ComponentStatus.RESOLVED; } diff --git a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spi/component/Component.java b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spi/component/Component.java index d5c600942..4148e670c 100644 --- a/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spi/component/Component.java +++ b/runtime-sofa-boot-starter/src/main/java/com/alipay/sofa/runtime/spi/component/Component.java @@ -17,6 +17,7 @@ package com.alipay.sofa.runtime.spi.component; import com.alipay.sofa.runtime.api.ServiceRuntimeException; +import com.alipay.sofa.runtime.api.component.ComponentLifeCycle; /** *
@@ -44,7 +45,7 @@
*
* @author xuanbei 18/2/28
*/
-public interface Component {
+public interface Component extends ComponentLifeCycle {
/**
* register component
*/
@@ -67,20 +68,6 @@ public interface Component {
*/
void unresolve() throws ServiceRuntimeException;
- /**
- * activate component
- *
- * @throws ServiceRuntimeException
- */
- void activate() throws ServiceRuntimeException;
-
- /**
- * deactivate component
- *
- * @throws ServiceRuntimeException
- */
- void deactivate() throws ServiceRuntimeException;
-
/**
* create an exception to describe error
*
diff --git a/runtime-sofa-boot-starter/src/test/java/com/alipay/sofa/runtime/beans/impl/ComponentLifeCycleServiceImpl.java b/runtime-sofa-boot-starter/src/test/java/com/alipay/sofa/runtime/beans/impl/ComponentLifeCycleServiceImpl.java
new file mode 100644
index 000000000..5e3220b64
--- /dev/null
+++ b/runtime-sofa-boot-starter/src/test/java/com/alipay/sofa/runtime/beans/impl/ComponentLifeCycleServiceImpl.java
@@ -0,0 +1,52 @@
+/*
+ * 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 com.alipay.sofa.runtime.beans.impl;
+
+import com.alipay.sofa.runtime.api.ServiceRuntimeException;
+import com.alipay.sofa.runtime.api.component.ComponentLifeCycle;
+import com.alipay.sofa.runtime.beans.service.LifeCycleService;
+
+/**
+ *
+ * @author ruoshan
+ * @since 2.6.0
+ */
+public class ComponentLifeCycleServiceImpl implements LifeCycleService, ComponentLifeCycle {
+
+ private boolean activated;
+ private boolean deactivated;
+
+ @Override
+ public void activate() throws ServiceRuntimeException {
+ activated = true;
+ }
+
+ @Override
+ public void deactivate() throws ServiceRuntimeException {
+ deactivated = true;
+ }
+
+ @Override
+ public boolean isActivated() {
+ return activated;
+ }
+
+ @Override
+ public boolean isDeactivated() {
+ return deactivated;
+ }
+}
diff --git a/runtime-sofa-boot-starter/src/test/java/com/alipay/sofa/runtime/beans/service/LifeCycleService.java b/runtime-sofa-boot-starter/src/test/java/com/alipay/sofa/runtime/beans/service/LifeCycleService.java
new file mode 100644
index 000000000..a4a001219
--- /dev/null
+++ b/runtime-sofa-boot-starter/src/test/java/com/alipay/sofa/runtime/beans/service/LifeCycleService.java
@@ -0,0 +1,29 @@
+/*
+ * 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 com.alipay.sofa.runtime.beans.service;
+
+/**
+ *
+ * @author ruoshan
+ * @since 2.6.0
+ */
+public interface LifeCycleService {
+
+ boolean isActivated();
+
+ boolean isDeactivated();
+}
\ No newline at end of file
diff --git a/runtime-sofa-boot-starter/src/test/java/com/alipay/sofa/runtime/integration/component/ComponentLifeCycleTest.java b/runtime-sofa-boot-starter/src/test/java/com/alipay/sofa/runtime/integration/component/ComponentLifeCycleTest.java
new file mode 100644
index 000000000..ed98785fd
--- /dev/null
+++ b/runtime-sofa-boot-starter/src/test/java/com/alipay/sofa/runtime/integration/component/ComponentLifeCycleTest.java
@@ -0,0 +1,57 @@
+/*
+ * 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 com.alipay.sofa.runtime.integration.component;
+
+import com.alipay.sofa.runtime.beans.service.LifeCycleService;
+import com.alipay.sofa.runtime.integration.base.SofaBootTestApplication;
+import com.alipay.sofa.runtime.spi.component.SofaRuntimeContext;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.context.junit4.SpringRunner;
+
+/**
+ * @author ruoshan
+ * @since 2.6.0
+ */
+@RunWith(SpringRunner.class)
+@SpringBootTest(classes = SofaBootTestApplication.class)
+public class ComponentLifeCycleTest {
+
+ @Autowired
+ private LifeCycleService lifeCycleService;
+
+ @Autowired
+ private SofaRuntimeContext sofaRuntimeContext;
+
+ @Test
+ public void testActivated() {
+ Assert.assertTrue(lifeCycleService.isActivated());
+ }
+
+ @Test
+ @DirtiesContext
+ public void testDeactivated() {
+ Assert.assertFalse(lifeCycleService.isDeactivated());
+ sofaRuntimeContext.getComponentManager().shutdown();
+ Assert.assertTrue(lifeCycleService.isDeactivated());
+ }
+
+}
\ No newline at end of file
diff --git a/runtime-sofa-boot-starter/src/test/resources/META-INF/spring/test-service.xml b/runtime-sofa-boot-starter/src/test/resources/META-INF/spring/test-service.xml
index d8e9de43f..4cc6f9138 100644
--- a/runtime-sofa-boot-starter/src/test/resources/META-INF/spring/test-service.xml
+++ b/runtime-sofa-boot-starter/src/test/resources/META-INF/spring/test-service.xml
@@ -30,4 +30,8 @@