├── .gitignore
├── LICENSE
├── README.md
├── doc
├── picture
│ ├── event_send.jpg
│ └── eventbus-redis.png
├── sql
│ └── demo-init.sql
├── 架构知之.md
└── 测试点.text
├── eventbus-core
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── github
│ │ └── likavn
│ │ └── eventbus
│ │ └── core
│ │ ├── ConnectionWatchdog.java
│ │ ├── DeliveryBus.java
│ │ ├── ListenerRegistry.java
│ │ ├── TaskRegistry.java
│ │ ├── annotation
│ │ ├── EventbusListener.java
│ │ ├── FailRetry.java
│ │ ├── Order.java
│ │ ├── Polling.java
│ │ └── ToDelay.java
│ │ ├── api
│ │ ├── MsgDelayListener.java
│ │ ├── MsgListener.java
│ │ ├── MsgSender.java
│ │ ├── RequestIdGenerator.java
│ │ └── interceptor
│ │ │ ├── DeliverAfterInterceptor.java
│ │ │ ├── DeliverBeforeInterceptor.java
│ │ │ ├── DeliverThrowableLastInterceptor.java
│ │ │ ├── SendAfterInterceptor.java
│ │ │ └── SendBeforeInterceptor.java
│ │ ├── base
│ │ ├── AbstractSenderAdapter.java
│ │ ├── AcquireListeners.java
│ │ ├── InterceptorContainer.java
│ │ ├── Lifecycle.java
│ │ ├── MsgListenerContainer.java
│ │ ├── NodeTestConnect.java
│ │ └── UUIDRequestIdGenerator.java
│ │ ├── constant
│ │ └── BusConstant.java
│ │ ├── exception
│ │ ├── DeliverAfterInterceptorSuccessException.java
│ │ ├── DeliverBeforeInterceptorException.java
│ │ ├── DeliverInvokeException.java
│ │ └── EventBusException.java
│ │ ├── metadata
│ │ ├── BusConfig.java
│ │ ├── BusType.java
│ │ ├── MsgType.java
│ │ ├── data
│ │ │ ├── Message.java
│ │ │ ├── MsgBody.java
│ │ │ ├── Request.java
│ │ │ └── Topic.java
│ │ └── support
│ │ │ ├── FailTrigger.java
│ │ │ ├── Listener.java
│ │ │ └── Trigger.java
│ │ ├── support
│ │ ├── Fast2jsonProvider.java
│ │ ├── FastjsonProvider.java
│ │ ├── GsonProvider.java
│ │ ├── JacksonProvider.java
│ │ ├── spi
│ │ │ └── IJson.java
│ │ └── task
│ │ │ ├── CronExpression.java
│ │ │ ├── CronTask.java
│ │ │ ├── PeriodTask.java
│ │ │ ├── Task.java
│ │ │ ├── Timer.java
│ │ │ └── TimerTask.java
│ │ └── utils
│ │ ├── Assert.java
│ │ ├── CalculateUtil.java
│ │ ├── Func.java
│ │ ├── GroupedThreadPoolExecutor.java
│ │ ├── NamedThreadFactory.java
│ │ ├── PollThreadPoolExecutor.java
│ │ └── WaitThreadPoolExecutor.java
│ └── resources
│ └── META-INF
│ └── services
│ └── com.github.likavn.eventbus.core.support.spi.IJson
├── eventbus-demo
├── pom.xml
└── springboot-demo
│ ├── pom.xml
│ └── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── github
│ │ │ └── likavn
│ │ │ └── eventbus
│ │ │ └── demo
│ │ │ ├── EventBusApplication.java
│ │ │ ├── config
│ │ │ ├── EventbusConfiguration.java
│ │ │ └── MybatisPlusConfig.java
│ │ │ ├── constant
│ │ │ └── MsgConstant.java
│ │ │ ├── controller
│ │ │ ├── DataTableController.java
│ │ │ ├── TriggerController.java
│ │ │ └── vo
│ │ │ │ └── BsConsumerVO.java
│ │ │ ├── domain
│ │ │ ├── R.java
│ │ │ ├── TMsg.java
│ │ │ ├── TestBody.java
│ │ │ └── TestDelayBody.java
│ │ │ ├── entity
│ │ │ ├── BsConsumer.java
│ │ │ └── BsData.java
│ │ │ ├── enums
│ │ │ └── ConsumerStatus.java
│ │ │ ├── helper
│ │ │ └── BsHelper.java
│ │ │ ├── interceptor
│ │ │ ├── DemoDeliverAfterInterceptor.java
│ │ │ ├── DemoDeliverBeforeInterceptor.java
│ │ │ ├── DemoDeliverBeforeInterceptor2.java
│ │ │ ├── DemoDeliverBeforeInterceptor3.java
│ │ │ ├── DemoDeliverThrowableLastInterceptor.java
│ │ │ ├── DemoSendAfterInterceptor.java
│ │ │ └── DemoSendBeforeInterceptor.java
│ │ │ ├── listener
│ │ │ ├── MsgDelayListener.java
│ │ │ ├── MsgDelayListener2.java
│ │ │ ├── MsgDelayListenerClassName.java
│ │ │ ├── MsgDelayListenerCode.java
│ │ │ ├── MsgListener.java
│ │ │ ├── MsgListener2.java
│ │ │ ├── MsgListenerClassName.java
│ │ │ ├── MsgListenerCode.java
│ │ │ └── test
│ │ │ │ ├── TestArrayListener.java
│ │ │ │ ├── TestBeanObjectListener.java
│ │ │ │ ├── TestIntegerListener.java
│ │ │ │ ├── TestListListener.java
│ │ │ │ ├── TestMapListener.java
│ │ │ │ └── TestStringListener.java
│ │ │ ├── mapper
│ │ │ ├── BsConsumerMapper.java
│ │ │ └── BsDataMapper.java
│ │ │ └── service
│ │ │ ├── BsConsumerService.java
│ │ │ ├── BsConsumerServiceImpl.java
│ │ │ ├── BsDataService.java
│ │ │ └── BsDataServiceImpl.java
│ └── resources
│ │ ├── application.yml
│ │ ├── banner.txt
│ │ ├── logback-spring.xml
│ │ ├── mapper
│ │ └── BsConsumerMapper.xml
│ │ └── static
│ │ └── index.html
│ └── test
│ └── java
│ └── com
│ └── github
│ └── likavn
│ └── eventbus
│ ├── JSONProviderTest.java
│ ├── MsgSenderTest.java
│ ├── TaskRegistryTest.java
│ └── utils
│ ├── CalculateUtilTest.java
│ └── GroupedThreadPoolExecutorTest.java
├── eventbus-spring-boot-starter
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── github
│ │ └── likavn
│ │ └── eventbus
│ │ ├── BootConnectionWatchdog.java
│ │ ├── ConditionalOnEventbusActive.java
│ │ ├── OnEventbusActiveCondition.java
│ │ ├── config
│ │ ├── EventBusAutoConfiguration.java
│ │ ├── EventBusEnvironmentContextInitializer.java
│ │ └── RequestIdGeneratorConfiguration.java
│ │ ├── prop
│ │ └── BusProperties.java
│ │ └── provider
│ │ ├── SFieldFunction.java
│ │ ├── SingleCodeListener.java
│ │ ├── rabbit
│ │ ├── RabbitMsgSender.java
│ │ ├── RabbitMsgSubscribeListener.java
│ │ ├── RabbitNodeTestConnect.java
│ │ ├── config
│ │ │ └── BusBootRabbitConfiguration.java
│ │ ├── constant
│ │ │ └── RabbitConstant.java
│ │ └── support
│ │ │ ├── AbstractRabbitRegisterContainer.java
│ │ │ └── RabbitListener.java
│ │ ├── redis
│ │ ├── RLock.java
│ │ ├── RedisMsgSender.java
│ │ ├── RedisMsgSubscribeListener.java
│ │ ├── RedisNodeTestConnect.java
│ │ ├── RedisPendingMsgResendTask.java
│ │ ├── RedisStreamExpiredTask.java
│ │ ├── RedisZSetPushMsgStreamTask.java
│ │ ├── config
│ │ │ └── BusBootRedisConfiguration.java
│ │ ├── constant
│ │ │ └── RedisConstant.java
│ │ └── support
│ │ │ ├── AbstractStreamListenerContainer.java
│ │ │ ├── RedisListener.java
│ │ │ ├── XDefaultStreamMessageListenerContainer.java
│ │ │ ├── XReadOffsetStrategy.java
│ │ │ └── XStreamPollTask.java
│ │ └── rocket
│ │ ├── RocketMsgSender.java
│ │ ├── RocketMsgSubscribeListener.java
│ │ ├── RocketNodeTestConnect.java
│ │ ├── config
│ │ └── BusBootRocketConfiguration.java
│ │ ├── constant
│ │ └── RocketConstant.java
│ │ └── support
│ │ ├── AbstractRocketRegisterContainer.java
│ │ └── RocketListener.java
│ └── resources
│ ├── META-INF
│ ├── spring-configuration-metadata.json
│ ├── spring.factories
│ └── spring
│ │ └── org.springframework.boot.autoconfigure.AutoConfiguration.imports
│ └── script
│ ├── lock.lua
│ ├── pushMsgStream.lua
│ └── zsetAdd.lua
├── eventbus-spring-boot3-starter
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── github
│ └── likavn
│ └── eventbus
│ └── provider
│ └── redis
│ └── RedisPendingMsgResendTask.java
└── pom.xml
/.gitignore:
--------------------------------------------------------------------------------
1 | ######################################################################
2 | # Build Tools
3 |
4 | .gradle
5 | /build/
6 | !gradle/wrapper/gradle-wrapper.jar
7 |
8 | target/
9 | !.mvn/wrapper/maven-wrapper.jar
10 |
11 | ######################################################################
12 | # IDE
13 |
14 | ### STS ###
15 | .apt_generated
16 | .classpath
17 | .factorypath
18 | .project
19 | .settings
20 | .springBeans
21 |
22 | ### IntelliJ IDEA ###
23 | .idea
24 | .logs
25 | *.iws
26 | *.iml
27 | *.ipr
28 |
29 | ### JRebel ###
30 | rebel.xml
31 |
32 | ### NetBeans ###
33 | nbproject/private/
34 | build/*
35 | nbbuild/
36 | dist/
37 | nbdist/
38 | .nb-gradle/
39 |
40 | ######################################################################
41 | # Others
42 | *mydev.yml
43 | *.log
44 | *.class
45 | *.xml.versionsBackup
46 | *.swp
47 | *.flattened-pom.xml
48 |
49 | !*/build/*.java
50 | !*/build/*.html
51 | !*/build/*.xml
52 |
--------------------------------------------------------------------------------
/doc/picture/event_send.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/likavn/eventbus/3bf9a0e559f929c22f91095141855fd5c2201f67/doc/picture/event_send.jpg
--------------------------------------------------------------------------------
/doc/picture/eventbus-redis.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/likavn/eventbus/3bf9a0e559f929c22f91095141855fd5c2201f67/doc/picture/eventbus-redis.png
--------------------------------------------------------------------------------
/doc/sql/demo-init.sql:
--------------------------------------------------------------------------------
1 | -- eventbus.bs_data definition
2 | DROP TABLE if EXISTS bs_data;
3 | CREATE TABLE `bs_data`
4 | (
5 | `request_id` varchar(64) NOT NULL COMMENT '事件ID/消息ID,默认UUID,可自定义实现接口(RequestIdGenerator)获取消息的ID',
6 | `service_id` varchar(100) NOT NULL COMMENT '消息所属来源服务ID/服务名',
7 | `code` varchar(256) DEFAULT NULL COMMENT '消息编码',
8 | `type` int(1) DEFAULT NULL COMMENT '消息类型,1及时消息、2延时消息',
9 | `delay_time` bigint DEFAULT NULL COMMENT '延时消息的延时时间,单位:秒',
10 | `headers` varchar(512) DEFAULT NULL COMMENT '消息头信息',
11 | `body` text COMMENT '消息数据的JSON串',
12 | `ip_address` varchar(32) DEFAULT NULL COMMENT '消息发送者IP地址',
13 | `create_time` datetime DEFAULT NULL COMMENT '创建时间',
14 | PRIMARY KEY (`request_id`)
15 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='eventbus数据表';
16 |
17 | -- eventbus.bs_consumer definition
18 | DROP TABLE if EXISTS bs_consumer;
19 | CREATE TABLE `bs_consumer`
20 | (
21 | `id` bigint(20) NOT NULL COMMENT 'id',
22 | `request_id` varchar(64) NOT NULL COMMENT '事件ID/消息ID,默认UUID,可自定义实现接口(RequestIdGenerator)获取消息的ID',
23 | `service_id` varchar(100) NOT NULL COMMENT '消息监听器所属服务ID/服务名',
24 | `code` varchar(256) DEFAULT NULL COMMENT '消息编码',
25 | `type` int(1) DEFAULT NULL COMMENT '消息类型,1及时消息、2延时消息',
26 | `delay_time` bigint DEFAULT NULL COMMENT '延时时间,单位:秒',
27 | `deliver_id` varchar(256) NOT NULL COMMENT '消息接收处理器(消费者ID)ID=消息监听器全类名{@link Trigger#getDeliverId()}',
28 | `deliver_count` int(11) DEFAULT NULL COMMENT '消息投递次数',
29 | `ip_address` varchar(32) DEFAULT NULL COMMENT '消息接收者IP地址',
30 | `polling_count` int(11) DEFAULT NULL COMMENT '消费者轮询次数',
31 | `fail_retry_count` int(11) DEFAULT NULL COMMENT '消费者接收失败后时,发起失败重试的次数',
32 | `to_delay` int(11) DEFAULT NULL COMMENT '是否已转为延迟消息,0否、1是',
33 | `status` int(1) DEFAULT NULL COMMENT '消息接收状态:0待处理、1处理成功、2处理失败',
34 | `success_time` datetime DEFAULT NULL COMMENT '接收消息处理成功时间',
35 | `exception_time` datetime DEFAULT NULL COMMENT '发生异常的时间',
36 | `exception_message` text DEFAULT NULL COMMENT '异常信息',
37 | `exception_stack_trace` mediumtext COMMENT '异常堆栈信息',
38 | `create_time` datetime DEFAULT NULL COMMENT '创建时间',
39 | `update_time` datetime DEFAULT NULL COMMENT '更新时间',
40 | PRIMARY KEY (`id`),
41 | KEY `bs_consumer_request_id_IDX` (`request_id`,`deliver_id`) USING BTREE
42 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='eventbus消费者';
--------------------------------------------------------------------------------
/doc/架构知之.md:
--------------------------------------------------------------------------------
1 | # eventbus架构知之
2 |
3 | ## RabbitMQ
4 |
5 | ### 及时消息
6 |
7 | #### 正常消息
8 |
9 | 交换机key: `ebus.t.exchange.服务ID`
10 | 路由key: `ebus.t.routingKey.服务ID|消息编码`
11 | 队列key: `ebus.t.queue.服务ID|消息编码|监听器全类名`
12 |
13 | #### 异常重试
14 |
15 | 交换机key: `ebus.d.exchange.服务ID`
16 | 路由key: `ebus.t.retry.routingKey.服务ID|消息编码|监听器全类名`
17 | 队列key: `ebus.t.retry.queue.服务ID|消息编码|监听器全类名`
18 |
19 | ### 延时消息
20 |
21 | #### 正常消息
22 |
23 | 交换机key: `ebus.d.exchange.服务ID`
24 | 路由key: `ebus.d.routingKey.服务ID|消息编码`
25 | 队列key: `ebus.d.queue.服务ID|消息编码|监听器全类名`
26 |
27 | #### 异常重试
28 |
29 | 交换机key: `ebus.d.exchange.服务ID`
30 | 路由key: `ebus.d.retry.routingKey.服务ID|消息编码|监听器全类名`
31 | 队列key: `ebus.d.retry.queue.服务ID|消息编码|监听器全类名`
32 |
33 | ## Redis
34 |
35 | ### 架构图
36 | 如下图:
37 |
38 |
39 | ### 及时消息:
40 |
41 | #### 正常队列
42 |
43 | stream队列key:`ebus:t:queue:{服务ID|消息编码}`
44 | stream消费者组key:`监听器类全类名`
45 |
46 | #### 失败重试
47 |
48 | zset key: `ebus:t:retry:zset:{服务ID|消息编码|监听器类全类名}`
49 | lock key: `ebus:t:retry:lock:{服务ID|消息编码|监听器类全类名}`
50 | stream queue key: `ebus:t:retry:queue:{服务ID|消息编码|监听器类全类名}`
51 | stream消费者组key: `监听器类全类名`
52 |
53 | ### 延时消息
54 |
55 | #### 正常消息
56 |
57 | zset key: `ebus:d:zset:{服务ID|消息编码}`
58 | lock key: `ebus:d:lock:{服务ID|消息编码}`
59 | stream队列key: `ebus:d:queue:{服务ID|消息编码}`
60 | stream消费者组key:`监听器类全类名`
61 |
62 | #### 失败重试
63 |
64 | zset key: `ebus:d:retry:zset:{服务ID|消息编码|监听器类全类名}`
65 | lock key: `ebus:d:retry:lock:{服务ID|消息编码|监听器类全类名}`
66 | stream队列key: `ebus:d:retry:queue:{服务ID|消息编码|监听器类全类名}`
67 | stream消费者组key: `监听器类全类名`
68 |
69 | ## RocketMQ
70 |
71 | ### 及时消息
72 |
73 | #### 正常消息
74 |
75 | 队列key: `ebus_t_服务ID|消息编码`
76 | 消费者组名称:`ebus_t_服务ID|消息编码|消息监听器全类名`
77 |
78 | #### 异常重试
79 |
80 | 队列key: `ebus_t_retry_服务ID|消息编码|消息监听器全类名`
81 | 消费者组名称: `ebus_t_retry_服务ID|消息编码|消息监听器全类名`
82 |
83 | ### 延时消息
84 |
85 | #### 正常消息
86 |
87 | 队列key: `ebus_d_服务ID|消息编码`
88 | 消费者组名称:`ebus_d_服务ID|消息编码|消息监听器全类名`
89 |
90 | #### 异常重试
91 |
92 | 队列key: `ebus_d_retry_服务ID|消息编码|消息监听器全类名`
93 | 消费者组名称: `ebus_d_retry_服务ID|消息编码|消息监听器全类名`
94 |
--------------------------------------------------------------------------------
/doc/测试点.text:
--------------------------------------------------------------------------------
1 | 公共:
2 | 1.不同数据类型(String/Integer(基础数据类型的包装类型)/自定义对象/数组/List/Map等)消息体发送及接收测试;
3 | 2.及时消息延时消息发送和接收测试;
4 | 3.接收消息抛出异常,测试失败注解@FailRetry,测试消息监听器的异常捕获方法、重试次数count、重试下次触发时间nextTime等;
5 | 4.接收消息抛出异常,接收消息失败重试;
6 | 5.监听器(消费者)消息组件启动/关闭,关闭时系统不在接收新的MQ消息;
7 | 6.网络断连,消息监听器自动关闭测试;
8 | 7.网络断连,定时联通检测,测试网络重连继续消费;
9 | 8.监听器(消费者)并发数、每次拉取消息数量变更测试;
10 | 9.接收消息时数据库事务是否有效;
11 |
12 | redis:
13 | 测试stream超时未确认消息重新投递;
14 | 测试stream过期消息删除;
15 | 测试定时任务断连重启;
--------------------------------------------------------------------------------
/eventbus-core/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | *
10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.likavn.eventbus.core.annotation; 17 | 18 | import com.github.likavn.eventbus.core.metadata.BusConfig; 19 | import com.github.likavn.eventbus.core.metadata.data.MsgBody; 20 | 21 | import java.lang.annotation.*; 22 | 23 | /** 24 | * 消息监听/订阅器注解 25 | *
26 | * 用于标记对及时/延时消息的监听类,可以通过此注解定制消息处理的详细行为,如所属服务、消息类型和并发级别等。 27 | * 28 | * @author likavn 29 | * @date 2024/07/27 30 | * @since 2.5 31 | **/ 32 | @Documented 33 | @Target({ElementType.TYPE}) 34 | @Retention(RetentionPolicy.RUNTIME) 35 | public @interface EventbusListener { 36 | 37 | /** 38 | * 消息所属来源服务ID或服务名。默认订阅本服务{@link BusConfig#getServiceId()}配置的ID 39 | * 40 | * @see BusConfig#getServiceId() 41 | */ 42 | String serviceId() default ""; 43 | 44 | /** 45 | * 监听器订阅的消息类型,用于区分不同的消息类型。 46 | *
47 | * 为空时: 48 | * 1.如果消息实体不继承接口{@link MsgBody},则默认为当前监听器的类名。 49 | * 2.如果消息实体继承接口{@link MsgBody},则默认为{@link MsgBody#code()}。 50 | *
51 | * 监听器订阅code优先如下: 52 | * 53 | * @see #codes 54 | * @see MsgBody#code() 55 | * 当前监听器类名 56 | */ 57 | String[] codes() default {}; 58 | 59 | /** 60 | * 消息接收并发数,默认值为-1。 61 | * 62 | * @return 返回并发级别的整数值。设置-1表示未设置,默认{@link BusConfig#getConcurrency()}。 63 | */ 64 | int concurrency() default -1; 65 | 66 | /** 67 | * 重发/重试消息接收并发数,默认值为-1。 68 | * 69 | * @return 返回并发级别的整数值。设置-1表示未设置,默认{@link BusConfig#getRetryConcurrency()} ()}。 70 | */ 71 | int retryConcurrency() default -1; 72 | } 73 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/com/github/likavn/eventbus/core/annotation/FailRetry.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2023-2033, likavn (likavn@163.com). 3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | *
10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.likavn.eventbus.core.annotation; 17 | 18 | import com.github.likavn.eventbus.core.metadata.BusConfig; 19 | import lombok.experimental.UtilityClass; 20 | 21 | import java.lang.annotation.*; 22 | 23 | /** 24 | * 投递重试注解 25 | *
26 | * 定义了一个注解@FailRetry,用于标注在方法上以控制消息订阅的轮询行为。 27 | * 轮询可以通过注解的属性count设置重试次数,nextTime或interval进行配置下次消息的重试时间。 28 | *
29 | * 获取有效的(间隔时间>0)下一次重试时间,优先级从上至下:
30 | * 1.通过{@link FailRetry.Keep#setNextTime(long)}设置下次重试时间;
31 | * 2.通过{@link FailRetry#nextTime()}设置下次重试时间;
32 | * 3.通过{@link FailRetry#interval()}设置下次重试时间的表达式;
33 | * 4.根据全局配置{@link BusConfig.Fail#getNextTime()} 设置下次重试时间。
34 | *
35 | * @author likavn
36 | * @date 2024/01/01
37 | */
38 | @Documented
39 | @Target({ElementType.METHOD})
40 | @Retention(RetentionPolicy.RUNTIME)
41 | public @interface FailRetry {
42 | /**
43 | * 消息投递失败时,一定时间内再次进行投递的次数
44 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.annotation;
17 |
18 | import java.lang.annotation.*;
19 |
20 | /**
21 | * 顺序注解
22 | *
23 | * 值越小则优先级越高
24 | * 可用于拦截器上, 多个同类型拦截器按顺序执行
25 | *
26 | * @author likavn
27 | * @date 2024/11/19
28 | */
29 | @Documented
30 | @Target({ElementType.TYPE})
31 | @Retention(RetentionPolicy.RUNTIME)
32 | public @interface Order {
33 | /**
34 | * 值越小则优先级越高
35 | */
36 | int value();
37 | }
38 |
--------------------------------------------------------------------------------
/eventbus-core/src/main/java/com/github/likavn/eventbus/core/annotation/Polling.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2023-2033, likavn (likavn@163.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.annotation;
17 |
18 | import lombok.experimental.UtilityClass;
19 |
20 | import java.lang.annotation.*;
21 |
22 | /**
23 | * 消息轮询行为注解
24 | *
25 | * 定义了一个注解@Polling,用于标注在方法上以控制消息订阅的轮询行为。
26 | * 轮询可以通过注解的属性count设置轮巡次数,nextTime或interval进行配置轮巡时间。
27 | *
28 | * 计算下一次轮巡时间,优先级从上至下:
29 | * 1.通过{@link Polling.Keep#over()}或{@link Keep#setNextTime(long)}设置;
30 | * 2.通过{@link Polling#nextTime()}设置;
31 | * 3.通过{@link Polling#interval()}设置;
32 | *
33 | * @author likavn
34 | * @date 2024/07/27
35 | * @since 2.3.2
36 | **/
37 | @Documented
38 | @Target({ElementType.METHOD})
39 | @Retention(RetentionPolicy.RUNTIME)
40 | public @interface Polling {
41 | /**
42 | * 定义了轮询的次数。默认值为-1,表示不进行轮询。
43 | * 如果设置了具体的轮询次数,则方法会在接收到消息后按照指定次数进行轮询。
44 | * 可通过{@link Polling.Keep#over()}编码方式调用,提前结束轮询任务。
45 | *
46 | * @return 轮询次数。
47 | */
48 | int count();
49 |
50 | /**
51 | * 下次轮巡的间隔时间,单位:秒,默认-1为设置无效。
52 | */
53 | long nextTime() default -1L;
54 |
55 | /**
56 | * 定义了轮询的时间间隔,可通过表达式(支持“+”、“-”、“*”、“/”等运算符)配置。
57 | * 表达式中可以使用三个变量:count(当前轮询次数)、deliverCount(当前投递次数)和intervalTime(本次轮询与上次轮询的时间间隔,单位为秒)。
58 | * 这使得可以灵活地根据轮询次数和时间间隔来动态确定下一次轮询的时间。
59 | * 引用变量时使用"$"+变量名,例如"$count"。
60 | * 示例:
61 | * 1. interval=7,表示轮询间隔为7秒。
62 | * 2. interval=$count*$intervalTime,表示轮询间隔为当前轮询次数与上次轮询的时间间隔的乘积。
63 | *
64 | * @return 轮询时间间隔的表达式。
65 | */
66 | String interval() default "";
67 |
68 | /**
69 | * 编码方式设置下次轮巡时间
70 | *
71 | * @author likavn
72 | * @date 2024/07/27
73 | * @since 2.3.2
74 | */
75 | @UtilityClass
76 | class Keep {
77 | // 保存当前轮巡的下次轮巡时间
78 | private static final ThreadLocal
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.annotation;
17 |
18 | import com.github.likavn.eventbus.core.metadata.data.Message;
19 |
20 | import java.lang.annotation.*;
21 |
22 | /**
23 | * 及时消息转延时消息注解
24 | *
25 | * 注解@ToDelay 用于接收及时消息的方法上,使得当前消息转成延时消息
26 | *
27 | * @author likavn
28 | * @date 2024/07/27
29 | * @since 2.5
30 | */
31 | @Documented
32 | @Target({ElementType.METHOD})
33 | @Retention(RetentionPolicy.RUNTIME)
34 | public @interface ToDelay {
35 |
36 | /**
37 | * 延迟时间,单位:秒
38 | * 该方法用于定义 ToDelay 注解的延迟时间属性
39 | *
40 | * @return 延迟时间 返回延迟执行的时间,单位为秒
41 | */
42 | long delayTime();
43 |
44 | /**
45 | * 是否需要接收首次投递的及时消息
46 | * 默认:false (第一次接收到及时消息时不执行接收方法{@link com.github.likavn.eventbus.core.api.MsgListener#onMessage(Message)})
47 | *
48 | * @return 是否接收第一次投递的及时消息
49 | */
50 | boolean firstDeliver() default false;
51 | }
52 |
53 |
--------------------------------------------------------------------------------
/eventbus-core/src/main/java/com/github/likavn/eventbus/core/api/MsgDelayListener.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2023-2033, likavn (likavn@163.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.api;
17 |
18 | /**
19 | * 延时消息监听器
20 | *
21 | * @param
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.api;
17 |
18 | import com.github.likavn.eventbus.core.metadata.data.Message;
19 |
20 | /**
21 | * 及时消息监听器
22 | *
23 | * @param
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.api;
17 |
18 | import com.github.likavn.eventbus.core.metadata.data.MsgBody;
19 | import com.github.likavn.eventbus.core.metadata.data.Request;
20 | import com.github.likavn.eventbus.core.utils.Func;
21 |
22 |
23 | /**
24 | * 消息发送者接口
25 | *
26 | * 提供了发送消息和发送延迟消息的能力
27 | *
28 | * @author likavn
29 | * @date 2024/01/01
30 | */
31 | public interface MsgSender {
32 |
33 | /**
34 | * 使用MsgBody对象发送消息
35 | * 该方法通过MsgBody对象的code方法获取消息代码,然后调用带Request参数的send方法发送消息
36 | *
37 | * @param body 消息体,包含消息代码和内容
38 | */
39 | default void send(MsgBody body) {
40 | send(body.code(), body);
41 | }
42 |
43 | /**
44 | * 发送消息请求
45 | *
46 | * 本方法通过提供的消息处理程序类和消息体来构建一个消息请求,并将其发送
47 | * 主要用于内部实现,提供类型安全和封装
48 | *
49 | * @param handlerClz 消息处理程序类,必须是MsgListener的子类
50 | * @param body 消息的具体内容
51 | */
52 | default void send(Class extends MsgListener>> handlerClz, Object body) {
53 | // 构建请求并发送
54 | send(Func.getMsgCode(handlerClz), body);
55 | }
56 |
57 | /**
58 | * 发送消息
59 | *
60 | * @param code 消息代码
61 | * @param body 消息的内容
62 | */
63 | default void send(String code, Object body) {
64 | send(Request.builder().code(code).body(body).build());
65 | }
66 |
67 | /**
68 | * 发送一个已经构建好的Request对象
69 | * 该方法是发送消息的核心方法,接收到一个已经构建好的Request对象并发送之
70 | *
71 | * @param request 已经构建好的请求对象,包含消息代码和内容
72 | */
73 | void send(Request> request);
74 |
75 | /**
76 | * 发送一个MsgBody对象延迟消息
77 | * 该方法通过MsgBody对象的code方法获取消息代码,并调用带Request参数的sendDelayMessage方法发送延迟消息
78 | *
79 | * @param body 消息体,包含消息代码和内容
80 | * @param delayTime 延迟时间,单位:秒
81 | */
82 | default void sendDelayMessage(MsgBody body, long delayTime) {
83 | sendDelayMessage(body.code(), body, delayTime);
84 | }
85 |
86 | /**
87 | * 发送延迟消息
88 | *
89 | * @param handlerClz 消息处理类类型,必须是MsgDelayListener的子类
90 | * @param body 消息体,发送的实际内容
91 | * @param delayTime 延迟时间,单位:秒
92 | */
93 | default void sendDelayMessage(Class extends MsgDelayListener>> handlerClz, Object body, long delayTime) {
94 | sendDelayMessage(Func.getMsgCode(handlerClz), body, delayTime);
95 | }
96 |
97 | /**
98 | * 使用字符串代码和任意类型的消息体延迟发送消息
99 | * 该方法构建一个Request对象,并调用带Request参数的sendDelayMessage方法发送延迟消息
100 | *
101 | * @param code 消息代码,用于标识消息类型
102 | * @param body 消息的内容,可以是任意类型
103 | * @param delayTime 延迟时间,单位:秒
104 | */
105 | default void sendDelayMessage(String code, Object body, long delayTime) {
106 | sendDelayMessage(Request.builder().code(code).body(body).delayTime(delayTime).build());
107 | }
108 |
109 | /**
110 | * 延迟发送一个已经构建好的Request对象
111 | * 该方法是延迟发送消息的核心方法,接收到一个已经构建好的Request对象并在指定时间后发送之
112 | *
113 | * @param request 已经构建好的请求对象,包含消息代码和内容
114 | */
115 | void sendDelayMessage(Request> request);
116 | }
117 |
--------------------------------------------------------------------------------
/eventbus-core/src/main/java/com/github/likavn/eventbus/core/api/RequestIdGenerator.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2023-2033, likavn (likavn@163.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.api;
17 |
18 | /**
19 | * 请求ID生成接口
20 | *
21 | * 该接口定义了获取唯一请求ID的方法,用于在分布式系统中唯一标识每个请求
22 | * 实现该接口的类需要确保生成的请求ID在系统中是唯一的
23 | *
24 | * @author likavn
25 | * @date 2024/4/2
26 | **/
27 | public interface RequestIdGenerator {
28 |
29 | /**
30 | * 获取请求ID
31 | * 该方法用于生成并返回一个唯一的请求ID
32 | * 实现该方法时,需要确保ID的全局唯一性和生成效率
33 | *
34 | * @return 请求ID 由实现类生成的唯一请求ID
35 | */
36 | String nextId();
37 | }
38 |
--------------------------------------------------------------------------------
/eventbus-core/src/main/java/com/github/likavn/eventbus/core/api/interceptor/DeliverAfterInterceptor.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2023-2033, likavn (likavn@163.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.api.interceptor;
17 |
18 | import com.github.likavn.eventbus.core.metadata.data.Request;
19 |
20 | /**
21 | * 投递后,全局拦截器
22 | *
23 | * @author likavn
24 | * @date 2024/01/01
25 | **/
26 | public interface DeliverAfterInterceptor {
27 |
28 | /**
29 | * 拦截器执行
30 | *
31 | * @param request request
32 | * @param throwable t
33 | */
34 | void execute(Request
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.api.interceptor;
17 |
18 | import com.github.likavn.eventbus.core.metadata.data.Request;
19 |
20 | /**
21 | * 投递前,全局拦截器
22 | *
23 | * @author likavn
24 | * @date 2024/01/01
25 | **/
26 | public interface DeliverBeforeInterceptor {
27 |
28 | /**
29 | * 拦截器执行
30 | *
31 | * @param request request
32 | */
33 | void execute(Request
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.api.interceptor;
17 |
18 | import com.github.likavn.eventbus.core.metadata.data.Request;
19 |
20 | /**
21 | * 投递异常,全局拦截器
22 | * 注:消息重试投递都失败时,最后一次消息投递任然失败时会调用该拦截器
23 | *
24 | * @author likavn
25 | * @date 2024/01/01
26 | **/
27 | public interface DeliverThrowableLastInterceptor {
28 |
29 | /**
30 | * 拦截器执行
31 | *
32 | * @param request request
33 | * @param throwable t
34 | */
35 | void execute(Request
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.api.interceptor;
17 |
18 | import com.github.likavn.eventbus.core.metadata.data.Request;
19 |
20 | /**
21 | * 发送后,全局拦截器
22 | *
23 | * @author likavn
24 | * @date 2024/01/01
25 | **/
26 | public interface SendAfterInterceptor {
27 |
28 | /**
29 | * 拦截器执行
30 | *
31 | * @param request request
32 | */
33 | void execute(Request
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.api.interceptor;
17 |
18 | import com.github.likavn.eventbus.core.metadata.data.Request;
19 |
20 | /**
21 | * 发送前,全局拦截器
22 | *
23 | * @author likavn
24 | * @date 2024/01/01
25 | **/
26 | public interface SendBeforeInterceptor {
27 |
28 | /**
29 | * 拦截器执行
30 | *
31 | * @param request request
32 | */
33 | void execute(Request
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.base;
17 |
18 | import com.github.likavn.eventbus.core.metadata.support.Listener;
19 |
20 | import java.util.List;
21 |
22 | /**
23 | * 获取消息监听器列表
24 | *
25 | * @author likavn
26 | * @date 2024/5/15
27 | */
28 | public interface AcquireListeners
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.base;
17 |
18 | import com.github.likavn.eventbus.core.exception.EventBusException;
19 |
20 | /**
21 | * 中间件实现监听容器初始化接口
22 | *
23 | * 该接口定义了中间件在容器初始化过程中需要实现的生命周期方法
24 | * 主要包括组件注册和组件销毁两个重要环节
25 | *
26 | * @author likavn
27 | * @date 2024/01/01
28 | **/
29 | public interface Lifecycle {
30 |
31 | /**
32 | * 监听组件注册
33 | *
34 | * 该方法用于在容器初始化时注册中间件的监听器
35 | * 可能会抛出与事件总线相关的异常
36 | *
37 | * @throws EventBusException e
38 | */
39 | void register() throws EventBusException;
40 |
41 | /**
42 | * 监听组件销毁
43 | *
44 | * 该方法用于在容器关闭时销毁中间件的监听器
45 | * 可能会抛出与事件总线相关的异常
46 | *
47 | * @throws EventBusException e
48 | */
49 | void destroy() throws EventBusException;
50 | }
51 |
--------------------------------------------------------------------------------
/eventbus-core/src/main/java/com/github/likavn/eventbus/core/base/MsgListenerContainer.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2023-2033, likavn (likavn@163.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.base;
17 |
18 |
19 | import com.github.likavn.eventbus.core.utils.Func;
20 | import lombok.Getter;
21 | import lombok.extern.slf4j.Slf4j;
22 |
23 | import java.util.Collection;
24 |
25 | /**
26 | * 接收器容器
27 | *
28 | * @author likavn
29 | * @date 2024/1/23
30 | **/
31 | @Slf4j
32 | public class MsgListenerContainer {
33 | private final Collection
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.base;
17 |
18 | /**
19 | * 节点连接状态检测接口,用于判断当前应用是否与节点断开连接
20 | *
21 | * @author likavn
22 | * @date 2024/01/01
23 | **/
24 | public interface NodeTestConnect {
25 |
26 | /**
27 | * 检测确认节点是否连接
28 | *
29 | * @return true已连接、false连接断开
30 | */
31 | boolean testConnect();
32 | }
33 |
--------------------------------------------------------------------------------
/eventbus-core/src/main/java/com/github/likavn/eventbus/core/base/UUIDRequestIdGenerator.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2023-2033, likavn (likavn@163.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.base;
17 |
18 | import com.github.likavn.eventbus.core.api.RequestIdGenerator;
19 |
20 | import java.util.UUID;
21 |
22 | /**
23 | * 默认请求id生成器,使用UUID
24 | *
25 | * @author likavn
26 | * @date 2024/4/2
27 | **/
28 | public class UUIDRequestIdGenerator implements RequestIdGenerator {
29 |
30 | /**
31 | * 获取请求id
32 | *
33 | * @return 请求id
34 | */
35 | @Override
36 | public String nextId() {
37 | return UUID.randomUUID().toString().replace("-", "");
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/eventbus-core/src/main/java/com/github/likavn/eventbus/core/constant/BusConstant.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2023-2033, likavn (likavn@163.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.constant;
17 |
18 | import com.github.likavn.eventbus.core.api.MsgDelayListener;
19 | import com.github.likavn.eventbus.core.api.MsgListener;
20 | import com.github.likavn.eventbus.core.metadata.data.Message;
21 |
22 | /**
23 | * eventbus常量
24 | *
25 | * @author likavn
26 | * @date 2024/01/01
27 | */
28 | public class BusConstant {
29 | private BusConstant() {
30 | }
31 |
32 | /**
33 | * 接口订阅器接收方法名
34 | *
35 | * @see MsgListener#onMessage(Message)
36 | * @see MsgDelayListener#onMessage(Message)
37 | */
38 | public static final String ON_MESSAGE = "onMessage";
39 |
40 | /**
41 | * 接口订阅器接收异常的方法名
42 | *
43 | * @see MsgListener#failHandler(Message, Throwable)
44 | * @see MsgDelayListener#failHandler(Message, Throwable)
45 | */
46 | public static final String FAIL_HANDLER = "failHandler";
47 |
48 | /**
49 | * thread name
50 | */
51 | public static final String TASK_NAME = "eventbus-task-pool-";
52 |
53 | /**
54 | * subscribe thread name
55 | */
56 | public static final String THREAD_NAME = "eventbus-msg-pool-";
57 |
58 | /**
59 | * 配置前缀
60 | */
61 | public static final String CONFIG_PREFIX = "eventbus";
62 |
63 | /**
64 | * 消息引擎配置key
65 | */
66 | public static final String TYPE_NAME = "type";
67 |
68 | /**
69 | * 迁移原有消息引擎配置key
70 | */
71 | public static final String OLD_TYPE_NAME = "oldType";
72 |
73 | /**
74 | * 校验名称提示
75 | */
76 | public static final String TIPS_VALID_NAME = "只能包含大小写字母、数字、横线(-)、下划线(_)";
77 | }
78 |
--------------------------------------------------------------------------------
/eventbus-core/src/main/java/com/github/likavn/eventbus/core/exception/DeliverAfterInterceptorSuccessException.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2023-2033, likavn (likavn@163.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.exception;
17 |
18 |
19 | /**
20 | * eventbus delivery interceptor success exception
21 | *
22 | * @author likavn
23 | * @date 2025/03/04
24 | * @since 2.5.1
25 | */
26 | public class DeliverAfterInterceptorSuccessException extends RuntimeException {
27 |
28 | public DeliverAfterInterceptorSuccessException(String message) {
29 | super(message);
30 | }
31 |
32 | public DeliverAfterInterceptorSuccessException(Throwable cause) {
33 | super(cause);
34 | }
35 |
36 | public DeliverAfterInterceptorSuccessException(String message, Throwable cause) {
37 | super(message, cause);
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/eventbus-core/src/main/java/com/github/likavn/eventbus/core/exception/DeliverBeforeInterceptorException.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2023-2033, likavn (likavn@163.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.exception;
17 |
18 |
19 | /**
20 | * eventbus delivery before interceptor exception
21 | *
22 | * @author likavn
23 | * @date 2025/03/04
24 | * @since 2.5.1
25 | */
26 | public class DeliverBeforeInterceptorException extends RuntimeException {
27 |
28 | public DeliverBeforeInterceptorException(String message) {
29 | super(message);
30 | }
31 |
32 | public DeliverBeforeInterceptorException(Throwable cause) {
33 | super(cause);
34 | }
35 |
36 | public DeliverBeforeInterceptorException(String message, Throwable cause) {
37 | super(message, cause);
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/eventbus-core/src/main/java/com/github/likavn/eventbus/core/exception/DeliverInvokeException.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2023-2033, likavn (likavn@163.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.exception;
17 |
18 |
19 | /**
20 | * eventbus delivery exception
21 | *
22 | * @author likavn
23 | * @date 2025/03/04
24 | * @since 2.5.1
25 | */
26 | public class DeliverInvokeException extends RuntimeException {
27 |
28 | public DeliverInvokeException(String message) {
29 | super(message);
30 | }
31 |
32 | public DeliverInvokeException(Throwable cause) {
33 | super(cause);
34 | }
35 |
36 | public DeliverInvokeException(String message, Throwable cause) {
37 | super(message, cause);
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/eventbus-core/src/main/java/com/github/likavn/eventbus/core/exception/EventBusException.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2023-2033, likavn (likavn@163.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.exception;
17 |
18 |
19 | /**
20 | * eventbus exception
21 | *
22 | * @author likavn
23 | * @date 2024/01/01
24 | */
25 | public class EventBusException extends RuntimeException {
26 |
27 | public EventBusException(String message) {
28 | super(message);
29 | }
30 |
31 | public EventBusException(Throwable cause) {
32 | super(cause);
33 | }
34 |
35 | public EventBusException(String message, Throwable cause) {
36 | super(message, cause);
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/eventbus-core/src/main/java/com/github/likavn/eventbus/core/metadata/BusConfig.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2023-2033, likavn (likavn@163.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.metadata;
17 |
18 | import lombok.Data;
19 |
20 | /**
21 | * eventbus全局配置
22 | *
23 | * @author likavn
24 | * @date 2024/01/01
25 | */
26 | @Data
27 | public class BusConfig {
28 | /**
29 | * 服务ID/消息来源ID,可以不用配置,默认等于${spring.application.name}的值。
30 | */
31 | private String serviceId;
32 |
33 | /**
34 | * 消息引擎类别(redis、rabbitmq、rocketmq)
35 | * {@link com.github.likavn.eventbus.core.metadata.BusType}
36 | */
37 | private String type;
38 |
39 | /**
40 | * 原消息引擎类别(redis、rabbitmq、rocketmq),用于消息引擎切换时兼容原始消息,消息引擎没做迁移时可不做配置
41 | * 默认等于={@link #type}
42 | * {@link com.github.likavn.eventbus.core.metadata.BusType}
43 | */
44 | private String oldType;
45 |
46 | /**
47 | * 消息接收并发数,默认为:2
48 | */
49 | private Integer concurrency = 2;
50 |
51 | /**
52 | * 重发/重试消息接收并发数,默认为:1
53 | */
54 | private Integer retryConcurrency = 1;
55 |
56 | /**
57 | * 单次获取消息数量,默认:16条
58 | */
59 | private Integer msgBatchSize = 16;
60 |
61 | /**
62 | * 节点联通性配置
63 | */
64 | private TestConnect testConnect = new TestConnect();
65 |
66 | /**
67 | * 消息投递失败时配置信息
68 | */
69 | private Fail fail = new Fail();
70 |
71 | /**
72 | * 消息引擎服务节点联通性配置
73 | */
74 | @Data
75 | public static class TestConnect {
76 | /**
77 | * 轮询检测时间间隔,单位:秒,默认:35秒进行检测一次
78 | */
79 | private Long pollSecond = 35L;
80 |
81 | /**
82 | * 丢失连接最长时间大于等于次值设置监听容器为连接断开,单位:秒,默认:120秒
83 | */
84 | private Long loseConnectMaxMilliSecond = 120L;
85 | }
86 |
87 | /**
88 | * 消息投递失败时配置
89 | */
90 | @Data
91 | public static class Fail {
92 | /**
93 | * 消息投递失败时,一定时间内再次进行投递的次数,默认:3次
94 | */
95 | private Integer retryCount = 3;
96 |
97 | /**
98 | * 失败重试下次触发时间,单位:秒,默认10秒 ,(rocketMq请修改为对应为18个延时消息级别)
99 | */
100 | private Long nextTime = 10L;
101 | }
102 | }
103 |
--------------------------------------------------------------------------------
/eventbus-core/src/main/java/com/github/likavn/eventbus/core/metadata/BusType.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2023-2033, likavn (likavn@163.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.metadata;
17 |
18 | import com.github.likavn.eventbus.core.utils.Assert;
19 | import com.github.likavn.eventbus.core.utils.Func;
20 | import lombok.Getter;
21 |
22 | /**
23 | * 消息引擎类别(redis、rabbitmq、rocketmq)
24 | *
25 | * @author likavn
26 | * @date 2023/03/06
27 | * @since 2.3.4
28 | **/
29 | @Getter
30 | public enum BusType {
31 | /**
32 | * redis
33 | */
34 | REDIS("redis"),
35 | RABBITMQ("rabbitmq"),
36 | ROCKETMQ("rocketmq"),
37 | ;
38 | private final String name;
39 |
40 | BusType(String name) {
41 | this.name = name;
42 | }
43 |
44 | public boolean valid(String name) {
45 | return this.name.equals(name);
46 | }
47 |
48 | public static BusType of(String name) {
49 | for (BusType en : values()) {
50 | if (en.valid(name)) {
51 | return en;
52 | }
53 | }
54 | return null;
55 | }
56 |
57 | public static void isValid(String name) {
58 | Assert.isTrue(!Func.isEmpty(name), "Eventbus type must not be empty");
59 |
60 | BusType busType = BusType.of(name);
61 | Assert.notNull(busType, "Eventbus type is not supported: " + name);
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/eventbus-core/src/main/java/com/github/likavn/eventbus/core/metadata/MsgType.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2023-2033, likavn (likavn@163.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.metadata;
17 |
18 | import lombok.Getter;
19 |
20 | /**
21 | * 消息类型
22 | *
23 | * @author likavn
24 | * @date 2024/01/01
25 | **/
26 | @Getter
27 | public enum MsgType {
28 | /**
29 | * 及时消息
30 | */
31 | TIMELY(1) {
32 | @Override
33 | public boolean isTimely() {
34 | return true;
35 | }
36 | },
37 |
38 | /**
39 | * 延迟消息
40 | */
41 | DELAY(2) {
42 | @Override
43 | public boolean isDelay() {
44 | return true;
45 | }
46 | };
47 |
48 | /**
49 | * 消息类型的值
50 | */
51 | private final Integer value;
52 |
53 | /**
54 | * 构造函数
55 | *
56 | * @param value 消息类型的值
57 | */
58 | MsgType(Integer value) {
59 | this.value = value;
60 | }
61 |
62 | /**
63 | * 判断消息类型是否为及时消息
64 | *
65 | * @return true代表是及时消息,false代表不是及时消息
66 | */
67 | public boolean isTimely() {
68 | return false;
69 | }
70 |
71 | /**
72 | * 判断消息类型是否为延迟消息
73 | *
74 | * @return true代表是延迟消息,false代表不是延迟消息
75 | */
76 | public boolean isDelay() {
77 | return false;
78 | }
79 |
80 | /**
81 | * 根据消息类型的值获取消息类型
82 | *
83 | * @param value 消息类型的值
84 | * @return 消息类型
85 | */
86 | public static MsgType of(Integer value) {
87 | for (MsgType msgType : MsgType.values()) {
88 | if (msgType.value.equals(value)) {
89 | return msgType;
90 | }
91 | }
92 | return null;
93 | }
94 | }
95 |
--------------------------------------------------------------------------------
/eventbus-core/src/main/java/com/github/likavn/eventbus/core/metadata/data/Message.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2023-2033, likavn (likavn@163.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.metadata.data;
17 |
18 | import java.util.Map;
19 |
20 | /**
21 | * 通知消息体
22 | *
23 | * @author likavn
24 | * @date 2024/01/01
25 | */
26 | public interface Message
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.metadata.data;
17 |
18 | /**
19 | * 业务消息体编码接口
20 | *
21 | * 发送消息时避免每次都要写code编码
22 | * 备注:实现类必须存在无参构造器
23 | *
24 | * @author likavn
25 | * @date 2024/04/19
26 | */
27 | public interface MsgBody {
28 |
29 | /**
30 | * 消息体code
31 | *
32 | * @return code编码
33 | */
34 | default String code() {
35 | return this.getClass().getSimpleName();
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/eventbus-core/src/main/java/com/github/likavn/eventbus/core/metadata/data/Request.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2023-2033, likavn (likavn@163.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.metadata.data;
17 |
18 | import com.github.likavn.eventbus.core.metadata.MsgType;
19 | import com.github.likavn.eventbus.core.metadata.support.Trigger;
20 | import com.github.likavn.eventbus.core.utils.Func;
21 | import lombok.AllArgsConstructor;
22 | import lombok.Data;
23 | import lombok.EqualsAndHashCode;
24 | import lombok.NoArgsConstructor;
25 | import lombok.experimental.SuperBuilder;
26 |
27 | import java.util.HashMap;
28 | import java.util.Map;
29 |
30 | /**
31 | * 通知消息体,eventbus原始消息体
32 | *
33 | * @author likavn
34 | * @date 2024/01/01
35 | */
36 | @Data
37 | @SuperBuilder
38 | @NoArgsConstructor
39 | @AllArgsConstructor
40 | @EqualsAndHashCode(callSuper = true)
41 | public class Request
46 | * 如需修改请实现此接口{@link com.github.likavn.eventbus.core.api.RequestIdGenerator)}
47 | */
48 | private String requestId;
49 |
50 | /**
51 | * 消息接收处理器(消费者/投递)ID=类完全限定名+方法名{@link Trigger#getDeliverId()}
52 | */
53 | private String deliverId;
54 |
55 | /**
56 | * 消息投递次数
57 | */
58 | private int deliverCount;
59 |
60 | /**
61 | * 消费者轮询次数
62 | */
63 | private int pollingCount;
64 |
65 | /**
66 | * 消费者接收失败后时,发起失败重试的次数
67 | */
68 | private int failRetryCount;
69 |
70 | /**
71 | * 消息类型,默认及时消息
72 | */
73 | private MsgType type;
74 |
75 | /**
76 | * 延时消息的延时时间,单位:秒
77 | */
78 | private long delayTime;
79 |
80 | /**
81 | * 及时消息是否已转为延迟消息
82 | */
83 | private boolean toDelay;
84 |
85 | /**
86 | * 是否为消息重发
87 | */
88 | private boolean retry;
89 |
90 | /**
91 | * 消息头
92 | */
93 | private Map
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.metadata.data;
17 |
18 | import lombok.AllArgsConstructor;
19 | import lombok.Data;
20 | import lombok.NoArgsConstructor;
21 | import lombok.experimental.SuperBuilder;
22 |
23 | import java.io.Serializable;
24 |
25 | /**
26 | * topic
27 | *
28 | * @author likavn
29 | * @date 2024/01/01
30 | **/
31 | @Data
32 | @SuperBuilder
33 | @AllArgsConstructor
34 | @NoArgsConstructor
35 | public abstract class Topic implements Serializable {
36 | private static final long serialVersionUID = 1L;
37 | /**
38 | * 消息所属来源服务ID,服务名
39 | */
40 | protected String serviceId;
41 |
42 | /**
43 | * 消息类型,用于区分不同的消息类型
44 | */
45 | protected String code;
46 |
47 | /**
48 | * 获取topic
49 | *
50 | * @return topic
51 | */
52 | public abstract String topic();
53 | }
54 |
--------------------------------------------------------------------------------
/eventbus-core/src/main/java/com/github/likavn/eventbus/core/metadata/support/FailTrigger.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2023-2033, likavn (likavn@163.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.metadata.support;
17 |
18 | import com.github.likavn.eventbus.core.annotation.FailRetry;
19 | import lombok.EqualsAndHashCode;
20 | import lombok.Getter;
21 |
22 | /**
23 | * 消息投递失败触发器
24 | *
25 | * @author likavn
26 | * @date 2024/01/01
27 | **/
28 | @Getter
29 | @EqualsAndHashCode(callSuper = true)
30 | public class FailTrigger extends Trigger {
31 | /**
32 | * 投递失败配置信息
33 | */
34 | private final FailRetry fail;
35 |
36 | public FailTrigger(FailRetry fail) {
37 | super(null, null);
38 | this.fail = fail;
39 | }
40 |
41 | public FailTrigger(FailRetry fail, Trigger trigger) {
42 | super(trigger.getInvokeBean(), trigger.getMethod());
43 | this.fail = fail;
44 | }
45 |
46 | public static FailTrigger of(FailRetry fail, Trigger trigger) {
47 | if (null != trigger) {
48 | return new FailTrigger(fail, trigger);
49 | }
50 | if (null != fail) {
51 | return new FailTrigger(fail);
52 | }
53 | return null;
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/eventbus-core/src/main/java/com/github/likavn/eventbus/core/support/Fast2jsonProvider.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2023-2033, likavn (likavn@163.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.support;
17 |
18 | import com.alibaba.fastjson2.JSON;
19 | import com.github.likavn.eventbus.core.support.spi.IJson;
20 |
21 | import java.lang.reflect.Type;
22 |
23 | /**
24 | * fastjson2
25 | *
26 | * @author likavn
27 | * @date 2024/04/15
28 | * @since 2.2
29 | */
30 | public class Fast2jsonProvider implements IJson {
31 | @Override
32 | public String className() {
33 | return "com.alibaba.fastjson2.JSON";
34 | }
35 |
36 | @Override
37 | public String toJsonString(Object value) {
38 | return JSON.toJSONString(value);
39 | }
40 |
41 | @Override
42 | public
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.support;
17 |
18 | import com.alibaba.fastjson.JSON;
19 | import com.github.likavn.eventbus.core.support.spi.IJson;
20 |
21 | import java.lang.reflect.Type;
22 |
23 | /**
24 | * fastjson
25 | *
26 | * @author likavn
27 | * @date 2024/04/15
28 | * @since 2.2
29 | */
30 | public class FastjsonProvider implements IJson {
31 | @Override
32 | public String className() {
33 | return "com.alibaba.fastjson.JSON";
34 | }
35 |
36 | @Override
37 | public String toJsonString(Object value) {
38 | return JSON.toJSONString(value);
39 | }
40 |
41 | @Override
42 | public
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.support;
17 |
18 | import com.github.likavn.eventbus.core.support.spi.IJson;
19 | import com.google.gson.Gson;
20 |
21 | import java.lang.reflect.Type;
22 |
23 | /**
24 | * gson
25 | *
26 | * @author likavn
27 | * @date 2024/04/15
28 | * @since 2.2
29 | */
30 | public class GsonProvider implements IJson {
31 |
32 | @Override
33 | public String className() {
34 | return "com.google.gson.Gson";
35 | }
36 |
37 | @Override
38 | public String toJsonString(Object value) {
39 | return GsonUtil.GSON.toJson(value);
40 | }
41 |
42 | @Override
43 | public
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.support;
17 |
18 | import com.fasterxml.jackson.annotation.JsonAutoDetect;
19 | import com.fasterxml.jackson.annotation.JsonInclude;
20 | import com.fasterxml.jackson.annotation.PropertyAccessor;
21 | import com.fasterxml.jackson.core.JsonProcessingException;
22 | import com.fasterxml.jackson.databind.DeserializationFeature;
23 | import com.fasterxml.jackson.databind.ObjectMapper;
24 | import com.github.likavn.eventbus.core.exception.EventBusException;
25 | import com.github.likavn.eventbus.core.support.spi.IJson;
26 |
27 | import java.io.IOException;
28 | import java.lang.reflect.Type;
29 |
30 | /**
31 | * jackson
32 | *
33 | * @author likavn
34 | * @date 2024/04/15
35 | * @since 2.2
36 | */
37 | public class JacksonProvider implements IJson {
38 |
39 | @Override
40 | public String className() {
41 | return "com.fasterxml.jackson.databind.ObjectMapper";
42 | }
43 |
44 | @Override
45 | public String toJsonString(Object value) {
46 | try {
47 | return JacksonUtil.MAPPER.writeValueAsString(value);
48 | } catch (JsonProcessingException e) {
49 | throw new EventBusException(e);
50 | }
51 | }
52 |
53 | @Override
54 | public
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.support.spi;
17 |
18 | import com.github.likavn.eventbus.core.utils.Func;
19 |
20 | import java.lang.reflect.Type;
21 |
22 | /**
23 | * JSON处理接口。
24 | * 提供了检查字符串是否为JSON格式、激活性检查、类名获取、对象转JSON字符串、JSON字符串转对象和获取处理顺序等方法。
25 | *
26 | * @author likavn
27 | * @date 2024/04/15
28 | * @since 2.2
29 | */
30 | public interface IJson {
31 | /**
32 | * 定义正则表达式,用于匹配JSON格式的字符串
33 | */
34 | String PATTERN_JSON = "(\\{.*\\}|\\[.*\\])";
35 |
36 | /**
37 | * 检查给定字符串是否为JSON格式。
38 | *
39 | * @param val 待检查的字符串
40 | * @return 如果字符串为JSON格式,则返回true;否则返回false。
41 | */
42 | default boolean isJson(String val) {
43 | if (Func.isEmpty(val)) {
44 | return false;
45 | }
46 | return val.matches(PATTERN_JSON);
47 | }
48 |
49 | /**
50 | * 检查当前实现类是否激活。
51 | * 通过尝试加载实现类的类名来判断其是否可用。
52 | *
53 | * @return 如果类可用,则返回true;否则返回false。
54 | */
55 | default boolean active() {
56 | try {
57 | Class.forName(className());
58 | } catch (ClassNotFoundException e) {
59 | return false;
60 | }
61 | return true;
62 | }
63 |
64 | /**
65 | * 获取当前实现类的类名。
66 | * 用于激活检查和可能的实例化操作。
67 | *
68 | * @return 当前实现类的类名。
69 | */
70 | String className();
71 |
72 | /**
73 | * 将对象转换为JSON格式的字符串。
74 | *
75 | * @param value 待转换的对象
76 | * @return 对象的JSON字符串表示
77 | */
78 | String toJsonString(Object value);
79 |
80 | /**
81 | * 将JSON字符串解析为指定类型的对象。
82 | *
83 | * @param text JSON字符串
84 | * @param type 目标对象的类型
85 | * @param
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.support.task;
17 |
18 | import com.github.likavn.eventbus.core.exception.EventBusException;
19 | import com.github.likavn.eventbus.core.utils.Assert;
20 | import lombok.extern.slf4j.Slf4j;
21 |
22 | import java.text.ParseException;
23 | import java.util.Date;
24 |
25 | /**
26 | * CronTask类继承自Task类,用于实现基于Cron表达式定时执行的任务。
27 | * 使用CronExpression来解析和计算任务的下一个执行时间。
28 | *
29 | * @author likavn
30 | * @date 2024/04/15
31 | * @since 2.2
32 | */
33 | @Slf4j
34 | public class CronTask extends Task {
35 | private CronExpression cronExpression;
36 |
37 | /**
38 | * 初始化Cron任务。
39 | *
40 | * @param name 任务名称。
41 | * @param cron Cron表达式,用于定义任务的执行周期。
42 | * @param runnable 任务的具体执行逻辑。
43 | * @throws EventBusException 如果Cron表达式解析失败,则抛出此异常。
44 | */
45 | public void initTask(String name, String cron, Runnable runnable) {
46 | init(name, runnable);
47 | Assert.isTrue(CronExpression.isValidExpression(cron), "cron表达式不合法");
48 | try {
49 | this.cronExpression = new CronExpression(cron);
50 | } catch (ParseException e) {
51 | throw new EventBusException(e);
52 | }
53 | }
54 |
55 | /**
56 | * 创建并初始化一个Cron任务。
57 | *
58 | * @param name 任务名称。
59 | * @param cron Cron表达式。
60 | * @param runnable 任务执行体。
61 | * @return 初始化后的CronTask实例。
62 | */
63 | public static CronTask create(String name, String cron, Runnable runnable) {
64 | CronTask task = new CronTask();
65 | task.initTask(name, cron, runnable);
66 | return task;
67 | }
68 |
69 | /**
70 | * 计算任务的下一个执行时间。
71 | *
72 | * @return 下一个执行时间的毫秒数,如果不存在下一个执行时间则返回0。
73 | */
74 | @Override
75 | public long nextExecutionTime() {
76 | Date next = cronExpression.getNextValidTimeAfter(new Date());
77 | return next != null ? next.getTime() : 0;
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/eventbus-core/src/main/java/com/github/likavn/eventbus/core/support/task/PeriodTask.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2023-2033, likavn (likavn@163.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.support.task;
17 |
18 | import java.util.Date;
19 | import java.util.concurrent.TimeUnit;
20 |
21 | /**
22 | * 定时任务类,用于执行周期性的任务。
23 | * 继承自Task类,增加了周期时间和时间单位的属性,用于精确控制任务的执行间隔。
24 | *
25 | * @author likavn
26 | * @date 2024/04/15
27 | * @since 2.2
28 | */
29 | public class PeriodTask extends Task {
30 |
31 | /**
32 | * 周期性任务的执行间隔时间
33 | */
34 | private long pollInterval;
35 | /**
36 | * 周期性任务的执行间隔时间单位
37 | */
38 | private TimeUnit timeUnit;
39 |
40 | /**
41 | * 初始化周期性任务。
42 | *
43 | * @param name 任务名称
44 | * @param pollInterval 任务执行的周期时间
45 | * @param timeUnit 任务执行周期的时间单位
46 | * @param runnable 要执行的任务
47 | */
48 | private void initTask(String name, long pollInterval, TimeUnit timeUnit, Runnable runnable) {
49 | init(name, runnable);
50 | this.pollInterval = pollInterval;
51 | this.timeUnit = timeUnit;
52 | }
53 |
54 | /**
55 | * 创建一个周期性任务,使用默认的时间单位(毫秒)。
56 | *
57 | * @param name 任务名称
58 | * @param pollInterval 任务执行的周期时间
59 | * @param runnable 要执行的任务
60 | * @return 创建的周期性任务实例
61 | */
62 | public static PeriodTask create(String name, long pollInterval, Runnable runnable) {
63 | return create(name, pollInterval, TimeUnit.MILLISECONDS, runnable);
64 | }
65 |
66 | /**
67 | * 创建一个周期性任务。
68 | *
69 | * @param name 任务名称
70 | * @param pollInterval 任务执行的周期时间
71 | * @param timeUnit 任务执行周期的时间单位
72 | * @param runnable 要执行的任务
73 | * @return 创建的周期性任务实例
74 | */
75 | public static PeriodTask create(String name, long pollInterval, TimeUnit timeUnit, Runnable runnable) {
76 | PeriodTask task = new PeriodTask();
77 | task.initTask(name, pollInterval, timeUnit, runnable);
78 | return task;
79 | }
80 |
81 | /**
82 | * 计算下一个执行时间。
83 | * 如果尚未执行过,则基于当前时间计算下一个执行时间。
84 | *
85 | * @return 下一个执行时间
86 | */
87 | @Override
88 | public long nextExecutionTime() {
89 | if (null == lastExecutionTime) {
90 | lastExecutionTime = new Date();
91 | }
92 | // 计算下一次执行时间,基于任务的周期和上次执行时间。
93 | return lastExecutionTime.getTime() + (pollInterval * timeUnit.toMillis(1L));
94 | }
95 | }
96 |
--------------------------------------------------------------------------------
/eventbus-core/src/main/java/com/github/likavn/eventbus/core/support/task/Task.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2023-2033, likavn (likavn@163.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.support.task;
17 |
18 | import com.github.likavn.eventbus.core.TaskRegistry;
19 | import com.github.likavn.eventbus.core.utils.WaitThreadPoolExecutor;
20 | import lombok.Getter;
21 | import lombok.Setter;
22 | import lombok.extern.slf4j.Slf4j;
23 |
24 | import java.util.Date;
25 |
26 | /**
27 | * 任务类,抽象类,继承自TimerTask,用于定义和管理任务。
28 | * 提供了任务初始化、执行、以及下次执行时间刷新等功能。
29 | *
30 | * @author likavn
31 | * @date 2024/04/15
32 | * @since 2.2
33 | */
34 | @Slf4j
35 | public abstract class Task extends TimerTask {
36 |
37 | /**
38 | * 任务名称。
39 | */
40 | @Getter
41 | private String name;
42 |
43 | /**
44 | * 任务执行的Runnable对象。
45 | */
46 | @Setter
47 | private Runnable runnable;
48 |
49 | /**
50 | * 上次任务执行的时间。
51 | */
52 | protected Date lastExecutionTime;
53 |
54 | /**
55 | * 任务注册表,用于管理任务。
56 | */
57 | private TaskRegistry taskRegistry;
58 |
59 | /**
60 | * 任务执行的线程池。
61 | */
62 | private WaitThreadPoolExecutor poolExecutor;
63 |
64 | /**
65 | * 标志任务是否已初始化。
66 | */
67 | @Getter
68 | private boolean initialized = false;
69 |
70 | /**
71 | * 初始化任务。
72 | *
73 | * @param name 任务名称。
74 | * @param runnable 任务执行体。
75 | */
76 | protected void init(String name, Runnable runnable) {
77 | this.name = name;
78 | this.runnable = runnable;
79 | this.nextExecutionTime = System.currentTimeMillis();
80 | this.initialized = true;
81 | }
82 |
83 | /**
84 | * 任务执行的方法。
85 | * 将任务提交给线程池执行,并更新下次执行时间。
86 | */
87 | @Override
88 | public void run() {
89 | this.lastExecutionTime = new Date();
90 | try {
91 | poolExecutor.execute(runnable);
92 | super.nextExecutionTime = nextExecutionTime();
93 | } catch (Exception e) {
94 | log.error("task run error", e);
95 | }
96 | }
97 |
98 | /**
99 | * 设置任务注册表。
100 | * 通过任务注册表获取线程池,并注册当前任务。
101 | *
102 | * @param taskRegistry 任务注册表。
103 | */
104 | public void setTaskRegistry(TaskRegistry taskRegistry) {
105 | this.poolExecutor = taskRegistry.getPoolExecutor();
106 | this.taskRegistry = taskRegistry;
107 | }
108 |
109 | /**
110 | * 刷新下次执行时间。
111 | * 如果传入的下次执行时间早于当前设定的下次执行时间,则更新下次执行时间,并通知任务注册表刷新。
112 | *
113 | * @param nextExecutionTime 下次执行时间。
114 | */
115 | public void refreshNextExecutionTime(long nextExecutionTime) {
116 | if (0 < nextExecutionTime && nextExecutionTime < this.nextExecutionTime) {
117 | this.nextExecutionTime = nextExecutionTime;
118 | this.taskRegistry.refresh();
119 | }
120 | }
121 |
122 | /**
123 | * 计算并返回下次任务执行的时间。
124 | * 该方法为抽象方法,由子类实现具体的计算逻辑。
125 | *
126 | * @return 下次任务执行的时间。
127 | */
128 | public abstract long nextExecutionTime();
129 | }
130 |
--------------------------------------------------------------------------------
/eventbus-core/src/main/java/com/github/likavn/eventbus/core/utils/NamedThreadFactory.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2023-2033, likavn (likavn@163.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.utils;
17 |
18 |
19 | import lombok.Getter;
20 |
21 | import java.util.LinkedList;
22 | import java.util.List;
23 | import java.util.concurrent.ThreadFactory;
24 |
25 | /**
26 | * 带有前缀名称的线程工厂
27 | *
28 | * @author likavn
29 | * @date 2024/01/01
30 | */
31 | @Getter
32 | public class NamedThreadFactory implements ThreadFactory {
33 |
34 | /**
35 | * 线程名前缀
36 | */
37 | private final String prefix;
38 |
39 | /**
40 | * 线程编号
41 | */
42 | private volatile int threadNumber = 0;
43 |
44 | /**
45 | * 回收的线程编号列表
46 | */
47 | private final List
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.utils;
17 |
18 | import java.util.concurrent.BlockingQueue;
19 | import java.util.concurrent.ThreadFactory;
20 | import java.util.concurrent.TimeUnit;
21 |
22 | /**
23 | * 继承自WaitThreadPoolExecutor的PollThreadPoolExecutor类,用于实现具有轮询功能的固定大小线程池。
24 | * 该线程池的主要特点是在任务执行前后进行特定的逻辑处理,以支持轮询重试等机制。
25 | *
26 | * @author likavn
27 | * @date 2024/01/01
28 | */
29 | public class PollThreadPoolExecutor extends WaitThreadPoolExecutor {
30 | /**
31 | * 标志线程池是否正在运行,用于控制任务的重新执行
32 | */
33 | private volatile boolean running = true;
34 |
35 | /**
36 | * 构造函数,初始化PollThreadPoolExecutor。
37 | *
38 | * @param corePoolSize 核心线程数
39 | * @param maximumPoolSize 最大线程数
40 | * @param keepAliveTime 线程的空闲存活时间
41 | * @param unit 时间单位
42 | * @param workQueue 工作队列,用于存储待执行的任务
43 | * @param threadFactory 线程工厂,用于创建线程
44 | */
45 | public PollThreadPoolExecutor(int corePoolSize,
46 | int maximumPoolSize,
47 | long keepAliveTime, TimeUnit unit, BlockingQueue
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.core.utils;
17 |
18 | import java.util.concurrent.BlockingQueue;
19 | import java.util.concurrent.ThreadFactory;
20 | import java.util.concurrent.ThreadPoolExecutor;
21 | import java.util.concurrent.TimeUnit;
22 |
23 | /**
24 | * 自定义的线程池执行器,继承自ThreadPoolExecutor,增加了对线程池状态的检查,
25 | * 以确保在执行任务时线程池是处于可以接受任务的状态。
26 | *
27 | * @author likavn
28 | * @date 2024/01/01
29 | */
30 | public class WaitThreadPoolExecutor extends ThreadPoolExecutor {
31 | /**
32 | * 构造函数,初始化线程池。
33 | *
34 | * @param corePoolSize 核心线程数
35 | * @param maximumPoolSize 最大线程数
36 | * @param keepAliveTime 线程空闲时的存活时间
37 | * @param unit 时间单位
38 | * @param workQueue 工作队列,用于存放待执行的任务
39 | * @param threadFactory 线程工厂,用于创建线程
40 | */
41 | public WaitThreadPoolExecutor(int corePoolSize,
42 | int maximumPoolSize,
43 | long keepAliveTime, TimeUnit unit, BlockingQueue
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.demo.config;
17 |
18 | import com.baomidou.mybatisplus.core.toolkit.Sequence;
19 | import com.github.likavn.eventbus.core.api.RequestIdGenerator;
20 | import org.springframework.context.annotation.Bean;
21 | import org.springframework.context.annotation.Configuration;
22 |
23 | import java.net.InetAddress;
24 | import java.net.UnknownHostException;
25 |
26 | /**
27 | * @author likavn
28 | * @date 2024/4/2
29 | **/
30 | @Configuration
31 | public class EventbusConfiguration {
32 |
33 | @Bean
34 | public RequestIdGenerator requestIdGenerator() throws UnknownHostException {
35 | Sequence sequence = new Sequence(InetAddress.getLocalHost());
36 | return () -> String.valueOf(sequence.nextId());
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/config/MybatisPlusConfig.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2023-2033, likavn (likavn@163.com).
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.demo.config;
17 |
18 | import com.baomidou.mybatisplus.annotation.DbType;
19 | import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
20 | import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
21 | import org.mybatis.spring.annotation.MapperScan;
22 | import org.springframework.context.annotation.Bean;
23 | import org.springframework.context.annotation.Configuration;
24 |
25 | /**
26 | * mybatis plus配置
27 | *
28 | * @author likavn
29 | * @date 2024/3/31
30 | **/
31 | @Configuration
32 | @MapperScan("com.github.likavn.eventbus.demo.mapper.**")
33 | public class MybatisPlusConfig {
34 | /**
35 | * 添加分页插件
36 | */
37 | @Bean
38 | public MybatisPlusInterceptor mybatisPlusInterceptor() {
39 | MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
40 | interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 如果配置多个插件, 切记分页最后添加
41 | // 如果有多数据源可以不配具体类型, 否则都建议配上具体的 DbType
42 | return interceptor;
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/constant/MsgConstant.java:
--------------------------------------------------------------------------------
1 | package com.github.likavn.eventbus.demo.constant;
2 |
3 | /**
4 | * @author likavn
5 | * @date 2024/01/01
6 | **/
7 | public class MsgConstant {
8 | public static final String MSG_LISTENER = "MsgListener";
9 | public static final String MSG_LISTENER_CODE = "MsgListenerCode";
10 | public static final String MSG_LISTENER_CODE_V2 = "MsgListenerCode_V2";
11 | public static final String MSG_DELAY_LISTENER = "MsgDelayListener";
12 | public static final String MSG_DELAY_LISTENER_CODE = "MsgDelayListenerCode";
13 | }
14 |
--------------------------------------------------------------------------------
/eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/controller/DataTableController.java:
--------------------------------------------------------------------------------
1 | package com.github.likavn.eventbus.demo.controller;
2 |
3 | import com.baomidou.mybatisplus.core.metadata.IPage;
4 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
5 | import com.github.likavn.eventbus.demo.controller.vo.BsConsumerVO;
6 | import com.github.likavn.eventbus.demo.domain.R;
7 | import com.github.likavn.eventbus.demo.service.BsConsumerService;
8 | import com.github.likavn.eventbus.demo.helper.BsHelper;
9 | import lombok.extern.slf4j.Slf4j;
10 | import org.springframework.context.annotation.Lazy;
11 | import org.springframework.web.bind.annotation.*;
12 |
13 | import javax.annotation.Resource;
14 |
15 | /**
16 | * @author likavn
17 | * @date 2024/1/15
18 | **/
19 | @Slf4j
20 | @RestController
21 | @RequestMapping("/eventbus/table")
22 | public class DataTableController {
23 | @Lazy
24 | @Resource
25 | private BsConsumerService bsConsumerService;
26 |
27 | @Resource
28 | private BsHelper bsHelper;
29 |
30 | @PostMapping(value = "/reSendMessage")
31 | public R
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.demo.entity;
17 |
18 | import com.baomidou.mybatisplus.annotation.TableId;
19 | import com.baomidou.mybatisplus.annotation.TableName;
20 | import lombok.Builder;
21 | import lombok.Data;
22 |
23 | import java.time.LocalDateTime;
24 |
25 | /**
26 | * 消息数据
27 | *
28 | * @author likavn
29 | * @date 2024/3/31
30 | **/
31 | @Data
32 | @Builder
33 | @TableName("bs_data")
34 | public class BsData {
35 | /**
36 | * 事件ID,默认UUID
37 | */
38 | @TableId
39 | private String requestId;
40 |
41 | /**
42 | * 消息所属来源服务ID,服务名
43 | */
44 | private String serviceId;
45 |
46 | /**
47 | * 消息类型,用于区分不同的消息类型
48 | */
49 | private String code;
50 |
51 | /**
52 | * 消息类型,1及时消息、2延时消息
53 | */
54 | private Integer type;
55 |
56 | /**
57 | * 延时消息的延时时间,单位:秒
58 | */
59 | private Long delayTime;
60 |
61 | /**
62 | * 消息头数据
63 | */
64 | private String headers;
65 |
66 | /**
67 | * 消息体,必须包含无参构造函数
68 | */
69 | private String body;
70 |
71 | /**
72 | * 发送者IP
73 | */
74 | private String ipAddress;
75 | /**
76 | * 创建时间
77 | */
78 | private LocalDateTime createTime;
79 | }
80 |
--------------------------------------------------------------------------------
/eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/enums/ConsumerStatus.java:
--------------------------------------------------------------------------------
1 | package com.github.likavn.eventbus.demo.enums;
2 |
3 | import lombok.Getter;
4 |
5 | import java.util.Arrays;
6 |
7 | /**
8 | * 消费状态
9 | */
10 | @Getter
11 | public enum ConsumerStatus {
12 | /**
13 | * 待消费
14 | */
15 | PROCESSING(0, "待消费"),
16 | /**
17 | * 消费成功
18 | */
19 | SUCCESS(1, "成功"),
20 | /**
21 | * 消费异常
22 | */
23 | EXCEPTION(2, "异常");
24 |
25 | private final Integer value;
26 | private final String name;
27 |
28 | ConsumerStatus(Integer value, String name) {
29 | this.value = value;
30 | this.name = name;
31 | }
32 |
33 | public static ConsumerStatus of(Integer value) {
34 | return Arrays.stream(values()).filter(e -> e.value.equals(value)).findFirst().orElse(null);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/interceptor/DemoDeliverAfterInterceptor.java:
--------------------------------------------------------------------------------
1 | package com.github.likavn.eventbus.demo.interceptor;
2 |
3 | import com.github.likavn.eventbus.core.annotation.Order;
4 | import com.github.likavn.eventbus.core.api.interceptor.DeliverAfterInterceptor;
5 | import com.github.likavn.eventbus.core.metadata.data.Request;
6 | import com.github.likavn.eventbus.demo.helper.BsHelper;
7 | import lombok.extern.slf4j.Slf4j;
8 | import org.springframework.context.annotation.Lazy;
9 | import org.springframework.stereotype.Component;
10 |
11 | import javax.annotation.Resource;
12 |
13 | /**
14 | * 消费后置拦截器
15 | *
16 | * @author likavn
17 | * @date 2024/01/01
18 | **/
19 | @Order(1)
20 | @Slf4j
21 | @Component
22 | public class DemoDeliverAfterInterceptor implements DeliverAfterInterceptor {
23 | @Lazy
24 | @Resource
25 | private BsHelper bsHelper;
26 |
27 | @Override
28 | public void execute(Request
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.demo.listener.test;
17 |
18 | import com.github.likavn.eventbus.core.annotation.EventbusListener;
19 | import com.github.likavn.eventbus.core.api.MsgListener;
20 | import com.github.likavn.eventbus.core.metadata.data.Message;
21 | import com.github.likavn.eventbus.demo.domain.TMsg;
22 |
23 | /**
24 | * @author likavn
25 | * @date 2024/5/25
26 | */
27 | //@Component
28 | @EventbusListener(codes = "testArrayListener")
29 | public class TestArrayListener implements MsgListener
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.demo.listener.test;
17 |
18 | import com.github.likavn.eventbus.core.annotation.EventbusListener;
19 | import com.github.likavn.eventbus.core.api.MsgListener;
20 | import com.github.likavn.eventbus.core.metadata.data.Message;
21 | import com.github.likavn.eventbus.demo.domain.TMsg;
22 |
23 | /**
24 | * @author likavn
25 | * @date 2024/5/25
26 | */
27 | //@Component
28 | @EventbusListener(codes = "testBeanObjectListener")
29 | public class TestBeanObjectListener implements MsgListener
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.demo.listener.test;
17 |
18 | import com.github.likavn.eventbus.core.annotation.EventbusListener;
19 | import com.github.likavn.eventbus.core.api.MsgListener;
20 | import com.github.likavn.eventbus.core.metadata.data.Message;
21 |
22 | /**
23 | * @author likavn
24 | * @date 2024/5/25
25 | */
26 | //@Component
27 | @EventbusListener(codes = "testIntegerListener")
28 | public class TestIntegerListener implements MsgListener
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.demo.listener.test;
17 |
18 | import com.github.likavn.eventbus.core.annotation.EventbusListener;
19 | import com.github.likavn.eventbus.core.api.MsgListener;
20 | import com.github.likavn.eventbus.core.metadata.data.Message;
21 | import com.github.likavn.eventbus.demo.domain.TMsg;
22 |
23 | import java.util.List;
24 |
25 | /**
26 | * @author likavn
27 | * @date 2024/5/25
28 | */
29 | //@Component
30 | @EventbusListener(codes = "testListListener")
31 | public class TestListListener implements MsgListener
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.github.likavn.eventbus.demo.listener.test;
17 |
18 | import com.github.likavn.eventbus.core.annotation.EventbusListener;
19 | import com.github.likavn.eventbus.core.api.MsgListener;
20 | import com.github.likavn.eventbus.core.metadata.data.Message;
21 |
22 | import java.util.Map;
23 |
24 | /**
25 | * @author likavn
26 | * @date 2024/5/25
27 | */
28 | //@Component
29 | @EventbusListener(codes = "testMapListener")
30 | public class TestMapListener implements MsgListenercount < 0
时根据全局配置{@link BusConfig.Fail#getRetryCount()} 默认为3次
45 | */
46 | int count() default -1;
47 |
48 | /**
49 | * 投递失败时,下次投递触发的间隔时间,单位:秒
50 | */
51 | long nextTime() default -1L;
52 |
53 | /**
54 | * 定义了投递失败时,下次重试消息的时间间隔,可通过表达式(支持“+”、“-”、“*”、“/”等运算符)配置。
55 | * 表达式中可以使用三个变量:count(当前失败重试次数)、deliverCount(当前投递次数)和intervalTime(本次重试与上次投递的时间间隔,单位:秒)。
56 | * 这使得可以灵活地根据重试次数和时间间隔来动态确定下一次重试的时间。
57 | * 引用变量时使用"$"+变量名,例如"$count"。
58 | * 示例:
59 | * 1. interval=7,表示重试间隔为7秒。
60 | * 2. interval=$count*$intervalTime,表示重试间隔为当前重试次数与上次投递的时间间隔的乘积。
61 | *
62 | * @return 重试时间间隔的表达式。
63 | */
64 | String interval() default "";
65 |
66 | /**
67 | * 编码方式设置失败重试时间
68 | *
69 | * @author likavn
70 | * @date 2025/02/07
71 | * @since 2.5.0
72 | */
73 | @UtilityClass
74 | class Keep {
75 | // 当前任务下次重试时间
76 | private static final ThreadLocal> {
32 |
33 | @Override
34 | public void onMessage(Message
> message) {
35 | System.out.println(message);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/listener/test/TestMapListener.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2023-2033, likavn (likavn@163.com).
3 | *