增加ResourceEvent

This commit is contained in:
Redkale
2022-07-06 22:44:44 +08:00
parent dc285b6c2f
commit 0c60700d82
4 changed files with 83 additions and 13 deletions

View 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();
}

View File

@@ -853,7 +853,8 @@ public final class ResourceFactory {
if (element.listener != null) {
try {
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) {
logger.log(Level.SEVERE, dest + " resource change listener error", e);
@@ -897,16 +898,16 @@ public final class ResourceFactory {
do {
RedkaleClassLoader.putReflectionDeclaredMethods(loop.getName());
for (Method method : loop.getDeclaredMethods()) {
if (method.getAnnotation(ResourceListener.class) != null
&& method.getParameterCount() == 3
&& String.class.isAssignableFrom(method.getParameterTypes()[0])
&& method.getParameterTypes()[1] == method.getParameterTypes()[2]
&& method.getParameterTypes()[1].isAssignableFrom(fieldType)) {
ResourceListener rl = method.getAnnotation(ResourceListener.class);
if (rl == null) continue;
if (method.getParameterCount() == 1 && method.getParameterTypes()[0] == ResourceEvent[].class) {
m = method;
m.setAccessible(true);
diff.set(method.getAnnotation(ResourceListener.class).different());
diff.set(rl.different());
RedkaleClassLoader.putReflectionMethod(loop.getName(), method);
break;
} else {
System.err.println("@" + ResourceListener.class.getSimpleName() + " must on method with " + ResourceEvent.class.getSimpleName() + "[] parameter type");
}
}
} 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 {
//
// protected Class<?> type;

View File

@@ -10,7 +10,7 @@ import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* &#64;Resource资源被更新时的监听事件。本注解只能标记在方法参数为(String name, T newVal, T oldVal)上。
* &#64;Resource资源被更新时的监听事件。本注解只能标记在方法参数为ResourceEvent[]上。
* 方法在资源被更新以后调用。
*
* <blockquote><pre>
@@ -23,8 +23,10 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
* private String name;
*
* &#64;ResourceListener
* private void changeResource(String name, Object newVal, Object oldVal) {
* System.out.println("@Resource = " + name + " 资源变更: newVal = " + newVal + ", oldVal = " + oldVal);
* private void changeResource(ResourceEvent[] events) {
* for(ResourceEvent event : events) {
* System.out.println("@Resource = " + event.name() + " 资源变更: newVal = " + event.newValue() + ", oldVal = " + event.oldValue());
* }
* }
*
* public static void main(String[] args) throws Exception {

View File

@@ -7,6 +7,7 @@ package org.redkale.test.util;
import java.math.*;
import javax.annotation.*;
import org.junit.jupiter.api.Test;
import org.redkale.util.*;
/**
@@ -15,7 +16,16 @@ import org.redkale.util.*;
*/
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();
factory.register("property.id", "2345"); //注入String类型的property.id
AService aservice = new AService();
@@ -58,8 +68,10 @@ class BService {
private String name = "";
@ResourceListener
private void changeResource(String name, Object newVal, Object oldVal) {
System.out.println("@Resource = " + name + " 资源变更: newVal = " + newVal + ", oldVal = " + oldVal);
private void changeResource(ResourceEvent[] events) {
for (ResourceEvent event : events) {
System.out.println("@Resource = " + event.name() + " 资源变更: newVal = " + event.newValue() + ", oldVal = " + event.oldValue());
}
}
@ConstructorParameters({"name"})