增加ResourceEvent
This commit is contained in:
20
src/main/java/org/redkale/util/ResourceEvent.java
Normal file
20
src/main/java/org/redkale/util/ResourceEvent.java
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
/*
|
||||||
|
*/
|
||||||
|
package org.redkale.util;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 详情见: https://redkale.org
|
||||||
|
*
|
||||||
|
* @param <T> 泛型
|
||||||
|
*
|
||||||
|
* @author zhangjx
|
||||||
|
* @since 2.7.0
|
||||||
|
*/
|
||||||
|
public interface ResourceEvent<T> {
|
||||||
|
|
||||||
|
public String name();
|
||||||
|
|
||||||
|
public T newValue();
|
||||||
|
|
||||||
|
public T oldValue();
|
||||||
|
}
|
||||||
@@ -853,7 +853,8 @@ public final class ResourceFactory {
|
|||||||
if (element.listener != null) {
|
if (element.listener != null) {
|
||||||
try {
|
try {
|
||||||
if (!element.different || !Objects.equals(newVal, oldVal)) {
|
if (!element.different || !Objects.equals(newVal, oldVal)) {
|
||||||
element.listener.invoke(dest, name, newVal, oldVal);
|
Object[] ps = new Object[]{new ResourceEvent[]{new ResourceChangeEvent(name, newVal, oldVal)}};
|
||||||
|
element.listener.invoke(dest, ps);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.log(Level.SEVERE, dest + " resource change listener error", e);
|
logger.log(Level.SEVERE, dest + " resource change listener error", e);
|
||||||
@@ -897,16 +898,16 @@ public final class ResourceFactory {
|
|||||||
do {
|
do {
|
||||||
RedkaleClassLoader.putReflectionDeclaredMethods(loop.getName());
|
RedkaleClassLoader.putReflectionDeclaredMethods(loop.getName());
|
||||||
for (Method method : loop.getDeclaredMethods()) {
|
for (Method method : loop.getDeclaredMethods()) {
|
||||||
if (method.getAnnotation(ResourceListener.class) != null
|
ResourceListener rl = method.getAnnotation(ResourceListener.class);
|
||||||
&& method.getParameterCount() == 3
|
if (rl == null) continue;
|
||||||
&& String.class.isAssignableFrom(method.getParameterTypes()[0])
|
if (method.getParameterCount() == 1 && method.getParameterTypes()[0] == ResourceEvent[].class) {
|
||||||
&& method.getParameterTypes()[1] == method.getParameterTypes()[2]
|
|
||||||
&& method.getParameterTypes()[1].isAssignableFrom(fieldType)) {
|
|
||||||
m = method;
|
m = method;
|
||||||
m.setAccessible(true);
|
m.setAccessible(true);
|
||||||
diff.set(method.getAnnotation(ResourceListener.class).different());
|
diff.set(rl.different());
|
||||||
RedkaleClassLoader.putReflectionMethod(loop.getName(), method);
|
RedkaleClassLoader.putReflectionMethod(loop.getName(), method);
|
||||||
break;
|
break;
|
||||||
|
} else {
|
||||||
|
System.err.println("@" + ResourceListener.class.getSimpleName() + " must on method with " + ResourceEvent.class.getSimpleName() + "[] parameter type");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while ((loop = loop.getSuperclass()) != Object.class);
|
} while ((loop = loop.getSuperclass()) != Object.class);
|
||||||
@@ -916,6 +917,41 @@ public final class ResourceFactory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class ResourceChangeEvent<T> implements ResourceEvent<T> {
|
||||||
|
|
||||||
|
public String name;
|
||||||
|
|
||||||
|
public T newValue;
|
||||||
|
|
||||||
|
public T oldValue;
|
||||||
|
|
||||||
|
public ResourceChangeEvent(String name, T newValue, T oldValue) {
|
||||||
|
this.name = name;
|
||||||
|
this.newValue = newValue;
|
||||||
|
this.oldValue = oldValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String name() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T newValue() {
|
||||||
|
return newValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T oldValue() {
|
||||||
|
return oldValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "{name = " + name() + ", newValue = " + newValue() + ", oldValue = " + oldValue() + "}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// public static class SimpleResourceTypeLoader implements ResourceTypeLoader {
|
// public static class SimpleResourceTypeLoader implements ResourceTypeLoader {
|
||||||
//
|
//
|
||||||
// protected Class<?> type;
|
// protected Class<?> type;
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import static java.lang.annotation.ElementType.METHOD;
|
|||||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Resource资源被更新时的监听事件。本注解只能标记在方法参数为(String name, T newVal, T oldVal)上。
|
* @Resource资源被更新时的监听事件。本注解只能标记在方法参数为ResourceEvent[]上。
|
||||||
* 方法在资源被更新以后调用。
|
* 方法在资源被更新以后调用。
|
||||||
*
|
*
|
||||||
* <blockquote><pre>
|
* <blockquote><pre>
|
||||||
@@ -23,8 +23,10 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
|||||||
* private String name;
|
* private String name;
|
||||||
*
|
*
|
||||||
* @ResourceListener
|
* @ResourceListener
|
||||||
* private void changeResource(String name, Object newVal, Object oldVal) {
|
* private void changeResource(ResourceEvent[] events) {
|
||||||
* System.out.println("@Resource = " + name + " 资源变更: newVal = " + newVal + ", oldVal = " + oldVal);
|
* for(ResourceEvent event : events) {
|
||||||
|
* System.out.println("@Resource = " + event.name() + " 资源变更: newVal = " + event.newValue() + ", oldVal = " + event.oldValue());
|
||||||
|
* }
|
||||||
* }
|
* }
|
||||||
*
|
*
|
||||||
* public static void main(String[] args) throws Exception {
|
* public static void main(String[] args) throws Exception {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ package org.redkale.test.util;
|
|||||||
|
|
||||||
import java.math.*;
|
import java.math.*;
|
||||||
import javax.annotation.*;
|
import javax.annotation.*;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
import org.redkale.util.*;
|
import org.redkale.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -15,7 +16,16 @@ import org.redkale.util.*;
|
|||||||
*/
|
*/
|
||||||
public class ResourceTest {
|
public class ResourceTest {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
private boolean main;
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Throwable {
|
||||||
|
ResourceTest test = new ResourceTest();
|
||||||
|
test.main = true;
|
||||||
|
test.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void run() throws Exception {
|
||||||
ResourceFactory factory = ResourceFactory.create();
|
ResourceFactory factory = ResourceFactory.create();
|
||||||
factory.register("property.id", "2345"); //注入String类型的property.id
|
factory.register("property.id", "2345"); //注入String类型的property.id
|
||||||
AService aservice = new AService();
|
AService aservice = new AService();
|
||||||
@@ -58,8 +68,10 @@ class BService {
|
|||||||
private String name = "";
|
private String name = "";
|
||||||
|
|
||||||
@ResourceListener
|
@ResourceListener
|
||||||
private void changeResource(String name, Object newVal, Object oldVal) {
|
private void changeResource(ResourceEvent[] events) {
|
||||||
System.out.println("@Resource = " + name + " 资源变更: newVal = " + newVal + ", oldVal = " + oldVal);
|
for (ResourceEvent event : events) {
|
||||||
|
System.out.println("@Resource = " + event.name() + " 资源变更: newVal = " + event.newValue() + ", oldVal = " + event.oldValue());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ConstructorParameters({"name"})
|
@ConstructorParameters({"name"})
|
||||||
|
|||||||
Reference in New Issue
Block a user