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
27 changes: 24 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,35 @@ 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: ['test/*', 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]
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]
branches: ['test/*', 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]

permissions:
contents: read

jobs:
build:
build-boot-2:
if: github.base_ref == '2.7.x' || startsWith(github.base_ref, 'test/') || github.ref_name == '2.7.x' || startsWith(github.ref_name, 'test/')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '8'
cache: maven
- uses: actions/checkout@v4
with:
repository: easy-4-java/disruptor-extension
ref: e9f81a75c8b4c8316dde4e4f852a092bffe59179
path: disruptor-extension-source
- name: Build matching extension snapshot
working-directory: disruptor-extension-source
run: mvn -B clean install
- name: Verify starter
run: mvn -B clean verify
build-other:
if: github.base_ref != '2.7.x' && !startsWith(github.base_ref, 'test/') && github.ref_name != '2.7.x' && !startsWith(github.ref_name, 'test/')
uses: easy-4-java/.github/.github/workflows/maven-starter-ci.yml@main
secrets: inherit
2 changes: 2 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ Starter 自动装配以下 Bean:

## 6. 快速开始

需要按事件路由到单个责任链处理器时,参见 [2.7.x 一对一匹配示例](docs/handler-chain.md)。

### 6.1 引入依赖

在 `pom.xml` 中添加上述依赖。
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 责任链的一对一匹配(2.7.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`,路由规则也必须包含对应的命名空间前缀。

`DisruptorComponentTests.oneToOneRouteCallsOnlyItsConfiguredHandler` 使用真实分发器验证两条路由分别只调用各自的处理器。多个处理器串联时,`definition-map` 的值用逗号分隔 Bean 名称,例如 `createdHandler,auditHandler`,并由处理器按责任链协议继续调用 `chain.doHandler(event)`。
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@
</additionalClasspathElements>
<includes>
<include>**/*Test.java</include>
<include>**/*_Test.java</include>
<include>**/*_Test.java</include>
<include>**/*Tests.java</include>
</includes>
<excludes>
<exclude>**/TestBean.java</exclude>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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 @@ -23,9 +24,9 @@
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 org.springframework.util.StringUtils;

import com.lmax.disruptor.EventFactory;
import com.lmax.disruptor.EventTranslatorOneArg;
Expand Down Expand Up @@ -53,7 +54,6 @@
import com.lmax.disruptor.event.translator.DisruptorEventThreeArgTranslator;
import com.lmax.disruptor.event.translator.DisruptorEventTwoArgTranslator;
import com.lmax.disruptor.hooks.DisruptorShutdownHook;
import com.lmax.disruptor.util.StringUtils;
import com.lmax.disruptor.util.WaitStrategys;
import com.lmax.disruptor.DisruptorTemplate;

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

return disruptorEventHandlers;
}
Expand All @@ -171,7 +171,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 @@ -264,7 +264,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
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
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;
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
Loading
Loading