forked from apache/dubbo
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Polish apache#4807 : [Feature] Add the callback mechanism ShutdownHook …
- Loading branch information
1 parent
c284dad
commit 3d84c35
Showing
5 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
dubbo-common/src/main/java/org/apache/dubbo/common/lang/Prioritized.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 org.apache.dubbo.common.lang; | ||
|
||
import static java.lang.Integer.compare; | ||
|
||
/** | ||
* {@code Prioritized} interface can be implemented by objects that | ||
* should be sorted, for example the tasks in executable queue. | ||
* | ||
* @since 2.7.4 | ||
*/ | ||
public interface Prioritized extends Comparable<Prioritized> { | ||
|
||
/** | ||
* The maximum priority | ||
*/ | ||
int MAX_PRIORITY = Integer.MIN_VALUE; | ||
|
||
/** | ||
* The minimum priority | ||
*/ | ||
int MIN_PRIORITY = Integer.MAX_VALUE; | ||
|
||
/** | ||
* Get the priority | ||
* | ||
* @return the default is {@link #MIN_PRIORITY minimum one} | ||
*/ | ||
default int getPriority() { | ||
return MIN_PRIORITY; | ||
} | ||
|
||
@Override | ||
default int compareTo(Prioritized that) { | ||
return compare(this.getPriority(), that.getPriority()); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
dubbo-common/src/main/java/org/apache/dubbo/common/lang/ShutdownHookCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* 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.common.lang; | ||
|
||
/** | ||
* Dubbo ShutdownHook callback interface | ||
* | ||
* @since 2.7.4 | ||
*/ | ||
public interface ShutdownHookCallback extends Prioritized { | ||
|
||
/** | ||
* Callback execution | ||
* | ||
* @throws Throwable if met with some errors | ||
*/ | ||
void callback() throws Throwable; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/DubboShutdownHookTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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; | ||
|
||
import org.apache.dubbo.common.lang.ShutdownHookCallback; | ||
import org.apache.dubbo.event.EventDispatcher; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
/** | ||
* {@link DubboShutdownHook} Test | ||
* | ||
* @since 2.7.4 | ||
*/ | ||
public class DubboShutdownHookTest implements ShutdownHookCallback { | ||
|
||
private final DubboShutdownHook dubboShutdownHook = DubboShutdownHook.getDubboShutdownHook(); | ||
|
||
private final EventDispatcher eventDispatcher = EventDispatcher.getDefaultExtension(); | ||
|
||
|
||
@BeforeEach | ||
public void before() { | ||
|
||
} | ||
|
||
@AfterEach | ||
public void after() { | ||
eventDispatcher.removeAllEventListeners(); | ||
dubboShutdownHook.clear(); | ||
} | ||
|
||
@Test | ||
public void testCallback() { | ||
assertEquals(this.getClass(), dubboShutdownHook.getCallbacks().iterator().next().getClass()); | ||
dubboShutdownHook.addCallback(this); | ||
assertEquals(2, dubboShutdownHook.getCallbacks().size()); | ||
} | ||
|
||
@Override | ||
public void callback() throws Throwable { | ||
|
||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...pi/src/test/resources/META-INF/services/org.apache.dubbo.common.lang.ShutdownHookCallback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.apache.dubbo.config.DubboShutdownHookTest |