mirror of
https://github.com/hs-web/hsweb-framework.git
synced 2026-06-21 20:52:46 +08:00
add FastListener
This commit is contained in:
@@ -30,5 +30,10 @@
|
||||
<groupId>org.hswebframework</groupId>
|
||||
<artifactId>hsweb-utils</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hswebframework.web</groupId>
|
||||
<artifactId>hsweb-boost-compiler</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,51 @@
|
||||
package org.hswebframework.web.eventbus.spring;
|
||||
|
||||
import org.hswebframework.web.boost.Compiler;
|
||||
import org.hswebframework.web.eventbus.EventListener;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* @author zhouhao
|
||||
* @since 3.0
|
||||
*/
|
||||
public class FastListener implements EventListener {
|
||||
|
||||
private Object target;
|
||||
|
||||
private FastListenerCallable callable;
|
||||
|
||||
public FastListener(Object target, Method method) {
|
||||
Class[] type = method.getParameterTypes();
|
||||
Class targetType = ClassUtils.getUserClass(target);
|
||||
this.target = target;
|
||||
StringBuilder methodImpl = new StringBuilder();
|
||||
|
||||
methodImpl.append("public void call(Object target, Object event) throws Exception{\n")
|
||||
.append(targetType.getName())
|
||||
.append(" targetObj=(")
|
||||
.append(targetType.getName())
|
||||
.append(")target;\n").append("targetObj.")
|
||||
.append(method.getName())
|
||||
.append("((").append(type[0].getName()).append(")event);\n")
|
||||
.append("}");
|
||||
|
||||
callable = Compiler.create(FastListenerCallable.class)
|
||||
.addMethod(methodImpl.toString())
|
||||
.newInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEvent(Object event) {
|
||||
try {
|
||||
callable.call(target, event);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public interface FastListenerCallable {
|
||||
void call(Object target, Object event) throws Exception;
|
||||
}
|
||||
}
|
||||
@@ -44,7 +44,7 @@ public class SpringEventBus extends AbstractEventBus implements BeanPostProcesso
|
||||
protected void createListener(Subscribe type, Object target, Method method) {
|
||||
EventListenerDefine define = EventListenerDefine.builder().eventMode(type.mode())
|
||||
.transaction(type.transaction())
|
||||
.listener(new ReflectListener(target, method))
|
||||
.listener(new FastListener(target, method))
|
||||
.build();
|
||||
|
||||
subscribe(method.getParameterTypes()[0], define);
|
||||
|
||||
@@ -5,8 +5,6 @@ import org.hswebframework.web.eventbus.annotation.Subscribe;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.BrokenBarrierException;
|
||||
import java.util.concurrent.CyclicBarrier;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
@@ -22,7 +20,9 @@ public class SpringEventBusTest {
|
||||
public void test() throws InterruptedException {
|
||||
System.out.println(Thread.currentThread().getName());
|
||||
eventBus.postProcessAfterInitialization(new Test2(), "test");
|
||||
long t = System.currentTimeMillis();
|
||||
eventBus.publish(eventBus);
|
||||
System.out.println(System.currentTimeMillis() - t);
|
||||
Thread.sleep(1000);
|
||||
Assert.assertEquals(counter.get(), 3);
|
||||
}
|
||||
@@ -30,22 +30,22 @@ public class SpringEventBusTest {
|
||||
public class Test2 {
|
||||
@Subscribe(mode = EventMode.SYNC)
|
||||
public void test1(SpringEventBus eventBus) {
|
||||
System.out.println(Thread.currentThread().getName());
|
||||
System.out.println(eventBus);
|
||||
// System.out.println(Thread.currentThread().getName());
|
||||
// System.out.println(eventBus);
|
||||
counter.addAndGet(1);
|
||||
}
|
||||
|
||||
@Subscribe(mode = EventMode.ASYNC, transaction = false)
|
||||
public void test2(SpringEventBus eventBus) {
|
||||
System.out.println(Thread.currentThread().getName());
|
||||
System.out.println(eventBus);
|
||||
// System.out.println(Thread.currentThread().getName());
|
||||
// System.out.println(eventBus);
|
||||
counter.addAndGet(1);
|
||||
}
|
||||
|
||||
@Subscribe(mode = EventMode.BACKGROUND, transaction = false)
|
||||
public void test3(SpringEventBus eventBus) {
|
||||
System.out.println(Thread.currentThread().getName());
|
||||
System.out.println(eventBus);
|
||||
// System.out.println(Thread.currentThread().getName());
|
||||
// System.out.println(eventBus);
|
||||
counter.addAndGet(1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user