diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index aabc353..e17c9da 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -3,7 +3,7 @@ name: CI
on:
workflow_dispatch:
push:
- branches: [2.3.x, 2.7.x, 3.0.x, 3.1.x, 3.2.x, 3.3.x, 3.4.x, 3.5.x, 4.0.x, 4.1.x]
+ branches: [2.3.x, 2.7.x, 3.0.x, 3.1.x, 3.2.x, 3.3.x, 3.4.x, 3.5.x, 4.0.x, 4.1.x, fix/disruptor-string-utils-3.4.x]
pull_request:
branches: [2.3.x, 2.7.x, 3.0.x, 3.1.x, 3.2.x, 3.3.x, 3.4.x, 3.5.x, 4.0.x, 4.1.x]
@@ -12,5 +12,7 @@ permissions:
jobs:
build:
- uses: easy-4-java/.github/.github/workflows/maven-starter-ci.yml@main
+ uses: easy-4-java/.github/.github/workflows/maven-starter-ci.yml@bf473d6e9490345a8ac2abeb895639f87448c7ff
+ with:
+ version-line: ${{ github.ref_name == 'fix/disruptor-string-utils-3.4.x' && '3.4.x' || '' }}
secrets: inherit
diff --git a/pom.xml b/pom.xml
index 185115a..21c05aa 100644
--- a/pom.xml
+++ b/pom.xml
@@ -60,6 +60,7 @@
${java.version}
${java.version}
${project.build.sourceEncoding}
+ full
512M
diff --git a/src/main/java/com/lmax/disruptor/spring/boot/DisruptorEventHandlerCreater.java b/src/main/java/com/lmax/disruptor/spring/boot/DisruptorEventHandlerCreater.java
index 6a79d84..a26e5be 100644
--- a/src/main/java/com/lmax/disruptor/spring/boot/DisruptorEventHandlerCreater.java
+++ b/src/main/java/com/lmax/disruptor/spring/boot/DisruptorEventHandlerCreater.java
@@ -10,12 +10,12 @@
import com.lmax.disruptor.event.handler.chain.HandlerChainManager;
import com.lmax.disruptor.event.handler.chain.def.DefaultHandlerChainManager;
import com.lmax.disruptor.event.handler.chain.def.PathMatchingHandlerChainResolver;
-import com.lmax.disruptor.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.core.OrderComparator;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
+import org.springframework.util.StringUtils;
import java.util.*;
@@ -131,7 +131,7 @@ public List create(DisruptorProperties properties) {
protected DisruptorEventDispatcher createDisruptorEventHandler(EventHandlerDefinition handlerDefinition,
Map> eventHandlers) {
- if (StringUtils.isNotEmpty(handlerDefinition.getDefinitions())) {
+ if (StringUtils.hasLength(handlerDefinition.getDefinitions())) {
handlerChainDefinitionMap.putAll(this.parseHandlerChainDefinitions(handlerDefinition.getDefinitions()));
} else if (!CollectionUtils.isEmpty(handlerDefinition.getDefinitionMap())) {
handlerChainDefinitionMap.putAll(handlerDefinition.getDefinitionMap());
diff --git a/src/test/java/com/lmax/disruptor/spring/boot/DisruptorAutoConfigurationTest.java b/src/test/java/com/lmax/disruptor/spring/boot/DisruptorAutoConfigurationTest.java
new file mode 100644
index 0000000..2485405
--- /dev/null
+++ b/src/test/java/com/lmax/disruptor/spring/boot/DisruptorAutoConfigurationTest.java
@@ -0,0 +1,38 @@
+package com.lmax.disruptor.spring.boot;
+
+import com.lmax.disruptor.dsl.Disruptor;
+import com.lmax.disruptor.event.DisruptorEvent;
+import com.lmax.disruptor.spring.boot.event.DisruptorApplicationEvent;
+import org.junit.jupiter.api.Test;
+import org.springframework.context.support.GenericApplicationContext;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+class DisruptorAutoConfigurationTest {
+ @Test
+ void startsDisruptorAndPublishesApplicationEvents() {
+ GenericApplicationContext spring = new GenericApplicationContext();
+ spring.refresh();
+ DisruptorAutoConfiguration configuration = new DisruptorAutoConfiguration();
+ configuration.setApplicationContext(spring);
+ assertSame(spring, configuration.getApplicationContext());
+ DisruptorProperties properties = new DisruptorProperties();
+ Disruptor disruptor = configuration.disruptor(properties, configuration.eventFactory());
+ try {
+ assertNotNull(disruptor.getRingBuffer());
+ assertNotNull(configuration.oneArgEventTranslator());
+ assertNotNull(configuration.twoArgEventTranslator());
+ assertNotNull(configuration.threeArgEventTranslator());
+ assertNotNull(configuration.disruptorTemplate(disruptor, configuration.oneArgEventTranslator()));
+ assertNotNull(configuration.disruptorEventAwareProcessor());
+ long before = disruptor.getRingBuffer().getCursor();
+ DisruptorEvent event = new DisruptorEvent("source");
+ configuration.disruptorEventListener(disruptor, configuration.oneArgEventTranslator())
+ .onApplicationEvent(new DisruptorApplicationEvent(event));
+ assertTrue(disruptor.getRingBuffer().getCursor() > before);
+ } finally {
+ disruptor.shutdown();
+ spring.close();
+ }
+ }
+}
diff --git a/src/test/java/com/lmax/disruptor/spring/boot/DisruptorEventHandlerCreaterTest.java b/src/test/java/com/lmax/disruptor/spring/boot/DisruptorEventHandlerCreaterTest.java
new file mode 100644
index 0000000..e2fa241
--- /dev/null
+++ b/src/test/java/com/lmax/disruptor/spring/boot/DisruptorEventHandlerCreaterTest.java
@@ -0,0 +1,109 @@
+package com.lmax.disruptor.spring.boot;
+
+import com.lmax.disruptor.annotation.EventRule;
+import com.lmax.disruptor.config.EventHandlerDefinition;
+import com.lmax.disruptor.event.DisruptorEvent;
+import com.lmax.disruptor.event.handler.DisruptorHandler;
+import com.lmax.disruptor.event.handler.Nameable;
+import com.lmax.disruptor.event.handler.chain.HandlerChain;
+import com.lmax.disruptor.event.handler.chain.HandlerChainManager;
+import com.lmax.disruptor.event.handler.chain.def.DefaultHandlerChainManager;
+import org.junit.jupiter.api.Test;
+import org.springframework.context.support.GenericApplicationContext;
+
+import java.util.Collections;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+class DisruptorEventHandlerCreaterTest {
+
+ @Test
+ void parsesNonEmptyDefinitionsBeforeUsingDefinitionMap() {
+ CapturingCreater creater = new CapturingCreater();
+ EventHandlerDefinition definition = new EventHandlerDefinition();
+ definition.setDefinitions("[/urls]\n/events = handler");
+ definition.setDefinitionMap(Collections.singletonMap("/fallback", "other"));
+
+ creater.createDisruptorEventHandler(definition, Collections.emptyMap());
+
+ assertEquals(1, creater.parseCalls);
+ assertEquals(Collections.singletonMap("/events", "handler"), creater.chainDefinitions);
+ }
+
+ @Test
+ void usesDefinitionMapWhenDefinitionsAreEmptyOrNull() {
+ for (String definitions : new String[] {"", ""}) {
+ CapturingCreater creater = new CapturingCreater();
+ EventHandlerDefinition definition = new EventHandlerDefinition();
+ definition.setDefinitions("".equals(definitions) ? null : definitions);
+ definition.setDefinitionMap(Collections.singletonMap("/fallback", "handler"));
+
+ creater.createDisruptorEventHandler(definition, Collections.emptyMap());
+
+ assertEquals(0, creater.parseCalls);
+ assertEquals(Collections.singletonMap("/fallback", "handler"), creater.chainDefinitions);
+ }
+ }
+
+ @Test
+ void discoversAnnotatedHandlersAndBuildsDefaultAndConfiguredChains() {
+ GenericApplicationContext context = new GenericApplicationContext();
+ context.registerBean("handler", RecordingHandler.class);
+ context.refresh();
+ try {
+ DisruptorEventHandlerCreater creater = new DisruptorEventHandlerCreater(context);
+ assertSame(context, creater.getApplicationContext());
+ Map> handlers = creater.getEventHandlers();
+ assertEquals(1, handlers.size());
+ assertSame(context.getBean("handler"), handlers.get("handler"));
+
+ HandlerChainManager manager = creater.createHandlerChainManager(
+ handlers, Collections.singletonMap("/events", "handler"));
+ assertTrue(manager.hasChains());
+ assertTrue(manager.getChainNames().contains("/events"));
+ assertEquals("handler", ((RecordingHandler) handlers.get("handler")).name);
+
+ DisruptorProperties defaults = new DisruptorProperties();
+ assertEquals(1, creater.create(defaults).size());
+ EventHandlerDefinition definition = new EventHandlerDefinition();
+ definition.setOrder(7);
+ definition.setDefinitions("[urls]\n/events = handler");
+ defaults.setHandlerDefinitions(Collections.singletonList(definition));
+ assertEquals(7, creater.create(defaults).get(0).getOrder());
+ assertEquals("handler", creater.parseHandlerChainDefinitions(definition.getDefinitions()).get("/events"));
+ } finally {
+ context.close();
+ }
+ }
+
+ @EventRule("/events")
+ public static class RecordingHandler implements DisruptorHandler, Nameable {
+ private String name;
+ @Override public void doHandler(DisruptorEvent event, HandlerChain chain) { }
+ @Override public void setName(String name) { this.name = name; }
+ }
+
+ private static final class CapturingCreater extends DisruptorEventHandlerCreater {
+ private int parseCalls;
+ private Map chainDefinitions;
+
+ private CapturingCreater() {
+ super(null);
+ }
+
+ @Override
+ protected Map parseHandlerChainDefinitions(String definitions) {
+ parseCalls++;
+ return Collections.singletonMap("/events", "handler");
+ }
+
+ @Override
+ protected HandlerChainManager createHandlerChainManager(
+ Map> eventHandlers,
+ Map handlerChainDefinitionMap) {
+ chainDefinitions = Map.copyOf(handlerChainDefinitionMap);
+ return new DefaultHandlerChainManager();
+ }
+ }
+}
diff --git a/src/test/java/com/lmax/disruptor/spring/boot/context/DisruptorContextTest.java b/src/test/java/com/lmax/disruptor/spring/boot/context/DisruptorContextTest.java
new file mode 100644
index 0000000..6b66d87
--- /dev/null
+++ b/src/test/java/com/lmax/disruptor/spring/boot/context/DisruptorContextTest.java
@@ -0,0 +1,58 @@
+package com.lmax.disruptor.spring.boot.context;
+
+import com.lmax.disruptor.event.DisruptorEvent;
+import com.lmax.disruptor.event.DisruptorEventPublisher;
+import com.lmax.disruptor.event.DisruptorEventPublisherAware;
+import com.lmax.disruptor.spring.boot.event.DisruptorApplicationEvent;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.Aware;
+import org.springframework.context.support.GenericApplicationContext;
+
+import java.util.concurrent.atomic.AtomicReference;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+class DisruptorContextTest {
+ @Test
+ void forwardsEventsToSpringContext() {
+ GenericApplicationContext spring = new GenericApplicationContext();
+ AtomicReference received = new AtomicReference<>();
+ spring.addApplicationListener(event -> {
+ if (event instanceof DisruptorApplicationEvent) {
+ received.set((DisruptorApplicationEvent) event);
+ }
+ });
+ spring.refresh();
+ try {
+ DisruptorApplicationContext context = new DisruptorApplicationContext();
+ context.setApplicationContext(spring);
+ DisruptorEvent event = new DisruptorEvent("source");
+ context.publishEvent(event);
+ assertSame(spring, context.getApplicationContext());
+ assertSame(event, received.get().getSource());
+ } finally {
+ spring.close();
+ }
+ }
+
+ @Test
+ void injectsPublisherIntoAwareBeans() throws Exception {
+ GenericApplicationContext spring = new GenericApplicationContext();
+ DisruptorEventAwareProcessor processor = new DisruptorEventAwareProcessor();
+ processor.setApplicationContext(spring);
+ processor.afterPropertiesSet();
+ PublisherAwareBean bean = new PublisherAwareBean();
+ assertSame(bean, processor.postProcessBeforeInitialization(bean, "bean"));
+ assertNotNull(bean.publisher);
+ assertSame(bean, processor.postProcessAfterInitialization(bean, "bean"));
+ Object ordinary = new Object();
+ assertSame(ordinary, processor.postProcessBeforeInitialization(ordinary, "ordinary"));
+ }
+
+ private static final class PublisherAwareBean implements Aware, DisruptorEventPublisherAware {
+ private DisruptorEventPublisher publisher;
+ @Override public void setDisruptorEventPublisher(DisruptorEventPublisher publisher) {
+ this.publisher = publisher;
+ }
+ }
+}
diff --git a/src/test/java/com/lmax/disruptor/spring/boot/context/event/EventPublicationInterceptorTest.java b/src/test/java/com/lmax/disruptor/spring/boot/context/event/EventPublicationInterceptorTest.java
new file mode 100644
index 0000000..e488b77
--- /dev/null
+++ b/src/test/java/com/lmax/disruptor/spring/boot/context/event/EventPublicationInterceptorTest.java
@@ -0,0 +1,48 @@
+package com.lmax.disruptor.spring.boot.context.event;
+
+import com.lmax.disruptor.event.DisruptorEvent;
+import org.aopalliance.intercept.MethodInvocation;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.AccessibleObject;
+import java.lang.reflect.Method;
+import java.util.concurrent.atomic.AtomicReference;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+class EventPublicationInterceptorTest {
+ @Test
+ void publishesAnEventAfterTheMethodReturns() throws Throwable {
+ EventPublicationInterceptor interceptor = new EventPublicationInterceptor();
+ AtomicReference published = new AtomicReference<>();
+ interceptor.setApplicationEventClass(PublishedEvent.class);
+ interceptor.setDisruptorEventPublisher(published::set);
+ interceptor.afterPropertiesSet();
+ Object target = new Object();
+ MethodInvocation invocation = new MethodInvocation() {
+ @Override public Method getMethod() { return null; }
+ @Override public Object[] getArguments() { return new Object[0]; }
+ @Override public Object proceed() { return "result"; }
+ @Override public Object getThis() { return target; }
+ @Override public AccessibleObject getStaticPart() { return null; }
+ };
+ assertEquals("result", interceptor.invoke(invocation));
+ assertSame(target, published.get().getSource());
+ }
+
+ @Test
+ void rejectsInvalidEventClassesAndMissingConfiguration() {
+ EventPublicationInterceptor interceptor = new EventPublicationInterceptor();
+ assertThrows(IllegalArgumentException.class, interceptor::afterPropertiesSet);
+ assertThrows(IllegalArgumentException.class, () -> interceptor.setApplicationEventClass(DisruptorEvent.class));
+ assertThrows(IllegalArgumentException.class, () -> interceptor.setApplicationEventClass(String.class));
+ assertThrows(IllegalArgumentException.class, () -> interceptor.setApplicationEventClass(NoObjectConstructor.class));
+ }
+
+ public static class PublishedEvent extends DisruptorEvent {
+ public PublishedEvent(Object source) { super(source); }
+ }
+ public static class NoObjectConstructor extends DisruptorEvent {
+ public NoObjectConstructor() { super(); }
+ }
+}
diff --git a/src/test/java/com/lmax/disruptor/spring/boot/event/DisruptorApplicationEventTest.java b/src/test/java/com/lmax/disruptor/spring/boot/event/DisruptorApplicationEventTest.java
new file mode 100644
index 0000000..e511d73
--- /dev/null
+++ b/src/test/java/com/lmax/disruptor/spring/boot/event/DisruptorApplicationEventTest.java
@@ -0,0 +1,17 @@
+package com.lmax.disruptor.spring.boot.event;
+
+import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.*;
+
+class DisruptorApplicationEventTest {
+ @Test
+ void carriesOptionalBoundPayload() {
+ Object source = new Object();
+ DisruptorApplicationEvent event = new DisruptorApplicationEvent(source);
+ assertSame(source, event.getSource());
+ assertNull(event.getBind());
+ event.bind("payload");
+ assertEquals("payload", event.getBind());
+ assertEquals("bound", new DisruptorApplicationEvent(source, "bound").getBind());
+ }
+}