Skip to content

Commit

Permalink
Merge 4040724 into 8a941a5
Browse files Browse the repository at this point in the history
  • Loading branch information
straybirdzls committed Dec 28, 2018
2 parents 8a941a5 + 4040724 commit 84b4d19
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* <p>
Expand Down Expand Up @@ -44,7 +45,7 @@
*
* @author xuanbei 18/2/28
*/
public interface Component {
public interface Component extends ComponentLifeCycle {
/**
* register component
*/
Expand All @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@
<aop:around method="doAround" pointcut-ref="serviceAround"/>
</aop:aspect>
</aop:config>

<bean id="lifeCycleService" class="com.alipay.sofa.runtime.beans.impl.ComponentLifeCycleServiceImpl" />
<sofa:service ref="lifeCycleService" interface="com.alipay.sofa.runtime.beans.service.LifeCycleService"/>

</beans>

0 comments on commit 84b4d19

Please sign in to comment.