add FastListener

This commit is contained in:
zhouhao
2018-03-24 14:49:24 +08:00
parent 2ab390f059
commit 87176f087d
4 changed files with 65 additions and 9 deletions

View File

@@ -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>

View File

@@ -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;
}
}

View File

@@ -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);

View File

@@ -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);
}
}