zhou-hao 7 роки тому
батько
коміт
edbd1a955a

+ 27 - 0
hsweb-eventbus/hsweb-eventbus-default/pom.xml

@@ -35,5 +35,32 @@
             <artifactId>hsweb-boost-compiler</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.hswebframework.web</groupId>
+            <artifactId>hsweb-spring-boot-starter</artifactId>
+            <version>${project.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>druid</artifactId>
+            <version>1.0.26</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>com.h2database</groupId>
+            <artifactId>h2</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>

+ 0 - 22
hsweb-eventbus/hsweb-eventbus-default/src/main/java/org/hswebframework/web/eventbus/DefaultEventPublisher.java

@@ -1,22 +0,0 @@
-package org.hswebframework.web.eventbus;
-
-import org.hswebframework.web.eventbus.executor.EventListenerExecutor;
-
-import java.util.List;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-
-/**
- * @author zhouhao
- * @since 1.0
- */
-public class DefaultEventPublisher implements EventPublisher {
-
-    private ConcurrentMap<String, List<EventListenerExecutor>> listenerStorage = new ConcurrentHashMap<>();
-
-
-    @Override
-    public void publish(Object event) {
-
-    }
-}

+ 3 - 1
hsweb-eventbus/hsweb-eventbus-default/src/main/java/org/hswebframework/web/eventbus/spring/FastListener.java

@@ -1,5 +1,6 @@
 package org.hswebframework.web.eventbus.spring;
 
+import lombok.extern.slf4j.Slf4j;
 import org.hswebframework.web.boost.Compiler;
 import org.hswebframework.web.eventbus.EventListener;
 import org.springframework.util.ClassUtils;
@@ -10,6 +11,7 @@ import java.lang.reflect.Method;
  * @author zhouhao
  * @since 3.0
  */
+@Slf4j
 public class FastListener implements EventListener {
 
     private Object target;
@@ -41,7 +43,7 @@ public class FastListener implements EventListener {
         try {
             callable.call(target, event);
         } catch (Exception e) {
-            e.printStackTrace();
+            log.error("执行event[{}]失败", event, e);
         }
     }
 

+ 16 - 0
hsweb-eventbus/hsweb-eventbus-default/src/main/java/org/hswebframework/web/eventbus/spring/SpringEventBusAutoConfiguration.java

@@ -0,0 +1,16 @@
+package org.hswebframework.web.eventbus.spring;
+
+import org.hswebframework.web.eventbus.EventBus;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class SpringEventBusAutoConfiguration {
+
+    @Bean
+    @ConditionalOnMissingBean(EventBus.class)
+    public SpringEventBus springEventBus() {
+        return new SpringEventBus();
+    }
+}

+ 3 - 3
hsweb-eventbus/hsweb-eventbus-default/src/main/java/org/hswebframework/web/eventbus/spring/SpringEventContainer.java

@@ -25,9 +25,9 @@ public class SpringEventContainer extends AbstractEventContainer {
     protected EventListenerExecutor newExecutor() {
         DefaultEventExecutor eventExecutor = new DefaultEventExecutor();
         List<EventExecuteTaskSupplier> suppliers = new ArrayList<>(this.suppliers);
-        if (hasAsyncTx()) {
-            // TODO: 18-3-24
-        }
+//        if (hasAsyncTx()) {
+//            // TODO: 18-3-24
+//        }
         eventExecutor.setSuppliers(suppliers);
         return eventExecutor;
     }

+ 3 - 0
hsweb-eventbus/hsweb-eventbus-default/src/main/resources/META-INF/spring.factories

@@ -0,0 +1,3 @@
+# Auto Configure
+org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
+org.hswebframework.web.eventbus.spring.SpringEventBusAutoConfiguration

+ 12 - 25
hsweb-eventbus/hsweb-eventbus-default/src/test/java/org/hswebframework/web/eventbus/spring/SpringEventBusTest.java

@@ -1,9 +1,16 @@
 package org.hswebframework.web.eventbus.spring;
 
+import org.hswebframework.web.eventbus.EventBus;
 import org.hswebframework.web.eventbus.annotation.EventMode;
 import org.hswebframework.web.eventbus.annotation.Subscribe;
 import org.junit.Assert;
 import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Service;
+import org.springframework.test.context.junit4.SpringRunner;
 
 import java.util.concurrent.atomic.AtomicInteger;
 
@@ -11,42 +18,22 @@ import java.util.concurrent.atomic.AtomicInteger;
  * @author zhouhao
  * @since 3.0
  */
+@RunWith(SpringRunner.class)
+@SpringBootTest(classes = TestApplication.class,webEnvironment = SpringBootTest.WebEnvironment.NONE)
 public class SpringEventBusTest {
-    SpringEventBus eventBus = new SpringEventBus();
 
-    AtomicInteger counter = new AtomicInteger();
+    @Autowired
+    EventBus eventBus;
 
     @Test
     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);
+        Assert.assertEquals(Test2.counter.get(), 3);
     }
 
-    public class Test2 {
-        @Subscribe(mode = EventMode.SYNC)
-        public void test1(SpringEventBus 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);
-            counter.addAndGet(1);
-        }
-
-        @Subscribe(mode = EventMode.BACKGROUND, transaction = false)
-        public void test3(SpringEventBus eventBus) {
-//            System.out.println(Thread.currentThread().getName());
-//            System.out.println(eventBus);
-            counter.addAndGet(1);
-        }
-    }
 }

+ 33 - 0
hsweb-eventbus/hsweb-eventbus-default/src/test/java/org/hswebframework/web/eventbus/spring/Test2.java

@@ -0,0 +1,33 @@
+package org.hswebframework.web.eventbus.spring;
+
+import org.hswebframework.web.eventbus.annotation.EventMode;
+import org.hswebframework.web.eventbus.annotation.Subscribe;
+import org.springframework.stereotype.Component;
+
+import java.util.concurrent.atomic.AtomicLong;
+
+@Component
+public class Test2 {
+    public static AtomicLong counter = new AtomicLong();
+
+    @Subscribe(mode = EventMode.SYNC)
+    public void test1(SpringEventBus 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);
+        counter.addAndGet(1);
+    }
+
+    @Subscribe(mode = EventMode.BACKGROUND, transaction = false)
+    public void test3(SpringEventBus eventBus) {
+//            System.out.println(Thread.currentThread().getName());
+//            System.out.println(eventBus);
+        counter.addAndGet(1);
+    }
+}

+ 13 - 0
hsweb-eventbus/hsweb-eventbus-default/src/test/java/org/hswebframework/web/eventbus/spring/TestApplication.java

@@ -0,0 +1,13 @@
+package org.hswebframework.web.eventbus.spring;
+
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.Bean;
+
+@SpringBootApplication
+public class TestApplication {
+
+    @Bean
+    public Test2 test2(){
+        return new Test2();
+    }
+}

+ 9 - 0
hsweb-eventbus/hsweb-eventbus-default/src/test/resources/application.yml

@@ -0,0 +1,9 @@
+spring:
+    aop:
+        auto: true
+    datasource:
+       url : jdbc:h2:mem:permission_test_mem
+       username : sa
+       password :
+       type: com.alibaba.druid.pool.DruidDataSource
+       driver-class-name : org.h2.Driver