Change callbacks
Updated at:
If your program needs to perform a callback when a switch value changes, the feature switch client provides two methods: global change callbacks and individual switch change callbacks.
Global change callbacks
You can use the Listener interface (full package name: com.taobao.csp.switchcenter.core.Listener) to listen for changes to any switch.
| Method | Example |
| SDK method | public class TestListener implements com.taobao.csp.switchcenter.core.Listener { @Override public void valueChange(String appName, String nameSpace, String name, String value) { // This method is called on a successful field value change. Do not cast the value. Use the corresponding field value directly. } } // Register the listener SwitchManager.addListener(new TestListener()); |
| Spring Boot starter method | @SwitchListenerpublic class TestListener implements com.taobao.csp.switchcenter.core.Listener { @Override public void valueChange(String appName, String nameSpace, String name, String value) { // This method is called on a successful field value change. Do not cast the value. Use the corresponding field value directly. }} |
Individual switch callbacks
You can implement the com.taobao.csp.switchcenter.core.SwitchCallback interface.
public class TestCallback implements SwitchCallback {
@Override
public void execute(String nameSpace, String name, String value) {
// TODO Auto-generated method stub
}
}In the AppSwitch annotation, you can set the callback field.
@AppSwitch(des = "Test switch", level = Level.p1, callback = TestCallback.class)
public static Map<String, String> test_switch = new HashMap<String, String>();
class TestCallback implements SwitchCallback {
public void execute(String nameSpace, String name, String value){
}
}Is this page helpful?