├── .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 | 5 | 6 | com.github.likavn 7 | eventbus 8 | ${revision} 9 | 10 | 4.0.0 11 | eventbus-core 12 | jar 13 | eventbus核心包 14 | 15 | 2.0.48 16 | 1.2.48 17 | 2.7 18 | 2.9.6 19 | 20 | 21 | 22 | org.slf4j 23 | slf4j-api 24 | 25 | 26 | 27 | 28 | com.alibaba.fastjson2 29 | fastjson2 30 | ${fastjson2.version} 31 | true 32 | 33 | 34 | 35 | com.alibaba 36 | fastjson 37 | ${fastjson.version} 38 | true 39 | 40 | 41 | 42 | com.fasterxml.jackson.core 43 | jackson-databind 44 | ${jackson.version} 45 | true 46 | 47 | 48 | 49 | com.google.code.gson 50 | gson 51 | ${gson.version} 52 | true 53 | 54 | 55 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/com/github/likavn/eventbus/core/annotation/EventbusListener.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 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 | * count < 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 NEXT_TIME = new ThreadLocal<>(); 77 | 78 | /** 79 | * 获取当前消息下次重试时间 80 | * 81 | * @return 下次重试时间,单位:秒 82 | */ 83 | public long nextTime() { 84 | return NEXT_TIME.get() == null ? 0 : NEXT_TIME.get(); 85 | } 86 | 87 | /** 88 | * 设置当前消息下次重试时间 89 | * 90 | * @param nextTime 下次重试时间,单位:秒 91 | */ 92 | public void setNextTime(long nextTime) { 93 | NEXT_TIME.set(nextTime); 94 | } 95 | 96 | /** 97 | * 清除 98 | */ 99 | public void clear() { 100 | NEXT_TIME.remove(); 101 | } 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/com/github/likavn/eventbus/core/annotation/Order.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 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 NEXT_TIME = new ThreadLocal<>(); 79 | 80 | /** 81 | * 获取当前线程的下一个时间戳 82 | * 83 | * @return 如果没有设置下一个时间戳,则返回0;否则返回设置的时间戳 84 | */ 85 | public long nextTime() { 86 | return NEXT_TIME.get() == null ? 0 : NEXT_TIME.get(); 87 | } 88 | 89 | /** 90 | * 设置当前线程的下一个时间戳 91 | * 92 | * @param nextTime 下一个时间戳 93 | */ 94 | public void setNextTime(long nextTime) { 95 | NEXT_TIME.set(nextTime); 96 | } 97 | 98 | /** 99 | * 检查当前线程的时间戳是否已经结束 100 | * 101 | * @return 如果时间戳小于0,则表示已经结束,返回true;否则返回false 102 | */ 103 | public boolean isOver() { 104 | return nextTime() < 0; 105 | } 106 | 107 | /** 108 | * 标记当前线程的时间戳为结束 109 | * 设置时间戳为-1,表示不再有下一个时间点 110 | */ 111 | public void over() { 112 | NEXT_TIME.set(-1L); 113 | } 114 | 115 | /** 116 | * 清除当前线程的下一个时间戳 117 | * 使用ThreadLocal的remove方法来避免内存泄漏 118 | */ 119 | public void clear() { 120 | NEXT_TIME.remove(); 121 | } 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/com/github/likavn/eventbus/core/annotation/ToDelay.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.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 消息体的数据类型 22 | * @author likavn 23 | * @date 2024/01/01 24 | */ 25 | public interface MsgDelayListener extends MsgListener { 26 | } 27 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/com/github/likavn/eventbus/core/api/MsgListener.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 | import com.github.likavn.eventbus.core.metadata.data.Message; 19 | 20 | /** 21 | * 及时消息监听器 22 | * 23 | * @param 消息体的数据类型 24 | * @author likavn 25 | * @date 2024/01/01 26 | */ 27 | public interface MsgListener { 28 | /** 29 | * 处理器 30 | * 当有新消息到达时,此方法被调用以处理消息 31 | * 32 | * @param message 消息体,包含延时消息的数据和元信息 33 | */ 34 | void onMessage(Message message); 35 | 36 | /** 37 | * 处理消息投递异常 38 | * 当消息投递过程中发生异常时,此方法被调用用于处理该异常 39 | * 40 | * @param message 消息,发生异常的消息对象 41 | * @param throwable 异常,投递过程中遇到的异常对象 42 | */ 43 | default void failHandler(Message message, Throwable throwable) { 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/com/github/likavn/eventbus/core/api/MsgSender.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 | 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> 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> 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 request, Throwable throwable); 35 | } 36 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/com/github/likavn/eventbus/core/api/interceptor/DeliverBeforeInterceptor.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 DeliverBeforeInterceptor { 27 | 28 | /** 29 | * 拦截器执行 30 | * 31 | * @param request request 32 | */ 33 | void execute(Request request); 34 | } 35 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/com/github/likavn/eventbus/core/api/interceptor/DeliverThrowableLastInterceptor.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 | * 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 request, Throwable throwable); 36 | } 37 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/com/github/likavn/eventbus/core/api/interceptor/SendAfterInterceptor.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 SendAfterInterceptor { 27 | 28 | /** 29 | * 拦截器执行 30 | * 31 | * @param request request 32 | */ 33 | void execute(Request request); 34 | } 35 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/com/github/likavn/eventbus/core/api/interceptor/SendBeforeInterceptor.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 SendBeforeInterceptor { 27 | 28 | /** 29 | * 拦截器执行 30 | * 31 | * @param request request 32 | */ 33 | void execute(Request request); 34 | } 35 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/com/github/likavn/eventbus/core/base/AcquireListeners.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.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 { 29 | 30 | /** 31 | * 获取消费者 32 | * 33 | * @return 消费者 34 | */ 35 | List getListeners(); 36 | } 37 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/com/github/likavn/eventbus/core/base/Lifecycle.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.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 listeners; 34 | /** 35 | * -- GETTER -- 36 | * 获取当前监听组件状态 37 | */ 38 | @Getter 39 | protected volatile boolean active = false; 40 | 41 | public MsgListenerContainer(Collection listeners) { 42 | this.listeners = listeners; 43 | } 44 | 45 | /** 46 | * 启动bus组件 47 | */ 48 | public synchronized void startup() { 49 | if (Func.isEmpty(listeners)) { 50 | return; 51 | } 52 | this.active = true; 53 | registerListeners(); 54 | } 55 | 56 | /** 57 | * 停止bus组件 58 | */ 59 | public synchronized void shutdown() { 60 | this.active = false; 61 | this.destroyListeners(); 62 | } 63 | 64 | /** 65 | * 注册所有监听组件 66 | */ 67 | protected synchronized void registerListeners() { 68 | if (!this.active) { 69 | return; 70 | } 71 | // 遍历组件列表 72 | for (Lifecycle listener : listeners) { 73 | // 注册组件 74 | listener.register(); 75 | } 76 | log.info("Eventbus register listeners success"); 77 | } 78 | 79 | /** 80 | * 销毁所有监听组件 81 | */ 82 | protected synchronized void destroyListeners() { 83 | // 遍历组件列表 84 | for (Lifecycle listener : listeners) { 85 | // 销毁组件 86 | listener.destroy(); 87 | } 88 | log.info("Eventbus destroy listeners..."); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/com/github/likavn/eventbus/core/base/NodeTestConnect.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 | * 节点连接状态检测接口,用于判断当前应用是否与节点断开连接 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 { 27 | /** 28 | * 获取消息ID 29 | * 30 | * @return 消息ID 31 | */ 32 | String getRequestId(); 33 | 34 | /** 35 | * 消息所属来源服务ID,服务名 36 | * 37 | * @return 应用服务ID 38 | */ 39 | String getServiceId(); 40 | 41 | /** 42 | * 消息编码code,用于区分不同的消息类型 43 | * 44 | * @return 消息类型 45 | */ 46 | String getCode(); 47 | 48 | /** 49 | * 获取消息投递次数 50 | * 51 | * @return 消息投递次数 52 | */ 53 | int getDeliverCount(); 54 | 55 | /** 56 | * 获取消息轮询次数 57 | * 58 | * @return 消息轮询次数 59 | */ 60 | int getPollingCount(); 61 | 62 | /** 63 | * 获取消费者接收失败后,发起的失败重试次数 64 | * 65 | * @return 返回重试的次数 66 | */ 67 | int getFailRetryCount(); 68 | 69 | /** 70 | * 是否为重试消息 71 | * 72 | * @return 是否为重试消息 73 | */ 74 | boolean isRetry(); 75 | 76 | /** 77 | * 获取消息头Map 78 | * 79 | * @return 消息头 80 | */ 81 | Map getHeaders(); 82 | 83 | /** 84 | * 添加消息头 85 | */ 86 | void addHeader(String key, String value); 87 | 88 | /** 89 | * 获取消息头 90 | */ 91 | String header(String key); 92 | 93 | /** 94 | * 获取消息体 95 | * 96 | * @return 消息体 97 | */ 98 | T getBody(); 99 | } 100 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/com/github/likavn/eventbus/core/metadata/data/MsgBody.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 | /** 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 extends Topic implements Message { 42 | private static final long serialVersionUID = 1L; 43 | /** 44 | * 事件ID,默认UUID 45 | *

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 headers; 94 | 95 | /** 96 | * 业务消息体 97 | * 注:必须包含无参构造函数 98 | */ 99 | @SuppressWarnings("all") 100 | private T body; 101 | 102 | @Override 103 | public String topic() { 104 | return Func.getTopic(serviceId, code); 105 | } 106 | 107 | /** 108 | * 将当前对象转换为JSON字符串 109 | */ 110 | public String toJson() { 111 | return Func.toJson(this); 112 | } 113 | 114 | @Override 115 | public void addHeader(String key, String value) { 116 | if (headers == null) { 117 | headers = new HashMap<>(4); 118 | } 119 | headers.put(key, value); 120 | } 121 | 122 | @Override 123 | public String header(String key) { 124 | return headers == null ? null : headers.get(key); 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/com/github/likavn/eventbus/core/metadata/data/Topic.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 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 T parseObject(String text, Type type) { 43 | return JSON.parseObject(text, type); 44 | } 45 | 46 | @Override 47 | public int getOrder() { 48 | return 1; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/com/github/likavn/eventbus/core/support/FastjsonProvider.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.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 T parseObject(String text, Type type) { 43 | return JSON.parseObject(text, type); 44 | } 45 | 46 | @Override 47 | public int getOrder() { 48 | return 2; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/com/github/likavn/eventbus/core/support/GsonProvider.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.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 T parseObject(String text, Type type) { 44 | return GsonUtil.GSON.fromJson(text, type); 45 | } 46 | 47 | @Override 48 | public int getOrder() { 49 | return 4; 50 | } 51 | 52 | private static class GsonUtil { 53 | public static final Gson GSON = new Gson(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/com/github/likavn/eventbus/core/support/JacksonProvider.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.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 T parseObject(String text, Type type) { 55 | try { 56 | return JacksonUtil.MAPPER.readValue(text, JacksonUtil.MAPPER.constructType(type)); 57 | } catch (IOException e) { 58 | throw new EventBusException(e); 59 | } 60 | } 61 | 62 | @Override 63 | public int getOrder() { 64 | return 3; 65 | } 66 | 67 | private static class JacksonUtil { 68 | private static final ObjectMapper MAPPER = new ObjectMapper(); 69 | 70 | static { 71 | MAPPER.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); 72 | MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 73 | MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/com/github/likavn/eventbus/core/support/spi/IJson.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.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 目标对象的泛型类型 86 | * @return 解析后的对象 87 | */ 88 | T parseObject(String text, Type type); 89 | 90 | /** 91 | * 获取当前实现的处理顺序。 92 | * 用于在多个实现存在时确定处理的优先级,order越小越优先。 93 | * 94 | * @return 处理顺序的整数表示 95 | */ 96 | int getOrder(); 97 | } 98 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/com/github/likavn/eventbus/core/support/task/CronTask.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.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 returnThreadNumbers = new LinkedList<>(); 48 | 49 | /** 50 | * 创建线程工厂 51 | * 52 | * @param prefix 线程名前缀 53 | */ 54 | public NamedThreadFactory(String prefix) { 55 | this.prefix = prefix; 56 | } 57 | 58 | @Override 59 | public Thread newThread(Runnable r) { 60 | return new Thread(null, r, prefix + increment()); 61 | } 62 | 63 | /** 64 | * 增加一个可用资源,采用原子操作保证线程安全。 65 | */ 66 | public synchronized int increment() { 67 | if (!returnThreadNumbers.isEmpty()) { 68 | return returnThreadNumbers.remove(0); 69 | } 70 | return ++threadNumber; 71 | } 72 | 73 | /** 74 | * 返回一个线程编号 75 | */ 76 | public synchronized void decrement(Integer theadNumber) { 77 | returnThreadNumbers.add(theadNumber); 78 | returnThreadNumbers.sort(Integer::compareTo); 79 | } 80 | 81 | /** 82 | * 清空线程编号 83 | */ 84 | public synchronized void clear() { 85 | threadNumber = 0; 86 | returnThreadNumbers.clear(); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/com/github/likavn/eventbus/core/utils/PollThreadPoolExecutor.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 | 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 workQueue, ThreadFactory threadFactory) { 48 | super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory); 49 | } 50 | 51 | /** 52 | * 执行任务前,检查线程池是否正在运行。 53 | * 如果是,则提交任务至线程池执行。 54 | * 55 | * @param r 要执行的任务 56 | */ 57 | @Override 58 | public synchronized void execute(Runnable r) { 59 | if (!running) { 60 | running = true; 61 | } 62 | super.execute(r); 63 | } 64 | 65 | /** 66 | * 任务执行后,如果线程池仍在运行,则尝试重新执行任务。 67 | * 这支持了任务的轮询重试机制。 68 | * 69 | * @param r 执行的任务 70 | * @param t 执行过程中可能抛出的异常 71 | */ 72 | @Override 73 | protected void afterExecute(Runnable r, Throwable t) { 74 | if (running) { 75 | super.execute(r); 76 | } 77 | } 78 | 79 | /** 80 | * 线程池终止时,将运行标志设置为false。 81 | * 这用于标记线程池不再接受新的任务。 82 | */ 83 | @Override 84 | public void terminated() { 85 | running = false; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/com/github/likavn/eventbus/core/utils/WaitThreadPoolExecutor.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 | 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 workQueue, ThreadFactory threadFactory) { 44 | super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory); 45 | } 46 | 47 | /** 48 | * 覆盖execute方法,添加对线程池状态的检查。 49 | * 50 | * @param r 要执行的任务 51 | */ 52 | @Override 53 | public synchronized void execute(Runnable r) { 54 | // 当线程池未关闭且未终止时,检查是否可以接受新任务 55 | while (!isShutdown() && !isTerminated()) { 56 | // 如果当前线程池大小小于最大线程数,或者工作队列还有剩余容量 57 | if (getPoolSize() < getMaximumPoolSize() 58 | || getQueue().remainingCapacity() > 0) { 59 | // 执行任务并退出循环 60 | super.execute(r); 61 | break; 62 | } 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /eventbus-core/src/main/resources/META-INF/services/com.github.likavn.eventbus.core.support.spi.IJson: -------------------------------------------------------------------------------- 1 | com.github.likavn.eventbus.core.support.Fast2jsonProvider 2 | com.github.likavn.eventbus.core.support.FastjsonProvider 3 | com.github.likavn.eventbus.core.support.JacksonProvider 4 | com.github.likavn.eventbus.core.support.GsonProvider -------------------------------------------------------------------------------- /eventbus-demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.github.likavn 7 | eventbus 8 | ${revision} 9 | 10 | 4.0.0 11 | eventbus-demo 12 | pom 13 | 14 | springboot-demo 15 | 16 | 17 | true 18 | 19 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/EventBusApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.likavn.eventbus.demo; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.boot.Banner; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | import org.springframework.context.annotation.EnableAspectJAutoProxy; 8 | 9 | /** 10 | * EventBusDemo 11 | * 12 | * @author nnnmar 13 | */ 14 | @Slf4j 15 | @SpringBootApplication 16 | @EnableAspectJAutoProxy(exposeProxy = true) 17 | @SuppressWarnings("all") 18 | public class EventBusApplication extends SpringApplication { 19 | private static final EventBusApplication that = new EventBusApplication(EventBusApplication.class); 20 | 21 | public EventBusApplication(Class... primarySources) { 22 | super(primarySources); 23 | setBannerMode(Banner.Mode.LOG); 24 | } 25 | 26 | public static void main(String[] args) { 27 | that.run(args); 28 | log.info("启动成功"); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/config/EventbusConfiguration.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.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 reSendMessage(@RequestParam("consumerDataId") Long consumerDataId) { 32 | bsHelper.resend(consumerDataId); 33 | return R.ok(Boolean.TRUE); 34 | } 35 | 36 | @GetMapping(value = "/page") 37 | public R> selectPage(Page page, BsConsumerVO consumer) { 38 | return R.ok(bsConsumerService.selectPage(page, consumer)); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/controller/vo/BsConsumerVO.java: -------------------------------------------------------------------------------- 1 | package com.github.likavn.eventbus.demo.controller.vo; 2 | 3 | import com.fasterxml.jackson.annotation.JsonFormat; 4 | import com.github.likavn.eventbus.demo.entity.BsConsumer; 5 | import lombok.Data; 6 | import lombok.EqualsAndHashCode; 7 | import org.springframework.format.annotation.DateTimeFormat; 8 | 9 | import java.time.LocalDateTime; 10 | 11 | @Data 12 | @EqualsAndHashCode(callSuper = true) 13 | public class BsConsumerVO extends BsConsumer { 14 | /** 15 | * 消息所属来源服务ID,服务名 16 | */ 17 | private String serviceId; 18 | 19 | /** 20 | * 消息类型,用于区分不同的消息类型 21 | */ 22 | private String code; 23 | 24 | /** 25 | * 消息体,必须包含无参构造函数 26 | */ 27 | private String body; 28 | 29 | /** 30 | * 发送者IP 31 | */ 32 | private String sendIpAddress; 33 | 34 | /** 35 | * 消息类型,1及时消息、2延时消息 36 | */ 37 | private String typeStr; 38 | 39 | /** 40 | * 消息接收状态:0待处理、1处理成功、2处理失败 41 | */ 42 | private String statusStr; 43 | /** 44 | * 创建时间 45 | */ 46 | @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") 47 | @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") 48 | private LocalDateTime dataCreateTime; 49 | } 50 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/domain/R.java: -------------------------------------------------------------------------------- 1 | package com.github.likavn.eventbus.demo.domain; 2 | 3 | import lombok.Data; 4 | 5 | import java.io.Serializable; 6 | 7 | /** 8 | * @author likavn 9 | * @date 2024/1/20 10 | **/ 11 | @Data 12 | public class R implements Serializable { 13 | private static final long serialVersionUID = 1L; 14 | private int code; 15 | private boolean success; 16 | private T data; 17 | private String msg; 18 | 19 | public static R ok(T data) { 20 | final R tr = new R<>(); 21 | tr.setCode(200); 22 | tr.setSuccess(true); 23 | tr.setData(data); 24 | return tr; 25 | } 26 | 27 | public static R fail(String msg) { 28 | final R tr = new R<>(); 29 | tr.setCode(400); 30 | tr.setSuccess(false); 31 | tr.setMsg(msg); 32 | return tr; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/domain/TMsg.java: -------------------------------------------------------------------------------- 1 | package com.github.likavn.eventbus.demo.domain; 2 | 3 | import lombok.Data; 4 | 5 | /** 6 | * @author likavn 7 | * @date 2024/01/01 8 | **/ 9 | @Data 10 | public class TMsg { 11 | private String id; 12 | private String name; 13 | private String content; 14 | private Integer type; 15 | private String time; 16 | private String status; 17 | } 18 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/domain/TestBody.java: -------------------------------------------------------------------------------- 1 | package com.github.likavn.eventbus.demo.domain; 2 | 3 | import com.github.likavn.eventbus.core.metadata.data.MsgBody; 4 | import com.github.likavn.eventbus.demo.constant.MsgConstant; 5 | import lombok.AllArgsConstructor; 6 | import lombok.Data; 7 | import lombok.NoArgsConstructor; 8 | 9 | /** 10 | * @author likavn 11 | */ 12 | @Data 13 | @AllArgsConstructor 14 | @NoArgsConstructor 15 | public class TestBody implements MsgBody { 16 | private String content; 17 | 18 | @Override 19 | public String code() { 20 | return MsgConstant.MSG_LISTENER; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/domain/TestDelayBody.java: -------------------------------------------------------------------------------- 1 | package com.github.likavn.eventbus.demo.domain; 2 | 3 | import com.github.likavn.eventbus.core.metadata.data.MsgBody; 4 | import com.github.likavn.eventbus.demo.constant.MsgConstant; 5 | import lombok.AllArgsConstructor; 6 | import lombok.Data; 7 | import lombok.NoArgsConstructor; 8 | 9 | /** 10 | * @author likavn 11 | */ 12 | @Data 13 | @AllArgsConstructor 14 | @NoArgsConstructor 15 | public class TestDelayBody implements MsgBody { 16 | private String content; 17 | 18 | @Override 19 | public String code() { 20 | return MsgConstant.MSG_DELAY_LISTENER; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/entity/BsData.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.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 request, Throwable throwable) { 29 | // log.info("DemoDeliverAfterInterceptor execute->{}", request.getRequestId()); 30 | // 异常为空时标识成功投递,否则为投递失败 31 | if (null == throwable) { 32 | bsHelper.deliverSuccess(request); 33 | return; 34 | } 35 | bsHelper.deliverException(request, throwable); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/interceptor/DemoDeliverBeforeInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.github.likavn.eventbus.demo.interceptor; 2 | 3 | import com.github.likavn.eventbus.core.api.interceptor.DeliverBeforeInterceptor; 4 | import com.github.likavn.eventbus.core.metadata.data.Request; 5 | import com.github.likavn.eventbus.demo.helper.BsHelper; 6 | import lombok.extern.slf4j.Slf4j; 7 | import org.springframework.context.annotation.Lazy; 8 | import org.springframework.stereotype.Component; 9 | 10 | import javax.annotation.Resource; 11 | 12 | /** 13 | * 消费前置拦截器 14 | * 可以设置上下文信息 15 | * 16 | * @author likavn 17 | * @date 2024/01/01 18 | **/ 19 | @Slf4j 20 | @Component 21 | public class DemoDeliverBeforeInterceptor implements DeliverBeforeInterceptor { 22 | @Lazy 23 | @Resource 24 | private BsHelper bsHelper; 25 | 26 | @Override 27 | public void execute(Request request) { 28 | //log.info("DemoDeliverBeforeInterceptor execute->{}", request.getBody()); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/interceptor/DemoDeliverBeforeInterceptor2.java: -------------------------------------------------------------------------------- 1 | package com.github.likavn.eventbus.demo.interceptor; 2 | 3 | import com.github.likavn.eventbus.core.api.interceptor.DeliverBeforeInterceptor; 4 | import com.github.likavn.eventbus.core.metadata.data.Request; 5 | import com.github.likavn.eventbus.demo.helper.BsHelper; 6 | import lombok.extern.slf4j.Slf4j; 7 | import org.springframework.context.annotation.Lazy; 8 | import org.springframework.stereotype.Component; 9 | 10 | import javax.annotation.Resource; 11 | 12 | /** 13 | * 消费前置拦截器 14 | * 可以设置上下文信息 15 | * 16 | * @author likavn 17 | * @date 2024/01/01 18 | **/ 19 | @Slf4j 20 | @Component 21 | public class DemoDeliverBeforeInterceptor2 implements DeliverBeforeInterceptor { 22 | @Lazy 23 | @Resource 24 | private BsHelper bsHelper; 25 | 26 | @Override 27 | public void execute(Request request) { 28 | //log.info("DemoDeliverBeforeInterceptor2 execute->{}", request.getBody()); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/interceptor/DemoDeliverBeforeInterceptor3.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.DeliverBeforeInterceptor; 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 | * 17 | * @author likavn 18 | * @date 2024/01/01 19 | **/ 20 | @Slf4j 21 | @Order(1) 22 | @Component 23 | public class DemoDeliverBeforeInterceptor3 implements DeliverBeforeInterceptor { 24 | @Lazy 25 | @Resource 26 | private BsHelper bsHelper; 27 | 28 | @Override 29 | public void execute(Request request) { 30 | //log.info("DemoDeliverBeforeInterceptor3 execute->{}", request.getBody()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/interceptor/DemoDeliverThrowableLastInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.github.likavn.eventbus.demo.interceptor; 2 | 3 | import com.github.likavn.eventbus.core.api.interceptor.DeliverThrowableLastInterceptor; 4 | import com.github.likavn.eventbus.core.metadata.data.Request; 5 | import com.github.likavn.eventbus.demo.helper.BsHelper; 6 | import lombok.extern.slf4j.Slf4j; 7 | import org.springframework.context.annotation.Lazy; 8 | import org.springframework.stereotype.Component; 9 | 10 | import javax.annotation.Resource; 11 | 12 | /** 13 | * @author likavn 14 | * @date 2024/01/01 15 | **/ 16 | @Slf4j 17 | @Component 18 | public class DemoDeliverThrowableLastInterceptor implements DeliverThrowableLastInterceptor { 19 | @Lazy 20 | @Resource 21 | private BsHelper bsHelper; 22 | 23 | @Override 24 | public void execute(Request request, Throwable throwable) { 25 | log.info("DemoDeliverThrowableLastInterceptor execute 最后一次投递仍失败!->{}", throwable.getMessage()); 26 | bsHelper.deliverException(request, throwable); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/interceptor/DemoSendAfterInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.github.likavn.eventbus.demo.interceptor; 2 | 3 | import com.github.likavn.eventbus.core.api.interceptor.SendAfterInterceptor; 4 | import com.github.likavn.eventbus.core.metadata.data.Request; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.springframework.stereotype.Component; 7 | 8 | /** 9 | * @author likavn 10 | * @date 2024/01/01 11 | **/ 12 | @Slf4j 13 | @Component 14 | public class DemoSendAfterInterceptor implements SendAfterInterceptor { 15 | @Override 16 | public void execute(Request request) { 17 | //log.debug("发送后拦截器,{}", request.toJson()); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/interceptor/DemoSendBeforeInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.github.likavn.eventbus.demo.interceptor; 2 | 3 | import com.github.likavn.eventbus.core.api.interceptor.SendBeforeInterceptor; 4 | import com.github.likavn.eventbus.core.metadata.data.Request; 5 | import com.github.likavn.eventbus.demo.helper.BsHelper; 6 | import lombok.extern.slf4j.Slf4j; 7 | import org.springframework.context.annotation.Lazy; 8 | import org.springframework.stereotype.Component; 9 | 10 | import javax.annotation.Resource; 11 | 12 | /** 13 | * @author likavn 14 | * @date 2024/01/01 15 | **/ 16 | @Slf4j 17 | @Component 18 | public class DemoSendBeforeInterceptor implements SendBeforeInterceptor { 19 | @Lazy 20 | @Resource 21 | private BsHelper bsHelper; 22 | 23 | @Override 24 | public void execute(Request request) { 25 | if (request.isRetry()) { 26 | return; 27 | } 28 | bsHelper.saveMessage(request); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/listener/MsgDelayListener.java: -------------------------------------------------------------------------------- 1 | package com.github.likavn.eventbus.demo.listener; 2 | 3 | import com.github.likavn.eventbus.core.annotation.EventbusListener; 4 | import com.github.likavn.eventbus.core.annotation.FailRetry; 5 | import com.github.likavn.eventbus.core.annotation.Polling; 6 | import com.github.likavn.eventbus.core.metadata.data.Message; 7 | import com.github.likavn.eventbus.demo.domain.TestDelayBody; 8 | import lombok.extern.slf4j.Slf4j; 9 | import org.springframework.stereotype.Component; 10 | 11 | /** 12 | * @author likavn 13 | * @date 2024/01/01 14 | **/ 15 | @Slf4j 16 | @Component 17 | @EventbusListener 18 | public class MsgDelayListener implements com.github.likavn.eventbus.core.api.MsgDelayListener { 19 | @Override 20 | @Polling(count = 3, interval = "3") 21 | @FailRetry(count = 1, nextTime = 2) 22 | public void onMessage(Message message) { 23 | TestDelayBody body = message.getBody(); 24 | log.info("接收消息: {}", message.getRequestId()); 25 | //throw new RuntimeException("DemoMsgDelayListener test"); 26 | } 27 | 28 | // @Override 29 | // public void failHandler(Message message, Throwable throwable) { 30 | // TestDelayBody body = message.getBody(); 31 | // log.error("消息投递失败!: {},{}", message.getRequestId(), throwable.getMessage()); 32 | // } 33 | } 34 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/listener/MsgDelayListener2.java: -------------------------------------------------------------------------------- 1 | package com.github.likavn.eventbus.demo.listener; 2 | 3 | import com.github.likavn.eventbus.core.annotation.EventbusListener; 4 | import com.github.likavn.eventbus.core.annotation.FailRetry; 5 | import com.github.likavn.eventbus.core.api.MsgDelayListener; 6 | import com.github.likavn.eventbus.core.metadata.data.Message; 7 | import com.github.likavn.eventbus.demo.domain.TestDelayBody; 8 | import lombok.extern.slf4j.Slf4j; 9 | import org.springframework.stereotype.Component; 10 | 11 | /** 12 | * @author likavn 13 | * @date 2024/01/01 14 | **/ 15 | @Slf4j 16 | @Component 17 | @EventbusListener 18 | public class MsgDelayListener2 implements MsgDelayListener { 19 | @Override 20 | @FailRetry(count = 1, nextTime = 15) 21 | public void onMessage(Message message) { 22 | TestDelayBody body = message.getBody(); 23 | log.info("接收消息: {}", message.getRequestId()); 24 | //throw new RuntimeException("DemoMsgDelayListener test"); 25 | } 26 | 27 | @Override 28 | public void failHandler(Message message, Throwable throwable) { 29 | TestDelayBody body = message.getBody(); 30 | log.error("消息投递失败!: {},{}", message.getRequestId(), throwable.getMessage()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/listener/MsgDelayListenerClassName.java: -------------------------------------------------------------------------------- 1 | package com.github.likavn.eventbus.demo.listener; 2 | 3 | import com.github.likavn.eventbus.core.annotation.EventbusListener; 4 | import com.github.likavn.eventbus.core.annotation.FailRetry; 5 | import com.github.likavn.eventbus.core.api.MsgDelayListener; 6 | import com.github.likavn.eventbus.core.metadata.data.Message; 7 | import lombok.extern.slf4j.Slf4j; 8 | import org.springframework.stereotype.Component; 9 | 10 | /** 11 | * @author likavn 12 | * @date 2024/01/01 13 | **/ 14 | @Slf4j 15 | @Component 16 | @EventbusListener 17 | public class MsgDelayListenerClassName implements MsgDelayListener { 18 | @Override 19 | @FailRetry(count = 1, nextTime = 2) 20 | public void onMessage(Message message) { 21 | String body = message.getBody(); 22 | log.info("接收消息: {}", message.getRequestId()); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/listener/MsgDelayListenerCode.java: -------------------------------------------------------------------------------- 1 | package com.github.likavn.eventbus.demo.listener; 2 | 3 | import com.github.likavn.eventbus.core.annotation.EventbusListener; 4 | import com.github.likavn.eventbus.core.annotation.FailRetry; 5 | import com.github.likavn.eventbus.core.api.MsgDelayListener; 6 | import com.github.likavn.eventbus.core.metadata.data.Message; 7 | import com.github.likavn.eventbus.demo.constant.MsgConstant; 8 | import com.github.likavn.eventbus.demo.domain.TMsg; 9 | import lombok.extern.slf4j.Slf4j; 10 | import org.springframework.stereotype.Component; 11 | 12 | /** 13 | * @author likavn 14 | * @date 2024/01/01 15 | **/ 16 | @Slf4j 17 | @Component 18 | @EventbusListener(codes = MsgConstant.MSG_DELAY_LISTENER_CODE) 19 | public class MsgDelayListenerCode implements MsgDelayListener { 20 | @Override 21 | @FailRetry(count = 1, nextTime = 2) 22 | public void onMessage(Message message) { 23 | TMsg body = message.getBody(); 24 | log.info("接收消息: {}", message.getRequestId()); 25 | throw new RuntimeException("DemoMsgDelayListener test"); 26 | } 27 | 28 | public void failHandler(Message message, Throwable throwable) { 29 | TMsg body = message.getBody(); 30 | log.error("消息投递失败!: {},{}", message.getRequestId(), throwable.getMessage()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/listener/MsgListener.java: -------------------------------------------------------------------------------- 1 | package com.github.likavn.eventbus.demo.listener; 2 | 3 | import com.github.likavn.eventbus.core.annotation.EventbusListener; 4 | import com.github.likavn.eventbus.core.annotation.FailRetry; 5 | import com.github.likavn.eventbus.core.annotation.Polling; 6 | import com.github.likavn.eventbus.core.annotation.ToDelay; 7 | import com.github.likavn.eventbus.core.metadata.data.Message; 8 | import com.github.likavn.eventbus.demo.domain.TestBody; 9 | import lombok.extern.slf4j.Slf4j; 10 | import org.springframework.stereotype.Component; 11 | 12 | import java.io.Serializable; 13 | 14 | /** 15 | * @author likavn 16 | * @date 2024/01/01 17 | **/ 18 | @Slf4j 19 | @Component 20 | @EventbusListener(serviceId = "EBServer") 21 | public class MsgListener implements Serializable, Cloneable, com.github.likavn.eventbus.core.api.MsgListener { 22 | 23 | @Override 24 | @ToDelay(firstDeliver = false, delayTime = 5) 25 | @Polling(count = 3, interval = "$deliverCount * 2.5") 26 | @FailRetry(count = 3, interval = "$deliverCount * 1.5") 27 | public void onMessage(Message msg) { 28 | TestBody body = msg.getBody(); 29 | log.info("接收数据,第{}次投递,轮询{}次,失败重试{}次:RequestId:{}", msg.getDeliverCount(), msg.getPollingCount(), msg.getFailRetryCount(), msg.getRequestId()); 30 | 31 | if (msg.getDeliverCount() == 2) { 32 | // throw new RuntimeException("DemoMsgListener test"); 33 | } 34 | 35 | //if (msg.getPollingCount() >= 2) { 36 | // 终止轮询 37 | //Polling.Keep.setNextTime(2); 38 | // Polling.Keep.over(); 39 | // log.info("终止轮询"); 40 | // } 41 | } 42 | 43 | @Override 44 | public void failHandler(Message message, Throwable throwable) { 45 | log.error("消息投递失败!: {},{}", message.getRequestId(), throwable.getMessage()); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/listener/MsgListener2.java: -------------------------------------------------------------------------------- 1 | package com.github.likavn.eventbus.demo.listener; 2 | 3 | import com.github.likavn.eventbus.core.annotation.EventbusListener; 4 | import com.github.likavn.eventbus.core.annotation.FailRetry; 5 | import com.github.likavn.eventbus.core.api.MsgListener; 6 | import com.github.likavn.eventbus.core.metadata.data.Message; 7 | import com.github.likavn.eventbus.demo.domain.TestBody; 8 | import lombok.extern.slf4j.Slf4j; 9 | import org.springframework.stereotype.Component; 10 | 11 | /** 12 | * @author likavn 13 | * @date 2024/01/01 14 | **/ 15 | @Slf4j 16 | @Component 17 | @EventbusListener 18 | public class MsgListener2 implements MsgListener { 19 | @Override 20 | @FailRetry(count = 3, nextTime = 7) 21 | public void onMessage(Message message) { 22 | TestBody body = message.getBody(); 23 | log.info("接收数据: {}", message.getRequestId()); 24 | // throw new RuntimeException("DemoMsgListener2 test"); 25 | } 26 | 27 | @Override 28 | public void failHandler(Message message, Throwable throwable) { 29 | log.error("消息投递失败!: {},{}", message.getRequestId(), throwable.getMessage()); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/listener/MsgListenerClassName.java: -------------------------------------------------------------------------------- 1 | package com.github.likavn.eventbus.demo.listener; 2 | 3 | import com.github.likavn.eventbus.core.annotation.EventbusListener; 4 | import com.github.likavn.eventbus.core.annotation.ToDelay; 5 | import com.github.likavn.eventbus.core.api.MsgListener; 6 | import com.github.likavn.eventbus.core.metadata.data.Message; 7 | import lombok.extern.slf4j.Slf4j; 8 | import org.springframework.stereotype.Component; 9 | 10 | /** 11 | * @author likavn 12 | * @date 2024/01/01 13 | **/ 14 | @Slf4j 15 | @Component 16 | @EventbusListener(concurrency = 5) 17 | public class MsgListenerClassName implements MsgListener { 18 | 19 | @Override 20 | @ToDelay(delayTime = 3) 21 | public void onMessage(Message message) { 22 | String body = message.getBody(); 23 | log.info("接收数据: {}", message.getRequestId()); 24 | //throw new RuntimeException("DemoMsgListener3 test"); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/listener/MsgListenerCode.java: -------------------------------------------------------------------------------- 1 | package com.github.likavn.eventbus.demo.listener; 2 | 3 | import com.github.likavn.eventbus.core.annotation.EventbusListener; 4 | import com.github.likavn.eventbus.core.annotation.FailRetry; 5 | import com.github.likavn.eventbus.core.api.MsgListener; 6 | import com.github.likavn.eventbus.core.metadata.data.Message; 7 | import com.github.likavn.eventbus.demo.constant.MsgConstant; 8 | import com.github.likavn.eventbus.demo.domain.TMsg; 9 | import lombok.extern.slf4j.Slf4j; 10 | import org.springframework.stereotype.Component; 11 | 12 | /** 13 | * @author likavn 14 | * @date 2024/01/01 15 | **/ 16 | @Slf4j 17 | @Component 18 | @EventbusListener( 19 | codes = { 20 | MsgConstant.MSG_LISTENER_CODE, 21 | MsgConstant.MSG_LISTENER_CODE_V2 22 | }, 23 | concurrency = 1) 24 | public class MsgListenerCode implements MsgListener { 25 | 26 | @Override 27 | @FailRetry(count = 1, nextTime = 7) 28 | public void onMessage(Message message) { 29 | TMsg body = message.getBody(); 30 | log.info("接收数据: {}", message.getRequestId()); 31 | //throw new RuntimeException("DemoMsgListener3 test"); 32 | } 33 | 34 | @Override 35 | public void failHandler(Message message, Throwable throwable) { 36 | log.error("消息投递失败!: {},{}", message.getRequestId(), throwable.getMessage()); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/listener/test/TestArrayListener.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.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 { 30 | @Override 31 | public void onMessage(Message message) { 32 | System.out.println(message); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/listener/test/TestBeanObjectListener.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.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 { 30 | 31 | 32 | @Override 33 | public void onMessage(Message message) { 34 | System.out.println(message); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/listener/test/TestIntegerListener.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.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 { 29 | 30 | @Override 31 | public void onMessage(Message message) { 32 | Integer body = message.getBody(); 33 | System.out.println(message); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/listener/test/TestListListener.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.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> { 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 | *

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 MsgListener> { 31 | 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/TestStringListener.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.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 = "testStringListener") 28 | public class TestStringListener implements MsgListener { 29 | 30 | @Override 31 | public void onMessage(Message message) { 32 | System.out.println(message); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/mapper/BsConsumerMapper.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.mapper; 17 | 18 | import com.baomidou.mybatisplus.core.mapper.BaseMapper; 19 | import com.baomidou.mybatisplus.core.metadata.IPage; 20 | import com.github.likavn.eventbus.demo.controller.vo.BsConsumerVO; 21 | import com.github.likavn.eventbus.demo.entity.BsConsumer; 22 | import org.apache.ibatis.annotations.Param; 23 | 24 | /** 25 | * @author likavn 26 | * @date 2024/3/31 27 | **/ 28 | public interface BsConsumerMapper extends BaseMapper { 29 | IPage listPage(@Param("page") IPage page, @Param("consumer") BsConsumerVO consumer); 30 | } 31 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/mapper/BsDataMapper.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.mapper; 17 | 18 | import com.baomidou.mybatisplus.core.mapper.BaseMapper; 19 | import com.github.likavn.eventbus.demo.entity.BsData; 20 | 21 | /** 22 | * @author likavn 23 | * @date 2024/3/31 24 | **/ 25 | public interface BsDataMapper extends BaseMapper { 26 | } 27 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/service/BsConsumerService.java: -------------------------------------------------------------------------------- 1 | package com.github.likavn.eventbus.demo.service; 2 | 3 | import com.baomidou.mybatisplus.core.metadata.IPage; 4 | import com.baomidou.mybatisplus.extension.service.IService; 5 | import com.github.likavn.eventbus.demo.controller.vo.BsConsumerVO; 6 | import com.github.likavn.eventbus.demo.entity.BsConsumer; 7 | 8 | public interface BsConsumerService extends IService { 9 | IPage selectPage(IPage page,BsConsumerVO consumer); 10 | } 11 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/service/BsConsumerServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.github.likavn.eventbus.demo.service; 2 | 3 | import com.baomidou.mybatisplus.core.metadata.IPage; 4 | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; 5 | import com.github.likavn.eventbus.core.utils.Func; 6 | import com.github.likavn.eventbus.demo.controller.vo.BsConsumerVO; 7 | import com.github.likavn.eventbus.demo.entity.BsConsumer; 8 | import com.github.likavn.eventbus.demo.enums.ConsumerStatus; 9 | import com.github.likavn.eventbus.demo.mapper.BsConsumerMapper; 10 | import org.springframework.stereotype.Service; 11 | 12 | @Service 13 | public class BsConsumerServiceImpl extends ServiceImpl implements BsConsumerService { 14 | 15 | @Override 16 | public IPage selectPage(IPage page, BsConsumerVO consumer) { 17 | IPage retPage = baseMapper.listPage(page, consumer); 18 | if (!Func.isEmpty(retPage.getRecords())) { 19 | retPage.getRecords().forEach(item -> { 20 | item.setTypeStr(Integer.valueOf(1).equals(item.getType()) ? "及时消息" : "延时消息"); 21 | ConsumerStatus consumerStatus = ConsumerStatus.of(item.getStatus()); 22 | if (null != consumerStatus) { 23 | item.setStatusStr(consumerStatus.getName()); 24 | } 25 | }); 26 | } 27 | return retPage; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/service/BsDataService.java: -------------------------------------------------------------------------------- 1 | package com.github.likavn.eventbus.demo.service; 2 | 3 | import com.baomidou.mybatisplus.extension.service.IService; 4 | import com.github.likavn.eventbus.demo.entity.BsData; 5 | 6 | public interface BsDataService extends IService { 7 | } 8 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/java/com/github/likavn/eventbus/demo/service/BsDataServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.github.likavn.eventbus.demo.service; 2 | 3 | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; 4 | import com.github.likavn.eventbus.demo.entity.BsData; 5 | import com.github.likavn.eventbus.demo.mapper.BsDataMapper; 6 | import org.springframework.stereotype.Service; 7 | 8 | @Service 9 | public class BsDataServiceImpl extends ServiceImpl implements BsDataService{ 10 | } 11 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8080 3 | spring: 4 | application: 5 | name: EBServer 6 | datasource: 7 | driver-class-name: com.mysql.cj.jdbc.Driver 8 | url: jdbc:mysql://127.0.0.1:3306/eventbus?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai 9 | username: root 10 | password: likavn@03.10 11 | hikari: 12 | max-lifetime: 120000 13 | #redis 配置 14 | redis: 15 | database: 0 16 | host: 127.0.0.1 17 | lettuce: 18 | pool: 19 | max-active: 8 #最大连接数据库连接数,设 -1 为没有限制 20 | max-idle: 8 #最大等待连接中的数量,设 0 为没有限制 21 | max-wait: -1ms #最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。 22 | min-idle: 0 #最小等待连接中的数量,设 0 为没有限制 23 | shutdown-timeout: 100ms 24 | port: 16379 25 | password: likavn@312 26 | 27 | rabbitmq: 28 | host: localhost 29 | port: 5672 30 | username: admin 31 | password: 123456 32 | # virtual-host: GHost 33 | virtual-host: / 34 | #rocketmq配置 35 | rocketmq: 36 | name-server: 192.168.0.108:9876 37 | producer: 38 | group: ${spring.application.name} # 生产者组 39 | 40 | eventbus: 41 | #type: rocketmq 42 | type: redis 43 | #type: rabbitmq 44 | redis: 45 | stream-expired-hours: 24 46 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | 2 | _ _ 3 | | | | | 4 | _____ _____ _ __ | |_| |__ _ _ ___ 5 | / _ \ \ / / _ \ '_ \| __| '_ \| | | / __| 6 | | __/\ V / __/ | | | |_| |_) | |_| \__ \ 7 | \___| \_/ \___|_| |_|\__|_.__/ \__,_|___/ 8 | 9 | Spring Boot Version: ${spring-boot.version}${spring-boot.formatted-version} -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/resources/logback-spring.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 12 | %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %highlight(%-5level) %cyan(%logger{50}:%L) - %msg%n 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | ${LOG_HOME}/eventbus-%d{yyyy-MM-dd}.%i.log 21 | 22 | 30 23 | 10MB 24 | 25 | 26 | 27 | %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}:%L - %msg%n 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/main/resources/mapper/BsConsumerMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 23 | 24 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/test/java/com/github/likavn/eventbus/JSONProviderTest.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; 17 | 18 | import com.github.likavn.eventbus.core.support.*; 19 | import com.github.likavn.eventbus.core.support.spi.IJson; 20 | import lombok.Data; 21 | import org.junit.jupiter.api.Assertions; 22 | import org.junit.jupiter.api.Test; 23 | 24 | import java.math.BigDecimal; 25 | import java.util.ArrayList; 26 | import java.util.List; 27 | 28 | /** 29 | * json测试 30 | * 31 | * @author likavn 32 | * @date 2024/5/23 33 | */ 34 | public class JSONProviderTest { 35 | 36 | @Test 37 | public void toJSONString() { 38 | String val1="{\"age\":18,\"name\":\"likavn\"}"; 39 | String val2="[{\"age\":20,\"name\":\"张三\"},{\"age\":18,\"name\":\"likavn\"}]"; 40 | List bodys = new ArrayList<>(2); 41 | TestBody body = new TestBody(); 42 | body.setName("张三"); 43 | body.setAge(20); 44 | bodys.add(body); 45 | body = new TestBody(); 46 | body.setName("likavn"); 47 | body.setAge(18); 48 | bodys.add(body); 49 | IJson json = new Fast2jsonProvider(); 50 | if (json.active()) { 51 | System.out.println("Fast2jsonProvider test"); 52 | String str = json.toJsonString(body); 53 | System.out.println(str); 54 | Assertions.assertEquals(val1, str); 55 | str = json.toJsonString(bodys); 56 | System.out.println(str); 57 | Assertions.assertEquals(val2, str); 58 | } 59 | json = new FastjsonProvider(); 60 | if (json.active()) { 61 | System.out.println("FastjsonProvider test"); 62 | String str = json.toJsonString(body); 63 | System.out.println(str); 64 | Assertions.assertEquals(val1, str); 65 | str = json.toJsonString(bodys); 66 | System.out.println(str); 67 | Assertions.assertEquals(val2, str); 68 | } 69 | json = new JacksonProvider(); 70 | if (json.active()) { 71 | System.out.println("JacksonProvider test"); 72 | String str = json.toJsonString(body); 73 | System.out.println(str); 74 | // Assertions.assertEquals(val1, str); 75 | str = json.toJsonString(bodys); 76 | System.out.println(str); 77 | // Assertions.assertEquals(val2, str); 78 | } 79 | json = new GsonProvider(); 80 | if (json.active()) { 81 | System.out.println("GsonProvider test"); 82 | String str = json.toJsonString(body); 83 | System.out.println(str); 84 | // Assertions.assertEquals(val1, str); 85 | str = json.toJsonString(bodys); 86 | System.out.println(str); 87 | // Assertions.assertEquals(val2, str); 88 | } 89 | 90 | } 91 | 92 | @Data 93 | public static class TestBody { 94 | private String name; 95 | private Integer age; 96 | private BigDecimal amount; 97 | } 98 | 99 | 100 | } 101 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/test/java/com/github/likavn/eventbus/MsgSenderTest.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; 17 | 18 | import com.github.likavn.eventbus.core.api.MsgSender; 19 | import com.github.likavn.eventbus.demo.EventBusApplication; 20 | import com.github.likavn.eventbus.demo.domain.TMsg; 21 | import org.junit.jupiter.api.Test; 22 | import org.junit.runner.RunWith; 23 | import org.springframework.boot.test.context.SpringBootTest; 24 | import org.springframework.test.context.junit4.SpringRunner; 25 | 26 | import javax.annotation.Resource; 27 | import java.util.ArrayList; 28 | import java.util.HashMap; 29 | import java.util.List; 30 | import java.util.Map; 31 | 32 | /** 33 | * json测试 34 | * 35 | * @author likavn 36 | * @date 2024/5/23 37 | */ 38 | @RunWith(SpringRunner.class) 39 | @SpringBootTest(classes = EventBusApplication.class) 40 | public class MsgSenderTest { 41 | 42 | @Resource 43 | private MsgSender msgSender; 44 | 45 | @Test 46 | public void testArrayListener() { 47 | TMsg tMsg = new TMsg(); 48 | tMsg.setId("123"); 49 | TMsg tMsg1 = new TMsg(); 50 | tMsg1.setId("4"); 51 | msgSender.send("testArrayListener", new TMsg[]{tMsg, tMsg1}); 52 | } 53 | 54 | @Test 55 | public void testBeanObjectListener() { 56 | TMsg tMsg = new TMsg(); 57 | tMsg.setId("123"); 58 | msgSender.send("testBeanObjectListener", tMsg); 59 | } 60 | 61 | @Test 62 | public void testIntegerListener() { 63 | msgSender.send("testIntegerListener", 123); 64 | } 65 | 66 | @Test 67 | public void testListListener() { 68 | List list = new ArrayList<>(2); 69 | TMsg tMsg = new TMsg(); 70 | tMsg.setId("123"); 71 | list.add(tMsg); 72 | TMsg tMsg1 = new TMsg(); 73 | tMsg1.setId("4"); 74 | list.add(tMsg1); 75 | msgSender.send("testListListener", list); 76 | } 77 | 78 | @Test 79 | public void testMapListener() { 80 | Map map = new HashMap<>(4); 81 | map.put("k1", 1); 82 | map.put("k2", "9"); 83 | msgSender.send("testMapListener", map); 84 | } 85 | 86 | @Test 87 | public void testStringListener() { 88 | msgSender.send("testStringListener", "test"); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/test/java/com/github/likavn/eventbus/TaskRegistryTest.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; 17 | 18 | import com.github.likavn.eventbus.core.TaskRegistry; 19 | import com.github.likavn.eventbus.core.support.task.CronTask; 20 | import com.github.likavn.eventbus.core.support.task.PeriodTask; 21 | import org.junit.jupiter.api.Test; 22 | 23 | import java.text.SimpleDateFormat; 24 | import java.util.Date; 25 | import java.util.concurrent.TimeUnit; 26 | 27 | /** 28 | * 测试定时任务 29 | * 30 | * @author likavn 31 | * @date 2024/4/24 32 | */ 33 | @SuppressWarnings("all") 34 | public class TaskRegistryTest { 35 | /** 36 | * 演示创建和注册CronTask。 37 | */ 38 | @Test 39 | public void taskRegistry() { 40 | TaskRegistry taskRegistry = new TaskRegistry(); 41 | 42 | CronTask cronTask = CronTask.create("test1", "0,35 * * * * ?", () -> { 43 | System.out.println("CronTask=" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ",thread=" + Thread.currentThread().getName()); 44 | }); 45 | taskRegistry.createTask(cronTask); 46 | // 47 | // PeriodTask periodTask = new PeriodTask("test", 10, TimeUnit.SECONDS, () -> { 48 | // System.out.println("PeriodTask=" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ",thread=" + Thread.currentThread().getName()); 49 | // }); 50 | // taskRegistry.createTask(periodTask); 51 | } 52 | 53 | public static void main(String[] args) throws InterruptedException { 54 | TaskRegistry taskRegistry = new TaskRegistry(); 55 | 56 | CronTask cronTask = CronTask.create("test1", "0/10 * * * * ? ", () -> { 57 | System.out.println("CronTask=" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ",thread=" + Thread.currentThread().getName()); 58 | }); 59 | taskRegistry.createTask(cronTask); 60 | 61 | PeriodTask periodTask = PeriodTask.create("test", 10, TimeUnit.SECONDS, () -> { 62 | System.out.println("PeriodTask=" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ",thread=" + Thread.currentThread().getName()); 63 | }); 64 | // taskRegistry.createTask(periodTask); 65 | // Thread.sleep(20 * 1000); 66 | // periodTask.cancel(); 67 | // Thread.sleep(20 * 1000); 68 | // periodTask.start(); 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/test/java/com/github/likavn/eventbus/utils/CalculateUtilTest.java: -------------------------------------------------------------------------------- 1 | package com.github.likavn.eventbus.utils; 2 | 3 | 4 | import com.github.likavn.eventbus.core.utils.CalculateUtil; 5 | import org.junit.jupiter.api.Assertions; 6 | import org.junit.jupiter.api.Test; 7 | 8 | public class CalculateUtilTest { 9 | 10 | @Test 11 | public void testCalculate() { 12 | String expression = "1+2*3/4"; 13 | double v = CalculateUtil.evalExpression(expression); 14 | Assertions.assertEquals(v, 2.5); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /eventbus-demo/springboot-demo/src/test/java/com/github/likavn/eventbus/utils/GroupedThreadPoolExecutorTest.java: -------------------------------------------------------------------------------- 1 | package com.github.likavn.eventbus.utils; 2 | 3 | import com.github.likavn.eventbus.core.utils.GroupedThreadPoolExecutor; 4 | import com.github.likavn.eventbus.core.utils.NamedThreadFactory; 5 | import org.junit.jupiter.api.Test; 6 | 7 | import java.util.concurrent.CountDownLatch; 8 | import java.util.concurrent.atomic.AtomicInteger; 9 | import java.util.stream.Stream; 10 | 11 | import static org.junit.jupiter.api.Assertions.assertEquals; 12 | 13 | public class GroupedThreadPoolExecutorTest { 14 | 15 | @Test 16 | public void testTask() throws InterruptedException { 17 | GroupedThreadPoolExecutor executor = new GroupedThreadPoolExecutor( 18 | 1, 1000 * 5, new NamedThreadFactory("test-thread-pool-")); 19 | AtomicInteger executeNum = new AtomicInteger(0); 20 | AtomicInteger index = new AtomicInteger(0); 21 | 22 | int concurrency = 5; 23 | int pollNum = 1000; 24 | int streamSize = 600; 25 | int realNum = pollNum * streamSize; 26 | CountDownLatch latch = new CountDownLatch(Math.toIntExact(realNum)); 27 | Stream.of(new Byte[pollNum]).parallel().forEach(t -> { 28 | new Thread(() -> Stream.of(new Byte[streamSize]).parallel().forEach(t1 -> { 29 | GroupedThreadPoolExecutor.GTask task = new GroupedThreadPoolExecutor.GTask(); 30 | task.setName("task index-" + index.incrementAndGet()); 31 | task.setConcurrency(concurrency); 32 | task.target(() -> { 33 | executeNum.incrementAndGet(); 34 | latch.countDown(); 35 | }); 36 | executor.execute(task); 37 | })).start(); 38 | }); 39 | latch.await(); 40 | if (executeNum.get() == realNum) { 41 | System.out.println("成功... " + executeNum.get()); 42 | } else { 43 | System.out.println("失败... " + executeNum.get() + ",相差:" + (realNum - executeNum.get())); 44 | } 45 | assertEquals(realNum, executeNum.get()); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /eventbus-spring-boot-starter/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.github.likavn 7 | eventbus 8 | ${revision} 9 | 10 | 4.0.0 11 | eventbus-spring-boot-starter 12 | jar 13 | 14 | 15 | 2.3.0.RELEASE 16 | 2.3.1 17 | 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-autoconfigure 22 | 23 | 24 | com.github.likavn 25 | eventbus-core 26 | ${revision} 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-amqp 32 | true 33 | 34 | 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-data-redis 39 | true 40 | 41 | 42 | 43 | org.apache.rocketmq 44 | rocketmq-spring-boot-starter 45 | ${rocketmq.version} 46 | true 47 | 48 | 49 | 50 | 51 | 52 | 53 | org.springframework.boot 54 | spring-boot-dependencies 55 | ${spring-boot.version} 56 | pom 57 | import 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /eventbus-spring-boot-starter/src/main/java/com/github/likavn/eventbus/BootConnectionWatchdog.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; 17 | 18 | import com.github.likavn.eventbus.core.ConnectionWatchdog; 19 | import com.github.likavn.eventbus.core.base.Lifecycle; 20 | import com.github.likavn.eventbus.core.base.NodeTestConnect; 21 | import com.github.likavn.eventbus.core.metadata.BusConfig; 22 | import lombok.extern.slf4j.Slf4j; 23 | import org.springframework.beans.factory.DisposableBean; 24 | import org.springframework.boot.ApplicationArguments; 25 | import org.springframework.boot.ApplicationRunner; 26 | 27 | import java.util.Collection; 28 | 29 | /** 30 | * 启动 31 | * 32 | * @author likavn 33 | * @date 2024/1/17 34 | **/ 35 | @Slf4j 36 | public class BootConnectionWatchdog 37 | extends ConnectionWatchdog implements ApplicationRunner, DisposableBean { 38 | public BootConnectionWatchdog(NodeTestConnect testConnect, 39 | BusConfig.TestConnect testConnectProperties, 40 | Collection listeners) { 41 | super(testConnect, testConnectProperties, listeners); 42 | } 43 | 44 | @Override 45 | public void run(ApplicationArguments args) { 46 | super.startup(); 47 | } 48 | 49 | @Override 50 | public void destroy() { 51 | super.shutdown(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /eventbus-spring-boot-starter/src/main/java/com/github/likavn/eventbus/ConditionalOnEventbusActive.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; 17 | 18 | import com.github.likavn.eventbus.core.metadata.BusType; 19 | import org.springframework.context.annotation.Conditional; 20 | 21 | import java.lang.annotation.*; 22 | 23 | /** 24 | * 消息中间件激活配置注解 25 | * 26 | * @author likavn 27 | * @date 2024/10/08 28 | **/ 29 | @Target({ElementType.TYPE, ElementType.METHOD}) 30 | @Retention(RetentionPolicy.RUNTIME) 31 | @Documented 32 | @Conditional(OnEventbusActiveCondition.class) 33 | public @interface ConditionalOnEventbusActive { 34 | 35 | /** 36 | * 消息中间件类型{@link BusType} 37 | * 38 | * @return type 39 | */ 40 | BusType value(); 41 | 42 | /** 43 | * 是否是消息生产者 44 | * 45 | * @return true/false 46 | */ 47 | boolean sender() default false; 48 | } 49 | -------------------------------------------------------------------------------- /eventbus-spring-boot-starter/src/main/java/com/github/likavn/eventbus/OnEventbusActiveCondition.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; 17 | 18 | import com.github.likavn.eventbus.core.constant.BusConstant; 19 | import com.github.likavn.eventbus.core.metadata.BusType; 20 | import org.springframework.boot.autoconfigure.condition.ConditionMessage; 21 | import org.springframework.boot.autoconfigure.condition.ConditionOutcome; 22 | import org.springframework.boot.autoconfigure.condition.SpringBootCondition; 23 | import org.springframework.context.annotation.ConditionContext; 24 | import org.springframework.core.Ordered; 25 | import org.springframework.core.annotation.Order; 26 | import org.springframework.core.env.PropertyResolver; 27 | import org.springframework.core.type.AnnotatedTypeMetadata; 28 | 29 | import java.util.Map; 30 | 31 | /** 32 | * 消息中间件激活配置 33 | * 34 | * @author likavn 35 | * @date 2024/10/08 36 | **/ 37 | @Order(Ordered.HIGHEST_PRECEDENCE + 40) 38 | public class OnEventbusActiveCondition extends SpringBootCondition { 39 | @Override 40 | public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { 41 | Map attributes = metadata.getAnnotationAttributes(ConditionalOnEventbusActive.class.getName()); 42 | assert attributes != null; 43 | BusType activeType = (BusType) attributes.get("value"); 44 | boolean sender = (boolean) attributes.get("sender"); 45 | return determineOutcome(context.getEnvironment(), activeType, sender); 46 | } 47 | 48 | private ConditionOutcome determineOutcome(PropertyResolver resolver, BusType activeType, boolean sender) { 49 | String type = getProperty(resolver, BusConstant.TYPE_NAME); 50 | String oldType = getProperty(resolver, BusConstant.OLD_TYPE_NAME); 51 | if (!sender) { 52 | if (activeType.valid(type) || activeType.valid(oldType)) { 53 | return ConditionOutcome.match(); 54 | } 55 | } else if (activeType.valid(type)) { 56 | return ConditionOutcome.match(); 57 | } 58 | return ConditionOutcome.noMatch(ConditionMessage.of("Active type does not match: " + activeType)); 59 | } 60 | 61 | private String getProperty(PropertyResolver resolver, String key) { 62 | return resolver.getProperty(BusConstant.CONFIG_PREFIX + "." + key); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /eventbus-spring-boot-starter/src/main/java/com/github/likavn/eventbus/config/RequestIdGeneratorConfiguration.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.config; 17 | 18 | import com.github.likavn.eventbus.core.api.RequestIdGenerator; 19 | import com.github.likavn.eventbus.core.base.UUIDRequestIdGenerator; 20 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 21 | import org.springframework.context.annotation.Bean; 22 | import org.springframework.context.annotation.Configuration; 23 | import org.springframework.context.annotation.Lazy; 24 | 25 | /** 26 | * @author likavn 27 | * @date 2024/4/2 28 | **/ 29 | @Lazy 30 | @Configuration(proxyBeanMethods = false) 31 | class RequestIdGeneratorConfiguration { 32 | private RequestIdGeneratorConfiguration() { 33 | } 34 | 35 | @Configuration(proxyBeanMethods = false) 36 | static class RequestIdAutoConfig { 37 | 38 | @Bean 39 | @ConditionalOnMissingBean 40 | public RequestIdGenerator requestIdGenerator() { 41 | return new UUIDRequestIdGenerator(); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /eventbus-spring-boot-starter/src/main/java/com/github/likavn/eventbus/prop/BusProperties.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.prop; 17 | 18 | import com.github.likavn.eventbus.core.metadata.BusConfig; 19 | import lombok.Data; 20 | import lombok.EqualsAndHashCode; 21 | import org.springframework.boot.context.properties.ConfigurationProperties; 22 | 23 | /** 24 | * 配置 25 | * 26 | * @author likavn 27 | * @date 2024/01/01 28 | */ 29 | @Data 30 | @EqualsAndHashCode(callSuper = true) 31 | @ConfigurationProperties(prefix = "eventbus") 32 | public class BusProperties extends BusConfig { 33 | 34 | /** 35 | * redis配置 36 | */ 37 | private RedisProperties redis = new RedisProperties(); 38 | 39 | /** 40 | * redis配置 41 | */ 42 | @Data 43 | public static class RedisProperties { 44 | /** 45 | * 轮询时拉取Redis Stream中消息的线程池大小,默认为:2 46 | */ 47 | private Integer pollThreadPoolSize = 2; 48 | /** 49 | * 轮询时拉取Redis Stream中消息的阻塞时间,单位:毫秒,默认为:5ms 50 | */ 51 | private Long pollBlockMillis = 5L; 52 | /** 53 | * 投递消息的初始化线程池大小,默认为:5 54 | */ 55 | private Integer deliverGroupThreadPoolSize = 5; 56 | /** 57 | * 投递消息线程池中空闲线程存活时长,单位:毫秒,默认为:60s 58 | */ 59 | private Long deliverGroupThreadKeepAliveTime = 1000L * 60; 60 | /** 61 | * 消息超时时间,超时消息未被确认,才会被重新投递,单位:秒,默认:5分钟 62 | */ 63 | private Long deliverTimeout = 60 * 5L; 64 | /** 65 | * 未确认消息,重新投递时每次最多拉取多少条待确认消息数据,默认:100条 66 | */ 67 | private Integer pendingMessagesBatchSize = 100; 68 | /** 69 | * stream 过期时间,6.2及以上版本支持,单位:小时,默认:3 天 70 | */ 71 | private Long streamExpiredHours = 24 * 3L; 72 | /** 73 | * stream 过期数据截取,值为当前保留的消息数,5.0~<6.2版本支持,单位:条,默认:10000条 74 | */ 75 | private Long streamExpiredLength = 10000L; 76 | /** 77 | * redis版本号,不用配置,系统自动设定 78 | */ 79 | private String redisVersion; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /eventbus-spring-boot-starter/src/main/java/com/github/likavn/eventbus/provider/SFieldFunction.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.provider; 17 | 18 | import com.github.likavn.eventbus.core.exception.EventBusException; 19 | import com.github.likavn.eventbus.core.utils.Func; 20 | 21 | import java.io.Serializable; 22 | import java.lang.invoke.SerializedLambda; 23 | import java.lang.reflect.Method; 24 | import java.util.function.Function; 25 | 26 | /** 27 | * 泛型函数接口,获取字段名 28 | * 29 | * @author likavn 30 | * @date 2024/10/01 31 | */ 32 | public interface SFieldFunction extends Function, Serializable { 33 | 34 | /** 35 | * 获取字段名 36 | * 37 | * @return 字段名 38 | */ 39 | @SuppressWarnings("all") 40 | default String getFieldName() { 41 | try { 42 | // 获取当前实例的writeReplace方法,该方法是获取SerializedLambda的关键 43 | Method method = this.getClass().getDeclaredMethod("writeReplace"); 44 | method.setAccessible(true); 45 | SerializedLambda serializedLambda = (SerializedLambda) method.invoke(this); 46 | // 获取函数方法名 47 | String methodName = serializedLambda.getImplMethodName(); 48 | // 如果方法名以"get"开头,去掉前缀"get" 49 | if (methodName.startsWith("get")) { 50 | methodName = methodName.substring(3); 51 | } else if (methodName.startsWith("is")) { 52 | // 如果方法名以"is"开头,去掉前缀"is" 53 | methodName = methodName.substring(2); 54 | } 55 | // 首字母变小写,以符合Java字段名的常规命名规范 56 | return Func.firstToLowerCase(methodName); 57 | } catch (Exception e) { 58 | throw new EventBusException(e); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /eventbus-spring-boot-starter/src/main/java/com/github/likavn/eventbus/provider/rabbit/RabbitMsgSender.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.provider.rabbit; 17 | 18 | import com.github.likavn.eventbus.core.ListenerRegistry; 19 | import com.github.likavn.eventbus.core.api.RequestIdGenerator; 20 | import com.github.likavn.eventbus.core.base.AbstractSenderAdapter; 21 | import com.github.likavn.eventbus.core.base.InterceptorContainer; 22 | import com.github.likavn.eventbus.core.metadata.BusConfig; 23 | import com.github.likavn.eventbus.core.metadata.data.Request; 24 | import com.github.likavn.eventbus.provider.rabbit.constant.RabbitConstant; 25 | import org.springframework.amqp.rabbit.connection.CorrelationData; 26 | import org.springframework.amqp.rabbit.core.RabbitTemplate; 27 | 28 | /** 29 | * rabbitMq生产者 30 | * 31 | * @author likavn 32 | * @since 2023/01/01 33 | */ 34 | public class RabbitMsgSender extends AbstractSenderAdapter { 35 | private final RabbitTemplate rabbitTemplate; 36 | 37 | public RabbitMsgSender(RabbitTemplate rabbitTemplate, BusConfig config, 38 | InterceptorContainer interceptorContainer, RequestIdGenerator requestIdGenerator, ListenerRegistry registry) { 39 | super(config, interceptorContainer, requestIdGenerator, registry); 40 | this.rabbitTemplate = rabbitTemplate; 41 | } 42 | 43 | @Override 44 | public void toSend(Request request) { 45 | rabbitTemplate.convertAndSend( 46 | String.format(RabbitConstant.TIMELY_EXCHANGE, request.getServiceId()), 47 | String.format(RabbitConstant.TIMELY_ROUTING_KEY, request.topic()), 48 | request.toJson(), 49 | message -> { 50 | message.getMessageProperties().setContentEncoding("utf-8"); 51 | return message; 52 | }, 53 | new CorrelationData(request.getRequestId())); 54 | } 55 | 56 | @Override 57 | public void toSendDelayMessage(Request request) { 58 | rabbitTemplate.convertAndSend( 59 | String.format(RabbitConstant.DELAY_EXCHANGE, request.getServiceId()), 60 | getDelayRoutingKey(request), 61 | request.toJson(), 62 | message -> { 63 | //配置消息的过期时间,单位:毫秒 64 | message.getMessageProperties().setHeader("x-delay", 1000L * request.getDelayTime()); 65 | return message; 66 | }, 67 | new CorrelationData(request.getRequestId()) 68 | ); 69 | } 70 | 71 | /** 72 | * 根据请求对象获取路由键 73 | */ 74 | private String getDelayRoutingKey(Request request) { 75 | return getDelayKey(request, RabbitConstant.DELAY_ROUTING_KEY, 76 | RabbitConstant.DELAY_RETRY_ROUTING_KEY, RabbitConstant.TIMELY_RETRY_ROUTING_KEY); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /eventbus-spring-boot-starter/src/main/java/com/github/likavn/eventbus/provider/rabbit/RabbitMsgSubscribeListener.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.provider.rabbit; 17 | 18 | import com.github.likavn.eventbus.core.DeliveryBus; 19 | import com.github.likavn.eventbus.core.ListenerRegistry; 20 | import com.github.likavn.eventbus.core.metadata.BusConfig; 21 | import com.github.likavn.eventbus.core.metadata.support.Listener; 22 | import com.github.likavn.eventbus.provider.rabbit.support.AbstractRabbitRegisterContainer; 23 | import com.github.likavn.eventbus.provider.rabbit.support.RabbitListener; 24 | import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; 25 | 26 | import java.util.List; 27 | 28 | /** 29 | * rabbitMq消息订阅器 30 | * 31 | * @author likavn 32 | * @since 2023/01/01 33 | **/ 34 | public class RabbitMsgSubscribeListener extends AbstractRabbitRegisterContainer { 35 | private final DeliveryBus deliveryBus; 36 | private final ListenerRegistry registry; 37 | 38 | public RabbitMsgSubscribeListener(CachingConnectionFactory connectionFactory, 39 | BusConfig config, 40 | DeliveryBus deliveryBus, 41 | ListenerRegistry registry) { 42 | super(connectionFactory, config); 43 | this.deliveryBus = deliveryBus; 44 | this.registry = registry; 45 | } 46 | 47 | @Override 48 | public List getListeners() { 49 | return RabbitListener.getAllListeners(registry); 50 | } 51 | 52 | @Override 53 | protected void deliver(Listener listener, byte[] body) { 54 | deliveryBus.deliver(listener, body); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /eventbus-spring-boot-starter/src/main/java/com/github/likavn/eventbus/provider/rabbit/RabbitNodeTestConnect.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.provider.rabbit; 17 | 18 | import com.github.likavn.eventbus.core.base.NodeTestConnect; 19 | import com.rabbitmq.client.Channel; 20 | import lombok.extern.slf4j.Slf4j; 21 | import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; 22 | import org.springframework.amqp.rabbit.connection.Connection; 23 | 24 | /** 25 | * rabbitMq连接状态测试 26 | * 27 | * @author likavn 28 | * @date 2023/5/30 29 | **/ 30 | @Slf4j 31 | public class RabbitNodeTestConnect implements NodeTestConnect { 32 | private final CachingConnectionFactory connectionFactory; 33 | private Connection connection; 34 | private Channel channel; 35 | 36 | public RabbitNodeTestConnect(CachingConnectionFactory connectionFactory) { 37 | this.connectionFactory = connectionFactory; 38 | } 39 | 40 | @Override 41 | public synchronized boolean testConnect() { 42 | try { 43 | if (null == connection) { 44 | connection = connectionFactory.createConnection(); 45 | } 46 | if (null == channel) { 47 | channel = connection.createChannel(false); 48 | } 49 | channel.basicQos(1); 50 | return true; 51 | } catch (Exception ex) { 52 | log.error("rabbitMq testConnect", ex); 53 | return false; 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /eventbus-spring-boot-starter/src/main/java/com/github/likavn/eventbus/provider/rabbit/config/BusBootRabbitConfiguration.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.provider.rabbit.config; 17 | 18 | import com.github.likavn.eventbus.ConditionalOnEventbusActive; 19 | import com.github.likavn.eventbus.core.DeliveryBus; 20 | import com.github.likavn.eventbus.core.ListenerRegistry; 21 | import com.github.likavn.eventbus.core.api.RequestIdGenerator; 22 | import com.github.likavn.eventbus.core.base.InterceptorContainer; 23 | import com.github.likavn.eventbus.core.metadata.BusConfig; 24 | import com.github.likavn.eventbus.core.metadata.BusType; 25 | import com.github.likavn.eventbus.provider.rabbit.RabbitMsgSender; 26 | import com.github.likavn.eventbus.provider.rabbit.RabbitMsgSubscribeListener; 27 | import com.github.likavn.eventbus.provider.rabbit.RabbitNodeTestConnect; 28 | import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; 29 | import org.springframework.amqp.rabbit.core.RabbitTemplate; 30 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 31 | import org.springframework.context.annotation.Bean; 32 | import org.springframework.context.annotation.Configuration; 33 | import org.springframework.context.annotation.Lazy; 34 | 35 | /** 36 | * rabbitMq实现配置 37 | * 38 | * @author likavn 39 | * @date 2024/01/01 40 | */ 41 | @Configuration 42 | @ConditionalOnEventbusActive(value = BusType.RABBITMQ) 43 | public class BusBootRabbitConfiguration { 44 | 45 | @Bean 46 | @ConditionalOnMissingBean(RabbitMsgSubscribeListener.class) 47 | public RabbitMsgSubscribeListener rabbitMsgSubscribeListener( 48 | CachingConnectionFactory connectionFactory, BusConfig config, DeliveryBus deliveryBus, ListenerRegistry registry) { 49 | return new RabbitMsgSubscribeListener(connectionFactory, config, deliveryBus, registry); 50 | } 51 | 52 | @Configuration 53 | @ConditionalOnEventbusActive(value = BusType.RABBITMQ, sender = true) 54 | static class RabbitSenderConfiguration { 55 | 56 | @Bean 57 | @ConditionalOnMissingBean(RabbitMsgSender.class) 58 | public RabbitMsgSender msgSender(RabbitTemplate rabbitTemplate, 59 | BusConfig config, 60 | @Lazy InterceptorContainer interceptorContainer, 61 | RequestIdGenerator requestIdGenerator, 62 | @Lazy ListenerRegistry registry) { 63 | return new RabbitMsgSender(rabbitTemplate, config, interceptorContainer, requestIdGenerator, registry); 64 | } 65 | 66 | @Bean 67 | @ConditionalOnMissingBean(RabbitNodeTestConnect.class) 68 | public RabbitNodeTestConnect rabbitNodeTestConnect(CachingConnectionFactory connectionFactory) { 69 | return new RabbitNodeTestConnect(connectionFactory); 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /eventbus-spring-boot-starter/src/main/java/com/github/likavn/eventbus/provider/rabbit/constant/RabbitConstant.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.provider.rabbit.constant; 17 | 18 | /** 19 | * rabbitmq常量 20 | * 21 | * @author likavn 22 | * @date 2024/01/01 23 | */ 24 | public class RabbitConstant { 25 | private RabbitConstant() { 26 | } 27 | 28 | /** 29 | * 前缀 30 | */ 31 | private static final String SUFFIX = "ebus."; 32 | 33 | /** 34 | * 及时消息交换机 35 | * 参数: 36 | *

37 | * 1.服务serviceId; 38 | */ 39 | public static final String TIMELY_EXCHANGE = SUFFIX + "t.exchange.%s"; 40 | 41 | /** 42 | * 及时消息路由key; 43 | * 参数: 44 | *

45 | * 1.服务serviceId|消息编码; 46 | */ 47 | public static final String TIMELY_ROUTING_KEY = SUFFIX + "t.routingKey.%s"; 48 | 49 | /** 50 | * 及时消息队列key; 51 | * 参数: 52 | *

53 | * 1.服务serviceId|消息编码|监听器全类名; 54 | */ 55 | public static final String TIMELY_QUEUE = SUFFIX + "t.queue.%s"; 56 | 57 | /** 58 | * 及时消息重试路由key; 59 | * 参数: 60 | *

61 | * 1.服务serviceId|消息编码|监听器全类名; 62 | */ 63 | public static final String TIMELY_RETRY_ROUTING_KEY = SUFFIX + "t.retry.routingKey.%s"; 64 | 65 | /** 66 | * 及时消息重试队列key; 67 | * 参数: 68 | *

69 | * 1.服务serviceId|消息编码|监听器全类名; 70 | */ 71 | public static final String TIMELY_RETRY_QUEUE = SUFFIX + "t.retry.queue.%s"; 72 | 73 | /** 74 | * 延时队列交换机 75 | * 参数: 76 | *

77 | * 1.服务serviceId; 78 | */ 79 | public static final String DELAY_EXCHANGE = SUFFIX + "d.exchange.%s"; 80 | 81 | /** 82 | * 延时队列路由key 83 | * 参数: 84 | *

85 | * 1.服务serviceId|消息编码 86 | */ 87 | public static final String DELAY_ROUTING_KEY = SUFFIX + "d.routingKey.%s"; 88 | 89 | /** 90 | * 延时队列名 91 | * 参数: 92 | *

93 | * 1.服务serviceId|消息编码|监听器全类名; 94 | */ 95 | public static final String DELAY_QUEUE = SUFFIX + "d.queue.%s"; 96 | 97 | /** 98 | * 延时队列路由key,异常重试路由 99 | * 参数: 100 | *

101 | * 1.服务serviceId|消息编码|监听器全类名; 102 | */ 103 | public static final String DELAY_RETRY_ROUTING_KEY = SUFFIX + "d.retry.routingKey.%s"; 104 | 105 | /** 106 | * 延时消息重试队列key 107 | * 参数: 108 | *

109 | * 1.服务serviceId|消息编码|监听器全类名; 110 | */ 111 | public static final String DELAY_RETRY_QUEUE = SUFFIX + "d.retry.queue.%s"; 112 | } 113 | -------------------------------------------------------------------------------- /eventbus-spring-boot-starter/src/main/java/com/github/likavn/eventbus/provider/redis/RLock.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.provider.redis; 17 | 18 | import org.springframework.data.redis.core.StringRedisTemplate; 19 | import org.springframework.data.redis.core.script.DefaultRedisScript; 20 | 21 | import java.util.Collections; 22 | 23 | /** 24 | * redis分布式锁 25 | * 26 | * @author likavn 27 | * @date 2023/2/22 28 | **/ 29 | public class RLock { 30 | /** 31 | * 分布式锁过期时间,单位:秒 32 | */ 33 | private static final Long LOCK_REDIS_TIMEOUT = 30L; 34 | private final StringRedisTemplate stringRedisTemplate; 35 | private final DefaultRedisScript lockRedisScript; 36 | 37 | public RLock(StringRedisTemplate stringRedisTemplate, DefaultRedisScript lockRedisScript) { 38 | this.stringRedisTemplate = stringRedisTemplate; 39 | this.lockRedisScript = lockRedisScript; 40 | } 41 | 42 | /** 43 | * 获取锁,默认超时时间30s 44 | * 45 | * @param key key 46 | * @return t 47 | */ 48 | public boolean getLock(String key) { 49 | return getLock(key, LOCK_REDIS_TIMEOUT); 50 | } 51 | 52 | /** 53 | * 获取锁 54 | * 55 | * @param key key 56 | * @param timeout 超时时间,单位:秒 57 | * @return t 58 | */ 59 | public boolean getLock(String key, long timeout) { 60 | Boolean flag = stringRedisTemplate.execute(lockRedisScript, Collections.singletonList(key), "" + timeout); 61 | return Boolean.TRUE.equals(flag); 62 | } 63 | 64 | /** 65 | * 释放锁 66 | * 67 | * @param key k 68 | */ 69 | public void releaseLock(String key) { 70 | stringRedisTemplate.delete(key); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /eventbus-spring-boot-starter/src/main/java/com/github/likavn/eventbus/provider/redis/RedisMsgSubscribeListener.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.provider.redis; 17 | 18 | import com.github.likavn.eventbus.core.DeliveryBus; 19 | import com.github.likavn.eventbus.core.ListenerRegistry; 20 | import com.github.likavn.eventbus.prop.BusProperties; 21 | import com.github.likavn.eventbus.provider.redis.support.AbstractStreamListenerContainer; 22 | import com.github.likavn.eventbus.provider.redis.support.RedisListener; 23 | import lombok.extern.slf4j.Slf4j; 24 | import org.springframework.data.redis.connection.stream.Record; 25 | import org.springframework.data.redis.core.StringRedisTemplate; 26 | 27 | import java.util.List; 28 | 29 | /** 30 | * redis消息监听器 31 | * 32 | * @author likavn 33 | * @since 2023/01/01 34 | **/ 35 | @Slf4j 36 | public class RedisMsgSubscribeListener extends AbstractStreamListenerContainer { 37 | private final ListenerRegistry registry; 38 | private final DeliveryBus deliveryBus; 39 | 40 | public RedisMsgSubscribeListener(StringRedisTemplate stringRedisTemplate, 41 | BusProperties busProperties, 42 | ListenerRegistry registry, 43 | DeliveryBus deliveryBus) { 44 | super(stringRedisTemplate, busProperties); 45 | this.registry = registry; 46 | this.deliveryBus = deliveryBus; 47 | } 48 | 49 | @Override 50 | public List getListeners() { 51 | return RedisListener.getAllListeners(registry); 52 | } 53 | 54 | @Override 55 | protected void deliver(RedisListener listener, Record msg) { 56 | deliveryBus.deliver(listener, msg.getValue()); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /eventbus-spring-boot-starter/src/main/java/com/github/likavn/eventbus/provider/redis/RedisNodeTestConnect.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.provider.redis; 17 | 18 | import com.github.likavn.eventbus.core.base.NodeTestConnect; 19 | import com.github.likavn.eventbus.core.metadata.BusConfig; 20 | import lombok.extern.slf4j.Slf4j; 21 | import org.springframework.data.redis.core.StringRedisTemplate; 22 | 23 | /** 24 | * redis连接状态测试 25 | * 26 | * @author likavn 27 | * @date 2023/5/30 28 | **/ 29 | @Slf4j 30 | public class RedisNodeTestConnect implements NodeTestConnect { 31 | 32 | private final StringRedisTemplate stringRedisTemplate; 33 | private final String testKey; 34 | 35 | public RedisNodeTestConnect(StringRedisTemplate stringRedisTemplate, BusConfig busConfig) { 36 | this.stringRedisTemplate = stringRedisTemplate; 37 | this.testKey = String.format("eventbus.{%s}", busConfig.getServiceId()); 38 | } 39 | 40 | @Override 41 | public boolean testConnect() { 42 | try { 43 | stringRedisTemplate.hasKey(testKey); 44 | return true; 45 | } catch (Exception ex) { 46 | log.error("redis timeout", ex); 47 | return false; 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /eventbus-spring-boot-starter/src/main/java/com/github/likavn/eventbus/provider/redis/constant/RedisConstant.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.provider.redis.constant; 17 | 18 | /** 19 | * redis常量 20 | * 21 | * @author likavn 22 | * @since 2023/01/01 23 | */ 24 | public class RedisConstant { 25 | private RedisConstant() { 26 | } 27 | 28 | /** 29 | * 前缀 30 | */ 31 | private static final String SUFFIX = "ebus:"; 32 | 33 | /** 34 | * 及时消息队列,stream key 35 | * 参数: 36 | *

37 | * 1. 服务ID|消息编码 38 | */ 39 | public static final String TIMELY_QUEUE = SUFFIX + "t:queue:{%s}"; 40 | 41 | /** 42 | * 及时消息重试,zset key 43 | * 参数: 44 | *

45 | * 1.服务ID|消息编码|消息监听器类全类名 46 | */ 47 | public static final String TIMELY_RETRY_ZSET = SUFFIX + "t:retry:zset:{%s}"; 48 | 49 | /** 50 | * 及时消息重试,lock key 51 | * 参数: 52 | *

53 | * 1.服务ID|消息编码|消息监听器类全类名 54 | */ 55 | public static final String TIMELY_RETRY_LOCK = SUFFIX + "t:retry:lock:{%s}"; 56 | 57 | /** 58 | * 及时消息重试,stream key 59 | * 参数: 60 | *

61 | * 1.服务ID|消息编码|消息监听器类全类名 62 | */ 63 | public static final String TIMELY_RETRY_QUEUE = SUFFIX + "t:retry:queue:{%s}"; 64 | 65 | /** 66 | * 延时消息,zset key 67 | * 参数: 68 | *

69 | * 1.服务ID|消息编码 70 | */ 71 | public static final String DELAY_ZSET = SUFFIX + "d:zset:{%s}"; 72 | /** 73 | * 延时消息,lock key 74 | * 参数: 75 | *

76 | * 1.服务ID|消息编码 77 | */ 78 | public static final String DELAY_LOCK = SUFFIX + "d:lock:{%s}"; 79 | /** 80 | * 延时消息,stream key 81 | * 参数: 82 | *

83 | * 1.服务ID|消息编码 84 | */ 85 | public static final String DELAY_QUEUE = SUFFIX + "d:queue:{%s}"; 86 | 87 | /** 88 | * 延时消息重试,zset key 89 | * 参数: 90 | *

91 | * 1.服务ID|消息编码|消息监听器类全类名 92 | */ 93 | public static final String DELAY_RETRY_ZSET = SUFFIX + "d:retry:zset:{%s}"; 94 | /** 95 | * 延时消息重试,lock key 96 | * 参数: 97 | *

98 | * 1.服务ID|消息编码|消息监听器类全类名 99 | */ 100 | public static final String DELAY_RETRY_LOCK = SUFFIX + "d:retry:lock:{%s}"; 101 | /** 102 | * 延时消息重试,stream key 103 | * 参数: 104 | *

105 | * 1.服务ID|消息编码|消息监听器类全类名 106 | */ 107 | public static final String DELAY_RETRY_QUEUE = SUFFIX + "d:retry:queue:{%s}"; 108 | 109 | } 110 | -------------------------------------------------------------------------------- /eventbus-spring-boot-starter/src/main/java/com/github/likavn/eventbus/provider/rocket/RocketMsgSender.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.provider.rocket; 17 | 18 | import com.github.likavn.eventbus.core.ListenerRegistry; 19 | import com.github.likavn.eventbus.core.api.RequestIdGenerator; 20 | import com.github.likavn.eventbus.core.base.AbstractSenderAdapter; 21 | import com.github.likavn.eventbus.core.base.InterceptorContainer; 22 | import com.github.likavn.eventbus.core.metadata.BusConfig; 23 | import com.github.likavn.eventbus.core.metadata.data.Request; 24 | import com.github.likavn.eventbus.provider.rocket.constant.RocketConstant; 25 | import org.apache.rocketmq.spring.core.RocketMQTemplate; 26 | import org.springframework.messaging.Message; 27 | import org.springframework.messaging.MessageHeaders; 28 | import org.springframework.messaging.support.MessageBuilder; 29 | 30 | import static com.github.likavn.eventbus.provider.rocket.support.RocketListener.keyFormat; 31 | 32 | /** 33 | * rocketMq生产者 34 | * 35 | * @author likavn 36 | * @since 2023/01/01 37 | * @since 2.2 38 | */ 39 | public class RocketMsgSender extends AbstractSenderAdapter { 40 | private final RocketMQTemplate rocketMqTemplate; 41 | 42 | public RocketMsgSender(RocketMQTemplate rocketMqTemplate, 43 | BusConfig config, 44 | InterceptorContainer interceptorContainer, 45 | RequestIdGenerator requestIdGenerator, 46 | ListenerRegistry registry) { 47 | super(config, interceptorContainer, requestIdGenerator, registry); 48 | this.rocketMqTemplate = rocketMqTemplate; 49 | } 50 | 51 | @Override 52 | public void toSend(Request request) { 53 | rocketMqTemplate.syncSend(keyFormat(String.format(RocketConstant.TIMELY_QUEUE, request.topic())), request.toJson()); 54 | } 55 | 56 | @Override 57 | public void toSendDelayMessage(Request request) { 58 | // 构建消息对象 59 | Message message = MessageBuilder 60 | .withPayload(request.toJson()) 61 | // 消息类型 62 | .setHeader(MessageHeaders.CONTENT_TYPE, "text/plain") 63 | .build(); 64 | rocketMqTemplate.syncSend( 65 | getDelayDestination(request), 66 | message, 67 | // 发送超时时间,十秒 68 | 10000, 69 | Math.toIntExact(request.getDelayTime())); 70 | } 71 | 72 | private String getDelayDestination(Request request) { 73 | return keyFormat(getDelayKey(request, RocketConstant.DELAY_QUEUE, RocketConstant.DELAY_RETRY_QUEUE, RocketConstant.TIMELY_RETRY_QUEUE)); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /eventbus-spring-boot-starter/src/main/java/com/github/likavn/eventbus/provider/rocket/RocketMsgSubscribeListener.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.provider.rocket; 17 | 18 | import com.github.likavn.eventbus.core.DeliveryBus; 19 | import com.github.likavn.eventbus.core.ListenerRegistry; 20 | import com.github.likavn.eventbus.core.metadata.BusConfig; 21 | import com.github.likavn.eventbus.core.metadata.support.Listener; 22 | import com.github.likavn.eventbus.provider.rocket.support.AbstractRocketRegisterContainer; 23 | import com.github.likavn.eventbus.provider.rocket.support.RocketListener; 24 | import org.apache.rocketmq.common.message.MessageExt; 25 | import org.apache.rocketmq.spring.autoconfigure.RocketMQProperties; 26 | 27 | import java.util.List; 28 | 29 | /** 30 | * rabbitMq消息订阅器 31 | * 32 | * @author likavn 33 | * @date 2023/01/01 34 | * @since 2.2 35 | **/ 36 | public class RocketMsgSubscribeListener extends AbstractRocketRegisterContainer { 37 | private final DeliveryBus deliveryBus; 38 | private final ListenerRegistry registry; 39 | 40 | public RocketMsgSubscribeListener(RocketMQProperties rocketMqProperties, 41 | BusConfig config, DeliveryBus deliveryBus, ListenerRegistry registry) { 42 | super(rocketMqProperties, config); 43 | this.deliveryBus = deliveryBus; 44 | this.registry = registry; 45 | } 46 | 47 | @Override 48 | public List getListeners() { 49 | return RocketListener.getAllListeners(registry); 50 | } 51 | 52 | @Override 53 | protected void deliver(Listener subscriber, MessageExt msg) { 54 | deliveryBus.deliver(subscriber, msg.getBody()); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /eventbus-spring-boot-starter/src/main/java/com/github/likavn/eventbus/provider/rocket/RocketNodeTestConnect.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.provider.rocket; 17 | 18 | import com.github.likavn.eventbus.core.base.NodeTestConnect; 19 | import lombok.extern.slf4j.Slf4j; 20 | import org.apache.rocketmq.client.ClientConfig; 21 | import org.apache.rocketmq.client.impl.MQClientManager; 22 | import org.apache.rocketmq.client.impl.factory.MQClientInstance; 23 | import org.apache.rocketmq.spring.autoconfigure.RocketMQProperties; 24 | 25 | /** 26 | * rocketmq连接状态测试 27 | * 28 | * @author likavn 29 | * @date 2023/5/30 30 | * @since 2.2 31 | **/ 32 | @Slf4j 33 | public class RocketNodeTestConnect implements NodeTestConnect { 34 | private final RocketMQProperties rocketMqProperties; 35 | private MQClientInstance clientFactory; 36 | 37 | public RocketNodeTestConnect(RocketMQProperties rocketMqProperties) { 38 | this.rocketMqProperties = rocketMqProperties; 39 | } 40 | 41 | @Override 42 | @SuppressWarnings("all") 43 | public synchronized boolean testConnect() { 44 | try { 45 | if (null == clientFactory) { 46 | ClientConfig clientConfig = new ClientConfig(); 47 | clientConfig.setNamesrvAddr(rocketMqProperties.getNameServer()); 48 | // 创建MQClientInstance,这是客户端的核心实例 49 | clientFactory = MQClientManager.getInstance().getOrCreateMQClientInstance(clientConfig); 50 | 51 | // 启动客户端实例,连接到NameServer 52 | clientFactory.start(); 53 | } 54 | // 设置客户端实例的NameServer地址 55 | clientFactory.getMQClientAPIImpl().getBrokerClusterInfo(1000 * 3L); 56 | return true; 57 | } catch (Exception ex) { 58 | log.error("rocketmq testConnect", ex); 59 | return false; 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /eventbus-spring-boot-starter/src/main/java/com/github/likavn/eventbus/provider/rocket/config/BusBootRocketConfiguration.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.provider.rocket.config; 17 | 18 | import com.github.likavn.eventbus.ConditionalOnEventbusActive; 19 | import com.github.likavn.eventbus.config.EventBusAutoConfiguration; 20 | import com.github.likavn.eventbus.core.DeliveryBus; 21 | import com.github.likavn.eventbus.core.ListenerRegistry; 22 | import com.github.likavn.eventbus.core.api.RequestIdGenerator; 23 | import com.github.likavn.eventbus.core.base.InterceptorContainer; 24 | import com.github.likavn.eventbus.core.metadata.BusConfig; 25 | import com.github.likavn.eventbus.core.metadata.BusType; 26 | import com.github.likavn.eventbus.provider.rocket.RocketMsgSender; 27 | import com.github.likavn.eventbus.provider.rocket.RocketMsgSubscribeListener; 28 | import com.github.likavn.eventbus.provider.rocket.RocketNodeTestConnect; 29 | import org.apache.rocketmq.spring.autoconfigure.RocketMQProperties; 30 | import org.apache.rocketmq.spring.core.RocketMQTemplate; 31 | import org.springframework.boot.autoconfigure.AutoConfigureAfter; 32 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 33 | import org.springframework.context.annotation.Bean; 34 | import org.springframework.context.annotation.Configuration; 35 | import org.springframework.context.annotation.Lazy; 36 | 37 | /** 38 | * rocketmq实现配置 39 | * 40 | * @author likavn 41 | * @date 2024/01/01 42 | * @since 2.2 43 | */ 44 | @Configuration 45 | @AutoConfigureAfter(EventBusAutoConfiguration.class) 46 | @ConditionalOnEventbusActive(value = BusType.ROCKETMQ) 47 | public class BusBootRocketConfiguration { 48 | @Bean 49 | @ConditionalOnMissingBean(RocketMsgSubscribeListener.class) 50 | public RocketMsgSubscribeListener rocketMsgSubscribeListener( 51 | RocketMQProperties properties, BusConfig busConfig, DeliveryBus deliveryBus, ListenerRegistry registry) { 52 | return new RocketMsgSubscribeListener(properties, busConfig, deliveryBus, registry); 53 | } 54 | 55 | @Configuration 56 | @ConditionalOnEventbusActive(value = BusType.ROCKETMQ, sender = true) 57 | static class RocketSenderConfiguration { 58 | 59 | @Bean 60 | @ConditionalOnMissingBean(RocketMsgSender.class) 61 | public RocketMsgSender msgSender(RocketMQTemplate template, BusConfig busConfig, 62 | @Lazy InterceptorContainer interceptorContainer, 63 | RequestIdGenerator requestIdGenerator, @Lazy ListenerRegistry registry) { 64 | return new RocketMsgSender(template, busConfig, interceptorContainer, requestIdGenerator, registry); 65 | } 66 | 67 | @Bean 68 | @ConditionalOnMissingBean(RocketNodeTestConnect.class) 69 | public RocketNodeTestConnect rocketNodeTestConnect(RocketMQProperties properties) { 70 | return new RocketNodeTestConnect(properties); 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /eventbus-spring-boot-starter/src/main/java/com/github/likavn/eventbus/provider/rocket/constant/RocketConstant.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.provider.rocket.constant; 17 | 18 | /** 19 | * rocketmq常量 20 | * 21 | * @author likavn 22 | * @date 2024/01/01 23 | * @since 2.2 24 | */ 25 | public class RocketConstant { 26 | private RocketConstant() { 27 | } 28 | 29 | /** 30 | * 前缀 31 | */ 32 | private static final String SUFFIX = "ebus_"; 33 | 34 | /** 35 | * 及时消息队列key 36 | * 参数: 37 | *

38 | * 1.服务ID|消息编码; 39 | */ 40 | public static final String TIMELY_QUEUE = SUFFIX + "t_%s"; 41 | 42 | /** 43 | * 及时消息重试队列key 44 | * 参数: 45 | *

46 | * 1.服务ID|消息编码|消息监听器全类名; 47 | */ 48 | public static final String TIMELY_RETRY_QUEUE = SUFFIX + "t_retry_%s"; 49 | 50 | /** 51 | * 延时消息队列名key 52 | * 参数: 53 | *

54 | * 1.服务ID|消息编码; 55 | */ 56 | public static final String DELAY_QUEUE = SUFFIX + "d_%s"; 57 | 58 | /** 59 | * 延时消息重试队列名key 60 | * 参数: 61 | *

62 | * 1.服务ID|消息编码|消息监听器全类名; 63 | */ 64 | public static final String DELAY_RETRY_QUEUE = SUFFIX + "d_retry_%s"; 65 | } 66 | -------------------------------------------------------------------------------- /eventbus-spring-boot-starter/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | # Initializers 2 | org.springframework.context.ApplicationContextInitializer=\ 3 | com.github.likavn.eventbus.config.EventBusEnvironmentContextInitializer 4 | 5 | # Auto Configure 6 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 7 | com.github.likavn.eventbus.config.EventBusAutoConfiguration 8 | -------------------------------------------------------------------------------- /eventbus-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports: -------------------------------------------------------------------------------- 1 | com.github.likavn.eventbus.config.EventBusAutoConfiguration -------------------------------------------------------------------------------- /eventbus-spring-boot-starter/src/main/resources/script/lock.lua: -------------------------------------------------------------------------------- 1 | --- 2 | --- 分布式锁实现 3 | --- Created by likavn 4 | --- DateTime: 2024/1/5 09:58 5 | --- 6 | --- 锁key 7 | local key = KEYS[1] 8 | --- 锁的时间,单位:秒 9 | local secondTime = ARGV[1] 10 | --- 判断是否存在锁 11 | if redis.call('exists', key) > 0 then 12 | return false 13 | else 14 | redis.call('set', key, 1) 15 | redis.call('expire', key, secondTime) 16 | return true 17 | end -------------------------------------------------------------------------------- /eventbus-spring-boot-starter/src/main/resources/script/pushMsgStream.lua: -------------------------------------------------------------------------------- 1 | --- 2 | --- 定时推送消息到redis delay stream 3 | --- Created by likavn 4 | --- DateTime: 2024/1/5 09:58 5 | --- 6 | --- delay zset key 7 | local delayKey = KEYS[1] 8 | local delayStreamKey = KEYS[2] 9 | --- 当前时间搓 10 | local currentTimeMillis = ARGV[1] 11 | --- 最大推送消息数,默认10万数据 12 | local maxPushCount = ARGV[2] or 1000000 13 | --- 每次处理的最大消息数 14 | local msgCount = 1000 15 | 16 | --- 轮询次数 17 | local pollCount = maxPushCount / msgCount 18 | local remainingPushCount = maxPushCount % msgCount 19 | 20 | --- 循环推送消息 21 | while pollCount >= 0 do 22 | if pollCount == 0 then 23 | if remainingPushCount > 0 then 24 | msgCount = remainingPushCount 25 | else 26 | break 27 | end 28 | end 29 | local expiredValues = redis.call('zrangebyscore', delayKey, 0, currentTimeMillis, 'limit', 0, msgCount) 30 | if #expiredValues > 0 then 31 | for _, v in ipairs(expiredValues) do 32 | redis.call('xadd', delayStreamKey, '*', 'payload', v) 33 | redis.call('zrem', delayKey, v) 34 | end 35 | else 36 | break 37 | end 38 | pollCount = pollCount - 1 39 | end 40 | 41 | --- 下个消息的过期时间 42 | local v = redis.call('zrange', delayKey, 0, 0, 'withscores'); 43 | if v[1] ~= nil then 44 | return tonumber(v[2]); 45 | end 46 | return nil; -------------------------------------------------------------------------------- /eventbus-spring-boot-starter/src/main/resources/script/zsetAdd.lua: -------------------------------------------------------------------------------- 1 | --- 2 | --- 延时数据发送 3 | --- Created by likavn 4 | --- DateTime: 2024/1/5 09:58 5 | --- 6 | --- 延时队列key 7 | local delayKey = KEYS[1] 8 | --- timeout 9 | local timeout = ARGV[1] 10 | --- 延时数据 11 | local jsBody = ARGV[2] 12 | ---- 添加数据 13 | redis.call('ZADD', delayKey, timeout, jsBody); 14 | 15 | --- 下个消息的过期时间 16 | local v = redis.call('zrange', delayKey, 0, 0, 'withscores'); 17 | if v[1] ~= nil then 18 | return tonumber(v[2]); 19 | end 20 | return nil; -------------------------------------------------------------------------------- /eventbus-spring-boot3-starter/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.github.likavn 7 | eventbus 8 | ${revision} 9 | 10 | 4.0.0 11 | eventbus-spring-boot3-starter 12 | jar 13 | 14 | 15 | 3.0.0 16 | 17 | UTF-8 18 | 17 19 | 17 20 | true 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-autoconfigure 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-data-redis 31 | true 32 | 33 | 34 | com.github.likavn 35 | eventbus-spring-boot-starter 36 | ${revision} 37 | 38 | 39 | 40 | 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-dependencies 45 | ${spring-boot.version} 46 | pom 47 | import 48 | 49 | 50 | 51 | --------------------------------------------------------------------------------