├── .github ├── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md ├── PULL_REQUEST_TEMPLATE └── workflows │ ├── codecov.yml │ ├── codeql-analysis.yml │ ├── integration-test.yml │ └── package-deploy.yml ├── .gitignore ├── LICENSE ├── README.md ├── README.zh-CN.md ├── build.gradle.kts ├── codecov.yml ├── config ├── checkstyle │ ├── checkstyle.xml │ └── suppressions.xml └── spotbugs │ └── exclude.xml ├── docs ├── Govern-EventBus.png └── init.sql ├── eventbus-bom └── build.gradle.kts ├── eventbus-core ├── build.gradle.kts └── src │ ├── main │ └── java │ │ └── me │ │ └── ahoo │ │ └── eventbus │ │ └── core │ │ ├── Consts.java │ │ ├── EventBusException.java │ │ ├── annotation │ │ ├── Event.java │ │ ├── Publish.java │ │ └── Subscribe.java │ │ ├── codec │ │ └── EventCodec.java │ │ ├── compensate │ │ ├── AbstractCompensateScheduler.java │ │ ├── CompensateConfig.java │ │ ├── CompensatePublishEvent.java │ │ ├── EventCompensate.java │ │ ├── PublishCompensateScheduler.java │ │ └── SubscribeCompensateScheduler.java │ │ ├── consistency │ │ ├── ConsistencyPublisher.java │ │ ├── ConsistencySubscriber.java │ │ ├── ConsistencySubscriberFactory.java │ │ └── impl │ │ │ ├── ConsistencyPublisherImpl.java │ │ │ ├── ConsistencySubscriberFactoryImpl.java │ │ │ └── ConsistencySubscriberImpl.java │ │ ├── publisher │ │ ├── EventDataGetter.java │ │ ├── EventDataIdGetter.java │ │ ├── EventDescriptor.java │ │ ├── EventDescriptorParser.java │ │ ├── EventMetadata.java │ │ ├── EventNameGenerator.java │ │ ├── PublishEvent.java │ │ ├── PublishException.java │ │ ├── Publisher.java │ │ └── impl │ │ │ ├── FieldEventDataGetter.java │ │ │ ├── FieldEventDataIdGetter.java │ │ │ ├── NoneEventDataIdGetter.java │ │ │ ├── SimpleEventDataGetter.java │ │ │ ├── SimpleEventDescriptor.java │ │ │ └── SimpleEventNameGenerator.java │ │ ├── repository │ │ ├── ConcurrentVersionConflictException.java │ │ ├── EventRepository.java │ │ ├── Identity.java │ │ ├── PublishEventRepository.java │ │ ├── PublishIdentity.java │ │ ├── PublishStatus.java │ │ ├── RepeatedSubscribeException.java │ │ ├── SubscribeEventRepository.java │ │ ├── SubscribeIdentity.java │ │ ├── SubscribeStatus.java │ │ ├── TimeTaken.java │ │ ├── Version.java │ │ └── entity │ │ │ ├── PublishEventCompensateEntity.java │ │ │ ├── PublishEventEntity.java │ │ │ ├── SubscribeEventCompensateEntity.java │ │ │ └── SubscribeEventEntity.java │ │ ├── serialize │ │ ├── Deserializer.java │ │ ├── Serializer.java │ │ └── json │ │ │ ├── JsonDeserializer.java │ │ │ └── JsonSerializer.java │ │ ├── subscriber │ │ ├── Subscriber.java │ │ ├── SubscriberNameGenerator.java │ │ ├── SubscriberRegistry.java │ │ ├── SubscriberScanner.java │ │ └── impl │ │ │ ├── SimpleSubscriber.java │ │ │ └── SimpleSubscriberNameGenerator.java │ │ └── utils │ │ ├── Dates.java │ │ └── Threads.java │ └── test │ └── java │ └── me │ └── ahoo │ └── eventbus │ └── core │ ├── AnnotationDemoEvent.java │ ├── DemoEvent.java │ ├── DemoSubscriber.java │ ├── EventDescriptorParserTest.java │ ├── ScheduledThreadPoolExecutorTests.java │ └── SubscriberScannerTests.java ├── eventbus-demo ├── build.gradle.kts └── src │ └── main │ ├── java │ └── me │ │ └── ahoo │ │ └── eventbus │ │ └── demo │ │ ├── DemoApplication.java │ │ ├── config │ │ └── AppConfig.java │ │ ├── controller │ │ ├── BusController.java │ │ └── OrderController.java │ │ ├── event │ │ ├── FieldEventData.java │ │ ├── FieldEventWrapper.java │ │ ├── OrderCreatedEvent.java │ │ ├── PublishDataEvent.java │ │ └── RePublishDataEvent.java │ │ └── service │ │ ├── BusService.java │ │ ├── NoticeService.java │ │ └── OrderService.java │ └── resources │ └── application.yml ├── eventbus-dependencies └── build.gradle.kts ├── eventbus-jdbc ├── build.gradle.kts └── src │ ├── main │ └── java │ │ └── me │ │ └── ahoo │ │ └── eventbus │ │ └── jdbc │ │ ├── JdbcPublishEventRepository.java │ │ └── JdbcSubscribeEventRepository.java │ └── test │ ├── java │ └── me │ │ └── ahoo │ │ └── eventbus │ │ └── core │ │ └── compensate │ │ └── db │ │ └── CompensateLeaderServiceTest.java │ └── resources │ └── logback.xml ├── eventbus-kafka ├── build.gradle.kts └── src │ └── main │ └── java │ └── me │ └── ahoo │ └── eventbus │ └── kafka │ ├── KafkaEventCodec.java │ ├── KafkaPublisher.java │ ├── KafkaSubscriberRegistry.java │ └── MethodKafkaListenerAdapter.java ├── eventbus-rabbit ├── build.gradle.kts └── src │ └── main │ └── java │ └── me │ └── ahoo │ └── eventbus │ └── rabbit │ ├── RabbitEventCodec.java │ ├── RabbitEventListener.java │ ├── RabbitPublisher.java │ ├── RabbitSubscriberRegistry.java │ └── config │ └── RabbitConfig.java ├── eventbus-spring-boot-autoconfigure ├── build.gradle.kts └── src │ └── main │ ├── java │ └── me │ │ └── ahoo │ │ └── eventbus │ │ └── spring │ │ └── boot │ │ └── autoconfigure │ │ ├── EnabledSuffix.java │ │ ├── EventBusAutoConfiguration.java │ │ ├── EventBusProperties.java │ │ ├── compensate │ │ ├── CompensateAutoConfiguration.java │ │ ├── CompensatePrefix.java │ │ ├── CompensateProperties.java │ │ └── ConditionalOnCompensateEnabled.java │ │ ├── kafka │ │ ├── BusKafkaAutoConfiguration.java │ │ ├── ConditionalOnKafkaEnabled.java │ │ └── KafkaProperties.java │ │ └── rabbit │ │ ├── BusRabbitAutoConfiguration.java │ │ ├── ConditionalOnRabbitEnabled.java │ │ └── RabbitProperties.java │ └── resources │ └── META-INF │ └── spring.factories ├── eventbus-spring-boot-starter └── build.gradle.kts ├── eventbus-spring-context ├── build.gradle.kts └── src │ └── main │ └── java │ └── me │ └── ahoo │ └── eventbus │ └── spring │ ├── annotation │ └── EnableEventBus.java │ └── support │ ├── EventBusBootstrapConfiguration.java │ ├── EventBusConfigurationSelector.java │ ├── PublishAnnotationAspect.java │ └── SubscriberLifecycle.java ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── renovate.json └── settings.gradle.kts /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: "\U0001F41B Bug Report" 3 | about: Something isn't working as expected 4 | --- 5 | 6 | ## Bug Report 7 | 8 | Before report a bug, make sure you have: 9 | 10 | - Searched open and closed [GitHub issues](https://github.com/Ahoo-Wang/govern-eventbus/issues). 11 | 12 | Please pay attention on issues you submitted, because we maybe need more details. 13 | If no response anymore and we cannot reproduce it on current information, we will **close it**. 14 | 15 | Please answer these questions before submitting your issue. Thanks! 16 | 17 | ### Which version of govern-eventbus did you use? 18 | 19 | ### Expected behavior 20 | 21 | ### Actual behavior 22 | 23 | ### Reason analyze (If you can) 24 | 25 | ### Steps to reproduce the behavior 26 | 27 | ### Example codes for reproduce this issue (such as a github link). 28 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE: -------------------------------------------------------------------------------- 1 | Fixes #ISSUSE_ID. 2 | 3 | Changes proposed in this pull request: 4 | - 5 | - 6 | - 7 | -------------------------------------------------------------------------------- /.github/workflows/codecov.yml: -------------------------------------------------------------------------------- 1 | name: Codecov 2 | on: [ push, pull_request ] 3 | jobs: 4 | codecov: 5 | name: Codecov 6 | runs-on: ubuntu-latest 7 | steps: 8 | - name: Checkout 9 | uses: actions/checkout@master 10 | 11 | - name: Set up JDK 8 12 | uses: actions/setup-java@v3 13 | with: 14 | java-version: '8' 15 | distribution: 'adopt' 16 | server-id: github 17 | settings-path: ${{ github.workspace }} 18 | 19 | - name: Check 20 | run: gradle check 21 | 22 | - name: Build Code Coverage Report 23 | run: gradle codeCoverageReport 24 | 25 | - name: Upload Code Coverage Report to Codecov 26 | uses: codecov/codecov-action@v3 27 | with: 28 | token: ${{ secrets.CODECOV_TOKEN }} 29 | flags: unittests # optional 30 | name: codecov-umbrella # optional 31 | fail_ci_if_error: true # optional (default = false) 32 | verbose: true # optional (default = false) 33 | files: ./build/reports/jacoco/codeCoverageReport/codeCoverageReport.xml 34 | path_to_write_report: ./coverage/codecov_report.txt 35 | -------------------------------------------------------------------------------- /.github/workflows/codeql-analysis.yml: -------------------------------------------------------------------------------- 1 | # For most projects, this workflow file will not need changing; you simply need 2 | # to commit it to your repository. 3 | # 4 | # You may wish to alter this file to override the set of languages analyzed, 5 | # or to provide custom queries or build logic. 6 | # 7 | # ******** NOTE ******** 8 | # We have attempted to detect the languages in your repository. Please check 9 | # the `language` matrix defined below to confirm you have the correct set of 10 | # supported CodeQL languages. 11 | # 12 | name: "CodeQL" 13 | 14 | on: 15 | push: 16 | branches: [ main ] 17 | pull_request: 18 | # The branches below must be a subset of the branches above 19 | branches: [ main ] 20 | schedule: 21 | - cron: '17 9 * * 5' 22 | 23 | jobs: 24 | analyze: 25 | name: Analyze 26 | runs-on: ubuntu-latest 27 | permissions: 28 | actions: read 29 | contents: read 30 | security-events: write 31 | 32 | strategy: 33 | fail-fast: false 34 | matrix: 35 | language: [ 'java' ] 36 | # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ] 37 | # Learn more about CodeQL language support at https://git.io/codeql-language-support 38 | 39 | steps: 40 | - name: Checkout repository 41 | uses: actions/checkout@v3 42 | 43 | # Initializes the CodeQL tools for scanning. 44 | - name: Initialize CodeQL 45 | uses: github/codeql-action/init@v2 46 | with: 47 | languages: ${{ matrix.language }} 48 | # If you wish to specify custom queries, you can do so here or in a config file. 49 | # By default, queries listed here will override any specified in a config file. 50 | # Prefix the list here with "+" to use these queries and those in the config file. 51 | # queries: ./path/to/local/query, your-org/your-repo/queries@main 52 | 53 | # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). 54 | # If this step fails, then you should remove it and run the build manually (see below) 55 | - name: Autobuild 56 | uses: github/codeql-action/autobuild@v2 57 | 58 | # ℹ️ Command-line programs to run using the OS shell. 59 | # 📚 https://git.io/JvXDl 60 | 61 | # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines 62 | # and modify them (or add more) to build your code if your project 63 | # uses a compiled language 64 | 65 | #- run: | 66 | # make bootstrap 67 | # make release 68 | 69 | - name: Perform CodeQL Analysis 70 | uses: github/codeql-action/analyze@v2 71 | -------------------------------------------------------------------------------- /.github/workflows/integration-test.yml: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # http://www.apache.org/licenses/LICENSE-2.0 7 | # Unless required by applicable law or agreed to in writing, software 8 | # distributed under the License is distributed on an "AS IS" BASIS, 9 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | # See the License for the specific language governing permissions and 11 | # limitations under the License. 12 | # 13 | 14 | name: Integration Test 15 | on: [ push, pull_request ] 16 | jobs: 17 | cosid-core-test: 18 | name: CosId Core Test 19 | runs-on: ubuntu-latest 20 | steps: 21 | - name: Checkout 22 | uses: actions/checkout@master 23 | 24 | - name: Set up JDK 8 25 | uses: actions/setup-java@v3 26 | with: 27 | java-version: '8' 28 | distribution: 'adopt' 29 | server-id: github 30 | settings-path: ${{ github.workspace }} 31 | 32 | - name: Test 33 | run: gradle clean build 34 | -------------------------------------------------------------------------------- /.github/workflows/package-deploy.yml: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # http://www.apache.org/licenses/LICENSE-2.0 7 | # Unless required by applicable law or agreed to in writing, software 8 | # distributed under the License is distributed on an "AS IS" BASIS, 9 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | # See the License for the specific language governing permissions and 11 | # limitations under the License. 12 | # 13 | 14 | name: Packages Deploy 15 | on: 16 | release: 17 | types: [created] 18 | 19 | jobs: 20 | github-deploy: 21 | runs-on: ubuntu-latest 22 | permissions: 23 | contents: read 24 | packages: write 25 | steps: 26 | - name: Checkout 27 | uses: actions/checkout@master 28 | 29 | - name: Set up JDK 8 30 | uses: actions/setup-java@v3 31 | with: 32 | java-version: '8' 33 | distribution: 'adopt' 34 | server-id: github 35 | settings-path: ${{ github.workspace }} 36 | 37 | - name: Publish package 38 | run: gradle publishAllPublicationsToGitHubPackagesRepository 39 | env: 40 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 41 | SIGNING_KEYID: ${{ secrets.SIGNING_KEYID }} 42 | SIGNING_SECRETKEY: ${{ secrets.SIGNING_SECRETKEY }} 43 | SIGNING_PASSWORD: ${{ secrets.SIGNING_PASSWORD }} 44 | central-deploy: 45 | runs-on: ubuntu-latest 46 | steps: 47 | - name: Checkout 48 | uses: actions/checkout@master 49 | 50 | - name: Set up JDK 8 51 | uses: actions/setup-java@v3 52 | with: 53 | java-version: '8' 54 | distribution: 'adopt' 55 | server-id: github 56 | settings-path: ${{ github.workspace }} 57 | 58 | - name: Publish package 59 | run: gradle publishToSonatype closeAndReleaseSonatypeStagingRepository 60 | env: 61 | MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }} 62 | MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }} 63 | SIGNING_KEYID: ${{ secrets.SIGNING_KEYID }} 64 | SIGNING_SECRETKEY: ${{ secrets.SIGNING_SECRETKEY }} 65 | SIGNING_PASSWORD: ${{ secrets.SIGNING_PASSWORD }} 66 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /build/ 3 | 4 | /out/ 5 | 6 | # Ignore Gradle GUI config 7 | gradle-app.setting 8 | 9 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 10 | !gradle-wrapper.jar 11 | 12 | # Cache of project 13 | .gradletasknamecache 14 | 15 | # # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 16 | # gradle/wrapper/gradle-wrapper.properties 17 | -------------------------------------------------------------------------------- /codecov.yml: -------------------------------------------------------------------------------- 1 | coverage: 2 | status: 3 | patch: 4 | default: 5 | threshold: 1% 6 | 7 | ignore: 8 | - "eventbus-demo/.*" 9 | -------------------------------------------------------------------------------- /config/checkstyle/suppressions.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /config/spotbugs/exclude.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /docs/Govern-EventBus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ahoo-Wang/govern-eventbus/343878f2666b50d3a48c15f7fc7217554bb55763/docs/Govern-EventBus.png -------------------------------------------------------------------------------- /docs/init.sql: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | create table simba_mutex 15 | ( 16 | mutex varchar(66) not null primary key comment 'mutex name', 17 | acquired_at bigint unsigned not null, 18 | ttl_at bigint unsigned not null, 19 | transition_at bigint unsigned not null, 20 | owner_id char(32) not null, 21 | version int unsigned not null 22 | ); 23 | 24 | create table publish_event 25 | ( 26 | id bigint unsigned auto_increment 27 | primary key, 28 | event_name varchar(100) not null, 29 | event_data_id bigint unsigned default 0 not null, 30 | event_data mediumtext not null, 31 | status smallint unsigned not null, 32 | published_time bigint unsigned default 0 not null, 33 | version smallint unsigned not null, 34 | create_time bigint unsigned not null 35 | ); 36 | 37 | create 38 | index idx_status 39 | on publish_event (status); 40 | 41 | create index idx_create_time 42 | on publish_event (create_time); 43 | 44 | create table publish_event_compensate 45 | ( 46 | id bigint unsigned auto_increment 47 | primary key, 48 | publish_event_id bigint unsigned not null, 49 | start_time bigint unsigned not null, 50 | taken bigint unsigned not null, 51 | failed_msg text null 52 | ); 53 | 54 | create table publish_event_failed 55 | ( 56 | id bigint unsigned auto_increment 57 | primary key, 58 | publish_event_id bigint unsigned not null, 59 | failed_msg text not null, 60 | create_time bigint unsigned not null 61 | ); 62 | 63 | create table subscribe_event 64 | ( 65 | id bigint unsigned auto_increment 66 | primary key, 67 | subscribe_name varchar(100) not null, 68 | status smallint unsigned not null, 69 | subscribe_time bigint unsigned not null, 70 | event_id bigint unsigned not null, 71 | event_name varchar(100) not null, 72 | event_data_id bigint unsigned default 0 not null, 73 | event_data mediumtext not null, 74 | event_create_time bigint unsigned not null, 75 | version smallint unsigned not null, 76 | create_time bigint unsigned not null, 77 | constraint uk_subscribe_name_even_id_event_name 78 | unique (subscribe_name, event_id, event_name) 79 | ); 80 | 81 | create 82 | index idx_status 83 | on subscribe_event (status); 84 | 85 | create 86 | index idx_event_create_time 87 | on subscribe_event (event_create_time); 88 | 89 | create table subscribe_event_compensate 90 | ( 91 | id bigint unsigned auto_increment 92 | primary key, 93 | subscribe_event_id bigint unsigned not null, 94 | start_time bigint unsigned not null, 95 | taken int unsigned not null, 96 | failed_msg text null 97 | ); 98 | 99 | create table subscribe_event_failed 100 | ( 101 | id bigint unsigned auto_increment 102 | primary key, 103 | subscribe_event_id bigint unsigned not null, 104 | failed_msg text not null, 105 | create_time bigint unsigned not null 106 | ); 107 | 108 | insert into simba_mutex 109 | (mutex, acquired_at, ttl_at, transition_at, owner_id, version) 110 | values ('eventbus_publish_leader', 0, 0, 0, '', 0); 111 | 112 | insert into simba_mutex 113 | (mutex, acquired_at, ttl_at, transition_at, owner_id, version) 114 | values ('eventbus_subscribe_leader', 0, 0, 0, '', 0); 115 | -------------------------------------------------------------------------------- /eventbus-bom/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | val libraryProjects = rootProject.ext.get("libraryProjects") as java.util.LinkedHashSet; 15 | 16 | dependencies { 17 | constraints { 18 | libraryProjects.forEach { 19 | api(it) 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /eventbus-core/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | dependencies { 15 | api("me.ahoo.simba:simba-core") 16 | api("me.ahoo.cosid:cosid-core") 17 | implementation("org.springframework:spring-context") 18 | implementation("org.springframework:spring-tx") 19 | implementation("com.fasterxml.jackson.core:jackson-databind") 20 | implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310") 21 | } 22 | 23 | description = "eventbus-core" 24 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/Consts.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core; 15 | 16 | /** 17 | * Govern EventBus Brand Consts. 18 | * 19 | * @author ahoo wang 20 | */ 21 | public interface Consts { 22 | String GOVERN_EVENTBUS = "govern-eventbus"; 23 | } 24 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/EventBusException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core; 15 | 16 | /** 17 | * EventBusException. 18 | * 19 | * @author ahoo wang 20 | */ 21 | public class EventBusException extends RuntimeException { 22 | private static final long serialVersionUID = 1; 23 | 24 | public EventBusException() { 25 | super(); 26 | } 27 | 28 | 29 | public EventBusException(String message) { 30 | super(message); 31 | } 32 | 33 | 34 | public EventBusException(String message, Throwable cause) { 35 | super(message, cause); 36 | } 37 | 38 | 39 | public EventBusException(Throwable cause) { 40 | super(cause); 41 | } 42 | 43 | 44 | protected EventBusException(String message, Throwable cause, 45 | boolean enableSuppression, 46 | boolean writableStackTrace) { 47 | super(message, cause, enableSuppression, writableStackTrace); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/annotation/Event.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.annotation; 15 | 16 | import me.ahoo.eventbus.core.publisher.EventDataIdGetter; 17 | 18 | import java.lang.annotation.Documented; 19 | import java.lang.annotation.ElementType; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.RetentionPolicy; 22 | import java.lang.annotation.Target; 23 | 24 | /** 25 | * Event. 26 | * 27 | * @author ahoo wang 28 | */ 29 | @Target({ElementType.TYPE, ElementType.FIELD}) 30 | @Documented 31 | @Retention(RetentionPolicy.RUNTIME) 32 | public @interface Event { 33 | /** 34 | * event name. 35 | *
36 |      * kafka:topic
37 |      * rabbit:routeKey
38 |      * 
39 | * 40 | * @return event name 41 | */ 42 | String value() default ""; 43 | 44 | /** 45 | * event data id's field name. 46 | * 47 | * @return event data id's field name 48 | */ 49 | String dataId() default EventDataIdGetter.DEFAULT_ID_FIELD_NAME; 50 | } 51 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/annotation/Publish.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.annotation; 15 | 16 | 17 | import java.lang.annotation.Documented; 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | /** 24 | * Publish. 25 | * 26 | * @author ahoo wang 27 | */ 28 | @Documented 29 | @Target(ElementType.METHOD) 30 | @Retention(RetentionPolicy.RUNTIME) 31 | public @interface Publish { 32 | 33 | } 34 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/annotation/Subscribe.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.annotation; 15 | 16 | import java.lang.annotation.Documented; 17 | import java.lang.annotation.ElementType; 18 | import java.lang.annotation.Retention; 19 | import java.lang.annotation.RetentionPolicy; 20 | import java.lang.annotation.Target; 21 | 22 | /** 23 | * Subscribe. 24 | * 25 | * @author : ahoo wang 26 | */ 27 | @Retention(RetentionPolicy.RUNTIME) 28 | @Documented 29 | @Target(ElementType.METHOD) 30 | public @interface Subscribe { 31 | /** 32 | * subscribe name. 33 | *
34 |      * kafka:consumerId
35 |      * rabbit:queueName
36 |      * 
37 | * 38 | * @return queue name 39 | */ 40 | String value() default ""; 41 | 42 | boolean rePublish() default false; 43 | } 44 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/codec/EventCodec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.codec; 15 | 16 | /** 17 | * EventCodec tag. 18 | * 19 | * @author ahoo wang 20 | */ 21 | public interface EventCodec { 22 | 23 | } 24 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/compensate/AbstractCompensateScheduler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.compensate; 15 | 16 | import me.ahoo.simba.core.MutexContendServiceFactory; 17 | import me.ahoo.simba.schedule.AbstractScheduler; 18 | import me.ahoo.simba.schedule.ScheduleConfig; 19 | 20 | import lombok.extern.slf4j.Slf4j; 21 | 22 | /** 23 | * AbstractCompensateScheduler. 24 | * 25 | * @author ahoo wang 26 | */ 27 | @Slf4j 28 | public abstract class AbstractCompensateScheduler extends AbstractScheduler implements EventCompensate { 29 | 30 | 31 | public AbstractCompensateScheduler(String mutex, 32 | ScheduleConfig scheduleConfig, 33 | MutexContendServiceFactory contendServiceFactory) { 34 | super(mutex, scheduleConfig, contendServiceFactory); 35 | } 36 | 37 | protected abstract String getWorker(); 38 | 39 | protected abstract void work(); 40 | 41 | } 42 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/compensate/CompensateConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.compensate; 15 | 16 | import java.time.Duration; 17 | 18 | /** 19 | * CompensateConfig. 20 | * 21 | * @author ahoo wang 22 | */ 23 | public class CompensateConfig { 24 | 25 | private Integer maxVersion = 10; 26 | private Integer batch = 10; 27 | private Duration before = Duration.ofMinutes(1); 28 | private Duration range = Duration.ofDays(7); 29 | 30 | public Integer getMaxVersion() { 31 | return maxVersion; 32 | } 33 | 34 | public void setMaxVersion(Integer maxVersion) { 35 | this.maxVersion = maxVersion; 36 | } 37 | 38 | public Integer getBatch() { 39 | return batch; 40 | } 41 | 42 | public void setBatch(Integer batch) { 43 | this.batch = batch; 44 | } 45 | 46 | public Duration getRange() { 47 | return range; 48 | } 49 | 50 | public void setRange(Duration range) { 51 | this.range = range; 52 | } 53 | 54 | public Duration getBefore() { 55 | return before; 56 | } 57 | 58 | public void setBefore(Duration before) { 59 | this.before = before; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/compensate/CompensatePublishEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.compensate; 15 | 16 | import me.ahoo.eventbus.core.publisher.PublishEvent; 17 | 18 | /** 19 | * CompensatePublishEvent. 20 | * 21 | * @author ahoo wang 22 | */ 23 | public class CompensatePublishEvent extends PublishEvent { 24 | } 25 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/compensate/EventCompensate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.compensate; 15 | 16 | import org.springframework.context.SmartLifecycle; 17 | 18 | /** 19 | * EventCompensate. 20 | * 21 | * @author ahoo wang 22 | */ 23 | public interface EventCompensate extends SmartLifecycle { 24 | 25 | } 26 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/consistency/ConsistencyPublisher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.consistency; 15 | 16 | import me.ahoo.eventbus.core.repository.PublishIdentity; 17 | 18 | import java.util.concurrent.Future; 19 | import java.util.function.Supplier; 20 | 21 | /** 22 | * execution flow. 23 | *
24 |  * 1. begin local transaction
25 |  * 2. invoke local biz code
26 |  * 3. insert publish event:INITIALIZED to local db
27 |  * 4. commit local transaction
28 |  * --- try
29 |  * 5. publish event to event-bus(MQ)
30 |  * 6. update publish event status to SUCCEEDED
31 |  * --- catch update publish event to FAILED
32 |  * 
33 | * 34 | * @author ahoo wang 35 | */ 36 | public interface ConsistencyPublisher { 37 | 38 | Object publish(Supplier publishDataSupplier); 39 | 40 | /** 41 | * publish event to bus. 42 | * 43 | * @param publishIdentity publish event id 44 | * @param publishEventData publish event data 45 | * @return the Future of publish 46 | */ 47 | Future publish(PublishIdentity publishIdentity, Object publishEventData); 48 | } 49 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/consistency/ConsistencySubscriber.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.consistency; 15 | 16 | import me.ahoo.eventbus.core.subscriber.Subscriber; 17 | 18 | /** 19 | * execution flow 20 | * getSubscribeEventBy event_id and event_name and subscribe_name 21 | * if exist 22 | * status is SUCCEEDED throw RepeatedSubscribeException 23 | * status is INITIALIZED 24 | * else insert subscribe event:INITIALIZED to local db 25 | * ---- try 26 | * 1. begin local transaction 27 | * 2. update subscribe event to SUCCEEDED (Optimistic lock update) 28 | * 3. invoke local biz code 29 | * 4. commit local transaction 30 | * ---- catch update subscribe event to FAILED 31 | * 32 | * @author ahoo wang 33 | */ 34 | public interface ConsistencySubscriber extends Subscriber { 35 | 36 | Subscriber getTargetSubscriber(); 37 | 38 | } 39 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/consistency/ConsistencySubscriberFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.consistency; 15 | 16 | import me.ahoo.eventbus.core.subscriber.Subscriber; 17 | 18 | /** 19 | * ConsistencySubscriberFactory. 20 | * 21 | * @author ahoo wang 22 | */ 23 | public interface ConsistencySubscriberFactory { 24 | ConsistencySubscriber create(Subscriber subscriber); 25 | } 26 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/consistency/impl/ConsistencySubscriberFactoryImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.consistency.impl; 15 | 16 | import me.ahoo.eventbus.core.consistency.ConsistencyPublisher; 17 | import me.ahoo.eventbus.core.consistency.ConsistencySubscriber; 18 | import me.ahoo.eventbus.core.consistency.ConsistencySubscriberFactory; 19 | import me.ahoo.eventbus.core.publisher.EventDescriptorParser; 20 | import me.ahoo.eventbus.core.repository.PublishEventRepository; 21 | import me.ahoo.eventbus.core.repository.SubscribeEventRepository; 22 | import me.ahoo.eventbus.core.subscriber.Subscriber; 23 | 24 | import org.springframework.transaction.PlatformTransactionManager; 25 | 26 | /** 27 | * ConsistencySubscriberFactoryImpl. 28 | * 29 | * @author ahoo wang 30 | */ 31 | public class ConsistencySubscriberFactoryImpl implements ConsistencySubscriberFactory { 32 | 33 | private final ConsistencyPublisher consistencyPublisher; 34 | 35 | private final PublishEventRepository publishEventRepository; 36 | private final SubscribeEventRepository subscribeEventRepository; 37 | private final PlatformTransactionManager transactionManager; 38 | private final EventDescriptorParser eventDescriptorParser; 39 | 40 | public ConsistencySubscriberFactoryImpl(ConsistencyPublisher consistencyPublisher, 41 | PublishEventRepository publishEventRepository, 42 | SubscribeEventRepository subscribeEventRepository, 43 | PlatformTransactionManager transactionManager, EventDescriptorParser eventDescriptorParser) { 44 | this.consistencyPublisher = consistencyPublisher; 45 | this.publishEventRepository = publishEventRepository; 46 | this.subscribeEventRepository = subscribeEventRepository; 47 | 48 | this.transactionManager = transactionManager; 49 | this.eventDescriptorParser = eventDescriptorParser; 50 | } 51 | 52 | @Override 53 | public ConsistencySubscriber create(Subscriber subscriber) { 54 | return new ConsistencySubscriberImpl(subscriber, consistencyPublisher, publishEventRepository, subscribeEventRepository, transactionManager, eventDescriptorParser); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/publisher/EventDataGetter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.publisher; 15 | 16 | /** 17 | * EventDataGetter. 18 | * 19 | * @author ahoo wang 20 | */ 21 | public interface EventDataGetter { 22 | default Object getEventData(Object targetObject) { 23 | return targetObject; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/publisher/EventDataIdGetter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.publisher; 15 | 16 | /** 17 | * EventDataIdGetter. 18 | * 19 | * @author ahoo wang 20 | */ 21 | public interface EventDataIdGetter { 22 | String DEFAULT_ID_FIELD_NAME = "id"; 23 | long DEFAULT_EVENT_DATA_ID = 0; 24 | 25 | default long getEventDataId(Object targetObject) { 26 | return DEFAULT_EVENT_DATA_ID; 27 | } 28 | 29 | static boolean availableType(Class idType) { 30 | return Long.class.equals(idType) || Long.TYPE.equals(idType); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/publisher/EventDescriptor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.publisher; 15 | 16 | /** 17 | * EventDescriptor. 18 | * 19 | * @author ahoo wang 20 | */ 21 | public interface EventDescriptor extends EventMetadata { 22 | 23 | default long getEventDataId(Object targetObject) { 24 | return 0; 25 | } 26 | 27 | default Object getEventData(Object targetObject) { 28 | return targetObject; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/publisher/EventMetadata.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.publisher; 15 | 16 | /** 17 | * EventMetadata. 18 | * 19 | * @author ahoo wang 20 | */ 21 | public interface EventMetadata { 22 | 23 | String getEventName(); 24 | 25 | Class getEventClass(); 26 | 27 | } 28 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/publisher/EventNameGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.publisher; 15 | 16 | import me.ahoo.eventbus.core.annotation.Event; 17 | 18 | import javax.annotation.Nullable; 19 | 20 | /** 21 | * EventNameGenerator. 22 | * 23 | * @author ahoo wang 24 | */ 25 | public interface EventNameGenerator { 26 | 27 | String generate(@Nullable Event eventAnnotation, Class eventClass); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/publisher/PublishEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.publisher; 15 | 16 | 17 | import me.ahoo.eventbus.core.repository.Identity; 18 | 19 | /** 20 | * PublishEvent. 21 | * 22 | * @author ahoo wang 23 | */ 24 | public class PublishEvent implements Identity { 25 | private Long id; 26 | private String eventName; 27 | private Long eventDataId = 0L; 28 | private TEventData eventData; 29 | private Long createTime; 30 | 31 | public Long getId() { 32 | return id; 33 | } 34 | 35 | public void setId(Long id) { 36 | this.id = id; 37 | } 38 | 39 | public String getEventName() { 40 | return eventName; 41 | } 42 | 43 | public void setEventName(String eventName) { 44 | this.eventName = eventName; 45 | } 46 | 47 | public Long getEventDataId() { 48 | return eventDataId; 49 | } 50 | 51 | public void setEventDataId(Long eventDataId) { 52 | this.eventDataId = eventDataId; 53 | } 54 | 55 | public TEventData getEventData() { 56 | return eventData; 57 | } 58 | 59 | public void setEventData(TEventData eventData) { 60 | this.eventData = eventData; 61 | } 62 | 63 | public Long getCreateTime() { 64 | return createTime; 65 | } 66 | 67 | public void setCreateTime(Long createTime) { 68 | this.createTime = createTime; 69 | } 70 | } 71 | 72 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/publisher/PublishException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.publisher; 15 | 16 | import me.ahoo.eventbus.core.EventBusException; 17 | 18 | /** 19 | * PublishException. 20 | * 21 | * @author ahoo wang 22 | */ 23 | public class PublishException extends EventBusException { 24 | 25 | public PublishException() { 26 | } 27 | 28 | public PublishException(String message) { 29 | super(message); 30 | } 31 | 32 | public PublishException(String message, Throwable cause) { 33 | super(message, cause); 34 | } 35 | 36 | public PublishException(Throwable cause) { 37 | super(cause); 38 | } 39 | 40 | public PublishException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { 41 | super(message, cause, enableSuppression, writableStackTrace); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/publisher/Publisher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.publisher; 15 | 16 | /** 17 | * Publisher. 18 | * 19 | * @author ahoo wang 20 | */ 21 | public interface Publisher { 22 | void publish(PublishEvent event) throws PublishException; 23 | 24 | } 25 | 26 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/publisher/impl/FieldEventDataGetter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.publisher.impl; 15 | 16 | import me.ahoo.eventbus.core.EventBusException; 17 | import me.ahoo.eventbus.core.publisher.EventDataGetter; 18 | 19 | import java.lang.reflect.Field; 20 | 21 | /** 22 | * FieldEventDataGetter. 23 | * 24 | * @author ahoo wang 25 | */ 26 | public class FieldEventDataGetter implements EventDataGetter { 27 | private final Field eventDataField; 28 | 29 | public FieldEventDataGetter(Field eventDataField) { 30 | this.eventDataField = eventDataField; 31 | eventDataField.setAccessible(true); 32 | } 33 | 34 | @Override 35 | public Object getEventData(Object targetObject) { 36 | try { 37 | return eventDataField.get(targetObject); 38 | } catch (IllegalAccessException e) { 39 | throw new EventBusException(e.getMessage(), e); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/publisher/impl/FieldEventDataIdGetter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.publisher.impl; 15 | 16 | import me.ahoo.eventbus.core.publisher.EventDataIdGetter; 17 | 18 | import java.lang.reflect.Field; 19 | import java.util.Objects; 20 | 21 | /** 22 | * FieldEventDataIdGetter. 23 | * 24 | * @author ahoo wang 25 | */ 26 | public class FieldEventDataIdGetter implements EventDataIdGetter { 27 | 28 | private final Field eventDataIdField; 29 | 30 | public FieldEventDataIdGetter(Field eventDataIdField) { 31 | this.eventDataIdField = eventDataIdField; 32 | eventDataIdField.setAccessible(true); 33 | } 34 | 35 | @Override 36 | public long getEventDataId(Object targetObject) { 37 | try { 38 | Object eventDataId = eventDataIdField.get(targetObject); 39 | if (Objects.isNull(eventDataId)) { 40 | return DEFAULT_EVENT_DATA_ID; 41 | } 42 | return (long) eventDataId; 43 | } catch (IllegalAccessException e) { 44 | return DEFAULT_EVENT_DATA_ID; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/publisher/impl/NoneEventDataIdGetter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.publisher.impl; 15 | 16 | import me.ahoo.eventbus.core.publisher.EventDataIdGetter; 17 | 18 | /** 19 | * NoneEventDataIdGetter. 20 | * 21 | * @author ahoo wang 22 | */ 23 | public class NoneEventDataIdGetter implements EventDataIdGetter { 24 | public static final EventDataIdGetter INSTANCE = new NoneEventDataIdGetter(); 25 | 26 | } 27 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/publisher/impl/SimpleEventDataGetter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.publisher.impl; 15 | 16 | import me.ahoo.eventbus.core.publisher.EventDataGetter; 17 | 18 | /** 19 | * SimpleEventDataGetter. 20 | * 21 | * @author ahoo wang 22 | */ 23 | public class SimpleEventDataGetter implements EventDataGetter { 24 | @Override 25 | public Object getEventData(Object targetObject) { 26 | return targetObject; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/publisher/impl/SimpleEventDescriptor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.publisher.impl; 15 | 16 | import me.ahoo.eventbus.core.publisher.EventDataGetter; 17 | import me.ahoo.eventbus.core.publisher.EventDataIdGetter; 18 | import me.ahoo.eventbus.core.publisher.EventDescriptor; 19 | 20 | /** 21 | * SimpleEventDescriptor. 22 | * 23 | * @author ahoo wang 24 | */ 25 | public class SimpleEventDescriptor implements EventDescriptor { 26 | 27 | private final String eventName; 28 | private final Class eventClass; 29 | private final EventDataGetter eventDataGetter; 30 | private final EventDataIdGetter eventDataIdGetter; 31 | 32 | public SimpleEventDescriptor(String eventName, 33 | Class eventClass, 34 | EventDataGetter eventDataGetter, 35 | EventDataIdGetter eventDataIdGetter) { 36 | this.eventName = eventName; 37 | this.eventClass = eventClass; 38 | this.eventDataGetter = eventDataGetter; 39 | this.eventDataIdGetter = eventDataIdGetter; 40 | } 41 | 42 | @Override 43 | public String getEventName() { 44 | return eventName; 45 | } 46 | 47 | @Override 48 | public Class getEventClass() { 49 | return eventClass; 50 | } 51 | 52 | @Override 53 | public Object getEventData(Object targetObject) { 54 | return eventDataGetter.getEventData(targetObject); 55 | } 56 | 57 | @Override 58 | public long getEventDataId(Object targetObject) { 59 | return eventDataIdGetter.getEventDataId(targetObject); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/publisher/impl/SimpleEventNameGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.publisher.impl; 15 | 16 | import me.ahoo.eventbus.core.annotation.Event; 17 | import me.ahoo.eventbus.core.publisher.EventNameGenerator; 18 | 19 | import com.google.common.base.Strings; 20 | 21 | import java.util.Objects; 22 | 23 | /** 24 | * SimpleEventNameGenerator. 25 | * 26 | * @author ahoo wang 27 | */ 28 | 29 | public class SimpleEventNameGenerator implements EventNameGenerator { 30 | 31 | @Override 32 | public String generate(Event eventAnnotation, Class eventClass) { 33 | if (Objects.nonNull(eventAnnotation) && !Strings.isNullOrEmpty(eventAnnotation.value())) { 34 | return eventAnnotation.value(); 35 | } 36 | return eventClass.getSimpleName(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/repository/ConcurrentVersionConflictException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.repository; 15 | 16 | import me.ahoo.eventbus.core.EventBusException; 17 | 18 | /** 19 | * 并发版本冲突异常. 20 | * 21 | * @author ahoo wang 22 | */ 23 | public class ConcurrentVersionConflictException extends EventBusException { 24 | 25 | private final Version version; 26 | 27 | public ConcurrentVersionConflictException(String message, Version version) { 28 | super(message); 29 | this.version = version; 30 | } 31 | 32 | public Version getVersion() { 33 | return version; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/repository/EventRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.repository; 15 | 16 | 17 | /** 18 | * 事件仓储标记. 19 | * 20 | * @author ahoo wang 21 | */ 22 | public interface EventRepository { 23 | 24 | 25 | } 26 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/repository/Identity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.repository; 15 | 16 | /** 17 | * Identity. 18 | * 19 | * @author ahoo wang 20 | */ 21 | public interface Identity { 22 | Long getId(); 23 | 24 | void setId(Long id); 25 | } 26 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/repository/PublishEventRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.repository; 15 | 16 | import me.ahoo.eventbus.core.repository.entity.PublishEventCompensateEntity; 17 | import me.ahoo.eventbus.core.repository.entity.PublishEventEntity; 18 | 19 | import java.time.Duration; 20 | import java.util.List; 21 | 22 | /** 23 | * 发布事件仓储. 24 | * 25 | * @author ahoo wang 26 | */ 27 | public interface PublishEventRepository extends EventRepository { 28 | 29 | default PublishIdentity initialize(String eventName, Object eventData) { 30 | return initialize(eventName, 0L, eventData); 31 | } 32 | 33 | PublishIdentity initialize(String eventName, long eventDataId, Object eventData); 34 | 35 | int markSucceeded(PublishIdentity publishIdentity) throws ConcurrentVersionConflictException; 36 | 37 | int markFailed(PublishIdentity publishIdentity, Throwable throwable) throws ConcurrentVersionConflictException; 38 | 39 | List queryFailed(int limit, int maxVersion, Duration before, Duration range); 40 | 41 | int compensate(PublishEventCompensateEntity publishEventCompensationEntity); 42 | 43 | } 44 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/repository/PublishIdentity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.repository; 15 | 16 | /** 17 | * PublishIdentity. 18 | * 19 | * @author ahoo wang 20 | */ 21 | public class PublishIdentity implements Version, Identity { 22 | private Long id; 23 | private String eventName; 24 | private Long eventDataId = 0L; 25 | private PublishStatus status; 26 | private Integer version; 27 | private Long createTime; 28 | 29 | public Long getId() { 30 | return id; 31 | } 32 | 33 | public void setId(Long id) { 34 | this.id = id; 35 | } 36 | 37 | public String getEventName() { 38 | return eventName; 39 | } 40 | 41 | public void setEventName(String eventName) { 42 | this.eventName = eventName; 43 | } 44 | 45 | public Long getEventDataId() { 46 | return eventDataId; 47 | } 48 | 49 | public void setEventDataId(Long eventDataId) { 50 | this.eventDataId = eventDataId; 51 | } 52 | 53 | public PublishStatus getStatus() { 54 | return status; 55 | } 56 | 57 | public void setStatus(PublishStatus status) { 58 | this.status = status; 59 | } 60 | 61 | public Integer getVersion() { 62 | return version; 63 | } 64 | 65 | public void setVersion(Integer version) { 66 | this.version = version; 67 | } 68 | 69 | public Long getCreateTime() { 70 | return createTime; 71 | } 72 | 73 | public void setCreateTime(Long createTime) { 74 | this.createTime = createTime; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/repository/PublishStatus.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.repository; 15 | 16 | /** 17 | * 发布事件状态. 18 | * 19 | * @author ahoo wang 20 | */ 21 | public enum PublishStatus { 22 | 23 | INITIALIZED(0), 24 | SUCCEEDED(1), 25 | FAILED(2); 26 | private int value; 27 | 28 | PublishStatus(int value) { 29 | this.value = value; 30 | } 31 | 32 | public int getValue() { 33 | return value; 34 | } 35 | 36 | public static PublishStatus valueOf(int value) { 37 | switch (value) { 38 | case 0: 39 | return INITIALIZED; 40 | case 1: 41 | return SUCCEEDED; 42 | case 2: 43 | return FAILED; 44 | default: 45 | throw new IllegalStateException("Unexpected value: " + value); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/repository/RepeatedSubscribeException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.repository; 15 | 16 | import me.ahoo.eventbus.core.EventBusException; 17 | import me.ahoo.eventbus.core.publisher.PublishEvent; 18 | import me.ahoo.eventbus.core.subscriber.Subscriber; 19 | 20 | /** 21 | * 重复订阅事件异常. 22 | * 23 | * @author ahoo wang 24 | */ 25 | public class RepeatedSubscribeException extends EventBusException { 26 | private final Subscriber subscriber; 27 | private final PublishEvent publishEvent; 28 | private final String errorMsg; 29 | 30 | public RepeatedSubscribeException(Subscriber subscriber, PublishEvent publishEvent) { 31 | this.subscriber = subscriber; 32 | this.publishEvent = publishEvent; 33 | errorMsg = String.format("Subscriber.name:[%s] -> id:[%d]", subscriber.getName(), publishEvent.getId()); 34 | } 35 | 36 | @Override 37 | public String getMessage() { 38 | return errorMsg; 39 | } 40 | 41 | public Subscriber getSubscriber() { 42 | return subscriber; 43 | } 44 | 45 | public PublishEvent getPublishEvent() { 46 | return publishEvent; 47 | } 48 | 49 | public String getErrorMsg() { 50 | return errorMsg; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/repository/SubscribeEventRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.repository; 15 | 16 | import me.ahoo.eventbus.core.publisher.PublishEvent; 17 | import me.ahoo.eventbus.core.repository.entity.SubscribeEventCompensateEntity; 18 | import me.ahoo.eventbus.core.repository.entity.SubscribeEventEntity; 19 | import me.ahoo.eventbus.core.subscriber.Subscriber; 20 | 21 | import java.time.Duration; 22 | import java.util.List; 23 | 24 | /** 25 | * 订阅事件仓储. 26 | * 27 | * @author ahoo wang 28 | */ 29 | public interface SubscribeEventRepository extends EventRepository { 30 | 31 | /** 32 | * initialize. 33 | * * getSubscribeEventBy event_id and event_name and subscribe_name 34 | * * if exist 35 | * * status is SUCCEEDED throw error 36 | * * status is INITIALIZED return 37 | * * else insert subscribe event:INITIALIZED to local db 38 | * 39 | * @param subscriber Subscriber 40 | * @param subscribePublishEvent PublishEventWrapper 41 | * @return SubscribeIdentity 42 | */ 43 | SubscribeIdentity initialize(Subscriber subscriber, PublishEvent subscribePublishEvent) throws RepeatedSubscribeException; 44 | 45 | 46 | /** 47 | * subscribeEventId and version. 48 | * 49 | * @param subscribeIdentity SubscribeIdentity 50 | * @return Returns the number of affected rows 51 | */ 52 | int markSucceeded(SubscribeIdentity subscribeIdentity) throws ConcurrentVersionConflictException; 53 | 54 | /** 55 | * subscribeEventId and version. 56 | * 57 | * @param subscribeIdentity SubscribeIdentity 58 | * @param throwable error 59 | * @return Returns the number of affected rows 60 | */ 61 | int markFailed(SubscribeIdentity subscribeIdentity, Throwable throwable) throws ConcurrentVersionConflictException; 62 | 63 | List queryFailed(int limit, int maxVersion, Duration before, Duration range); 64 | 65 | int compensate(SubscribeEventCompensateEntity subscribeEventCompensationEntity); 66 | 67 | 68 | } 69 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/repository/SubscribeIdentity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.repository; 15 | 16 | /** 17 | * SubscribeIdentity. 18 | * 19 | * @author ahoo wang 20 | */ 21 | public class SubscribeIdentity implements Version, Identity { 22 | private Long id; 23 | private String subscriberName; 24 | private SubscribeStatus status; 25 | private Integer taken; 26 | private Integer version; 27 | private Long eventCreateTime; 28 | 29 | public Long getId() { 30 | return id; 31 | } 32 | 33 | public void setId(Long id) { 34 | this.id = id; 35 | } 36 | 37 | public String getSubscriberName() { 38 | return subscriberName; 39 | } 40 | 41 | public void setSubscriberName(String subscriberName) { 42 | this.subscriberName = subscriberName; 43 | } 44 | 45 | public SubscribeStatus getStatus() { 46 | return status; 47 | } 48 | 49 | public void setStatus(SubscribeStatus status) { 50 | this.status = status; 51 | } 52 | 53 | public Integer getTaken() { 54 | return taken; 55 | } 56 | 57 | public void setTaken(Integer taken) { 58 | this.taken = taken; 59 | } 60 | 61 | public Integer getVersion() { 62 | return version; 63 | } 64 | 65 | public void setVersion(Integer version) { 66 | this.version = version; 67 | } 68 | 69 | public Long getEventCreateTime() { 70 | return eventCreateTime; 71 | } 72 | 73 | public void setEventCreateTime(Long eventCreateTime) { 74 | this.eventCreateTime = eventCreateTime; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/repository/SubscribeStatus.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.repository; 15 | 16 | /** 17 | * 订阅状态. 18 | * 19 | * @author ahoo wang 20 | */ 21 | public enum SubscribeStatus { 22 | 23 | INITIALIZED(0), 24 | SUCCEEDED(1), 25 | FAILED(2); 26 | private final int value; 27 | 28 | SubscribeStatus(int value) { 29 | this.value = value; 30 | } 31 | 32 | public int getValue() { 33 | return value; 34 | } 35 | 36 | public static SubscribeStatus valueOf(int value) { 37 | 38 | switch (value) { 39 | case 0: 40 | return INITIALIZED; 41 | case 1: 42 | return SUCCEEDED; 43 | case 2: 44 | return FAILED; 45 | default: 46 | throw new IllegalStateException("Unexpected value: " + value); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/repository/TimeTaken.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.repository; 15 | 16 | /** 17 | * 执行耗时,单位 ms. 18 | * 19 | * @author ahoo wang 20 | */ 21 | public interface TimeTaken { 22 | 23 | Integer getTaken(); 24 | 25 | void setTaken(Integer taken); 26 | } 27 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/repository/Version.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.repository; 15 | 16 | /** 17 | * 版本号. 18 | * 19 | * @author ahoo wang 20 | */ 21 | public interface Version { 22 | Integer INITIAL_VALUE = 1; 23 | 24 | Integer getVersion(); 25 | 26 | void setVersion(Integer version); 27 | } 28 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/repository/entity/PublishEventCompensateEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.repository.entity; 15 | 16 | import me.ahoo.eventbus.core.repository.Identity; 17 | 18 | import lombok.Builder; 19 | 20 | /** 21 | * PublishEventCompensateEntity. 22 | * 23 | * @author ahoo wang 24 | */ 25 | @Builder 26 | public class PublishEventCompensateEntity implements Identity { 27 | private Long id; 28 | private Long publishEventId; 29 | private Long startTime; 30 | private Long taken; 31 | private String failedMsg; 32 | 33 | public Long getId() { 34 | return id; 35 | } 36 | 37 | public void setId(Long id) { 38 | this.id = id; 39 | } 40 | 41 | public Long getPublishEventId() { 42 | return publishEventId; 43 | } 44 | 45 | public void setPublishEventId(Long publishEventId) { 46 | this.publishEventId = publishEventId; 47 | } 48 | 49 | public Long getStartTime() { 50 | return startTime; 51 | } 52 | 53 | public void setStartTime(Long startTime) { 54 | this.startTime = startTime; 55 | } 56 | 57 | public Long getTaken() { 58 | return taken; 59 | } 60 | 61 | public void setTaken(Long taken) { 62 | this.taken = taken; 63 | } 64 | 65 | public String getFailedMsg() { 66 | return failedMsg; 67 | } 68 | 69 | public void setFailedMsg(String failedMsg) { 70 | this.failedMsg = failedMsg; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/repository/entity/PublishEventEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.repository.entity; 15 | 16 | import me.ahoo.eventbus.core.publisher.PublishEvent; 17 | import me.ahoo.eventbus.core.repository.PublishStatus; 18 | 19 | /** 20 | * PublishEventEntity. 21 | * 22 | * @author ahoo wang 23 | */ 24 | public class PublishEventEntity extends PublishEvent { 25 | private PublishStatus status; 26 | private Integer version; 27 | private Long publishedTime; 28 | 29 | public PublishStatus getStatus() { 30 | return status; 31 | } 32 | 33 | public void setStatus(PublishStatus status) { 34 | this.status = status; 35 | } 36 | 37 | public Integer getVersion() { 38 | return version; 39 | } 40 | 41 | public void setVersion(Integer version) { 42 | this.version = version; 43 | } 44 | 45 | public Long getPublishedTime() { 46 | return publishedTime; 47 | } 48 | 49 | public void setPublishedTime(Long publishedTime) { 50 | this.publishedTime = publishedTime; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/repository/entity/SubscribeEventCompensateEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.repository.entity; 15 | 16 | import me.ahoo.eventbus.core.repository.Identity; 17 | 18 | import lombok.Builder; 19 | 20 | /** 21 | * SubscribeEventCompensateEntity. 22 | * 23 | * @author ahoo wang 24 | */ 25 | @Builder 26 | public class SubscribeEventCompensateEntity implements Identity { 27 | private Long id; 28 | private Long subscribeEventId; 29 | private Long startTime; 30 | private Long taken; 31 | private String failedMsg; 32 | 33 | public Long getId() { 34 | return id; 35 | } 36 | 37 | public void setId(Long id) { 38 | this.id = id; 39 | } 40 | 41 | public Long getSubscribeEventId() { 42 | return subscribeEventId; 43 | } 44 | 45 | public void setSubscribeEventId(Long subscribeEventId) { 46 | this.subscribeEventId = subscribeEventId; 47 | } 48 | 49 | public Long getStartTime() { 50 | return startTime; 51 | } 52 | 53 | public void setStartTime(Long startTime) { 54 | this.startTime = startTime; 55 | } 56 | 57 | public Long getTaken() { 58 | return taken; 59 | } 60 | 61 | public void setTaken(Long taken) { 62 | this.taken = taken; 63 | } 64 | 65 | public String getFailedMsg() { 66 | return failedMsg; 67 | } 68 | 69 | public void setFailedMsg(String failedMsg) { 70 | this.failedMsg = failedMsg; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/repository/entity/SubscribeEventEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.repository.entity; 15 | 16 | import me.ahoo.eventbus.core.repository.SubscribeIdentity; 17 | 18 | /** 19 | * SubscribeEventEntity. 20 | * 21 | * @author ahoo wang 22 | */ 23 | public class SubscribeEventEntity extends SubscribeIdentity { 24 | private Long subscribeTime; 25 | private Long eventId; 26 | private String eventName; 27 | private Long eventDataId = 0L; 28 | private String eventData; 29 | private Long eventCreateTime; 30 | private Long createTime; 31 | 32 | public Long getSubscribeTime() { 33 | return subscribeTime; 34 | } 35 | 36 | public void setSubscribeTime(Long subscribeTime) { 37 | this.subscribeTime = subscribeTime; 38 | } 39 | 40 | public Long getEventId() { 41 | return eventId; 42 | } 43 | 44 | public void setEventId(Long eventId) { 45 | this.eventId = eventId; 46 | } 47 | 48 | public String getEventName() { 49 | return eventName; 50 | } 51 | 52 | public void setEventName(String eventName) { 53 | this.eventName = eventName; 54 | } 55 | 56 | public Long getEventDataId() { 57 | return eventDataId; 58 | } 59 | 60 | public void setEventDataId(Long eventDataId) { 61 | this.eventDataId = eventDataId; 62 | } 63 | 64 | public String getEventData() { 65 | return eventData; 66 | } 67 | 68 | public void setEventData(String eventData) { 69 | this.eventData = eventData; 70 | } 71 | 72 | public Long getEventCreateTime() { 73 | return eventCreateTime; 74 | } 75 | 76 | public void setEventCreateTime(Long eventCreateTime) { 77 | this.eventCreateTime = eventCreateTime; 78 | } 79 | 80 | public Long getCreateTime() { 81 | return createTime; 82 | } 83 | 84 | public void setCreateTime(Long createTime) { 85 | this.createTime = createTime; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/serialize/Deserializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.serialize; 15 | 16 | /** 17 | * Deserializer. 18 | * 19 | * @author ahoo wang 20 | **/ 21 | public interface Deserializer { 22 | 23 | T deserialize(String payload, Class deserializeType); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/serialize/Serializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.serialize; 15 | 16 | /** 17 | * Serializer. 18 | * 19 | * @author ahoo wang 20 | **/ 21 | public interface Serializer { 22 | 23 | String serialize(T payloadObj); 24 | } 25 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/serialize/json/JsonDeserializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.serialize.json; 15 | 16 | import me.ahoo.eventbus.core.serialize.Deserializer; 17 | 18 | import com.fasterxml.jackson.databind.ObjectMapper; 19 | import lombok.SneakyThrows; 20 | 21 | 22 | /** 23 | * JsonDeserializer. 24 | * 25 | * @author ahoo wang 26 | **/ 27 | public class JsonDeserializer implements Deserializer { 28 | private final ObjectMapper objectMapper; 29 | 30 | public JsonDeserializer(ObjectMapper objectMapper) { 31 | this.objectMapper = objectMapper; 32 | } 33 | 34 | @SneakyThrows 35 | @Override 36 | public T deserialize(String payload, Class deserializeType) { 37 | return objectMapper.readValue(payload, deserializeType); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/serialize/json/JsonSerializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.serialize.json; 15 | 16 | import me.ahoo.eventbus.core.serialize.Serializer; 17 | 18 | import com.fasterxml.jackson.databind.ObjectMapper; 19 | import lombok.SneakyThrows; 20 | 21 | /** 22 | * JsonSerializer. 23 | * 24 | * @author ahoo wang 25 | **/ 26 | public class JsonSerializer implements Serializer { 27 | private final ObjectMapper objectMapper; 28 | 29 | public JsonSerializer(ObjectMapper objectMapper) { 30 | this.objectMapper = objectMapper; 31 | } 32 | 33 | @SneakyThrows 34 | @Override 35 | public String serialize(Object payloadObj) { 36 | return objectMapper.writeValueAsString(payloadObj); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/subscriber/Subscriber.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.subscriber; 15 | 16 | import me.ahoo.eventbus.core.publisher.PublishEvent; 17 | 18 | import java.lang.reflect.Method; 19 | 20 | /** 21 | * Subscriber. 22 | * 23 | * @author ahoo wang 24 | */ 25 | public interface Subscriber { 26 | 27 | String getName(); 28 | 29 | Object invoke(PublishEvent publishEvent); 30 | 31 | Object getTarget(); 32 | 33 | Method getMethod(); 34 | 35 | String getSubscribeEventName(); 36 | 37 | Class getSubscribeEventClass(); 38 | 39 | boolean rePublish(); 40 | } 41 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/subscriber/SubscriberNameGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.subscriber; 15 | 16 | import java.lang.reflect.Method; 17 | 18 | /** 19 | * SubscriberNameGenerator. 20 | * 21 | * @author ahoo wang 22 | */ 23 | public interface SubscriberNameGenerator { 24 | 25 | String generate(Method subscriberMethod); 26 | 27 | } 28 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/subscriber/SubscriberRegistry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.subscriber; 15 | 16 | /** 17 | * SubscriberRegistry. 18 | * 19 | * @author ahoo wang 20 | */ 21 | public interface SubscriberRegistry { 22 | void subscribe(Subscriber subscriber); 23 | 24 | Subscriber getSubscriber(String subscriberName); 25 | 26 | } 27 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/subscriber/SubscriberScanner.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.subscriber; 15 | 16 | import me.ahoo.eventbus.core.annotation.Event; 17 | import me.ahoo.eventbus.core.annotation.Subscribe; 18 | import me.ahoo.eventbus.core.publisher.EventDescriptor; 19 | import me.ahoo.eventbus.core.publisher.EventDescriptorParser; 20 | import me.ahoo.eventbus.core.subscriber.impl.SimpleSubscriber; 21 | 22 | import com.google.common.base.Preconditions; 23 | import lombok.extern.slf4j.Slf4j; 24 | import org.springframework.aop.support.AopUtils; 25 | import org.springframework.core.annotation.AnnotationUtils; 26 | import org.springframework.util.ReflectionUtils; 27 | 28 | import java.lang.reflect.Method; 29 | import java.util.ArrayList; 30 | import java.util.List; 31 | import java.util.Objects; 32 | 33 | /** 34 | * SubscriberScanner. 35 | * 36 | * @author ahoo wang 37 | */ 38 | @Slf4j 39 | public class SubscriberScanner { 40 | 41 | private final SubscriberNameGenerator subscriberNameGenerator; 42 | private final EventDescriptorParser eventDescriptorParser; 43 | 44 | public SubscriberScanner(SubscriberNameGenerator subscriberNameGenerator, EventDescriptorParser eventDescriptorParser) { 45 | this.subscriberNameGenerator = subscriberNameGenerator; 46 | this.eventDescriptorParser = eventDescriptorParser; 47 | } 48 | 49 | private SimpleSubscriber parseSubscriber(Object subscribeTarget, Method method, Subscribe subscribeAnnotation) { 50 | Preconditions.checkState(method.getParameterCount() == 1, "method:[%s] ParameterCount must be 1.", method); 51 | String subscribeName = subscriberNameGenerator.generate(method); 52 | Class subscribeParameterType = method.getParameterTypes()[0]; 53 | EventDescriptor subscribeEventDescriptor = eventDescriptorParser.get(subscribeParameterType); 54 | boolean rePublish = subscribeAnnotation.rePublish(); 55 | Class returnType = method.getReturnType(); 56 | Event returnTypeAnnotation = returnType.getAnnotation(Event.class); 57 | if (Objects.nonNull(returnTypeAnnotation)) { 58 | rePublish = true; 59 | } 60 | return new SimpleSubscriber(subscribeName, subscribeTarget, method, subscribeEventDescriptor.getEventName(), subscribeEventDescriptor.getEventClass(), rePublish); 61 | } 62 | 63 | public List scan(Object subscribeTarget) { 64 | 65 | Class subscribeClass = AopUtils.getTargetClass(subscribeTarget); 66 | List subscribers = new ArrayList(); 67 | 68 | ReflectionUtils.doWithMethods(subscribeClass, method -> { 69 | Subscribe subscribeAnnotation = AnnotationUtils.findAnnotation(method, Subscribe.class); 70 | if (Objects.isNull(subscribeAnnotation)) { 71 | return; 72 | } 73 | SimpleSubscriber subscriber = parseSubscriber(subscribeTarget, method, subscribeAnnotation); 74 | subscribers.add(subscriber); 75 | }, ReflectionUtils.USER_DECLARED_METHODS); 76 | 77 | return subscribers; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/subscriber/impl/SimpleSubscriber.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.subscriber.impl; 15 | 16 | import me.ahoo.eventbus.core.EventBusException; 17 | import me.ahoo.eventbus.core.publisher.PublishEvent; 18 | import me.ahoo.eventbus.core.subscriber.Subscriber; 19 | 20 | import java.lang.reflect.InvocationTargetException; 21 | import java.lang.reflect.Method; 22 | 23 | /** 24 | * SimpleSubscriber. 25 | * 26 | * @author ahoo wang 27 | */ 28 | public class SimpleSubscriber implements Subscriber { 29 | private final String name; 30 | private final Object target; 31 | private final Method method; 32 | private final String subscribeEventName; 33 | private final Class subscribeEventClass; 34 | private final boolean rePublish; 35 | 36 | public SimpleSubscriber(String name, 37 | Object target, 38 | Method method, 39 | String subscribeEventName, 40 | Class subscribeEventClass, 41 | boolean rePublish) { 42 | this.name = name; 43 | this.target = target; 44 | this.method = method; 45 | this.subscribeEventName = subscribeEventName; 46 | this.subscribeEventClass = subscribeEventClass; 47 | this.rePublish = rePublish; 48 | } 49 | 50 | @Override 51 | public Object invoke(PublishEvent publishEvent) { 52 | try { 53 | return this.method.invoke(this.target, publishEvent.getEventData()); 54 | } catch (IllegalAccessException illegalAccessException) { 55 | throw new EventBusException(illegalAccessException.getMessage(), illegalAccessException); 56 | } catch (InvocationTargetException invocationTargetException) { 57 | throw new EventBusException(invocationTargetException.getTargetException()); 58 | } 59 | } 60 | 61 | public String getName() { 62 | return name; 63 | } 64 | 65 | public Object getTarget() { 66 | return target; 67 | } 68 | 69 | public Method getMethod() { 70 | return method; 71 | } 72 | 73 | public String getSubscribeEventName() { 74 | return subscribeEventName; 75 | } 76 | 77 | public Class getSubscribeEventClass() { 78 | return subscribeEventClass; 79 | } 80 | 81 | public boolean rePublish() { 82 | return rePublish; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/subscriber/impl/SimpleSubscriberNameGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.subscriber.impl; 15 | 16 | import me.ahoo.eventbus.core.annotation.Subscribe; 17 | import me.ahoo.eventbus.core.subscriber.SubscriberNameGenerator; 18 | 19 | import com.google.common.base.Strings; 20 | import lombok.extern.slf4j.Slf4j; 21 | 22 | import java.lang.reflect.Method; 23 | 24 | /** 25 | * SimpleSubscriberNameGenerator. 26 | * 27 | * @author ahoo wang 28 | */ 29 | @Slf4j 30 | public class SimpleSubscriberNameGenerator implements SubscriberNameGenerator { 31 | 32 | private final String prefix; 33 | 34 | public SimpleSubscriberNameGenerator(String prefix) { 35 | this.prefix = prefix; 36 | } 37 | 38 | @Override 39 | public String generate(Method subscriberMethod) { 40 | Subscribe subscribeAnnotation = subscriberMethod.getAnnotation(Subscribe.class); 41 | String subscribeName = subscribeAnnotation.value(); 42 | if (Strings.isNullOrEmpty(subscribeName)) { 43 | subscribeName = subscriberMethod.getName(); 44 | if (log.isWarnEnabled()) { 45 | log.warn("generate - method:[{}] -> subscribeName is empty,will use method name:[{}] as subscribeName!", subscriberMethod, subscribeName); 46 | } 47 | } 48 | return prefix + subscribeName; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/utils/Dates.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.utils; 15 | 16 | import java.time.LocalDateTime; 17 | import java.time.ZoneId; 18 | import java.util.Date; 19 | 20 | /** 21 | * Data tool class. 22 | * 23 | * @author ahoo wang 24 | */ 25 | @Deprecated 26 | public abstract class Dates { 27 | 28 | public static LocalDateTime of(Date date) { 29 | return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); 30 | } 31 | 32 | public static Date of(LocalDateTime localDateTime) { 33 | return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /eventbus-core/src/main/java/me/ahoo/eventbus/core/utils/Threads.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core.utils; 15 | 16 | import com.google.common.util.concurrent.ThreadFactoryBuilder; 17 | import lombok.var; 18 | 19 | import java.util.concurrent.ThreadFactory; 20 | 21 | public class Threads { 22 | public static ThreadFactory defaultFactory(String domain) { 23 | var nameFormat = domain + "-%d"; 24 | return new ThreadFactoryBuilder() 25 | .setDaemon(false) 26 | .setNameFormat(nameFormat) 27 | .build(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /eventbus-core/src/test/java/me/ahoo/eventbus/core/AnnotationDemoEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core; 15 | 16 | import me.ahoo.eventbus.core.annotation.Event; 17 | 18 | /** 19 | * @author ahoo wang 20 | */ 21 | @Event 22 | public class AnnotationDemoEvent { 23 | } 24 | -------------------------------------------------------------------------------- /eventbus-core/src/test/java/me/ahoo/eventbus/core/DemoEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core; 15 | 16 | /** 17 | * @author ahoo wang 18 | */ 19 | public class DemoEvent { 20 | } 21 | -------------------------------------------------------------------------------- /eventbus-core/src/test/java/me/ahoo/eventbus/core/DemoSubscriber.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core; 15 | 16 | import lombok.extern.slf4j.Slf4j; 17 | import me.ahoo.eventbus.core.annotation.Subscribe; 18 | 19 | /** 20 | * @author ahoo wang 21 | */ 22 | @Slf4j 23 | public class DemoSubscriber { 24 | 25 | @Subscribe 26 | public void subscriber(AnnotationDemoEvent annotationDemoEvent) { 27 | log.info("subscriber"); 28 | } 29 | 30 | @Subscribe 31 | public AnnotationDemoEvent subscriberPublish(AnnotationDemoEvent annotationDemoEvent) { 32 | log.info("subscriberThenPublish"); 33 | return annotationDemoEvent; 34 | } 35 | 36 | 37 | @Subscribe(rePublish = true) 38 | public DemoEvent subscriberThenPublishUseIsPublish(DemoEvent demoEvent) { 39 | log.info("subscriberThenPublish"); 40 | return demoEvent; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /eventbus-core/src/test/java/me/ahoo/eventbus/core/ScheduledThreadPoolExecutorTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core; 15 | 16 | import lombok.SneakyThrows; 17 | import me.ahoo.eventbus.core.utils.Threads; 18 | import org.junit.jupiter.api.Test; 19 | 20 | import java.util.concurrent.ScheduledThreadPoolExecutor; 21 | import java.util.concurrent.TimeUnit; 22 | import java.util.concurrent.atomic.AtomicInteger; 23 | import java.util.concurrent.atomic.LongAdder; 24 | 25 | /** 26 | * @author ahoo wang 27 | */ 28 | public class ScheduledThreadPoolExecutorTests { 29 | private final ScheduledThreadPoolExecutor scheduledThreadPoolExecutor; 30 | 31 | public ScheduledThreadPoolExecutorTests() { 32 | this.scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(2, Threads.defaultFactory("SubscribeCompensation")); 33 | } 34 | 35 | @SneakyThrows 36 | @Test 37 | public void schedule() { 38 | LongAdder longAdder = new LongAdder(); 39 | AtomicInteger atomicInteger = new AtomicInteger(); 40 | scheduledThreadPoolExecutor.scheduleAtFixedRate(() -> { 41 | try { 42 | longAdder.increment(); 43 | 44 | System.out.println("run-" + Thread.currentThread().getName() + "-" + longAdder.intValue()); 45 | TimeUnit.SECONDS.sleep(2); 46 | } catch (Throwable throwable) { 47 | throwable.printStackTrace(); 48 | } 49 | }, 1, 1, TimeUnit.SECONDS); 50 | scheduledThreadPoolExecutor.scheduleAtFixedRate(() -> { 51 | try { 52 | longAdder.increment(); 53 | System.out.println("run-" + Thread.currentThread().getName() + "-" + longAdder.intValue()); 54 | TimeUnit.SECONDS.sleep(2); 55 | } catch (Throwable throwable) { 56 | throwable.printStackTrace(); 57 | } 58 | }, 1, 1, TimeUnit.SECONDS); 59 | TimeUnit.SECONDS.sleep(20); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /eventbus-core/src/test/java/me/ahoo/eventbus/core/SubscriberScannerTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.core; 15 | 16 | import lombok.var; 17 | import me.ahoo.eventbus.core.publisher.EventDescriptorParser; 18 | import me.ahoo.eventbus.core.publisher.EventNameGenerator; 19 | import me.ahoo.eventbus.core.publisher.impl.SimpleEventNameGenerator; 20 | import me.ahoo.eventbus.core.subscriber.SubscriberNameGenerator; 21 | import me.ahoo.eventbus.core.subscriber.SubscriberScanner; 22 | import me.ahoo.eventbus.core.subscriber.impl.SimpleSubscriberNameGenerator; 23 | import org.junit.jupiter.api.Assertions; 24 | import org.junit.jupiter.api.Test; 25 | 26 | 27 | /** 28 | * @author ahoo wang 29 | */ 30 | public class SubscriberScannerTests { 31 | 32 | private final SubscriberScanner subscriberScanner; 33 | 34 | public SubscriberScannerTests() { 35 | EventNameGenerator eventNameGenerator = new SimpleEventNameGenerator(); 36 | EventDescriptorParser eventDescriptorParser = new EventDescriptorParser(eventNameGenerator); 37 | SubscriberNameGenerator subscriberNameGenerator = new SimpleSubscriberNameGenerator("eventbus-"); 38 | this.subscriberScanner = new SubscriberScanner(subscriberNameGenerator, eventDescriptorParser); 39 | } 40 | 41 | 42 | @Test 43 | public void scan() { 44 | var list = subscriberScanner.scan(new DemoSubscriber()); 45 | Assertions.assertNotNull(list); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /eventbus-demo/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | plugins { 15 | application 16 | } 17 | 18 | application { 19 | mainClass.set("me.ahoo.eventbus.demo.DemoApplication") 20 | } 21 | 22 | configurations { 23 | compileOnly { 24 | extendsFrom(configurations.annotationProcessor.get()) 25 | } 26 | } 27 | 28 | dependencies { 29 | implementation(platform(project(":eventbus-dependencies"))) 30 | implementation("io.springfox:springfox-boot-starter") 31 | implementation("org.springframework.boot:spring-boot-starter-web") 32 | implementation("org.springframework.cloud:spring-cloud-starter-openfeign") 33 | compileOnly("org.projectlombok:lombok:${rootProject.ext.get("lombokVersion")}") 34 | annotationProcessor("org.springframework.boot:spring-boot-configuration-processor:${rootProject.ext.get("springBootVersion")}") 35 | annotationProcessor("org.projectlombok:lombok:${rootProject.ext.get("lombokVersion")}") 36 | testImplementation("org.springframework.boot:spring-boot-starter-test") 37 | implementation("org.springframework.boot:spring-boot-starter-actuator") 38 | runtimeOnly("org.mariadb.jdbc:mariadb-java-client") 39 | implementation(project(":eventbus-spring-boot-starter")) 40 | implementation(project(":eventbus-spring-boot-autoconfigure")) { 41 | capabilities { 42 | requireCapability("me.ahoo.eventbus:rabbit-bus-support") 43 | } 44 | } 45 | // implementation(project(":eventbus-spring-boot-autoconfigure")) { 46 | // capabilities { 47 | // requireCapability("me.ahoo.eventbus:kafka-bus-support") 48 | // } 49 | // } 50 | implementation(project(":eventbus-spring-boot-autoconfigure")) { 51 | capabilities { 52 | requireCapability("me.ahoo.eventbus:simba-jdbc-support") 53 | } 54 | } 55 | 56 | // implementation("org.springframework.cloud:spring-cloud-zookeeper-core") 57 | api("me.ahoo.cosid:cosid-jdbc") 58 | implementation("me.ahoo.cosid:cosid-spring-boot-starter") 59 | implementation("me.ahoo.cosid:cosid-shardingsphere") 60 | implementation("org.apache.shardingsphere:shardingsphere-jdbc-core-spring-boot-starter:${rootProject.ext.get("shardingsphereVersion")}") 61 | } 62 | 63 | tasks.withType { 64 | useJUnitPlatform() 65 | } 66 | 67 | description = "eventbus-demo" 68 | -------------------------------------------------------------------------------- /eventbus-demo/src/main/java/me/ahoo/eventbus/demo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.demo; 15 | 16 | import org.springframework.boot.SpringApplication; 17 | import org.springframework.boot.autoconfigure.SpringBootApplication; 18 | 19 | /** 20 | * DemoApplication. 21 | * 22 | * @author ahoo wang 23 | */ 24 | @SpringBootApplication 25 | public class DemoApplication { 26 | public static void main(String[] args) { 27 | SpringApplication.run(DemoApplication.class); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /eventbus-demo/src/main/java/me/ahoo/eventbus/demo/config/AppConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.demo.config; 15 | 16 | import org.springframework.context.annotation.Configuration; 17 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 18 | 19 | /** 20 | * AppConfig. 21 | * 22 | * @author ahoo wang 23 | */ 24 | @Configuration 25 | @EnableSwagger2 26 | public class AppConfig { 27 | } 28 | -------------------------------------------------------------------------------- /eventbus-demo/src/main/java/me/ahoo/eventbus/demo/controller/BusController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.demo.controller; 15 | 16 | import me.ahoo.eventbus.demo.event.FieldEventWrapper; 17 | import me.ahoo.eventbus.demo.event.PublishDataEvent; 18 | import me.ahoo.eventbus.demo.service.BusService; 19 | 20 | import org.springframework.web.bind.annotation.GetMapping; 21 | import org.springframework.web.bind.annotation.RequestMapping; 22 | import org.springframework.web.bind.annotation.RestController; 23 | 24 | /** 25 | * BusController. 26 | * 27 | * @author ahoo wang 28 | */ 29 | @RestController 30 | @RequestMapping("bus") 31 | public class BusController { 32 | 33 | public final BusService busService; 34 | 35 | public BusController(BusService busService) { 36 | this.busService = busService; 37 | } 38 | 39 | @GetMapping("publish") 40 | public PublishDataEvent publish() { 41 | return busService.publish(); 42 | } 43 | 44 | @GetMapping("nestedPublish") 45 | public PublishDataEvent nestedPublish() { 46 | return busService.nestedPublish(); 47 | } 48 | 49 | @GetMapping("nestedPublishWithProxy") 50 | public PublishDataEvent nestedPublishWithProxy() { 51 | return busService.nestedPublishWithProxy(); 52 | } 53 | 54 | @GetMapping("fieldEventPublish") 55 | public FieldEventWrapper fieldEventPublish() { 56 | return busService.fieldEventPublish(); 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /eventbus-demo/src/main/java/me/ahoo/eventbus/demo/controller/OrderController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.demo.controller; 15 | 16 | import me.ahoo.eventbus.demo.event.OrderCreatedEvent; 17 | import me.ahoo.eventbus.demo.service.OrderService; 18 | 19 | import org.springframework.web.bind.annotation.PostMapping; 20 | import org.springframework.web.bind.annotation.RequestMapping; 21 | import org.springframework.web.bind.annotation.RestController; 22 | 23 | /** 24 | * OrderController. 25 | * 26 | * @author ahoo wang 27 | */ 28 | @RestController 29 | @RequestMapping("order") 30 | public class OrderController { 31 | private final OrderService orderService; 32 | 33 | public OrderController(OrderService orderService) { 34 | this.orderService = orderService; 35 | } 36 | 37 | @PostMapping 38 | public OrderCreatedEvent create() { 39 | return orderService.createOrder(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /eventbus-demo/src/main/java/me/ahoo/eventbus/demo/event/FieldEventData.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.demo.event; 15 | 16 | /** 17 | * FieldEventData. 18 | * 19 | * @author ahoo wang 20 | */ 21 | public class FieldEventData { 22 | private Long id; 23 | 24 | public Long getId() { 25 | return id; 26 | } 27 | 28 | public void setId(Long id) { 29 | this.id = id; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /eventbus-demo/src/main/java/me/ahoo/eventbus/demo/event/FieldEventWrapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.demo.event; 15 | 16 | import me.ahoo.eventbus.core.annotation.Event; 17 | 18 | /** 19 | * FieldEventWrapper. 20 | * 21 | * @author ahoo wang 22 | */ 23 | public class FieldEventWrapper { 24 | private String resp; 25 | @Event 26 | private FieldEventData fieldEventData; 27 | 28 | public String getResp() { 29 | return resp; 30 | } 31 | 32 | public void setResp(String resp) { 33 | this.resp = resp; 34 | } 35 | 36 | public FieldEventData getFieldEventData() { 37 | return fieldEventData; 38 | } 39 | 40 | public void setFieldEventData(FieldEventData fieldEventData) { 41 | this.fieldEventData = fieldEventData; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /eventbus-demo/src/main/java/me/ahoo/eventbus/demo/event/OrderCreatedEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.demo.event; 15 | 16 | import me.ahoo.eventbus.core.annotation.Event; 17 | 18 | import com.google.common.base.MoreObjects; 19 | 20 | /** 21 | * OrderCreatedEvent. 22 | * 23 | * @author ahoo wang 24 | */ 25 | @Event(dataId = "orderId") 26 | public class OrderCreatedEvent { 27 | private long orderId; 28 | 29 | public long getOrderId() { 30 | return orderId; 31 | } 32 | 33 | public void setOrderId(long orderId) { 34 | this.orderId = orderId; 35 | } 36 | 37 | @Override 38 | public String toString() { 39 | return MoreObjects.toStringHelper(this) 40 | .add("orderId", orderId) 41 | .toString(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /eventbus-demo/src/main/java/me/ahoo/eventbus/demo/event/PublishDataEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.demo.event; 15 | 16 | import me.ahoo.eventbus.core.annotation.Event; 17 | 18 | /** 19 | * PublishDataEvent. 20 | * 21 | * @author ahoo wang 22 | */ 23 | @Event 24 | public class PublishDataEvent { 25 | private long id; 26 | 27 | public long getId() { 28 | return id; 29 | } 30 | 31 | public void setId(long id) { 32 | this.id = id; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /eventbus-demo/src/main/java/me/ahoo/eventbus/demo/event/RePublishDataEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.demo.event; 15 | 16 | import me.ahoo.eventbus.core.annotation.Event; 17 | 18 | /** 19 | * RePublishDataEvent. 20 | * 21 | * @author ahoo wang 22 | */ 23 | @Event 24 | public class RePublishDataEvent { 25 | private long id; 26 | 27 | public long getId() { 28 | return id; 29 | } 30 | 31 | public void setId(long id) { 32 | this.id = id; 33 | } 34 | 35 | 36 | } 37 | -------------------------------------------------------------------------------- /eventbus-demo/src/main/java/me/ahoo/eventbus/demo/service/BusService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.demo.service; 15 | 16 | 17 | import me.ahoo.cosid.IdGenerator; 18 | import me.ahoo.cosid.snowflake.SafeJavaScriptSnowflakeId; 19 | import me.ahoo.eventbus.core.annotation.Publish; 20 | import me.ahoo.eventbus.core.annotation.Subscribe; 21 | import me.ahoo.eventbus.demo.event.FieldEventData; 22 | import me.ahoo.eventbus.demo.event.FieldEventWrapper; 23 | import me.ahoo.eventbus.demo.event.PublishDataEvent; 24 | import me.ahoo.eventbus.demo.event.RePublishDataEvent; 25 | 26 | import lombok.SneakyThrows; 27 | import lombok.extern.slf4j.Slf4j; 28 | import lombok.var; 29 | import org.springframework.beans.factory.annotation.Autowired; 30 | import org.springframework.stereotype.Service; 31 | 32 | import java.util.concurrent.TimeUnit; 33 | 34 | /** 35 | * BusService. 36 | * 37 | * @author ahoo wang 38 | */ 39 | @Slf4j 40 | @Service 41 | public class BusService { 42 | 43 | @Autowired 44 | private BusService proxyBusService; 45 | private final IdGenerator idGenerator; 46 | 47 | public BusService() { 48 | idGenerator = SafeJavaScriptSnowflakeId.ofMillisecond(0); 49 | } 50 | 51 | /** 52 | * publish use proxy. 53 | * 54 | */ 55 | public PublishDataEvent nestedPublishWithProxy() { 56 | return proxyBusService.publish(); 57 | } 58 | 59 | /** 60 | * can not publish this. 61 | * 62 | */ 63 | public PublishDataEvent nestedPublish() { 64 | log.info("here will can not publish event use event bus."); 65 | return this.publish(); 66 | } 67 | 68 | @Publish 69 | public PublishDataEvent publish() { 70 | log.info("publish"); 71 | var event = new PublishDataEvent(); 72 | 73 | event.setId(idGenerator.generate()); 74 | return event; 75 | } 76 | 77 | @SneakyThrows 78 | @Subscribe 79 | public void subscribePublishDataEvent(PublishDataEvent publishDataEvent) { 80 | int sleepSeconds = 5; 81 | log.info("subscribePublishDataEvent->>id:{} sleep:[{}]", publishDataEvent.getId(), sleepSeconds); 82 | TimeUnit.SECONDS.sleep(sleepSeconds); 83 | 84 | } 85 | 86 | @Subscribe 87 | public PublishDataEvent subscribeThenPublishNull(PublishDataEvent publishDataEvent) { 88 | log.info("subscribeThenPublishNull->>id:{}", publishDataEvent.getId()); 89 | return null; 90 | } 91 | 92 | @Subscribe("subscribeThenPublish.customizeQueue") 93 | public RePublishDataEvent subscribeThenPublish(PublishDataEvent publishDataEvent) { 94 | log.info("rePublish->>id:{}", publishDataEvent.getId()); 95 | var event = new RePublishDataEvent(); 96 | event.setId(idGenerator.generate()); 97 | return event; 98 | } 99 | 100 | @Subscribe 101 | public void subscribePublishDataEventOther(PublishDataEvent publishDataEvent) { 102 | log.info("subscribePublishDataEventOther->>id:{}", publishDataEvent.getId()); 103 | } 104 | 105 | @Subscribe 106 | public void subscribeError(RePublishDataEvent rePublishDataEvent) { 107 | log.info("subscribeError->>error:{}", rePublishDataEvent.getId()); 108 | throw new RuntimeException("we are error"); 109 | } 110 | 111 | @Publish 112 | public FieldEventWrapper fieldEventPublish() { 113 | log.info("fieldEventPublish"); 114 | FieldEventWrapper fieldEventWrapper = new FieldEventWrapper(); 115 | fieldEventWrapper.setResp("any response"); 116 | FieldEventData fieldEventData = new FieldEventData(); 117 | fieldEventData.setId(1L); 118 | fieldEventWrapper.setFieldEventData(fieldEventData); 119 | return fieldEventWrapper; 120 | } 121 | 122 | @Subscribe 123 | public void subscribeFieldEvent(FieldEventData fieldEventData) { 124 | log.info("subscribeFieldEvent"); 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /eventbus-demo/src/main/java/me/ahoo/eventbus/demo/service/NoticeService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.demo.service; 15 | 16 | import me.ahoo.eventbus.core.annotation.Subscribe; 17 | import me.ahoo.eventbus.demo.event.OrderCreatedEvent; 18 | 19 | import lombok.extern.slf4j.Slf4j; 20 | import org.springframework.stereotype.Service; 21 | 22 | /** 23 | * NoticeService. 24 | * 25 | * @author ahoo wang 26 | */ 27 | @Slf4j 28 | @Service 29 | public class NoticeService { 30 | 31 | @Subscribe 32 | public void handleOrderCreated(OrderCreatedEvent orderCreatedEvent) { 33 | log.info("handleOrderCreated - event:[{}].", orderCreatedEvent); 34 | /** 35 | * Execute business code 36 | * send sms / email ? 37 | */ 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /eventbus-demo/src/main/java/me/ahoo/eventbus/demo/service/OrderService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.demo.service; 15 | 16 | import me.ahoo.cosid.IdGenerator; 17 | import me.ahoo.cosid.snowflake.SafeJavaScriptSnowflakeId; 18 | import me.ahoo.eventbus.core.annotation.Publish; 19 | import me.ahoo.eventbus.demo.event.OrderCreatedEvent; 20 | 21 | import org.springframework.stereotype.Service; 22 | 23 | /** 24 | * OrderService. 25 | * 26 | * @author ahoo wang 27 | */ 28 | @Service 29 | public class OrderService { 30 | private final IdGenerator idGenerator; 31 | 32 | public OrderService() { 33 | idGenerator = SafeJavaScriptSnowflakeId.ofMillisecond(0); 34 | } 35 | 36 | @Publish 37 | public OrderCreatedEvent createOrder() { 38 | OrderCreatedEvent orderCreatedEvent = new OrderCreatedEvent(); 39 | orderCreatedEvent.setOrderId(idGenerator.generate()); 40 | return orderCreatedEvent; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /eventbus-demo/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | logging: 2 | level: 3 | me.ahoo: debug 4 | # org: 5 | # springframework: 6 | # jdbc: info 7 | spring: 8 | application: 9 | name: eventbus-demo 10 | rabbitmq: 11 | host: localhost 12 | username: eventbus 13 | password: eventbus 14 | # datasource: 15 | # url: jdbc:mariadb://localhost:3306/eventbus_db 16 | # username: root 17 | # password: root 18 | 19 | shardingsphere: 20 | datasource: 21 | names: ds0 22 | ds0: 23 | type: com.zaxxer.hikari.HikariDataSource 24 | jdbcUrl: jdbc:mariadb://localhost:3306/eventbus_db?serverTimezone=GMT%2B8&characterEncoding=utf-8 25 | username: root 26 | password: root 27 | props: 28 | sql-show: true 29 | rules: 30 | sharding: 31 | tables: 32 | publish_event: 33 | actual-data-nodes: ds0.publish_event_$->{202110..202112},ds0.publish_event_$->{202201..202212} 34 | table-strategy: 35 | standard: 36 | sharding-column: create_time 37 | sharding-algorithm-name: publish-event-interval 38 | subscribe_event: 39 | actual-data-nodes: ds0.subscribe_event_$->{202110..202112},ds0.subscribe_event_$->{202201..202212} 40 | table-strategy: 41 | standard: 42 | sharding-column: event_create_time 43 | sharding-algorithm-name: subscribe-event-interval 44 | sharding-algorithms: 45 | publish-event-interval: 46 | type: COSID_INTERVAL 47 | props: 48 | logic-name-prefix: publish_event_ 49 | datetime-lower: 2021-10-01 00:00:00 50 | datetime-upper: 2022-12-31 23:59:59 51 | sharding-suffix-pattern: yyyyMM 52 | datetime-interval-unit: MONTHS 53 | datetime-interval-amount: 1 54 | subscribe-event-interval: 55 | type: COSID_INTERVAL 56 | props: 57 | logic-name-prefix: subscribe_event_ 58 | datetime-lower: 2021-10-01 00:00:00 59 | datetime-upper: 2022-12-31 23:59:59 60 | sharding-suffix-pattern: yyyyMM 61 | datetime-interval-unit: MONTHS 62 | datetime-interval-amount: 1 63 | 64 | # kafka: 65 | # bootstrap-servers: localhost:9092 66 | # properties: 67 | # security.protocol: SASL_PLAINTEXT 68 | # sasl.mechanism: SCRAM-SHA-256 69 | # sasl.jaas.config: org.apache.kafka.common.security.scram.ScramLoginModule required username="admin" password="admin"; 70 | # listener: 71 | # missing-topics-fatal: false 72 | 73 | govern: 74 | eventbus: 75 | rabbit: 76 | exchange: eventbus 77 | compensate: 78 | publish: 79 | schedule: 80 | initial-delay: 30s 81 | period: 10s 82 | range: 60D 83 | subscribe: 84 | schedule: 85 | initial-delay: 30s 86 | period: 10s 87 | enabled: true 88 | subscriber: 89 | prefix: ${spring.application.name}. 90 | 91 | cosid: 92 | namespace: ${spring.application.name} 93 | snowflake: 94 | enabled: true 95 | machine: 96 | distributor: 97 | type: jdbc 98 | provider: 99 | eventbus: 100 | converter: 101 | type: radix 102 | -------------------------------------------------------------------------------- /eventbus-dependencies/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | dependencies { 15 | api(platform("org.springframework.boot:spring-boot-dependencies:${rootProject.ext.get("springBootVersion")}")) 16 | api(platform("org.springframework.cloud:spring-cloud-dependencies:${rootProject.ext.get("springCloudVersion")}")) 17 | api(platform("me.ahoo.cosid:cosid-bom:${rootProject.ext.get("cosIdVersion")}")) 18 | api(platform("me.ahoo.simba:simba-bom:${rootProject.ext.get("simbaVersion")}")) 19 | constraints { 20 | api("org.projectlombok:lombok:${rootProject.ext.get("lombokVersion")}") 21 | api("com.google.guava:guava:${rootProject.ext.get("guavaVersion")}") 22 | api("io.springfox:springfox-boot-starter:${rootProject.ext.get("springfoxVersion")}") 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /eventbus-jdbc/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | dependencies { 15 | implementation(project(":eventbus-core")) 16 | implementation("org.springframework:spring-jdbc") 17 | testImplementation("mysql:mysql-connector-java") 18 | } 19 | 20 | description = "eventbus-jdbc" 21 | -------------------------------------------------------------------------------- /eventbus-jdbc/src/test/java/me/ahoo/eventbus/core/compensate/db/CompensateLeaderServiceTest.java: -------------------------------------------------------------------------------- 1 | ///* 2 | // * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | // * Licensed under the Apache License, Version 2.0 (the "License"); 4 | // * you may not use this file except in compliance with the License. 5 | // * You may obtain a copy of the License at 6 | // * http://www.apache.org/licenses/LICENSE-2.0 7 | // * Unless required by applicable law or agreed to in writing, software 8 | // * distributed under the License is distributed on an "AS IS" BASIS, 9 | // * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // * See the License for the specific language governing permissions and 11 | // * limitations under the License. 12 | // */ 13 | // 14 | //package me.ahoo.eventbus.core.compensate.db; 15 | // 16 | //import com.mysql.cj.jdbc.MysqlDataSource; 17 | //import me.ahoo.eventbus.core.compensate.db.config.LeaderConfig; 18 | //import me.ahoo.eventbus.jdbc.JdbcPublishEventRepository; 19 | //import org.junit.jupiter.api.Assertions; 20 | //import org.junit.jupiter.api.BeforeAll; 21 | //import org.junit.jupiter.api.Test; 22 | //import org.junit.jupiter.api.TestInstance; 23 | //import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; 24 | // 25 | //import java.time.Duration; 26 | //import java.util.UUID; 27 | //import java.util.concurrent.CompletableFuture; 28 | //import java.util.concurrent.locks.LockSupport; 29 | // 30 | ///** 31 | // * @author ahoo wang 32 | // */ 33 | //@TestInstance(TestInstance.Lifecycle.PER_CLASS) 34 | //class CompensateLeaderServiceTest { 35 | // 36 | // LeaderRepository leaderRepository; 37 | // 38 | // @BeforeAll 39 | // public void setup() { 40 | // MysqlDataSource dataSource = new MysqlDataSource(); 41 | // dataSource.setUrl("jdbc:mysql://localhost:3306/eventbus_db?serverTimezone=GMT%2B8&characterEncoding=utf-8"); 42 | // dataSource.setUser("root"); 43 | // dataSource.setPassword("root"); 44 | // NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource); 45 | // leaderRepository = new JdbcPublishEventRepository(null, namedParameterJdbcTemplate); 46 | // } 47 | // 48 | // @Test 49 | // void fightLeadership() { 50 | // LeaderConfig leaderConfig = new LeaderConfig(); 51 | // leaderConfig.setTermLength(1); 52 | // leaderConfig.setTransitionLength(1); 53 | // String leaderId1 = UUID.randomUUID().toString(); 54 | // String leaderId2 = UUID.randomUUID().toString(); 55 | // boolean succeeded = CompensateLeaderService.fightLeadership(leaderRepository, leaderId1, leaderConfig).isSucceeded(); 56 | // Assertions.assertTrue(succeeded); 57 | // succeeded = CompensateLeaderService.fightLeadership(leaderRepository, leaderId2, leaderConfig).isSucceeded(); 58 | // Assertions.assertFalse(succeeded); 59 | // succeeded = leaderRepository.releaseLeadership(leaderId1); 60 | // Assertions.assertTrue(succeeded); 61 | // } 62 | // 63 | // @Test 64 | // void fightLeadershipConcurrent() { 65 | // LeaderConfig leaderConfig = new LeaderConfig(); 66 | // leaderConfig.setTermLength(1); 67 | // leaderConfig.setTransitionLength(5); 68 | // String[] leaderIds = new String[100]; 69 | // for (int i = 0; i < 100; i++) { 70 | // leaderIds[i] = UUID.randomUUID().toString(); 71 | // } 72 | // 73 | // CompletableFuture[] futures = new CompletableFuture[300]; 74 | // for (int i = 0; i < 100; i++) { 75 | // String leaderId = leaderIds[i]; 76 | // futures[i] = CompletableFuture.supplyAsync(() -> CompensateLeaderService.fightLeadership(leaderRepository, leaderId, leaderConfig)); 77 | // } 78 | // for (int i = 0; i < 100; i++) { 79 | // String leaderId = leaderIds[i]; 80 | // futures[100 + i] = CompletableFuture.supplyAsync(() -> CompensateLeaderService.fightLeadership(leaderRepository, leaderId, leaderConfig)); 81 | // } 82 | // LockSupport.parkNanos(Duration.ofSeconds(2).toNanos()); 83 | // for (int i = 0; i < 100; i++) { 84 | // String leaderId = leaderIds[i]; 85 | // futures[200 + i] = CompletableFuture.supplyAsync(() -> CompensateLeaderService.fightLeadership(leaderRepository, leaderId, leaderConfig)); 86 | // } 87 | // CompletableFuture.allOf(futures).join(); 88 | // } 89 | //} 90 | -------------------------------------------------------------------------------- /eventbus-jdbc/src/test/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 16 | 17 | 18 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /eventbus-kafka/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | dependencies { 15 | implementation(project(":eventbus-core")) 16 | api("org.springframework.kafka:spring-kafka") 17 | } 18 | 19 | description = "eventbus-kafka" 20 | -------------------------------------------------------------------------------- /eventbus-kafka/src/main/java/me/ahoo/eventbus/kafka/KafkaEventCodec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.kafka; 15 | 16 | 17 | import me.ahoo.eventbus.core.codec.EventCodec; 18 | import me.ahoo.eventbus.core.compensate.CompensatePublishEvent; 19 | import me.ahoo.eventbus.core.publisher.PublishEvent; 20 | import me.ahoo.eventbus.core.serialize.Deserializer; 21 | import me.ahoo.eventbus.core.serialize.Serializer; 22 | 23 | import org.apache.kafka.clients.consumer.ConsumerRecord; 24 | import org.apache.kafka.clients.producer.ProducerRecord; 25 | import org.apache.kafka.common.header.Header; 26 | import org.apache.kafka.common.header.Headers; 27 | import org.apache.kafka.common.header.internals.RecordHeaders; 28 | 29 | import java.nio.charset.StandardCharsets; 30 | import java.util.Objects; 31 | 32 | /** 33 | * KafkaEventCodec. 34 | * 35 | * @author ahoo wang 36 | */ 37 | public class KafkaEventCodec implements EventCodec { 38 | public static final String EVENT_DATA_ID = "event_data_id"; 39 | private final Serializer serializer; 40 | private final Deserializer deserializer; 41 | 42 | public KafkaEventCodec(Serializer serializer, Deserializer deserializer) { 43 | this.serializer = serializer; 44 | this.deserializer = deserializer; 45 | } 46 | 47 | public ProducerRecord encode(PublishEvent publishEvent) { 48 | String eventDataStr; 49 | if (publishEvent instanceof CompensatePublishEvent) { 50 | eventDataStr = ((CompensatePublishEvent) publishEvent).getEventData(); 51 | } else { 52 | eventDataStr = serializer.serialize(publishEvent.getEventData()); 53 | } 54 | Headers headers = new RecordHeaders(); 55 | headers.add(EVENT_DATA_ID, publishEvent.getEventDataId().toString().getBytes(StandardCharsets.UTF_8)); 56 | 57 | return new ProducerRecord<>(publishEvent.getEventName(), null, publishEvent.getCreateTime(), publishEvent.getId(), eventDataStr, headers); 58 | } 59 | 60 | public PublishEvent decode(ConsumerRecord consumerRecord, Class eventDataClass) { 61 | String eventName = consumerRecord.topic(); 62 | long id = consumerRecord.key(); 63 | Header eventDataIdHeader = consumerRecord.headers().lastHeader(EVENT_DATA_ID); 64 | PublishEvent publishEvent = new PublishEvent(); 65 | publishEvent.setId(id); 66 | publishEvent.setEventName(eventName); 67 | if (Objects.nonNull(eventDataIdHeader)) { 68 | String eventDataIdStr = new String(eventDataIdHeader.value(), StandardCharsets.UTF_8); 69 | long eventDataId = Long.parseLong(eventDataIdStr); 70 | publishEvent.setEventDataId(eventDataId); 71 | } 72 | Long timestamp = consumerRecord.timestamp(); 73 | publishEvent.setCreateTime(timestamp); 74 | Object eventData = deserializer.deserialize(consumerRecord.value(), eventDataClass); 75 | 76 | publishEvent.setEventData(eventData); 77 | return publishEvent; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /eventbus-kafka/src/main/java/me/ahoo/eventbus/kafka/KafkaPublisher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.kafka; 15 | 16 | import me.ahoo.eventbus.core.publisher.PublishEvent; 17 | import me.ahoo.eventbus.core.publisher.PublishException; 18 | import me.ahoo.eventbus.core.publisher.Publisher; 19 | 20 | import org.apache.kafka.clients.producer.ProducerRecord; 21 | import org.springframework.kafka.core.KafkaTemplate; 22 | 23 | /** 24 | * KafkaPublisher. 25 | * 26 | * @author ahoo wang 27 | */ 28 | public class KafkaPublisher implements Publisher { 29 | private final KafkaTemplate kafkaTemplate; 30 | private final KafkaEventCodec kafkaEventCodec; 31 | 32 | public KafkaPublisher(KafkaEventCodec kafkaEventCodec, KafkaTemplate kafkaTemplate) { 33 | this.kafkaEventCodec = kafkaEventCodec; 34 | this.kafkaTemplate = kafkaTemplate; 35 | } 36 | 37 | @Override 38 | public void publish(PublishEvent event) throws PublishException { 39 | ProducerRecord producerRecord = kafkaEventCodec.encode(event); 40 | 41 | kafkaTemplate.send(producerRecord); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /eventbus-kafka/src/main/java/me/ahoo/eventbus/kafka/KafkaSubscriberRegistry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.kafka; 15 | 16 | import me.ahoo.eventbus.core.consistency.ConsistencySubscriber; 17 | import me.ahoo.eventbus.core.consistency.ConsistencySubscriberFactory; 18 | import me.ahoo.eventbus.core.publisher.PublishEvent; 19 | import me.ahoo.eventbus.core.subscriber.Subscriber; 20 | import me.ahoo.eventbus.core.subscriber.SubscriberRegistry; 21 | 22 | import org.springframework.kafka.config.KafkaListenerContainerFactory; 23 | import org.springframework.kafka.config.KafkaListenerEndpointRegistry; 24 | import org.springframework.kafka.config.MethodKafkaListenerEndpoint; 25 | import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory; 26 | import org.springframework.messaging.handler.annotation.support.MessageHandlerMethodFactory; 27 | 28 | import java.util.concurrent.ConcurrentHashMap; 29 | 30 | /** 31 | * KafkaSubscriberRegistry. 32 | * 33 | * @author ahoo wang 34 | */ 35 | public class KafkaSubscriberRegistry implements SubscriberRegistry { 36 | 37 | private final KafkaEventCodec kafkaEventCodec; 38 | private final ConcurrentHashMap nameMapSubscribers; 39 | private final MessageHandlerMethodFactory messageHandlerMethodFactory; 40 | private final ConsistencySubscriberFactory subscriberFactory; 41 | private final KafkaListenerEndpointRegistry listenerEndpointRegistry; 42 | private final KafkaListenerContainerFactory listenerContainerFactory; 43 | 44 | public KafkaSubscriberRegistry(KafkaEventCodec kafkaEventCodec, ConsistencySubscriberFactory subscriberFactory, 45 | KafkaListenerEndpointRegistry listenerEndpointRegistry, 46 | KafkaListenerContainerFactory listenerContainerFactory) { 47 | this.kafkaEventCodec = kafkaEventCodec; 48 | this.subscriberFactory = subscriberFactory; 49 | this.listenerEndpointRegistry = listenerEndpointRegistry; 50 | this.listenerContainerFactory = listenerContainerFactory; 51 | this.nameMapSubscribers = new ConcurrentHashMap<>(); 52 | this.messageHandlerMethodFactory = new DefaultMessageHandlerMethodFactory(); 53 | } 54 | 55 | @Override 56 | public void subscribe(Subscriber subscriber) { 57 | ConsistencySubscriber consistencySubscriber = subscriberFactory.create(subscriber); 58 | registerListener(consistencySubscriber); 59 | registerSubscriber(consistencySubscriber); 60 | } 61 | 62 | private void registerListener(Subscriber subscriber) { 63 | MethodKafkaListenerAdapter methodKafkaListenerAdapter = new MethodKafkaListenerAdapter(kafkaEventCodec, subscriber); 64 | MethodKafkaListenerEndpoint endpoint = new MethodKafkaListenerEndpoint(); 65 | endpoint.setBean(methodKafkaListenerAdapter); 66 | endpoint.setMethod(MethodKafkaListenerAdapter.INVOKE_METHOD); 67 | endpoint.setId(subscriber.getName()); 68 | endpoint.setGroupId(subscriber.getName()); 69 | endpoint.setTopics(subscriber.getSubscribeEventName()); 70 | endpoint.setMessageHandlerMethodFactory(messageHandlerMethodFactory); 71 | this.listenerEndpointRegistry.registerListenerContainer(endpoint, this.listenerContainerFactory, true); 72 | } 73 | 74 | private void registerSubscriber(Subscriber subscriber) { 75 | nameMapSubscribers.put(subscriber.getName(), subscriber); 76 | } 77 | 78 | @Override 79 | public Subscriber getSubscriber(String subscriberName) { 80 | return nameMapSubscribers.get(subscriberName); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /eventbus-kafka/src/main/java/me/ahoo/eventbus/kafka/MethodKafkaListenerAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.kafka; 15 | 16 | import me.ahoo.eventbus.core.publisher.PublishEvent; 17 | import me.ahoo.eventbus.core.subscriber.Subscriber; 18 | 19 | import lombok.extern.slf4j.Slf4j; 20 | import org.apache.kafka.clients.consumer.ConsumerRecord; 21 | import org.springframework.kafka.listener.MessageListener; 22 | 23 | import java.lang.reflect.Method; 24 | 25 | /** 26 | * MethodKafkaListenerAdapter. 27 | * 28 | * @author ahoo wang 29 | */ 30 | @Slf4j 31 | public class MethodKafkaListenerAdapter implements MessageListener { 32 | 33 | public static final Method INVOKE_METHOD; 34 | private final KafkaEventCodec kafkaEventCodec; 35 | 36 | static { 37 | try { 38 | INVOKE_METHOD = MethodKafkaListenerAdapter.class.getMethod("onMessage", ConsumerRecord.class); 39 | } catch (NoSuchMethodException e) { 40 | throw new RuntimeException(e); 41 | } 42 | } 43 | 44 | private final Subscriber subscriber; 45 | 46 | public MethodKafkaListenerAdapter(KafkaEventCodec kafkaEventCodec, Subscriber subscriber) { 47 | this.kafkaEventCodec = kafkaEventCodec; 48 | this.subscriber = subscriber; 49 | } 50 | 51 | @Override 52 | public void onMessage(ConsumerRecord data) { 53 | try { 54 | PublishEvent publishEvent = kafkaEventCodec.decode(data, subscriber.getSubscribeEventClass()); 55 | if (log.isInfoEnabled()) { 56 | log.info("onMessage - received event subscriber:[{}]-> id:[{}] ,eventName:[{}].", subscriber.getName(), publishEvent.getId(), publishEvent.getEventName()); 57 | } 58 | this.subscriber.invoke(publishEvent); 59 | } catch (Throwable throwable) { 60 | String payloadStr = data.value(); 61 | log.error(String.format("onMessage - received event ERROR -> routeKey:[%s] , payload: %n %s", subscriber.getSubscribeEventName(), payloadStr), throwable); 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /eventbus-rabbit/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | dependencies { 15 | implementation(project(":eventbus-core")) 16 | api("org.springframework.amqp:spring-rabbit") 17 | } 18 | 19 | description = "eventbus-rabbit" 20 | -------------------------------------------------------------------------------- /eventbus-rabbit/src/main/java/me/ahoo/eventbus/rabbit/RabbitEventCodec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.rabbit; 15 | 16 | import me.ahoo.eventbus.core.codec.EventCodec; 17 | import me.ahoo.eventbus.core.compensate.CompensatePublishEvent; 18 | import me.ahoo.eventbus.core.publisher.PublishEvent; 19 | import me.ahoo.eventbus.core.serialize.Deserializer; 20 | import me.ahoo.eventbus.core.serialize.Serializer; 21 | 22 | import com.google.common.base.Charsets; 23 | import com.google.common.base.Preconditions; 24 | import org.springframework.amqp.core.Message; 25 | import org.springframework.amqp.core.MessageBuilder; 26 | import org.springframework.amqp.core.MessageProperties; 27 | import org.springframework.amqp.core.MessagePropertiesBuilder; 28 | 29 | import java.util.Objects; 30 | 31 | /** 32 | * RabbitEventCodec. 33 | * 34 | * @author ahoo wang 35 | */ 36 | public class RabbitEventCodec implements EventCodec { 37 | 38 | public static final String EVENT_ID = "event_id"; 39 | public static final String EVENT_NAME = "event_name"; 40 | public static final String EVENT_DATA_ID = "event_data_id"; 41 | public static final String EVENT_CREATE_TIME = "event_create_time"; 42 | 43 | private final Serializer serializer; 44 | private final Deserializer deserializer; 45 | 46 | public RabbitEventCodec(Serializer serializer, Deserializer deserializer) { 47 | this.serializer = serializer; 48 | this.deserializer = deserializer; 49 | } 50 | 51 | public Message encode(PublishEvent publishEvent) { 52 | String eventName = publishEvent.getEventName(); 53 | 54 | MessageProperties messageProperties = MessagePropertiesBuilder.newInstance() 55 | .setHeaderIfAbsent(EVENT_ID, publishEvent.getId()) 56 | .setHeaderIfAbsent(EVENT_DATA_ID, publishEvent.getEventDataId()) 57 | .setHeaderIfAbsent(EVENT_NAME, eventName) 58 | .setHeaderIfAbsent(EVENT_CREATE_TIME, publishEvent.getCreateTime()).build(); 59 | 60 | byte[] eventBuff; 61 | if (publishEvent instanceof CompensatePublishEvent) { 62 | eventBuff = ((CompensatePublishEvent) publishEvent).getEventData().getBytes(Charsets.UTF_8); 63 | } else { 64 | eventBuff = serializer.serialize(publishEvent.getEventData()).getBytes(Charsets.UTF_8); 65 | } 66 | return MessageBuilder.withBody(eventBuff).andProperties(messageProperties).build(); 67 | } 68 | 69 | public PublishEvent decode(Message message, Class eventDataClass) { 70 | MessageProperties messageProperties = message.getMessageProperties(); 71 | Long eventId = messageProperties.getHeader(EVENT_ID); 72 | Preconditions.checkNotNull(eventId, "%s can not be null.", EVENT_ID); 73 | 74 | String eventName = messageProperties.getHeader(EVENT_NAME); 75 | Preconditions.checkNotNull(eventName, "%s can not be null.", EVENT_NAME); 76 | 77 | Long eventCreateTime = messageProperties.getHeader(EVENT_CREATE_TIME); 78 | Preconditions.checkNotNull(eventCreateTime, "%s can not be null.", EVENT_CREATE_TIME); 79 | 80 | PublishEvent publishEvent = new PublishEvent(); 81 | publishEvent.setId(eventId); 82 | publishEvent.setEventName(eventName); 83 | 84 | Long eventDataId = messageProperties.getHeader(EVENT_DATA_ID); 85 | if (Objects.nonNull(eventDataId)) { 86 | publishEvent.setEventDataId(eventDataId); 87 | } 88 | Object typedEventData = deserializer.deserialize(new String(message.getBody(), Charsets.UTF_8), eventDataClass); 89 | publishEvent.setEventData(typedEventData); 90 | publishEvent.setCreateTime(eventCreateTime); 91 | return publishEvent; 92 | 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /eventbus-rabbit/src/main/java/me/ahoo/eventbus/rabbit/RabbitEventListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.rabbit; 15 | 16 | import me.ahoo.eventbus.core.publisher.PublishEvent; 17 | import me.ahoo.eventbus.core.subscriber.Subscriber; 18 | 19 | import com.google.common.base.Charsets; 20 | import lombok.extern.slf4j.Slf4j; 21 | import org.springframework.amqp.core.Message; 22 | import org.springframework.amqp.core.MessageListener; 23 | 24 | /** 25 | * RabbitEventListener. 26 | * 27 | * @author ahoo wang 28 | */ 29 | @Slf4j 30 | public class RabbitEventListener implements MessageListener { 31 | private final Subscriber subscriber; 32 | private final RabbitEventCodec rabbitEventCodec; 33 | 34 | public RabbitEventListener(RabbitEventCodec rabbitEventCodec, Subscriber subscriber) { 35 | this.rabbitEventCodec = rabbitEventCodec; 36 | this.subscriber = subscriber; 37 | } 38 | 39 | @Override 40 | public void onMessage(Message message) { 41 | try { 42 | PublishEvent publishEvent = rabbitEventCodec.decode(message, subscriber.getSubscribeEventClass()); 43 | if (log.isInfoEnabled()) { 44 | log.info("onMessage - received event subscriber:[{}]-> id:[{}] ,eventName:[{}].", subscriber.getName(), publishEvent.getId(), publishEvent.getEventName()); 45 | } 46 | this.subscriber.invoke(publishEvent); 47 | } catch (Throwable throwable) { 48 | String payloadStr = new String(message.getBody(), Charsets.UTF_8); 49 | if (log.isErrorEnabled()) { 50 | log.error(String.format("onMessage - received event ERROR -> routeKey:[%s] , payload: %n %s", subscriber.getSubscribeEventName(), payloadStr), throwable); 51 | } 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /eventbus-rabbit/src/main/java/me/ahoo/eventbus/rabbit/RabbitPublisher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.rabbit; 15 | 16 | import me.ahoo.eventbus.core.publisher.PublishEvent; 17 | import me.ahoo.eventbus.core.publisher.PublishException; 18 | import me.ahoo.eventbus.core.publisher.Publisher; 19 | import me.ahoo.eventbus.rabbit.config.RabbitConfig; 20 | 21 | import lombok.SneakyThrows; 22 | import lombok.extern.slf4j.Slf4j; 23 | import org.springframework.amqp.core.Message; 24 | import org.springframework.amqp.rabbit.connection.ConnectionFactory; 25 | import org.springframework.amqp.rabbit.core.RabbitTemplate; 26 | 27 | /** 28 | * RabbitPublisher. 29 | * 30 | * @author ahoo wang 31 | */ 32 | @Slf4j 33 | public class RabbitPublisher implements Publisher, AutoCloseable { 34 | private final RabbitTemplate rabbitTemplate; 35 | private final RabbitEventCodec rabbitEventCodec; 36 | 37 | @SneakyThrows 38 | public RabbitPublisher(RabbitEventCodec rabbitEventCodec, RabbitConfig rabbitConfig, ConnectionFactory connectionFactory) { 39 | this.rabbitEventCodec = rabbitEventCodec; 40 | this.rabbitTemplate = new RabbitTemplate(connectionFactory); 41 | this.rabbitTemplate.setExchange(rabbitConfig.getExchange()); 42 | } 43 | 44 | @Override 45 | public void publish(PublishEvent publishEvent) { 46 | try { 47 | String eventName = publishEvent.getEventName(); 48 | Message message = rabbitEventCodec.encode(publishEvent); 49 | 50 | rabbitTemplate.send(eventName, message); 51 | if (log.isInfoEnabled()) { 52 | log.info("publish - eventName:[{}] -> eventId:[{}] ", eventName, publishEvent.getId()); 53 | } 54 | } catch (Throwable throwable) { 55 | throw new PublishException(throwable.getMessage(), throwable); 56 | } 57 | } 58 | 59 | @Override 60 | public void close() throws Exception { 61 | if (log.isInfoEnabled()) { 62 | log.info("close - closing resources!"); 63 | } 64 | 65 | rabbitTemplate.destroy(); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /eventbus-rabbit/src/main/java/me/ahoo/eventbus/rabbit/config/RabbitConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.rabbit.config; 15 | 16 | import me.ahoo.eventbus.core.Consts; 17 | 18 | /** 19 | * RabbitConfig. 20 | * 21 | * @author ahoo wang 22 | */ 23 | public class RabbitConfig { 24 | private String exchange = Consts.GOVERN_EVENTBUS; 25 | 26 | public String getExchange() { 27 | return exchange; 28 | } 29 | 30 | public void setExchange(String exchange) { 31 | this.exchange = exchange; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /eventbus-spring-boot-autoconfigure/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | java { 15 | registerFeature("rabbitBusSupport") { 16 | usingSourceSet(sourceSets[SourceSet.MAIN_SOURCE_SET_NAME]) 17 | capability(group.toString(), "rabbit-bus-support", version.toString()) 18 | } 19 | registerFeature("kafkaBusSupport") { 20 | usingSourceSet(sourceSets[SourceSet.MAIN_SOURCE_SET_NAME]) 21 | capability(group.toString(), "kafka-bus-support", version.toString()) 22 | } 23 | registerFeature("simbaJdbcSupport") { 24 | usingSourceSet(sourceSets[SourceSet.MAIN_SOURCE_SET_NAME]) 25 | capability(group.toString(), "simba-jdbc-support", version.toString()) 26 | } 27 | } 28 | 29 | dependencies { 30 | api(project(":eventbus-core")) 31 | "rabbitBusSupportImplementation"(project(":eventbus-rabbit")) 32 | "kafkaBusSupportImplementation"(project(":eventbus-kafka")) 33 | "simbaJdbcSupportImplementation"("me.ahoo.simba:simba-jdbc") 34 | implementation(project(":eventbus-jdbc")) 35 | implementation(project(":eventbus-spring-context")) 36 | implementation("org.springframework.boot:spring-boot-starter") 37 | implementation("org.springframework.boot:spring-boot-starter-jdbc") 38 | implementation("com.fasterxml.jackson.core:jackson-databind") 39 | implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310") 40 | 41 | annotationProcessor("org.springframework.boot:spring-boot-configuration-processor:${rootProject.ext.get("springBootVersion")}") 42 | annotationProcessor("org.springframework.boot:spring-boot-autoconfigure-processor:${rootProject.ext.get("springBootVersion")}") 43 | } 44 | 45 | description = "eventbus-spring-boot-autoconfigure" 46 | -------------------------------------------------------------------------------- /eventbus-spring-boot-autoconfigure/src/main/java/me/ahoo/eventbus/spring/boot/autoconfigure/EnabledSuffix.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.spring.boot.autoconfigure; 15 | 16 | /** 17 | * EnabledSuffix. 18 | * 19 | * @author ahoo wang 20 | */ 21 | public interface EnabledSuffix { 22 | String SUFFIX = ".enabled"; 23 | } 24 | -------------------------------------------------------------------------------- /eventbus-spring-boot-autoconfigure/src/main/java/me/ahoo/eventbus/spring/boot/autoconfigure/EventBusProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.spring.boot.autoconfigure; 15 | 16 | import org.springframework.boot.context.properties.ConfigurationProperties; 17 | import org.springframework.boot.context.properties.NestedConfigurationProperty; 18 | 19 | /** 20 | * EventBusProperties. 21 | * 22 | * @author : ahoo wang 23 | */ 24 | @ConfigurationProperties(EventBusProperties.PREFIX) 25 | public class EventBusProperties { 26 | public static final String PREFIX = "govern.eventbus"; 27 | @NestedConfigurationProperty 28 | private Subscriber subscriber; 29 | 30 | public EventBusProperties() { 31 | subscriber = new Subscriber(); 32 | } 33 | 34 | public static class Subscriber { 35 | private String prefix; 36 | 37 | public String getPrefix() { 38 | return prefix; 39 | } 40 | 41 | public void setPrefix(String prefix) { 42 | this.prefix = prefix; 43 | } 44 | } 45 | 46 | public Subscriber getSubscriber() { 47 | return subscriber; 48 | } 49 | 50 | public void setSubscriber(Subscriber subscriber) { 51 | this.subscriber = subscriber; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /eventbus-spring-boot-autoconfigure/src/main/java/me/ahoo/eventbus/spring/boot/autoconfigure/compensate/CompensatePrefix.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.spring.boot.autoconfigure.compensate; 15 | 16 | import me.ahoo.eventbus.spring.boot.autoconfigure.EventBusProperties; 17 | 18 | /** 19 | * CompensatePrefix. 20 | * 21 | * @author ahoo wang 22 | */ 23 | 24 | public interface CompensatePrefix { 25 | String PREFIX = EventBusProperties.PREFIX + ".compensate"; 26 | } 27 | -------------------------------------------------------------------------------- /eventbus-spring-boot-autoconfigure/src/main/java/me/ahoo/eventbus/spring/boot/autoconfigure/compensate/ConditionalOnCompensateEnabled.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.spring.boot.autoconfigure.compensate; 15 | 16 | import me.ahoo.eventbus.spring.boot.autoconfigure.EnabledSuffix; 17 | 18 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 19 | 20 | import java.lang.annotation.ElementType; 21 | import java.lang.annotation.Retention; 22 | import java.lang.annotation.RetentionPolicy; 23 | import java.lang.annotation.Target; 24 | 25 | /** 26 | * ConditionalOnCompensateEnabled. 27 | * 28 | * @author ahoo wang 29 | */ 30 | @Retention(RetentionPolicy.RUNTIME) 31 | @Target({ElementType.TYPE, ElementType.METHOD}) 32 | @ConditionalOnProperty(value = CompensatePrefix.PREFIX + EnabledSuffix.SUFFIX, havingValue = "true", matchIfMissing = true) 33 | public @interface ConditionalOnCompensateEnabled { 34 | 35 | } 36 | -------------------------------------------------------------------------------- /eventbus-spring-boot-autoconfigure/src/main/java/me/ahoo/eventbus/spring/boot/autoconfigure/kafka/BusKafkaAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.spring.boot.autoconfigure.kafka; 15 | 16 | import me.ahoo.eventbus.core.consistency.ConsistencySubscriberFactory; 17 | import me.ahoo.eventbus.core.publisher.Publisher; 18 | import me.ahoo.eventbus.core.serialize.Deserializer; 19 | import me.ahoo.eventbus.core.serialize.Serializer; 20 | import me.ahoo.eventbus.core.subscriber.SubscriberRegistry; 21 | import me.ahoo.eventbus.kafka.KafkaEventCodec; 22 | import me.ahoo.eventbus.kafka.KafkaPublisher; 23 | import me.ahoo.eventbus.kafka.KafkaSubscriberRegistry; 24 | 25 | import org.springframework.boot.autoconfigure.AutoConfigureAfter; 26 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 27 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 28 | import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration; 29 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 30 | import org.springframework.context.annotation.Bean; 31 | import org.springframework.kafka.config.KafkaListenerContainerFactory; 32 | import org.springframework.kafka.config.KafkaListenerEndpointRegistry; 33 | import org.springframework.kafka.core.KafkaTemplate; 34 | 35 | /** 36 | * BusKafkaAutoConfiguration. 37 | * 38 | * @author ahoo wang 39 | */ 40 | @AutoConfigureAfter(KafkaAutoConfiguration.class) 41 | @ConditionalOnClass(KafkaPublisher.class) 42 | @EnableConfigurationProperties(KafkaProperties.class) 43 | @ConditionalOnKafkaEnabled 44 | public class BusKafkaAutoConfiguration { 45 | 46 | @Bean 47 | @ConditionalOnMissingBean 48 | public KafkaEventCodec eventCodec(Serializer serializer, Deserializer deserializer) { 49 | return new KafkaEventCodec(serializer, deserializer); 50 | } 51 | 52 | @Bean 53 | @ConditionalOnMissingBean 54 | public Publisher kafkaPublisher(KafkaEventCodec kafkaEventCodec, 55 | KafkaTemplate kafkaTemplate) { 56 | return new KafkaPublisher(kafkaEventCodec, kafkaTemplate); 57 | } 58 | 59 | @Bean 60 | @ConditionalOnMissingBean 61 | public SubscriberRegistry kafkaSubscriberRegistry( 62 | KafkaEventCodec kafkaEventCodec, 63 | ConsistencySubscriberFactory consistencySubscriberFactory, 64 | KafkaListenerEndpointRegistry listenerEndpointRegistry, 65 | KafkaListenerContainerFactory listenerContainerFactory) { 66 | return new KafkaSubscriberRegistry(kafkaEventCodec, consistencySubscriberFactory, listenerEndpointRegistry, listenerContainerFactory); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /eventbus-spring-boot-autoconfigure/src/main/java/me/ahoo/eventbus/spring/boot/autoconfigure/kafka/ConditionalOnKafkaEnabled.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.spring.boot.autoconfigure.kafka; 15 | 16 | import me.ahoo.eventbus.spring.boot.autoconfigure.EnabledSuffix; 17 | 18 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 19 | 20 | import java.lang.annotation.ElementType; 21 | import java.lang.annotation.Retention; 22 | import java.lang.annotation.RetentionPolicy; 23 | import java.lang.annotation.Target; 24 | 25 | /** 26 | * ConditionalOnKafkaEnabled. 27 | * 28 | * @author ahoo wang 29 | */ 30 | @Retention(RetentionPolicy.RUNTIME) 31 | @Target({ElementType.TYPE, ElementType.METHOD}) 32 | @ConditionalOnProperty(value = KafkaProperties.PREFIX + EnabledSuffix.SUFFIX, havingValue = "true") 33 | public @interface ConditionalOnKafkaEnabled { 34 | 35 | } 36 | -------------------------------------------------------------------------------- /eventbus-spring-boot-autoconfigure/src/main/java/me/ahoo/eventbus/spring/boot/autoconfigure/kafka/KafkaProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.spring.boot.autoconfigure.kafka; 15 | 16 | import me.ahoo.eventbus.spring.boot.autoconfigure.EventBusProperties; 17 | 18 | import org.springframework.boot.context.properties.ConfigurationProperties; 19 | 20 | /** 21 | * KafkaProperties. 22 | * 23 | * @author ahoo wang 24 | */ 25 | @ConfigurationProperties(KafkaProperties.PREFIX) 26 | public class KafkaProperties { 27 | public static final String PREFIX = EventBusProperties.PREFIX + ".kafka"; 28 | private boolean enabled = false; 29 | 30 | public boolean isEnabled() { 31 | return enabled; 32 | } 33 | 34 | public void setEnabled(boolean enabled) { 35 | this.enabled = enabled; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /eventbus-spring-boot-autoconfigure/src/main/java/me/ahoo/eventbus/spring/boot/autoconfigure/rabbit/BusRabbitAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.spring.boot.autoconfigure.rabbit; 15 | 16 | import me.ahoo.eventbus.core.consistency.ConsistencySubscriberFactory; 17 | import me.ahoo.eventbus.core.publisher.Publisher; 18 | import me.ahoo.eventbus.core.serialize.Deserializer; 19 | import me.ahoo.eventbus.core.serialize.Serializer; 20 | import me.ahoo.eventbus.core.subscriber.SubscriberRegistry; 21 | import me.ahoo.eventbus.rabbit.RabbitEventCodec; 22 | import me.ahoo.eventbus.rabbit.RabbitPublisher; 23 | import me.ahoo.eventbus.rabbit.RabbitSubscriberRegistry; 24 | 25 | import org.springframework.amqp.rabbit.connection.ConnectionFactory; 26 | import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry; 27 | import org.springframework.boot.autoconfigure.AutoConfigureAfter; 28 | import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration; 29 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 30 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 31 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 32 | import org.springframework.context.annotation.Bean; 33 | 34 | /** 35 | * BusRabbitAutoConfiguration. 36 | * 37 | * @author ahoo wang 38 | */ 39 | @EnableConfigurationProperties(RabbitProperties.class) 40 | @AutoConfigureAfter(RabbitAutoConfiguration.class) 41 | @ConditionalOnRabbitEnabled 42 | @ConditionalOnClass(RabbitPublisher.class) 43 | public class BusRabbitAutoConfiguration { 44 | 45 | private final RabbitProperties rabbitProperties; 46 | 47 | public BusRabbitAutoConfiguration(RabbitProperties rabbitProperties) { 48 | this.rabbitProperties = rabbitProperties; 49 | } 50 | 51 | @Bean 52 | @ConditionalOnMissingBean 53 | public RabbitEventCodec eventCodec(Serializer serializer, Deserializer deserializer) { 54 | return new RabbitEventCodec(serializer, deserializer); 55 | } 56 | 57 | @Bean 58 | @ConditionalOnMissingBean 59 | public Publisher rabbitPublisher( 60 | RabbitEventCodec rabbitEventCodec, 61 | ConnectionFactory connectionFactory) { 62 | return new RabbitPublisher(rabbitEventCodec, rabbitProperties, connectionFactory); 63 | } 64 | 65 | @Bean 66 | @ConditionalOnMissingBean 67 | public SubscriberRegistry rabbitSubscriberRegistry( 68 | RabbitEventCodec rabbitEventCodec, 69 | ConnectionFactory connectionFactory, 70 | ConsistencySubscriberFactory subscriberFactory, 71 | RabbitListenerEndpointRegistry rabbitListenerEndpointRegistry) { 72 | return new RabbitSubscriberRegistry(rabbitEventCodec, rabbitProperties, connectionFactory, subscriberFactory, rabbitListenerEndpointRegistry); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /eventbus-spring-boot-autoconfigure/src/main/java/me/ahoo/eventbus/spring/boot/autoconfigure/rabbit/ConditionalOnRabbitEnabled.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.spring.boot.autoconfigure.rabbit; 15 | 16 | import me.ahoo.eventbus.spring.boot.autoconfigure.EnabledSuffix; 17 | 18 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 19 | 20 | import java.lang.annotation.ElementType; 21 | import java.lang.annotation.Retention; 22 | import java.lang.annotation.RetentionPolicy; 23 | import java.lang.annotation.Target; 24 | 25 | /** 26 | * ConditionalOnRabbitEnabled. 27 | * 28 | * @author ahoo wang 29 | */ 30 | @Retention(RetentionPolicy.RUNTIME) 31 | @Target({ElementType.TYPE, ElementType.METHOD}) 32 | @ConditionalOnProperty(value = RabbitProperties.PREFIX + EnabledSuffix.SUFFIX, havingValue = "true", matchIfMissing = true) 33 | public @interface ConditionalOnRabbitEnabled { 34 | 35 | } 36 | -------------------------------------------------------------------------------- /eventbus-spring-boot-autoconfigure/src/main/java/me/ahoo/eventbus/spring/boot/autoconfigure/rabbit/RabbitProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.spring.boot.autoconfigure.rabbit; 15 | 16 | import me.ahoo.eventbus.rabbit.config.RabbitConfig; 17 | import me.ahoo.eventbus.spring.boot.autoconfigure.EventBusProperties; 18 | 19 | import org.springframework.boot.context.properties.ConfigurationProperties; 20 | 21 | /** 22 | * RabbitProperties. 23 | * 24 | * @author ahoo wang 25 | */ 26 | @ConfigurationProperties(RabbitProperties.PREFIX) 27 | public class RabbitProperties extends RabbitConfig { 28 | public static final String PREFIX = EventBusProperties.PREFIX + ".rabbit"; 29 | private boolean enabled = true; 30 | 31 | public boolean isEnabled() { 32 | return enabled; 33 | } 34 | 35 | public void setEnabled(boolean enabled) { 36 | this.enabled = enabled; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /eventbus-spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | # Auto Configure 2 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 3 | me.ahoo.eventbus.spring.boot.autoconfigure.EventBusAutoConfiguration,\ 4 | me.ahoo.eventbus.spring.boot.autoconfigure.kafka.BusKafkaAutoConfiguration,\ 5 | me.ahoo.eventbus.spring.boot.autoconfigure.rabbit.BusRabbitAutoConfiguration,\ 6 | me.ahoo.eventbus.spring.boot.autoconfigure.compensate.CompensateAutoConfiguration 7 | -------------------------------------------------------------------------------- /eventbus-spring-boot-starter/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | dependencies { 15 | api(project(":eventbus-spring-boot-autoconfigure")) 16 | } 17 | 18 | description = "eventbus-spring-boot-starter" 19 | -------------------------------------------------------------------------------- /eventbus-spring-context/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | dependencies { 15 | implementation(project(":eventbus-core")) 16 | implementation("org.springframework:spring-context") 17 | implementation("org.aspectj:aspectjweaver") 18 | } 19 | 20 | description = "eventbus-spring-context" 21 | -------------------------------------------------------------------------------- /eventbus-spring-context/src/main/java/me/ahoo/eventbus/spring/annotation/EnableEventBus.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.spring.annotation; 15 | 16 | import me.ahoo.eventbus.spring.support.EventBusConfigurationSelector; 17 | 18 | import org.springframework.context.annotation.Import; 19 | 20 | import java.lang.annotation.Documented; 21 | import java.lang.annotation.ElementType; 22 | import java.lang.annotation.Retention; 23 | import java.lang.annotation.RetentionPolicy; 24 | import java.lang.annotation.Target; 25 | 26 | /** 27 | * EnableEventBus. 28 | * 29 | * @author : ahoo wang 30 | * @see EventBusConfigurationSelector 31 | */ 32 | @Documented 33 | @Retention(RetentionPolicy.RUNTIME) 34 | @Target(ElementType.TYPE) 35 | @Import(EventBusConfigurationSelector.class) 36 | public @interface EnableEventBus { 37 | } 38 | -------------------------------------------------------------------------------- /eventbus-spring-context/src/main/java/me/ahoo/eventbus/spring/support/EventBusBootstrapConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.spring.support; 15 | 16 | import org.springframework.beans.factory.support.BeanDefinitionRegistry; 17 | import org.springframework.beans.factory.support.RootBeanDefinition; 18 | import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; 19 | import org.springframework.core.type.AnnotationMetadata; 20 | 21 | /** 22 | * EventBusBootstrapConfiguration. 23 | * 24 | * @author : ahoo wang 25 | */ 26 | public class EventBusBootstrapConfiguration implements ImportBeanDefinitionRegistrar { 27 | @Override 28 | public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { 29 | 30 | if (!registry.containsBeanDefinition(PublishAnnotationAspect.BEAN_NAME)) { 31 | registry.registerBeanDefinition(PublishAnnotationAspect.BEAN_NAME, 32 | new RootBeanDefinition(PublishAnnotationAspect.class)); 33 | } 34 | 35 | if (!registry.containsBeanDefinition(SubscriberLifecycle.BEAN_NAME)) { 36 | registry.registerBeanDefinition(SubscriberLifecycle.BEAN_NAME, 37 | new RootBeanDefinition(SubscriberLifecycle.class)); 38 | } 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /eventbus-spring-context/src/main/java/me/ahoo/eventbus/spring/support/EventBusConfigurationSelector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.spring.support; 15 | 16 | import me.ahoo.eventbus.spring.annotation.EnableEventBus; 17 | 18 | import org.springframework.context.annotation.DeferredImportSelector; 19 | import org.springframework.core.type.AnnotationMetadata; 20 | 21 | /** 22 | * EventBusConfigurationSelector. 23 | * 24 | * @author : ahoo wang 25 | * @see EnableEventBus 26 | */ 27 | public class EventBusConfigurationSelector implements DeferredImportSelector { 28 | @Override 29 | public String[] selectImports(AnnotationMetadata importingClassMetadata) { 30 | return new String[] 31 | { 32 | EventBusBootstrapConfiguration.class.getName() 33 | }; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /eventbus-spring-context/src/main/java/me/ahoo/eventbus/spring/support/PublishAnnotationAspect.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.spring.support; 15 | 16 | import me.ahoo.eventbus.core.EventBusException; 17 | import me.ahoo.eventbus.core.consistency.ConsistencyPublisher; 18 | 19 | import org.aspectj.lang.ProceedingJoinPoint; 20 | import org.aspectj.lang.annotation.Around; 21 | import org.aspectj.lang.annotation.Aspect; 22 | import org.aspectj.lang.annotation.Pointcut; 23 | 24 | /** 25 | * PublishAnnotationAspect. 26 | * 27 | * @author : ahoo wang 28 | */ 29 | @Aspect 30 | public class PublishAnnotationAspect { 31 | 32 | public static final String BEAN_NAME = PublishAnnotationAspect.class.getName(); 33 | private final ConsistencyPublisher consistencyPublisher; 34 | 35 | public PublishAnnotationAspect(ConsistencyPublisher consistencyPublisher) { 36 | this.consistencyPublisher = consistencyPublisher; 37 | } 38 | 39 | @Pointcut(value = "@annotation(me.ahoo.eventbus.core.annotation.Publish)") 40 | public void publish() { 41 | 42 | } 43 | 44 | 45 | @Around("publish()") 46 | public Object publishConsistency(ProceedingJoinPoint proceedingJoinPoint) { 47 | return consistencyPublisher.publish(() -> { 48 | try { 49 | return proceedingJoinPoint.proceed(); 50 | } catch (Throwable throwable) { 51 | throw new EventBusException(throwable.getMessage(), throwable); 52 | } 53 | }); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /eventbus-spring-context/src/main/java/me/ahoo/eventbus/spring/support/SubscriberLifecycle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | package me.ahoo.eventbus.spring.support; 15 | 16 | import me.ahoo.eventbus.core.subscriber.Subscriber; 17 | import me.ahoo.eventbus.core.subscriber.SubscriberRegistry; 18 | import me.ahoo.eventbus.core.subscriber.SubscriberScanner; 19 | 20 | import org.springframework.context.ApplicationContext; 21 | import org.springframework.context.SmartLifecycle; 22 | 23 | import java.util.List; 24 | import java.util.concurrent.ConcurrentHashMap; 25 | import java.util.concurrent.atomic.AtomicBoolean; 26 | 27 | /** 28 | * SubscriberLifecycle. 29 | * 30 | * @author ahoo wang 31 | */ 32 | public class SubscriberLifecycle implements SmartLifecycle { 33 | 34 | public static final String BEAN_NAME = SubscriberLifecycle.class.getName(); 35 | private final ApplicationContext context; 36 | private static final AtomicBoolean running = new AtomicBoolean(false); 37 | private final ConcurrentHashMap registeredBeans; 38 | 39 | public SubscriberLifecycle(ApplicationContext applicationContext) { 40 | this.context = applicationContext; 41 | this.registeredBeans = new ConcurrentHashMap<>(); 42 | } 43 | 44 | @Override 45 | public void start() { 46 | if (!running.compareAndSet(false, true)) { 47 | return; 48 | } 49 | 50 | for (String beanName : context.getBeanDefinitionNames()) { 51 | Object bean = context.getBean(beanName); 52 | register(bean, beanName); 53 | } 54 | } 55 | 56 | private void register(Object bean, String beanName) { 57 | final SubscriberScanner subscriberScanner = context.getBean(SubscriberScanner.class); 58 | List list = subscriberScanner.scan(bean); 59 | if (list.isEmpty()) { 60 | return; 61 | } 62 | final SubscriberRegistry subscriberRegistry = context.getBean(SubscriberRegistry.class); 63 | registeredBeans.computeIfAbsent(beanName, name -> { 64 | list.forEach(subscriberRegistry::subscribe); 65 | return bean; 66 | }); 67 | } 68 | 69 | @Override 70 | public void stop() { 71 | if (!running.compareAndSet(true, false)) { 72 | return; 73 | } 74 | } 75 | 76 | @Override 77 | public boolean isRunning() { 78 | return running.get(); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # http://www.apache.org/licenses/LICENSE-2.0 7 | # Unless required by applicable law or agreed to in writing, software 8 | # distributed under the License is distributed on an "AS IS" BASIS, 9 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | # See the License for the specific language governing permissions and 11 | # limitations under the License. 12 | # 13 | 14 | group=me.ahoo.eventbus 15 | version=1.0.8 16 | description=Govern Service RPC & Event-Driven Architecture Framework 17 | website=https://github.com/Ahoo-Wang/govern-eventbus 18 | issues=https://github.com/Ahoo-Wang/govern-eventbus/issues 19 | vcs=https://github.com/Ahoo-Wang/govern-eventbus.git 20 | license_name=The Apache Software License, Version 2.0 21 | license_url=https://www.apache.org/licenses/LICENSE-2.0.txt 22 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ahoo-Wang/govern-eventbus/343878f2666b50d3a48c15f7fc7217554bb55763/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # http://www.apache.org/licenses/LICENSE-2.0 7 | # Unless required by applicable law or agreed to in writing, software 8 | # distributed under the License is distributed on an "AS IS" BASIS, 9 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | # See the License for the specific language governing permissions and 11 | # limitations under the License. 12 | # 13 | 14 | distributionBase=GRADLE_USER_HOME 15 | distributionPath=wrapper/dists 16 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip 17 | zipStoreBase=GRADLE_USER_HOME 18 | zipStorePath=wrapper/dists 19 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "config:base" 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /settings.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright [2021-2021] [ahoo wang (https://github.com/Ahoo-Wang)]. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | rootProject.name = "govern-eventbus" 15 | include(":eventbus-rabbit") 16 | include(":eventbus-spring-boot-starter") 17 | include(":eventbus-spring-context") 18 | include(":eventbus-kafka") 19 | include(":eventbus-demo") 20 | include(":eventbus-spring-boot-autoconfigure") 21 | include(":eventbus-core") 22 | include(":eventbus-jdbc") 23 | include(":eventbus-bom") 24 | include(":eventbus-dependencies") 25 | 26 | buildscript { 27 | repositories { 28 | gradlePluginPortal() 29 | } 30 | dependencies { 31 | classpath("me.champeau.jmh:jmh-gradle-plugin:0.6.8") 32 | classpath("io.github.gradle-nexus:publish-plugin:1.1.0") 33 | classpath("com.github.spotbugs.snom:spotbugs-gradle-plugin:5.0.12") 34 | } 35 | } 36 | --------------------------------------------------------------------------------