Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions .github/workflows/ci-main-boot26.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: CI main Boot 2.6

on:
workflow_dispatch:
push:
branches: [main]
pull_request:
branches: [main]

permissions:
contents: read

jobs:
verify:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- name: Checkout starter
uses: actions/checkout@v4
with:
path: starter
- name: Set up JDK 8
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '8'
cache: maven
- name: Checkout matching Disruptor extension
uses: actions/checkout@v4
with:
repository: easy-4-java/disruptor-extension
ref: e9f81a75c8b4c8316dde4e4f852a092bffe59179
path: disruptor-extension-source
- name: Install matching extension snapshot
working-directory: disruptor-extension-source
run: mvn -B --no-transfer-progress clean install
- name: Verify Boot 2.6 starter
working-directory: starter
run: mvn -B --no-transfer-progress clean verify
- name: Upload test reports
if: always()
uses: actions/upload-artifact@v4
with:
name: boot26-test-reports
path: starter/target/surefire-reports
if-no-files-found: ignore
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,13 @@ Then inject the auto-configured bean in your code:
private WaitStrategy waitStrategy;
```

For one-to-one handler routing on the Spring Boot 2.6 line, see the [handler chain example](docs/handler-chain.md).

## 7. Configuration Reference

### 7.1 Config Prefix

`disruptor`
`spring.disruptor`

### 7.2 Configuration Items

Expand Down
4 changes: 3 additions & 1 deletion README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,13 @@ public class Application {
private WaitStrategy waitStrategy;
```

Spring Boot 2.6 版本线的一对一处理器路由配置见[责任链示例](docs/handler-chain.md)。

## 7. 配置参考

### 7.1 配置前缀

`disruptor`
`spring.disruptor`

### 7.2 配置项

Expand Down
29 changes: 29 additions & 0 deletions docs/handler-chain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Disruptor 责任链的一对一匹配(Spring Boot 2.6.x)

`DisruptorEvent` 使用 `namespace/topic/tag` 生成路由;`namespace` 为空时,路由以 `/topic/tag` 开头。每条路由可以只指定一个 Spring Bean 名称,因此一个事件只进入对应的处理器。配置前缀是 `spring.disruptor`。

```yaml
spring:
disruptor:
enabled: true
handler-definitions:
- order: 0
definition-map:
/orders/created: createdHandler
/orders/cancelled: cancelledHandler
```
```java
@Component("createdHandler")
@EventRule("/orders/created")
public class CreatedHandler implements DisruptorHandler<DisruptorEvent> {
@Override
public void doHandler(DisruptorEvent event, HandlerChain<DisruptorEvent> chain) {
// 处理订单创建事件
}
}
```

发布事件时,设置 `topic=orders`、`tag=created`,其路由即 `/orders/created`。`cancelledHandler` 不会处理该事件。两个处理器的定义与上例相同,只需替换 Bean 名称、`@EventRule` 路由和业务逻辑。若设置了 `namespace`,路由规则也必须包含对应的命名空间前缀。

`DisruptorRoutingTests.routesEachOrderEventToItsConfiguredHandler` 使用真实分发器验证两条路由分别只调用各自的处理器。多个处理器串联时,`definition-map` 的值用逗号分隔 Bean 名称,例如 `createdHandler,auditHandler`,并由处理器按责任链协议继续调用 `chain.doHandler(event)`。
7 changes: 3 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,6 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<!-- 跳过单元测试 -->
<skip>true</skip>
<skipTests>true</skipTests>
<!-- forkMode 可设置值有 “never”, “once”, “always” 和 “pertest”。 pretest:
每一个测试创建一个新进程,为每个测试创建新的JVM是单独测试的最彻底方式,但也是最慢的,不适合hudson上持续回归。 once:在一个进程中进行所有测试。once为默认设置,在Hudson上持续回归时建议使用默认设置。
always:在一个进程中并行的运行脚本,Junit4.7以上版本才可以使用,surefire的版本要在2.6以上提供这个功能,其中 threadCount:执行时,指定可分配的线程数量。只和参数parallel配合使用有效。默认:5。 -->
Expand All @@ -175,7 +172,9 @@
</additionalClasspathElements>
<includes>
<include>**/*Test.java</include>
<include>**/*Tests.java</include>
</includes>
<failIfNoTests>true</failIfNoTests>
<excludes>
<exclude>**/TestBean.java</exclude>
</excludes>
Expand Down Expand Up @@ -361,7 +360,7 @@
<java.version>1.8</java.version>
<commons-collections4.version>4.1</commons-collections4.version>
<disruptor.version>3.4.3</disruptor.version>
<disruptor-extension.version>1.0.1.20260630-SNAPSHOT</disruptor-extension.version>
<disruptor-extension.version>1.0.x.20260630-SNAPSHOT</disruptor-extension.version>
<maven.version>3.0</maven.version>
<maven-gpg-plugin.version>1.6</maven-gpg-plugin.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.lmax.disruptor.spring.boot;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
Expand All @@ -11,6 +11,7 @@
import java.util.concurrent.ThreadFactory;

import com.lmax.disruptor.*;
import com.lmax.disruptor.DisruptorTemplate;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -25,33 +26,32 @@
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.OrderComparator;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.EventHandlerGroup;
import com.lmax.disruptor.dsl.ProducerType;
import com.lmax.disruptor.spring.boot.annotation.EventRule;
import com.lmax.disruptor.spring.boot.config.EventHandlerDefinition;
import com.lmax.disruptor.spring.boot.config.Ini;
import com.lmax.disruptor.annotation.EventRule;
import com.lmax.disruptor.config.EventHandlerDefinition;
import com.lmax.disruptor.config.Ini;
import com.lmax.disruptor.spring.boot.context.DisruptorEventAwareProcessor;
import com.lmax.disruptor.spring.boot.event.DisruptorApplicationEvent;
import com.lmax.disruptor.spring.boot.event.DisruptorEvent;
import com.lmax.disruptor.spring.boot.event.factory.DisruptorBindEventFactory;
import com.lmax.disruptor.spring.boot.event.factory.DisruptorEventThreadFactory;
import com.lmax.disruptor.spring.boot.event.handler.DisruptorEventDispatcher;
import com.lmax.disruptor.spring.boot.event.handler.DisruptorHandler;
import com.lmax.disruptor.spring.boot.event.handler.Nameable;
import com.lmax.disruptor.spring.boot.event.handler.chain.HandlerChainManager;
import com.lmax.disruptor.spring.boot.event.handler.chain.def.DefaultHandlerChainManager;
import com.lmax.disruptor.spring.boot.event.handler.chain.def.PathMatchingHandlerChainResolver;
import com.lmax.disruptor.spring.boot.event.translator.DisruptorEventOneArgTranslator;
import com.lmax.disruptor.spring.boot.event.translator.DisruptorEventThreeArgTranslator;
import com.lmax.disruptor.spring.boot.event.translator.DisruptorEventTwoArgTranslator;
import com.lmax.disruptor.spring.boot.hooks.DisruptorShutdownHook;
import com.lmax.disruptor.spring.boot.util.StringUtils;
import com.lmax.disruptor.spring.boot.util.WaitStrategys;
import com.lmax.disruptor.event.DisruptorEvent;
import com.lmax.disruptor.event.DisruptorEventFactory;
import com.lmax.disruptor.event.factory.DisruptorEventThreadFactory;
import com.lmax.disruptor.event.handler.DisruptorEventDispatcher;
import com.lmax.disruptor.event.handler.DisruptorHandler;
import com.lmax.disruptor.event.handler.Nameable;
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.event.translator.DisruptorEventOneArgTranslator;
import com.lmax.disruptor.event.translator.DisruptorEventThreeArgTranslator;
import com.lmax.disruptor.event.translator.DisruptorEventTwoArgTranslator;
import com.lmax.disruptor.hooks.DisruptorShutdownHook;
import org.springframework.util.StringUtils;
import com.lmax.disruptor.util.WaitStrategys;


import org.springframework.util.CollectionUtils;
Expand Down Expand Up @@ -89,7 +89,7 @@ public ThreadFactory threadFactory() {
@Bean
@ConditionalOnMissingBean
public EventFactory<DisruptorEvent> eventFactory() {
return new DisruptorBindEventFactory();
return new DisruptorEventFactory();
}

/*
Expand Down Expand Up @@ -158,7 +158,7 @@ public List<DisruptorEventDispatcher> disruptorEventHandlers(DisruptorProperties
}
}
// 进行排序
Collections.sort(disruptorEventHandlers, new OrderComparator());
disruptorEventHandlers.sort(Comparator.comparingInt(DisruptorEventDispatcher::getOrder));

return disruptorEventHandlers;
}
Expand All @@ -169,7 +169,7 @@ public List<DisruptorEventDispatcher> disruptorEventHandlers(DisruptorProperties
protected DisruptorEventDispatcher createDisruptorEventHandler(EventHandlerDefinition handlerDefinition,
Map<String, DisruptorHandler<DisruptorEvent>> 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());
Expand Down Expand Up @@ -262,7 +262,7 @@ public Disruptor<DisruptorEvent> disruptor(
if (!ObjectUtils.isEmpty(disruptorEventHandlers)) {

// 进行排序
Collections.sort(disruptorEventHandlers, new OrderComparator());
disruptorEventHandlers.sort(Comparator.comparingInt(DisruptorEventDispatcher::getOrder));

// 使用disruptor创建消费者组
EventHandlerGroup<DisruptorEvent> handlerGroup = null;
Expand Down Expand Up @@ -310,7 +310,7 @@ public EventTranslatorThreeArg<DisruptorEvent, String, String, String> threeArgE
}

@Bean
public DisruptorTemplate disruptorTemplate(Disruptor<DisruptorEvent> disruptor, DisruptorEventOneArgTranslator oneArgEventTranslator) {
public DisruptorTemplate disruptorTemplate(Disruptor<DisruptorEvent> disruptor, EventTranslatorOneArg<DisruptorEvent, DisruptorEvent> oneArgEventTranslator) {
return new DisruptorTemplate(disruptor, oneArgEventTranslator);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import org.springframework.boot.context.properties.ConfigurationProperties;

import com.lmax.disruptor.spring.boot.config.EventHandlerDefinition;
import com.lmax.disruptor.config.EventHandlerDefinition;

@ConfigurationProperties(DisruptorProperties.PREFIX)
public class DisruptorProperties {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import com.lmax.disruptor.SequenceBarrier;
import com.lmax.disruptor.WaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.spring.boot.event.DisruptorEvent;
import com.lmax.disruptor.spring.boot.event.factory.DisruptorBindEventFactory;
import com.lmax.disruptor.spring.boot.event.handler.DisruptorEventDispatcher;
import com.lmax.disruptor.spring.boot.util.WaitStrategys;
import com.lmax.disruptor.event.DisruptorEvent;
import com.lmax.disruptor.event.DisruptorEventFactory;
import com.lmax.disruptor.event.handler.DisruptorEventDispatcher;
import com.lmax.disruptor.util.WaitStrategys;

@Configuration
@ConditionalOnClass({ Disruptor.class })
Expand All @@ -47,7 +47,7 @@ public WaitStrategy waitStrategy() {
@Bean
@ConditionalOnMissingBean
public EventFactory<DisruptorEvent> eventFactory() {
return new DisruptorBindEventFactory();
return new DisruptorEventFactory();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.lmax.disruptor.spring.boot.event.DisruptorEventPublisher;
import com.lmax.disruptor.event.DisruptorEventPublisher;
import com.lmax.disruptor.spring.boot.event.DisruptorApplicationEvent;
import com.lmax.disruptor.spring.boot.event.DisruptorEvent;
import com.lmax.disruptor.event.DisruptorEvent;

public class DisruptorApplicationContext implements ApplicationContextAware, DisruptorEventPublisher {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@
import java.security.PrivilegedAction;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.Aware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.support.SecurityContextProvider;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.lmax.disruptor.spring.boot.event.DisruptorEventPublisherAware;
import com.lmax.disruptor.event.DisruptorEventPublisherAware;

public class DisruptorEventAwareProcessor implements ApplicationContextAware ,BeanPostProcessor, InitializingBean {

Expand Down Expand Up @@ -95,12 +94,9 @@ public Object run() {
}

protected void invokeAwareInterfaces(Object bean) {
if (bean instanceof Aware) {
//扩展 DisruptorEventPublisherAware
if (bean instanceof DisruptorEventPublisherAware) {
DisruptorEventPublisherAware awareBean = (DisruptorEventPublisherAware) bean;
awareBean.setDisruptorEventPublisher( this.disruptorContext );
}
if (bean instanceof DisruptorEventPublisherAware) {
DisruptorEventPublisherAware awareBean = (DisruptorEventPublisherAware) bean;
awareBean.setDisruptorEventPublisher(this.disruptorContext);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

import java.lang.reflect.Constructor;

import com.lmax.disruptor.spring.boot.event.DisruptorEventPublisher;
import com.lmax.disruptor.spring.boot.event.DisruptorEventPublisherAware;
import com.lmax.disruptor.event.DisruptorEventPublisher;
import com.lmax.disruptor.event.DisruptorEventPublisherAware;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.beans.factory.InitializingBean;

import com.lmax.disruptor.spring.boot.event.DisruptorEvent;
import com.lmax.disruptor.event.DisruptorEvent;

public class EventPublicationInterceptor
implements MethodInterceptor, DisruptorEventPublisherAware, InitializingBean {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
package org.apache.rocketmq.spring.boot;

import org.junit.Test;
import org.junit.runner.RunWith;
import com.lmax.disruptor.dsl.Disruptor;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

@Configuration
@RunWith(SpringRunner.class)
@SpringBootTest
import java.util.concurrent.Executor;

import static org.junit.jupiter.api.Assertions.assertNotNull;

@SpringBootTest(classes = DemoApplicationTests.TestApplication.class,
properties = {"spring.disruptor.enabled=true", "spring.config.name=disruptor-test"})
public class DemoApplicationTests {

@Test
public void contextLoads() {
}
@Autowired
private ApplicationContext applicationContext;

@Test
public void contextLoadsWithTwoExecutors() {
assertNotNull(applicationContext.getBean(Disruptor.class));
}

@SpringBootConfiguration
@EnableAutoConfiguration
static class TestApplication {

@Bean("applicationTaskExecutor")
Executor applicationTaskExecutor() {
return Runnable::run;
}

@Bean("taskScheduler")
Executor taskScheduler() {
return Runnable::run;
}
}
}
Loading
Loading