├── static └── img │ ├── img.png │ ├── logo.png │ └── wx.jpg ├── mq-idempotent-core ├── src │ ├── main │ │ ├── resources │ │ │ └── META-INF │ │ │ │ ├── wh │ │ │ │ └── com.mq.idempotent.core.alert.strategy.AlertStrategy │ │ │ │ └── spring.factories │ │ └── java │ │ │ └── com │ │ │ └── mq │ │ │ └── idempotent │ │ │ └── core │ │ │ ├── constants │ │ │ └── RedisConstants.java │ │ │ ├── utils │ │ │ ├── Matcher.java │ │ │ ├── Func0.java │ │ │ ├── ThreadFactoryImpl.java │ │ │ ├── TransactionUtil.java │ │ │ ├── ReflectUtil.java │ │ │ ├── SimpleCache.java │ │ │ └── ArrayUtil.java │ │ │ ├── exception │ │ │ └── MessageConcurrencyException.java │ │ │ ├── alert │ │ │ ├── strategy │ │ │ │ ├── AlertStrategy.java │ │ │ │ ├── AlertProperties.java │ │ │ │ ├── AlertStrategyFactory.java │ │ │ │ └── LarkAlarmStrategy.java │ │ │ └── AlertDTO.java │ │ │ ├── strategy │ │ │ ├── IdempotentStrategy.java │ │ │ ├── impl │ │ │ │ ├── JDBCIdempotentStrategy.java │ │ │ │ └── RedisIdempotentStrategy.java │ │ │ └── AbstractIdempotentStrategy.java │ │ │ ├── spi │ │ │ ├── ExtensionFactory.java │ │ │ ├── Join.java │ │ │ ├── SpiExtensionFactory.java │ │ │ ├── SPI.java │ │ │ └── ExtensionLoader.java │ │ │ ├── aop │ │ │ ├── MessageConverter.java │ │ │ ├── MqIdempotentAnnotationInterceptor.java │ │ │ └── MqIdempotentAnnotationAdvisor.java │ │ │ ├── annotation │ │ │ └── Idempotent.java │ │ │ ├── config │ │ │ └── IdempotentProperties.java │ │ │ ├── model │ │ │ └── IdempotentConfig.java │ │ │ └── autoconfigure │ │ │ └── MqIdempotentAutoConfiguration.java │ └── test │ │ ├── resources │ │ └── META-INF │ │ │ └── wh │ │ │ └── com.mq.idempotent.core.alert.strategy.AlertStrategy │ │ └── java │ │ └── com │ │ └── mq │ │ └── idempotent │ │ └── core │ │ └── alert │ │ └── strategy │ │ ├── AlertStrategyFactoryTest.java │ │ └── LarkAlarmStrategyTests.java └── pom.xml ├── mq-idempotent-spring-boot-starter ├── mq-idempotent-spring-boot-starter-rabbitmq │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ └── META-INF │ │ │ │ └── spring.factories │ │ │ └── java │ │ │ └── com │ │ │ └── mq │ │ │ └── idempotent │ │ │ └── rabbitmq │ │ │ └── converter │ │ │ └── RabbitMQConverter.java │ └── pom.xml ├── mq-idempotent-spring-boot-starter-rocketmq │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ └── META-INF │ │ │ │ └── spring.factories │ │ │ └── java │ │ │ └── com │ │ │ └── mq │ │ │ └── idempotent │ │ │ └── rocketmq │ │ │ └── converter │ │ │ └── RocketMQMessageConverter.java │ └── pom.xml ├── mq-idempotent-spring-boot-starter-aliyun-rocketmq │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ └── META-INF │ │ │ │ └── spring.factories │ │ │ └── java │ │ │ └── com │ │ │ └── mq │ │ │ └── idempotent │ │ │ └── aliyun │ │ │ └── rocketmq │ │ │ └── converter │ │ │ └── AliYunRocketMQMessageConverter.java │ └── pom.xml └── pom.xml ├── mq-idempotent-samples ├── springboot-aliyun-rocketmq │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ ├── test.sql │ │ │ ├── mybatis │ │ │ │ └── TestMapper.xml │ │ │ └── application.yml │ │ │ └── java │ │ │ └── com │ │ │ └── samples │ │ │ ├── mapper │ │ │ └── TestMapper.java │ │ │ ├── Application.java │ │ │ ├── entity │ │ │ └── TestDO.java │ │ │ ├── config │ │ │ ├── RedisConfig.java │ │ │ ├── AliyunMQConfig.java │ │ │ └── MybatisPlusConfig.java │ │ │ └── consumer │ │ │ └── MessageEventHandler.java │ └── pom.xml ├── springboot-rabbitmq │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ └── application.yml │ │ │ └── java │ │ │ └── com │ │ │ └── rabbitmq │ │ │ ├── message │ │ │ └── TestMessage.java │ │ │ ├── Application.java │ │ │ ├── constants │ │ │ └── RabbitMQConstants.java │ │ │ ├── consumer │ │ │ └── RabbitMQConsumer.java │ │ │ ├── config │ │ │ ├── DirectRabbitConfig.java │ │ │ └── RedisConfig.java │ │ │ └── controller │ │ │ └── SendMessageController.java │ └── pom.xml ├── springboot-rocketmq │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ └── application.yml │ │ │ └── java │ │ │ └── com │ │ │ └── samples │ │ │ ├── Application.java │ │ │ ├── consumer │ │ │ └── MessageEventHandler.java │ │ │ ├── config │ │ │ ├── RedisConfig.java │ │ │ └── MQConfig.java │ │ │ └── controller │ │ │ └── SendMessageController.java │ └── pom.xml └── pom.xml ├── dev-support ├── license-header └── spotless_mq-idempotent_formatter.xml ├── .gitignore ├── README.md ├── README_en.md ├── pom.xml ├── mq-idempotent-dependencies └── pom.xml └── LICENSE /static/img/img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/weihubeats/mq-idempotent/HEAD/static/img/img.png -------------------------------------------------------------------------------- /static/img/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/weihubeats/mq-idempotent/HEAD/static/img/logo.png -------------------------------------------------------------------------------- /static/img/wx.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/weihubeats/mq-idempotent/HEAD/static/img/wx.jpg -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/resources/META-INF/wh/com.mq.idempotent.core.alert.strategy.AlertStrategy: -------------------------------------------------------------------------------- 1 | lark = com.mq.idempotent.core.alert.strategy.LarkAlarmStrategy -------------------------------------------------------------------------------- /mq-idempotent-core/src/test/resources/META-INF/wh/com.mq.idempotent.core.alert.strategy.AlertStrategy: -------------------------------------------------------------------------------- 1 | lark = com.mq.idempotent.core.alert.strategy.LarkAlarmStrategy -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 2 | com.mq.idempotent.core.autoconfigure.MqIdempotentAutoConfiguration 3 | -------------------------------------------------------------------------------- /mq-idempotent-spring-boot-starter/mq-idempotent-spring-boot-starter-rabbitmq/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 2 | com.mq.idempotent.rabbitmq.converter.RabbitMQConverter 3 | -------------------------------------------------------------------------------- /mq-idempotent-spring-boot-starter/mq-idempotent-spring-boot-starter-rocketmq/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 2 | com.mq.idempotent.rocketmq.converter.RocketMQMessageConverter 3 | -------------------------------------------------------------------------------- /mq-idempotent-samples/springboot-aliyun-rocketmq/src/main/resources/test.sql: -------------------------------------------------------------------------------- 1 | create table test 2 | ( 3 | id bigserial 4 | constraint test_pk 5 | primary key, 6 | msg text, 7 | add_time timestamp default CURRENT_TIMESTAMP not null 8 | ); -------------------------------------------------------------------------------- /mq-idempotent-samples/springboot-rabbitmq/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8080 3 | spring: 4 | application: 5 | name: rabbitmq 6 | rabbitmq: 7 | host: 192.168.111.117 8 | port: 5672 9 | username: guest 10 | password: guest -------------------------------------------------------------------------------- /mq-idempotent-spring-boot-starter/mq-idempotent-spring-boot-starter-aliyun-rocketmq/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 2 | com.mq.idempotent.aliyun.rocketmq.converter.AliYunRocketMQMessageConverter 3 | -------------------------------------------------------------------------------- /mq-idempotent-samples/springboot-rocketmq/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: rocketmq 4 | 5 | server: 6 | port: 9001 7 | 8 | 9 | 10 | idempotent: 11 | redisKey: "mq::unique::" 12 | redisValue : "ss" 13 | tryLockTime: 2 14 | redisTimeOut: 3 15 | -------------------------------------------------------------------------------- /mq-idempotent-samples/springboot-aliyun-rocketmq/src/main/resources/mybatis/TestMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /mq-idempotent-samples/springboot-aliyun-rocketmq/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: idempotent 4 | profiles: 5 | active: dev 6 | 7 | db: 8 | pg: 9 | connectionTestQuery: select 1 10 | jdbcUrl: 127.0.0.1:1921/plutus 11 | password: test 12 | username: 123456 13 | 14 | -------------------------------------------------------------------------------- /dev-support/license-header: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | -------------------------------------------------------------------------------- /mq-idempotent-spring-boot-starter/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 4.0.0 6 | 7 | io.github.weihubeats 8 | mq-idempotent 9 | ${revision} 10 | ../pom.xml 11 | 12 | 13 | mq-idempotent-spring-boot-starter 14 | pom 15 | 16 | 17 | mq-idempotent-spring-boot-starter-rocketmq 18 | mq-idempotent-spring-boot-starter-aliyun-rocketmq 19 | mq-idempotent-spring-boot-starter-rabbitmq 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /mq-idempotent-samples/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 4.0.0 6 | 7 | io.github.weihubeats 8 | mq-idempotent 9 | ${revision} 10 | ../pom.xml 11 | 12 | 13 | mq-idempotent-samples 14 | pom 15 | 16 | 17 | springboot-aliyun-rocketmq 18 | springboot-rocketmq 19 | springboot-rabbitmq 20 | 21 | 22 | 23 | true 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /mq-idempotent-spring-boot-starter/mq-idempotent-spring-boot-starter-rocketmq/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | io.github.weihubeats 7 | mq-idempotent-spring-boot-starter 8 | ${revision} 9 | ../pom.xml 10 | 11 | 12 | mq-idempotent-spring-boot-starter-rocketmq 13 | 14 | 15 | 16 | org.apache.rocketmq 17 | rocketmq-client 18 | 19 | 20 | io.github.weihubeats 21 | mq-idempotent-core 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /mq-idempotent-spring-boot-starter/mq-idempotent-spring-boot-starter-rabbitmq/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | io.github.weihubeats 7 | mq-idempotent-spring-boot-starter 8 | ${revision} 9 | ../pom.xml 10 | 11 | 12 | mq-idempotent-spring-boot-starter-rabbitmq 13 | 14 | 15 | 11 16 | 11 17 | 18 | 19 | 20 | 21 | io.github.weihubeats 22 | mq-idempotent-core 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /mq-idempotent-spring-boot-starter/mq-idempotent-spring-boot-starter-aliyun-rocketmq/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | io.github.weihubeats 7 | mq-idempotent-spring-boot-starter 8 | ${revision} 9 | ../pom.xml 10 | 11 | 12 | mq-idempotent-spring-boot-starter-aliyun-rocketmq 13 | 14 | 15 | 16 | com.aliyun.openservices 17 | ons-client 18 | 19 | 20 | io.github.weihubeats 21 | mq-idempotent-core 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/constants/RedisConstants.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.constants; 19 | 20 | /** 21 | * @author : wh 22 | * @date : 2021/11/4 16:06 23 | * @description: redis常量 24 | */ 25 | public interface RedisConstants { 26 | 27 | String KEY = "mq:unique:"; 28 | 29 | } 30 | -------------------------------------------------------------------------------- /mq-idempotent-samples/springboot-rocketmq/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | io.github.weihubeats 7 | mq-idempotent-samples 8 | ${revision} 9 | 10 | 11 | springboot-rocketmq 12 | 13 | 14 | 8 15 | 8 16 | 17 | 18 | 19 | 20 | io.github.weihubeats 21 | mq-idempotent-spring-boot-starter-rocketmq 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled source # 2 | ################### 3 | *.com 4 | *.class 5 | *.dll 6 | *.exe 7 | *.o 8 | *.so 9 | 10 | 11 | # Packages # 12 | ############ 13 | # it's better to unpack these files and commit the raw source 14 | # git has its own built in compression methods 15 | *.7z 16 | *.dmg 17 | *.gz 18 | *.iso 19 | *.jar 20 | *.rar 21 | *.tar 22 | *.zip 23 | *.war 24 | *.del 25 | *.pmd 26 | .tern-project 27 | 28 | 29 | # Logs and databases # 30 | ###################### 31 | *.log 32 | *.log.* 33 | # OS generated files # 34 | ###################### 35 | .DS_Store* 36 | ehthumbs.db 37 | Icon? 38 | Thumbs.db 39 | 40 | 41 | # Editor Files # 42 | ################ 43 | *~ 44 | *.swp 45 | 46 | 47 | # Gradle Files # 48 | ################ 49 | .gradle 50 | 51 | 52 | # Build output directies 53 | /target 54 | */target 55 | /build 56 | */build 57 | 58 | 59 | # IntelliJ specific files/directories 60 | out 61 | .idea 62 | *.ipr 63 | *.iws 64 | *.iml 65 | atlassian-ide-plugin.xml 66 | 67 | 68 | # Eclipse specific files/directories 69 | .classpath 70 | .project 71 | .settings 72 | .metadata 73 | .myeclipse 74 | 75 | 76 | # NetBeans specific files/directories 77 | .nbattrs 78 | 79 | *.mymetadata 80 | /logs 81 | */logs 82 | /cache 83 | */cache 84 | 85 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/utils/Matcher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.utils; 19 | 20 | /** 21 | * @author : wh 22 | * @date : 2022/1/12 11:52 23 | * @description: 24 | */ 25 | @FunctionalInterface 26 | public interface Matcher { 27 | 28 | /** 29 | * 给定对象是否匹配 30 | * 31 | * @param t 对象 32 | * @return 是否匹配 33 | */ 34 | boolean match(T t); 35 | } 36 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/exception/MessageConcurrencyException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.exception; 19 | 20 | /** 21 | * @author : wh 22 | * @date : 2022/3/12 17:50 23 | * @description: 24 | */ 25 | public class MessageConcurrencyException extends RuntimeException { 26 | 27 | public MessageConcurrencyException(String message) { 28 | super(message); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /mq-idempotent-samples/springboot-rabbitmq/src/main/java/com/rabbitmq/message/TestMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.rabbitmq.message; 19 | 20 | import lombok.Data; 21 | 22 | import java.io.Serializable; 23 | 24 | /** 25 | * @author : wh 26 | * @date : 2022/1/12 13:44 27 | * @description: 28 | */ 29 | @Data 30 | public class TestMessage implements Serializable { 31 | 32 | private Integer id; 33 | 34 | private String msg; 35 | } 36 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/alert/strategy/AlertStrategy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.alert.strategy; 19 | 20 | import com.mq.idempotent.core.alert.AlertDTO; 21 | import com.mq.idempotent.core.spi.SPI; 22 | 23 | /** 24 | * @author : wh 25 | * @date : 2022/6/15 18:04 26 | * @description: 27 | */ 28 | @SPI 29 | public interface AlertStrategy { 30 | 31 | boolean sendMsg(AlertDTO alertDTO); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /mq-idempotent-samples/springboot-aliyun-rocketmq/src/main/java/com/samples/mapper/TestMapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.samples.mapper; 19 | 20 | import com.baomidou.mybatisplus.core.mapper.BaseMapper; 21 | import com.samples.entity.TestDO; 22 | import org.apache.ibatis.annotations.Mapper; 23 | 24 | /** 25 | *@author : wh 26 | *@date : 2022/7/20 14:27 27 | *@description: 28 | */ 29 | @Mapper 30 | public interface TestMapper extends BaseMapper { 31 | } 32 | -------------------------------------------------------------------------------- /mq-idempotent-samples/springboot-rabbitmq/src/main/java/com/rabbitmq/Application.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.rabbitmq; 19 | 20 | import org.springframework.boot.SpringApplication; 21 | import org.springframework.boot.autoconfigure.SpringBootApplication; 22 | 23 | @SpringBootApplication 24 | public class Application { 25 | 26 | public static void main(String[] args) { 27 | SpringApplication.run(Application.class, args); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /mq-idempotent-samples/springboot-rocketmq/src/main/java/com/samples/Application.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.samples; 19 | 20 | import org.springframework.boot.SpringApplication; 21 | import org.springframework.boot.autoconfigure.SpringBootApplication; 22 | 23 | @SpringBootApplication 24 | public class Application { 25 | 26 | public static void main(String[] args) { 27 | SpringApplication.run(Application.class, args); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /mq-idempotent-samples/springboot-aliyun-rocketmq/src/main/java/com/samples/Application.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.samples; 19 | 20 | import org.springframework.boot.SpringApplication; 21 | import org.springframework.boot.autoconfigure.SpringBootApplication; 22 | 23 | @SpringBootApplication 24 | public class Application { 25 | 26 | public static void main(String[] args) { 27 | SpringApplication.run(Application.class, args); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /mq-idempotent-samples/springboot-rabbitmq/src/main/java/com/rabbitmq/constants/RabbitMQConstants.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.rabbitmq.constants; 19 | 20 | /** 21 | * @author : wh 22 | * @date : 2022/1/11 19:45 23 | * @description: 24 | */ 25 | public interface RabbitMQConstants { 26 | 27 | String QUEUE_NAME = "TestQueue"; 28 | 29 | String TEST_DIRECT_EXCHANGE = "TestDirectExchange"; 30 | 31 | String ROUTING_KEY = "TestDirectRouting"; 32 | } 33 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/strategy/IdempotentStrategy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.strategy; 19 | 20 | /** 21 | * @author : wh 22 | * @date : 2022/3/12 18:03 23 | * @description: 24 | */ 25 | public interface IdempotentStrategy { 26 | 27 | boolean lock(String lockName); 28 | 29 | void save(String uniqueKey); 30 | 31 | void unlock(String lockName); 32 | 33 | boolean exitKey(String key); 34 | 35 | } 36 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/alert/strategy/AlertProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.alert.strategy; 19 | 20 | import lombok.Data; 21 | 22 | /** 23 | * @author : wh 24 | * @date : 2022/6/16 17:12 25 | * @description: 26 | */ 27 | @Data 28 | public class AlertProperties { 29 | 30 | private String webHook; 31 | 32 | private String template; 33 | 34 | /** 35 | * 是否@所有人 36 | */ 37 | private boolean atAll; 38 | 39 | } 40 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/alert/strategy/AlertStrategyFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.alert.strategy; 19 | 20 | import com.mq.idempotent.core.spi.ExtensionLoader; 21 | 22 | /** 23 | * @author : wh 24 | * @date : 2022/6/15 18:07 25 | * @description: 26 | */ 27 | public class AlertStrategyFactory { 28 | 29 | public static AlertStrategy newInstance(final String alertStrategyName) { 30 | return ExtensionLoader.getExtensionLoader(AlertStrategy.class).getJoin(alertStrategyName); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/spi/ExtensionFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.spi; 19 | 20 | /** 21 | * @author : wh 22 | * @date : 2022/6/15 15:52 23 | * @description: 24 | */ 25 | @SPI("spi") 26 | public interface ExtensionFactory { 27 | 28 | /** 29 | * Gets Extension. 30 | * 31 | * @param the type parameter 32 | * @param key the key 33 | * @param clazz the clazz 34 | * @return the extension 35 | */ 36 | T getExtension(String key, Class clazz); 37 | } 38 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/spi/Join.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.spi; 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 | * Join 28 | * Adding this annotation to a class indicates joining the extension mechanism. 29 | * 30 | */ 31 | @Documented 32 | @Retention(RetentionPolicy.RUNTIME) 33 | @Target(ElementType.TYPE) 34 | public @interface Join { 35 | } 36 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/aop/MessageConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.aop; 19 | 20 | import java.lang.reflect.Method; 21 | 22 | /** 23 | * @author : wh 24 | * @date : 2021/11/15 13:51 25 | * @description: 消息转换器 26 | */ 27 | public interface MessageConverter { 28 | 29 | /** 30 | * 31 | * @param t 消息 32 | * @param field 注解 Idempotent 中的 field值 33 | * @param method 被拦截方法 34 | * @param args 被拦截方法参数 35 | * @return 36 | */ 37 | String getUniqueKey(T t, String field, Method method, Object[] args); 38 | 39 | } 40 | -------------------------------------------------------------------------------- /mq-idempotent-samples/springboot-aliyun-rocketmq/src/main/java/com/samples/entity/TestDO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.samples.entity; 19 | 20 | import com.baomidou.mybatisplus.annotation.IdType; 21 | import com.baomidou.mybatisplus.annotation.TableId; 22 | import com.baomidou.mybatisplus.annotation.TableName; 23 | import lombok.Data; 24 | 25 | /** 26 | *@author : wh 27 | *@date : 2022/7/20 14:26 28 | *@description: 29 | */ 30 | @TableName("test") 31 | @Data 32 | public class TestDO { 33 | 34 | @TableId(type = IdType.AUTO) 35 | private Long id; 36 | 37 | private String msg; 38 | 39 | } 40 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/test/java/com/mq/idempotent/core/alert/strategy/AlertStrategyFactoryTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.alert.strategy; 19 | 20 | import org.junit.jupiter.api.Test; 21 | 22 | import static org.junit.jupiter.api.Assertions.assertTrue; 23 | 24 | /** 25 | * @author : wh 26 | * @date : 2022/6/15 18:17 27 | * @description: 28 | */ 29 | public class AlertStrategyFactoryTest { 30 | 31 | @Test 32 | public void test() { 33 | AlertStrategy lark = AlertStrategyFactory.newInstance("lark"); 34 | assertTrue(lark instanceof LarkAlarmStrategy); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /mq-idempotent-samples/springboot-rabbitmq/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | io.github.weihubeats 7 | mq-idempotent-samples 8 | ${revision} 9 | 10 | 11 | springboot-rabbitmq 12 | 13 | 14 | 8 15 | 8 16 | 17 | 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter-amqp 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | com.alibaba 29 | fastjson 30 | 1.2.83 31 | 32 | 33 | 34 | io.github.weihubeats 35 | mq-idempotent-spring-boot-starter-rabbitmq 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /mq-idempotent-samples/springboot-aliyun-rocketmq/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 4.0.0 6 | 7 | io.github.weihubeats 8 | mq-idempotent-samples 9 | ${revision} 10 | 11 | 12 | springboot-aliyun-rocketmq 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter-web 18 | true 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter 23 | 24 | 25 | 26 | io.github.weihubeats 27 | mq-idempotent-spring-boot-starter-aliyun-rocketmq 28 | 29 | 30 | 31 | org.postgresql 32 | postgresql 33 | 34 | 35 | 36 | com.baomidou 37 | mybatis-plus-boot-starter 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/annotation/Idempotent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.annotation; 19 | 20 | import java.lang.annotation.*; 21 | 22 | /** 23 | * @author : wh 24 | * @date : 2021/11/12 11:27 25 | * @description: 26 | */ 27 | @Target({ElementType.METHOD}) 28 | @Retention(RetentionPolicy.RUNTIME) 29 | @Documented 30 | public @interface Idempotent { 31 | 32 | /** 33 | * idempotent property 34 | * @return 35 | */ 36 | String field() default "msgId"; 37 | 38 | /** 39 | * 是否开启并发锁控制 40 | * @return 41 | */ 42 | boolean lock() default true; 43 | 44 | /** 45 | * 是否开启事务 46 | * @return 47 | */ 48 | boolean transactional() default false; 49 | 50 | } 51 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/alert/AlertDTO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.alert; 19 | 20 | import java.lang.reflect.Method; 21 | 22 | import lombok.AllArgsConstructor; 23 | import lombok.Data; 24 | import lombok.NoArgsConstructor; 25 | 26 | /** 27 | *@author : wh 28 | *@date : 2022/7/19 14:27 29 | *@description: 30 | */ 31 | @Data 32 | @AllArgsConstructor 33 | @NoArgsConstructor 34 | public class AlertDTO { 35 | 36 | /** 37 | * 唯一key 38 | */ 39 | private String key; 40 | 41 | /** 42 | * 方法 43 | */ 44 | private Method method; 45 | 46 | /** 47 | * 异常 48 | */ 49 | private Throwable throwable; 50 | 51 | /** 52 | * 报警url 53 | */ 54 | private String webHook; 55 | 56 | } 57 | -------------------------------------------------------------------------------- /mq-idempotent-spring-boot-starter/mq-idempotent-spring-boot-starter-rabbitmq/src/main/java/com/mq/idempotent/rabbitmq/converter/RabbitMQConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.rabbitmq.converter; 19 | 20 | import java.lang.reflect.Method; 21 | 22 | import com.mq.idempotent.core.aop.MessageConverter; 23 | import com.mq.idempotent.core.utils.ReflectUtil; 24 | 25 | /** 26 | * @author : wh 27 | * @date : 2022/1/12 11:11 28 | * @description: 29 | */ 30 | public class RabbitMQConverter implements MessageConverter { 31 | 32 | @Override 33 | public String getUniqueKey(Object o, String field, Method method, Object[] args) { 34 | try { 35 | return ReflectUtil.getFieldValue(o, field).toString(); 36 | } catch (Exception e) { 37 | throw new RuntimeException(e); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/spi/SpiExtensionFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.spi; 19 | 20 | import com.mq.idempotent.core.alert.strategy.AlertStrategy; 21 | import com.mq.idempotent.core.alert.strategy.LarkAlarmStrategy; 22 | 23 | import java.util.Optional; 24 | 25 | /** 26 | * @author : wh 27 | * @date : 2022/6/15 15:51 28 | * @description: 29 | */ 30 | @Join 31 | public class SpiExtensionFactory implements ExtensionFactory { 32 | 33 | @Override 34 | public T getExtension(final String key, final Class clazz) { 35 | return Optional.ofNullable(clazz) 36 | .filter(Class::isInterface) 37 | .filter(cls -> cls.isAnnotationPresent(SPI.class)) 38 | .map(ExtensionLoader::getExtensionLoader) 39 | .map(ExtensionLoader::getDefaultJoin) 40 | .orElse(null); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /mq-idempotent-spring-boot-starter/mq-idempotent-spring-boot-starter-rocketmq/src/main/java/com/mq/idempotent/rocketmq/converter/RocketMQMessageConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.rocketmq.converter; 19 | 20 | import java.lang.reflect.Method; 21 | 22 | import com.mq.idempotent.core.aop.MessageConverter; 23 | import org.apache.commons.lang3.StringUtils; 24 | import org.apache.rocketmq.common.message.MessageExt; 25 | 26 | import org.springframework.stereotype.Component; 27 | 28 | /** 29 | * @author : wh 30 | * @date : 2021/11/15 14:29 31 | * @description: 32 | */ 33 | @Component 34 | public class RocketMQMessageConverter implements MessageConverter { 35 | 36 | @Override 37 | public String getUniqueKey(MessageExt messageExt, String field, Method method, Object[] args) { 38 | return StringUtils.isNoneBlank(messageExt.getKeys()) ? messageExt.getKeys() : messageExt.getMsgId(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/utils/Func0.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.utils; 19 | 20 | import java.io.Serializable; 21 | 22 | /** 23 | * 无参数的函数对象
24 | * 接口灵感来自于ActFramework
25 | * 一个函数接口代表一个一个函数,用于包装一个函数为对象
26 | * 在JDK8之前,Java的函数并不能作为参数传递,也不能作为返回值存在,此接口用于将一个函数包装成为一个对象,从而传递对象 27 | * 28 | * @param 返回值类型 29 | * @author Looly 30 | */ 31 | @FunctionalInterface 32 | public interface Func0 extends Serializable { 33 | 34 | /** 35 | * 执行函数 36 | * 37 | * @return 函数执行结果 38 | * @throws Exception 自定义异常 39 | */ 40 | R call() throws Exception; 41 | 42 | /** 43 | * 执行函数,异常包装为RuntimeException 44 | * 45 | * @return 函数执行结果 46 | */ 47 | default R callWithRuntimeException() { 48 | try { 49 | return call(); 50 | } catch (Exception e) { 51 | throw new RuntimeException(e); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /mq-idempotent-spring-boot-starter/mq-idempotent-spring-boot-starter-aliyun-rocketmq/src/main/java/com/mq/idempotent/aliyun/rocketmq/converter/AliYunRocketMQMessageConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.aliyun.rocketmq.converter; 19 | 20 | import java.lang.reflect.Method; 21 | 22 | import com.aliyun.openservices.ons.api.Message; 23 | import com.mq.idempotent.core.aop.MessageConverter; 24 | 25 | import org.springframework.util.ObjectUtils; 26 | 27 | /** 28 | * @author : wh 29 | * @date : 2021/11/15 14:00 30 | * @description: 31 | */ 32 | public class AliYunRocketMQMessageConverter implements MessageConverter { 33 | 34 | @Override 35 | public String getUniqueKey(Message message, String field, Method method, Object[] args) { 36 | String messageKey = message.getKey(); 37 | return !ObjectUtils.isEmpty(messageKey) ? messageKey : message.getMsgID(); 38 | } 39 | 40 | public static void main(String[] args) { 41 | System.out.println(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/spi/SPI.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.spi; 19 | 20 | /** 21 | * @author : wh 22 | * @date : 2022/6/15 15:50 23 | * @description: 24 | */ 25 | 26 | import java.lang.annotation.Documented; 27 | import java.lang.annotation.ElementType; 28 | import java.lang.annotation.Retention; 29 | import java.lang.annotation.RetentionPolicy; 30 | import java.lang.annotation.Target; 31 | 32 | /** 33 | * SPI Extend the processing. 34 | * All spi system reference the apache implementation of 35 | * Apache Dubbo Common Extension. 36 | * 37 | * @see ExtensionFactory 38 | * @see ExtensionLoader 39 | */ 40 | @Documented 41 | @Retention(RetentionPolicy.RUNTIME) 42 | @Target(ElementType.TYPE) 43 | public @interface SPI { 44 | 45 | /** 46 | * Value string. 47 | * 48 | * @return the string 49 | */ 50 | String value() default ""; 51 | } 52 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/utils/ThreadFactoryImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.utils; 19 | 20 | import java.util.concurrent.ThreadFactory; 21 | import java.util.concurrent.atomic.AtomicLong; 22 | 23 | /** 24 | * @author : wh 25 | * @date : 2021/10/3 15:17 26 | * @description: 线程创建工厂 27 | */ 28 | public class ThreadFactoryImpl implements ThreadFactory { 29 | 30 | private final AtomicLong threadIndex = new AtomicLong(0); 31 | private final String threadNamePrefix; 32 | private final boolean daemon; 33 | 34 | public ThreadFactoryImpl(final String threadNamePrefix) { 35 | this(threadNamePrefix, false); 36 | } 37 | 38 | public ThreadFactoryImpl(final String threadNamePrefix, boolean daemon) { 39 | this.threadNamePrefix = threadNamePrefix; 40 | this.daemon = daemon; 41 | } 42 | 43 | @Override 44 | public Thread newThread(Runnable r) { 45 | Thread thread = new Thread(r, threadNamePrefix + this.threadIndex.incrementAndGet()); 46 | thread.setDaemon(daemon); 47 | return thread; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /mq-idempotent-samples/springboot-rabbitmq/src/main/java/com/rabbitmq/consumer/RabbitMQConsumer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.rabbitmq.consumer; 19 | 20 | import com.mq.idempotent.core.annotation.Idempotent; 21 | import com.rabbitmq.constants.RabbitMQConstants; 22 | import com.rabbitmq.message.TestMessage; 23 | import org.springframework.amqp.rabbit.annotation.RabbitHandler; 24 | import org.springframework.amqp.rabbit.annotation.RabbitListener; 25 | import org.springframework.stereotype.Component; 26 | 27 | /** 28 | * @author : wh 29 | * @date : 2022/1/11 19:43 30 | * @description: 31 | */ 32 | @Component 33 | public class RabbitMQConsumer { 34 | 35 | /* 36 | * @RabbitHandler 37 | * 38 | * @RabbitListener(queues = RabbitMQConstants.QUEUE_NAME) 39 | * 40 | * @Idempotent public void process(Map testMessage) { System.out.println("DirectReceiver消费者收到消息 : " + testMessage.toString()); } 41 | */ 42 | 43 | @RabbitHandler 44 | @RabbitListener(queues = RabbitMQConstants.QUEUE_NAME) 45 | @Idempotent(field = "id") 46 | public void consumerTestMessage(TestMessage testMessage) { 47 | System.out.println("DirectReceiver消费者收到消息 : " + testMessage.toString()); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /mq-idempotent-samples/springboot-rocketmq/src/main/java/com/samples/consumer/MessageEventHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.samples.consumer; 19 | 20 | import com.mq.idempotent.core.annotation.Idempotent; 21 | import org.apache.rocketmq.common.message.MessageExt; 22 | import org.springframework.beans.factory.annotation.Autowired; 23 | import org.springframework.stereotype.Service; 24 | 25 | import java.util.Objects; 26 | 27 | /** 28 | * @author : wh 29 | * @date : 2021/11/15 20:02 30 | * @description: 31 | */ 32 | @Service 33 | public class MessageEventHandler { 34 | 35 | @Autowired 36 | private MessageEventHandler messageEventHandler; 37 | 38 | public void consumer(MessageExt messageExt) { 39 | if (Objects.equals(messageExt.getTags(), "TagA")) { 40 | // 防止AOP失效失效 41 | messageEventHandler.testConsumer(messageExt); 42 | } 43 | 44 | } 45 | 46 | @Idempotent 47 | public void testConsumer(MessageExt messageExt) { 48 | String msg = new String(messageExt.getBody()); 49 | System.out.println("消息id " + messageExt.getMsgId()); 50 | System.out.println("消息keys " + messageExt.getKeys()); 51 | System.out.println("消费成功, msg " + msg); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/test/java/com/mq/idempotent/core/alert/strategy/LarkAlarmStrategyTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.alert.strategy; 19 | 20 | import java.lang.reflect.Method; 21 | import java.util.concurrent.TimeUnit; 22 | 23 | import com.mq.idempotent.core.alert.AlertDTO; 24 | import org.junit.jupiter.api.Test; 25 | import org.junit.jupiter.api.extension.ExtendWith; 26 | import org.mockito.Mock; 27 | import org.mockito.junit.jupiter.MockitoExtension; 28 | 29 | import static org.mockito.Mockito.when; 30 | 31 | /** 32 | *@author : wh 33 | *@date : 2022/7/19 14:22 34 | *@description: 35 | */ 36 | @ExtendWith(MockitoExtension.class) 37 | public class LarkAlarmStrategyTests { 38 | 39 | @Mock 40 | private Method method; 41 | 42 | @Test 43 | public void sendMsg() throws Exception { 44 | AlertStrategy lark = AlertStrategyFactory.newInstance("lark"); 45 | when(method.getName()).thenReturn("testName"); 46 | AlertDTO alertDTO = new AlertDTO(); 47 | alertDTO.setKey("testKey"); 48 | alertDTO.setMethod(method); 49 | alertDTO.setThrowable(new Throwable("test error")); 50 | alertDTO.setWebHook(""); 51 | lark.sendMsg(alertDTO); 52 | TimeUnit.SECONDS.sleep(3); 53 | } 54 | } -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/strategy/impl/JDBCIdempotentStrategy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.strategy.impl; 19 | 20 | import com.mq.idempotent.core.aop.MessageConverter; 21 | import com.mq.idempotent.core.model.IdempotentConfig; 22 | import com.mq.idempotent.core.strategy.AbstractIdempotentStrategy; 23 | import org.springframework.jdbc.core.JdbcTemplate; 24 | 25 | /** 26 | * @author : wh 27 | * @date : 2022/3/13 09:46 28 | * @description: 29 | */ 30 | public class JDBCIdempotentStrategy extends AbstractIdempotentStrategy { 31 | 32 | private final JdbcTemplate jdbcTemplate; 33 | 34 | public JDBCIdempotentStrategy(IdempotentConfig idempotentConfig, MessageConverter messageConverter, JdbcTemplate jdbcTemplate) { 35 | super(idempotentConfig, messageConverter); 36 | this.jdbcTemplate = jdbcTemplate; 37 | } 38 | 39 | @Override 40 | public boolean lock(String lockName) { 41 | return false; 42 | } 43 | 44 | @Override 45 | public void save(String uniqueKey) { 46 | 47 | } 48 | 49 | @Override 50 | public void unlock(String lockName) { 51 | 52 | } 53 | 54 | @Override 55 | public boolean exitKey(String key) { 56 | return false; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/config/IdempotentProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.config; 19 | 20 | import lombok.Getter; 21 | import lombok.Setter; 22 | import org.springframework.boot.context.properties.ConfigurationProperties; 23 | 24 | /** 25 | * @author : wh 26 | * @date : 2021/12/30 21:20 27 | * @description: 28 | */ 29 | @Getter 30 | @Setter 31 | @ConfigurationProperties(prefix = IdempotentProperties.PREFIX) 32 | public class IdempotentProperties { 33 | 34 | public static final String PREFIX = "idempotent"; 35 | 36 | /** 37 | * 去重key redis名字 38 | */ 39 | private String uniqueKeyPrefix = "mq:unique:"; 40 | 41 | /** 42 | * 去重记录后缀 43 | */ 44 | private String recordKeySuffix = "Record"; 45 | 46 | /** 47 | * redis值 48 | */ 49 | private String uniqueValue = "s"; 50 | 51 | /** 52 | * 并发获取锁等待时间 53 | */ 54 | private Long tryLockTime = 1L; 55 | 56 | /** 57 | * 消费key存放时间默认3天 58 | */ 59 | private Long keyTimeOut = 3L; 60 | 61 | /** 62 | * 是否开启并发控制 63 | */ 64 | private Boolean concurrency = true; 65 | 66 | /** 67 | * 报警 url 68 | */ 69 | private String webHook; 70 | 71 | /** 72 | * 报警策略 73 | */ 74 | private String alertName; 75 | 76 | } 77 | -------------------------------------------------------------------------------- /mq-idempotent-samples/springboot-rabbitmq/src/main/java/com/rabbitmq/config/DirectRabbitConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.rabbitmq.config; 19 | 20 | import com.rabbitmq.constants.RabbitMQConstants; 21 | import org.springframework.amqp.core.Binding; 22 | import org.springframework.amqp.core.BindingBuilder; 23 | import org.springframework.amqp.core.DirectExchange; 24 | import org.springframework.amqp.core.Queue; 25 | import org.springframework.context.annotation.Bean; 26 | import org.springframework.context.annotation.Configuration; 27 | 28 | /** 29 | * @author : wh 30 | * @date : 2022/1/11 19:32 31 | * @description: 32 | */ 33 | @Configuration 34 | public class DirectRabbitConfig { 35 | 36 | /** 37 | * 邮件队列 38 | * @return 39 | */ 40 | @Bean 41 | public Queue testDirectQueue() { 42 | return new Queue(RabbitMQConstants.QUEUE_NAME, true); 43 | } 44 | 45 | /** 46 | * 交换机 47 | * @return 48 | */ 49 | @Bean 50 | DirectExchange TestDirectExchange() { 51 | return new DirectExchange(RabbitMQConstants.TEST_DIRECT_EXCHANGE, true, false); 52 | } 53 | 54 | /** 55 | * 将队列和交换机绑定 56 | * @return 57 | */ 58 | @Bean 59 | Binding bindingDirect() { 60 | return BindingBuilder.bind(testDirectQueue()).to(TestDirectExchange()).with(RabbitMQConstants.ROUTING_KEY); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /mq-idempotent-samples/springboot-rabbitmq/src/main/java/com/rabbitmq/config/RedisConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.rabbitmq.config; 19 | 20 | import org.redisson.Redisson; 21 | import org.redisson.api.RedissonClient; 22 | import org.redisson.config.Config; 23 | import org.redisson.config.SingleServerConfig; 24 | import org.springframework.beans.factory.annotation.Value; 25 | import org.springframework.context.annotation.Bean; 26 | import org.springframework.context.annotation.Configuration; 27 | 28 | /** 29 | * @author : wh 30 | * @date : 2021/11/6 11:02 31 | * @description: 32 | */ 33 | @Configuration 34 | public class RedisConfig { 35 | 36 | @Value("${redis.host:127.0.0.1}") 37 | private String redisLoginHost; 38 | @Value("${redis.port:6379}") 39 | private Integer redisLoginPort; 40 | @Value("${redis.password:123456}") 41 | private String redisLoginPassword; 42 | 43 | @Bean 44 | public RedissonClient redissonClient() { 45 | return createRedis(redisLoginHost, redisLoginPort, redisLoginPassword); 46 | } 47 | 48 | private RedissonClient createRedis(String redisHost, Integer redisPort, String redisPassword) { 49 | Config config = new Config(); 50 | SingleServerConfig singleServerConfig = config.useSingleServer(); 51 | singleServerConfig.setAddress("redis://" + redisHost + ":" + redisPort + ""); 52 | singleServerConfig.setPassword(redisPassword); 53 | return Redisson.create(config); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /mq-idempotent-samples/springboot-rocketmq/src/main/java/com/samples/config/RedisConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.samples.config; 19 | 20 | import org.redisson.Redisson; 21 | import org.redisson.api.RedissonClient; 22 | import org.redisson.config.Config; 23 | import org.redisson.config.SingleServerConfig; 24 | import org.springframework.beans.factory.annotation.Value; 25 | import org.springframework.context.annotation.Bean; 26 | import org.springframework.context.annotation.Configuration; 27 | 28 | /** 29 | * @author : wh 30 | * @date : 2021/11/6 11:02 31 | * @description: 32 | */ 33 | @Configuration 34 | public class RedisConfig { 35 | 36 | @Value("${redis.host:127.0.0.1}") 37 | private String redisLoginHost; 38 | @Value("${redis.port:6379}") 39 | private Integer redisLoginPort; 40 | @Value("${redis.password:123456}") 41 | private String redisLoginPassword; 42 | 43 | @Bean 44 | public RedissonClient redissonClient() { 45 | return createRedis(redisLoginHost, redisLoginPort, redisLoginPassword); 46 | } 47 | 48 | private RedissonClient createRedis(String redisHost, Integer redisPort, String redisPassword) { 49 | Config config = new Config(); 50 | SingleServerConfig singleServerConfig = config.useSingleServer(); 51 | singleServerConfig.setAddress("redis://" + redisHost + ":" + redisPort + ""); 52 | singleServerConfig.setPassword(redisPassword); 53 | return Redisson.create(config); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /mq-idempotent-samples/springboot-aliyun-rocketmq/src/main/java/com/samples/config/RedisConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.samples.config; 19 | 20 | import org.redisson.Redisson; 21 | import org.redisson.api.RedissonClient; 22 | import org.redisson.config.Config; 23 | import org.redisson.config.SingleServerConfig; 24 | import org.springframework.beans.factory.annotation.Value; 25 | import org.springframework.context.annotation.Bean; 26 | import org.springframework.context.annotation.Configuration; 27 | 28 | /** 29 | * @author : wh 30 | * @date : 2021/11/6 11:02 31 | * @description: 32 | */ 33 | @Configuration 34 | public class RedisConfig { 35 | 36 | @Value("${redis.host:127.0.0.1}") 37 | private String redisLoginHost; 38 | @Value("${redis.port:6379}") 39 | private Integer redisLoginPort; 40 | @Value("${redis.password:123456}") 41 | private String redisLoginPassword; 42 | 43 | @Bean 44 | public RedissonClient redissonClient() { 45 | return createRedis(redisLoginHost, redisLoginPort, redisLoginPassword); 46 | } 47 | 48 | private RedissonClient createRedis(String redisHost, Integer redisPort, String redisPassword) { 49 | Config config = new Config(); 50 | SingleServerConfig singleServerConfig = config.useSingleServer(); 51 | singleServerConfig.setAddress("redis://" + redisHost + ":" + redisPort + ""); 52 | singleServerConfig.setPassword(redisPassword); 53 | return Redisson.create(config); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /mq-idempotent-samples/springboot-aliyun-rocketmq/src/main/java/com/samples/consumer/MessageEventHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.samples.consumer; 19 | 20 | import com.aliyun.openservices.ons.api.Message; 21 | import com.mq.idempotent.core.annotation.Idempotent; 22 | import com.samples.entity.TestDO; 23 | import com.samples.mapper.TestMapper; 24 | import lombok.extern.slf4j.Slf4j; 25 | 26 | import org.springframework.beans.factory.annotation.Autowired; 27 | import org.springframework.stereotype.Service; 28 | import org.springframework.transaction.annotation.Transactional; 29 | 30 | /** 31 | * @author : wh 32 | * @date : 2021/11/15 20:02 33 | * @description: 34 | */ 35 | @Service 36 | @Slf4j 37 | public class MessageEventHandler { 38 | 39 | @Autowired 40 | TestMapper testMapper; 41 | 42 | @Idempotent 43 | @Transactional(rollbackFor = Exception.class) 44 | public boolean consumer(Message message) { 45 | String msg = new String(message.getBody()); 46 | System.out.println("消息id " + message.getMsgID()); 47 | System.out.println("消息key " + message.getKey()); 48 | System.out.println("消费成功, msg " + msg); 49 | TestDO testDO = new TestDO(); 50 | testDO.setMsg("test"); 51 | try { 52 | testMapper.insert(testDO); 53 | } catch (Exception e) { 54 | log.error("插入数据库异常", e); 55 | throw new RuntimeException(e); 56 | } 57 | System.out.println("插入数据库成功"); 58 | return true; 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/model/IdempotentConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.model; 19 | 20 | import java.util.concurrent.TimeUnit; 21 | 22 | import com.mq.idempotent.core.config.IdempotentProperties; 23 | import lombok.Data; 24 | 25 | /** 26 | * @author : wh 27 | * @date : 2021/11/15 11:07 28 | * @description: 29 | */ 30 | @Data 31 | public class IdempotentConfig { 32 | 33 | /** 34 | * 去重key 前缀 35 | */ 36 | private String uniqueKeyPrefix; 37 | 38 | /** 39 | * 去重值 40 | */ 41 | private String uniqueValue; 42 | 43 | private String recordKeySuffix; 44 | 45 | /** 46 | * 并发获取锁等待时间 TimeUnit.SECONDS 47 | */ 48 | private Long tryLockTime; 49 | /** 50 | * 并发获取锁等待时间单位 51 | */ 52 | private TimeUnit tryLockTimeUnit = TimeUnit.SECONDS; 53 | 54 | /** 55 | * 存放已消费消息时间,过期则删除。 56 | */ 57 | private Long keyTimeOut; 58 | 59 | /** 60 | * 消费key存放redis时间单位 61 | */ 62 | private TimeUnit timeOutTimeUnit = TimeUnit.DAYS; 63 | 64 | /** 65 | * 报警策略 66 | */ 67 | private String alertName; 68 | 69 | private String webHook; 70 | 71 | public void initConfig(IdempotentProperties properties) { 72 | this.uniqueKeyPrefix = properties.getUniqueKeyPrefix(); 73 | this.uniqueValue = properties.getUniqueValue(); 74 | this.tryLockTime = properties.getTryLockTime(); 75 | this.keyTimeOut = properties.getKeyTimeOut(); 76 | this.recordKeySuffix = properties.getRecordKeySuffix(); 77 | this.webHook = properties.getWebHook(); 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/strategy/AbstractIdempotentStrategy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.strategy; 19 | 20 | import java.lang.reflect.Method; 21 | import java.util.concurrent.TimeUnit; 22 | 23 | import com.mq.idempotent.core.aop.MessageConverter; 24 | import com.mq.idempotent.core.model.IdempotentConfig; 25 | import lombok.AllArgsConstructor; 26 | 27 | /** 28 | * @author : wh 29 | * @date : 2022/3/12 18:07 30 | * @description: 31 | */ 32 | @AllArgsConstructor 33 | public abstract class AbstractIdempotentStrategy implements IdempotentStrategy { 34 | 35 | private final IdempotentConfig idempotentConfig; 36 | 37 | private final MessageConverter messageConverter; 38 | 39 | public Long getTryLockTime() { 40 | return idempotentConfig.getTryLockTime(); 41 | } 42 | 43 | public TimeUnit getTryLockTimeUnit() { 44 | return idempotentConfig.getTryLockTimeUnit(); 45 | } 46 | 47 | public String getUniqueKey(Object o, String field, Method method, Object[] args) { 48 | String uniqueKeyPrefix = idempotentConfig.getUniqueKeyPrefix(); 49 | return uniqueKeyPrefix + messageConverter.getUniqueKey(o, field, method, args); 50 | } 51 | 52 | public IdempotentConfig getIdempotentConfig() { 53 | return idempotentConfig; 54 | } 55 | 56 | public String getUniqueValue() { 57 | return idempotentConfig.getUniqueValue(); 58 | } 59 | 60 | public Long getKeyTimeOut() { 61 | return idempotentConfig.getKeyTimeOut(); 62 | } 63 | 64 | public TimeUnit getTimeOutTimeUnit() { 65 | return idempotentConfig.getTimeOutTimeUnit(); 66 | } 67 | 68 | public String getWebHook() { 69 | return idempotentConfig.getWebHook(); 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /mq-idempotent-samples/springboot-rocketmq/src/main/java/com/samples/controller/SendMessageController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.samples.controller; 19 | 20 | import org.apache.rocketmq.client.producer.DefaultMQProducer; 21 | import org.apache.rocketmq.client.producer.SendResult; 22 | import org.apache.rocketmq.common.message.Message; 23 | import org.apache.rocketmq.remoting.common.RemotingHelper; 24 | import org.springframework.web.bind.annotation.GetMapping; 25 | import org.springframework.web.bind.annotation.RestController; 26 | 27 | /** 28 | * @author : wh 29 | * @date : 2022/1/12 16:28 30 | * @description: 31 | */ 32 | @RestController 33 | public class SendMessageController { 34 | 35 | @GetMapping("/convert/send") 36 | public String send() throws Exception { 37 | DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name"); 38 | producer.setNamesrvAddr("127.0.0.1:9876"); 39 | producer.start(); 40 | for (int i = 0; i < 10; i++) { 41 | try { 42 | /* 43 | * Create a message instance, specifying topic, tag and message body. 44 | */ 45 | Message msg = new Message("TopicTest" /* Topic */, 46 | "TagA" /* Tag */, 47 | ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */ 48 | ); 49 | msg.setKeys("testKey5"); 50 | /* 51 | * Call send message to deliver message to one of brokers. 52 | */ 53 | SendResult sendResult = producer.send(msg); 54 | System.out.printf("%s%n", sendResult); 55 | } catch (Exception e) { 56 | e.printStackTrace(); 57 | Thread.sleep(2000); 58 | } 59 | } 60 | return "success"; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /mq-idempotent-samples/springboot-aliyun-rocketmq/src/main/java/com/samples/config/AliyunMQConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.samples.config; 19 | 20 | import java.util.Properties; 21 | 22 | import com.aliyun.openservices.ons.api.Action; 23 | import com.aliyun.openservices.ons.api.Consumer; 24 | import com.aliyun.openservices.ons.api.ONSFactory; 25 | import com.aliyun.openservices.ons.api.PropertyKeyConst; 26 | import com.samples.consumer.MessageEventHandler; 27 | import lombok.extern.slf4j.Slf4j; 28 | 29 | import org.springframework.beans.factory.annotation.Autowired; 30 | import org.springframework.beans.factory.annotation.Value; 31 | import org.springframework.context.annotation.Bean; 32 | import org.springframework.context.annotation.Configuration; 33 | 34 | /** 35 | * @author : wh 36 | * @date : 2021/11/8 10:55 37 | * @description: 38 | */ 39 | @Configuration 40 | @Slf4j 41 | public class AliyunMQConfig { 42 | 43 | @Value("${alimq.accessKey:test}") 44 | private String aclAccessKey; 45 | 46 | @Value("${alimq.accessKey:test}") 47 | private String aclAccessSecret; 48 | 49 | @Value("${alimq.orderActionTopicNameSerAddr:test}") 50 | private String orderNameSerAddr; 51 | 52 | @Autowired 53 | MessageEventHandler handler; 54 | 55 | @Bean(initMethod = "start", destroyMethod = "shutdown") 56 | public Consumer consumer() { 57 | Properties properties = new Properties(); 58 | properties.put(PropertyKeyConst.AccessKey, ""); 59 | properties.put(PropertyKeyConst.SecretKey, ""); 60 | properties.put(PropertyKeyConst.NAMESRV_ADDR, ""); 61 | properties.put(PropertyKeyConst.MaxReconsumeTimes, 20); 62 | properties.put(PropertyKeyConst.GROUP_ID, ""); 63 | Consumer consumer = ONSFactory.createConsumer(properties); 64 | consumer.subscribe("topic", "*", (message, context) -> handler.consumer(message) 65 | ? Action.CommitMessage 66 | : Action.ReconsumeLater); 67 | return consumer; 68 | 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/utils/TransactionUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.utils; 19 | 20 | import java.util.function.Supplier; 21 | 22 | import lombok.AllArgsConstructor; 23 | import lombok.extern.slf4j.Slf4j; 24 | 25 | import org.springframework.transaction.PlatformTransactionManager; 26 | import org.springframework.transaction.TransactionDefinition; 27 | import org.springframework.transaction.TransactionStatus; 28 | import org.springframework.transaction.support.DefaultTransactionDefinition; 29 | 30 | /** 31 | *@author : wh 32 | *@date : 2022/7/20 11:21 33 | *@description: 34 | */ 35 | @AllArgsConstructor 36 | @Slf4j 37 | public class TransactionUtil { 38 | 39 | private final PlatformTransactionManager transactionManager; 40 | 41 | public boolean transact(Runnable runnable, TransactionDefinition transactionDefinition) { 42 | TransactionStatus status = transactionManager.getTransaction(transactionDefinition); 43 | try { 44 | runnable.run(); 45 | transactionManager.commit(status); 46 | return true; 47 | } catch (Exception e) { 48 | transactionManager.rollback(status); 49 | throw e; 50 | } 51 | 52 | } 53 | 54 | public T transact(Supplier supplier, TransactionDefinition transactionDefinition) { 55 | TransactionStatus status = transactionManager.getTransaction(transactionDefinition); 56 | try { 57 | T t = supplier.get(); 58 | transactionManager.commit(status); 59 | return t; 60 | } catch (Exception e) { 61 | transactionManager.rollback(status); 62 | throw e; 63 | } 64 | 65 | } 66 | 67 | /** 68 | * 默认创博行为 PROPAGATION_REQUIRED 69 | * @param runnable 70 | * @return 71 | */ 72 | public boolean transact(Runnable runnable) { 73 | return transact(runnable, new DefaultTransactionDefinition()); 74 | } 75 | 76 | public T transact(Supplier supplier) { 77 | return transact(supplier, new DefaultTransactionDefinition()); 78 | 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/strategy/impl/RedisIdempotentStrategy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.strategy.impl; 19 | 20 | import com.mq.idempotent.core.aop.MessageConverter; 21 | import com.mq.idempotent.core.model.IdempotentConfig; 22 | import com.mq.idempotent.core.strategy.AbstractIdempotentStrategy; 23 | import org.redisson.api.RBucket; 24 | import org.redisson.api.RLock; 25 | import org.redisson.api.RedissonClient; 26 | 27 | /** 28 | * @author : wh 29 | * @date : 2022/3/12 18:05 30 | * @description: 31 | */ 32 | public class RedisIdempotentStrategy extends AbstractIdempotentStrategy { 33 | 34 | private final RedissonClient redissonClient; 35 | 36 | public RedisIdempotentStrategy(IdempotentConfig idempotentConfig, MessageConverter messageConverter, RedissonClient redissonClient) { 37 | super(idempotentConfig, messageConverter); 38 | this.redissonClient = redissonClient; 39 | } 40 | 41 | @Override 42 | public boolean lock(String lockName) { 43 | RLock stockLock = redissonClient.getLock(lockName); 44 | try { 45 | return stockLock.tryLock(getTryLockTime(), getTryLockTimeUnit()); 46 | } catch (InterruptedException e) { 47 | e.printStackTrace(); 48 | } 49 | return false; 50 | } 51 | 52 | @Override 53 | public void save(String uniqueKey) { 54 | final String recordKey = getRecordKey(uniqueKey); 55 | RBucket bucket = redissonClient.getBucket(recordKey); 56 | bucket.set(getUniqueValue(), getKeyTimeOut(), getTimeOutTimeUnit()); 57 | } 58 | 59 | @Override 60 | public void unlock(String lockName) { 61 | RLock stockLock = redissonClient.getLock(lockName); 62 | stockLock.unlock(); 63 | } 64 | 65 | @Override 66 | public boolean exitKey(String key) { 67 | final String recordKey = getRecordKey(key); 68 | RBucket record = redissonClient.getBucket(recordKey); 69 | final RBucket currentLock = redissonClient.getBucket(key); 70 | 71 | return record.isExists() || currentLock.isExists(); 72 | } 73 | 74 | private String getRecordKey(String uniqueKey) { 75 | return uniqueKey + "_" + getIdempotentConfig().getRecordKeySuffix(); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /mq-idempotent-core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | io.github.weihubeats 7 | mq-idempotent 8 | ${revision} 9 | ../pom.xml 10 | 11 | 12 | mq-idempotent-core 13 | 14 | 15 | 11 16 | 11 17 | 18 | 19 | 20 | 21 | org.projectlombok 22 | lombok 23 | 24 | 25 | org.redisson 26 | redisson 27 | 28 | 29 | org.apache.commons 30 | commons-lang3 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-web 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-configuration-processor 41 | true 42 | 43 | 44 | 45 | org.springframework.boot 46 | spring-boot-starter-aop 47 | 48 | 49 | 50 | org.springframework.boot 51 | spring-boot-starter-jdbc 52 | true 53 | 54 | 55 | 56 | com.squareup.okhttp3 57 | okhttp 58 | 59 | 60 | 61 | org.springframework.boot 62 | spring-boot-starter-test 63 | test 64 | 65 | 66 | org.junit.jupiter 67 | junit-jupiter 68 | test 69 | 70 | 71 | org.mockito 72 | mockito-junit-jupiter 73 | test 74 | 75 | 76 | org.mockito 77 | mockito-core 78 | test 79 | 80 | 81 | org.mockito 82 | mockito-inline 83 | test 84 | 85 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /mq-idempotent-samples/springboot-aliyun-rocketmq/src/main/java/com/samples/config/MybatisPlusConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.samples.config; 19 | 20 | import javax.sql.DataSource; 21 | 22 | import com.baomidou.mybatisplus.core.MybatisConfiguration; 23 | import com.baomidou.mybatisplus.core.config.GlobalConfig; 24 | import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; 25 | import org.apache.ibatis.type.JdbcType; 26 | import org.mybatis.spring.annotation.MapperScan; 27 | 28 | import org.springframework.boot.context.properties.ConfigurationProperties; 29 | import org.springframework.boot.jdbc.DataSourceBuilder; 30 | import org.springframework.context.annotation.Bean; 31 | import org.springframework.context.annotation.Configuration; 32 | import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 33 | import org.springframework.core.io.support.ResourcePatternResolver; 34 | import org.springframework.jdbc.datasource.DataSourceTransactionManager; 35 | 36 | /** 37 | *@author : wh 38 | *@date : 2022/7/20 11:54 39 | *@description: 40 | */ 41 | @Configuration 42 | @MapperScan("com.samples.mapper") 43 | public class MybatisPlusConfig { 44 | 45 | @ConfigurationProperties(prefix = "db.pg") 46 | @Bean 47 | public DataSource dataSource() { 48 | return DataSourceBuilder.create().build(); 49 | } 50 | 51 | @Bean 52 | public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean() throws Exception { 53 | MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean(); 54 | factoryBean.setDataSource(dataSource()); 55 | ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); 56 | factoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml")); 57 | MybatisConfiguration configuration = new MybatisConfiguration(); 58 | // 开启下划线转驼峰 59 | configuration.setMapUnderscoreToCamelCase(true); 60 | configuration.setJdbcTypeForNull(JdbcType.NULL); 61 | GlobalConfig globalConfig = new GlobalConfig(); 62 | globalConfig.setBanner(false); 63 | factoryBean.setGlobalConfig(globalConfig); 64 | factoryBean.setConfiguration(configuration); 65 | return factoryBean; 66 | } 67 | 68 | @Bean 69 | public DataSourceTransactionManager transactionManager() { 70 | return new DataSourceTransactionManager(dataSource()); 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /mq-idempotent-samples/springboot-rabbitmq/src/main/java/com/rabbitmq/controller/SendMessageController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.rabbitmq.controller; 19 | 20 | import com.rabbitmq.constants.RabbitMQConstants; 21 | import com.rabbitmq.message.TestMessage; 22 | import org.springframework.amqp.rabbit.core.RabbitTemplate; 23 | import org.springframework.beans.factory.annotation.Autowired; 24 | import org.springframework.web.bind.annotation.GetMapping; 25 | import org.springframework.web.bind.annotation.RestController; 26 | 27 | import java.time.LocalDateTime; 28 | import java.time.format.DateTimeFormatter; 29 | import java.util.HashMap; 30 | import java.util.Map; 31 | import java.util.UUID; 32 | 33 | /** 34 | * @author : wh 35 | * @date : 2022/1/11 19:39 36 | * @description: 37 | */ 38 | @RestController 39 | public class SendMessageController { 40 | 41 | @Autowired 42 | private RabbitTemplate rabbitTemplate; 43 | 44 | /* 45 | * @GetMapping("/convert/send") public String send(String queueName) { Map map = getMessageMap(); rabbitTemplate.convertAndSend(RabbitMQConstants.TEST_DIRECT_EXCHANGE, 46 | * RabbitMQConstants.ROUTING_KEY, map); return "success"; 47 | * 48 | * } 49 | */ 50 | 51 | @GetMapping("/convert/send") 52 | public String send1(String queueName) { 53 | TestMessage testMessage = new TestMessage(); 54 | testMessage.setId(1); 55 | testMessage.setMsg("test message, hello"); 56 | rabbitTemplate.convertAndSend(RabbitMQConstants.TEST_DIRECT_EXCHANGE, RabbitMQConstants.ROUTING_KEY, testMessage); 57 | return "success"; 58 | 59 | } 60 | 61 | /* 62 | * @GetMapping("/sendMsg") public String sendMessage() { Map map = getMessageMap(); // 指定消息类型 MessageProperties props = MessagePropertiesBuilder.newInstance() 63 | * .setContentType(MessageProperties.CONTENT_TYPE_JSON) .build(); Message message = new Message( JSON.toJSONString(map).getBytes(StandardCharsets.UTF_8), props); 64 | * rabbitTemplate.send(RabbitMQConstants.TEST_DIRECT_EXCHANGE, RabbitMQConstants.ROUTING_KEY,message); return "success"; } 65 | */ 66 | 67 | public Map getMessageMap() { 68 | String messageId = String.valueOf(UUID.randomUUID()); 69 | String messageData = "test message, hello!"; 70 | String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); 71 | Map map = new HashMap<>(); 72 | map.put("messageId", messageId); 73 | map.put("messageData", messageData); 74 | map.put("createTime", createTime); 75 | return map; 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /dev-support/spotless_mq-idempotent_formatter.xml: -------------------------------------------------------------------------------- 1 | 2 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /mq-idempotent-samples/springboot-rocketmq/src/main/java/com/samples/config/MQConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.samples.config; 19 | 20 | import com.samples.consumer.MessageEventHandler; 21 | import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer; 22 | import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus; 23 | import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently; 24 | import org.apache.rocketmq.common.consumer.ConsumeFromWhere; 25 | import org.apache.rocketmq.common.message.MessageExt; 26 | import org.apache.rocketmq.common.protocol.heartbeat.MessageModel; 27 | import org.springframework.beans.factory.annotation.Autowired; 28 | import org.springframework.beans.factory.annotation.Value; 29 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 30 | import org.springframework.context.annotation.Bean; 31 | import org.springframework.context.annotation.Configuration; 32 | import org.springframework.util.CollectionUtils; 33 | 34 | /** 35 | * @author : wh 36 | * @date : 2021/11/15 15:44 37 | * @description: 38 | */ 39 | @Configuration 40 | public class MQConfig { 41 | 42 | @Value("${rocketmq.consumer.namesrvAddr:127.0.0.1:9876}") 43 | private String namesrvAddr; 44 | 45 | @Value("${rocketmq.consumer.groupName:please_rename_unique_group_name_4}") 46 | private String groupName; 47 | 48 | @Value("${rocketmq.consumer.consumeThreadMin:10}") 49 | private int consumeThreadMin; 50 | 51 | @Value("${rocketmq.consumer.consumeThreadMax:10}") 52 | private int consumeThreadMax; 53 | 54 | @Value("${rocketmq.consumer.topics:TopicTest}") 55 | private String topics; 56 | 57 | @Value("${rocketmq.consumer.consumeMessageBatchMaxSize:1}") 58 | private int consumeMessageBatchMaxSize; 59 | 60 | @Autowired 61 | MessageEventHandler handler; 62 | 63 | @Bean 64 | @ConditionalOnMissingBean 65 | public DefaultMQPushConsumer defaultMQPushConsumer() throws RuntimeException { 66 | 67 | DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(groupName); 68 | consumer.setNamesrvAddr(namesrvAddr); 69 | consumer.setConsumeThreadMin(consumeThreadMin); 70 | consumer.setConsumeThreadMax(consumeThreadMax); 71 | 72 | // 设置 consumer 第一次启动是从队列头部开始消费还是队列尾部开始消费 73 | // 如果非第一次启动,那么按照上次消费的位置继续消费 74 | consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); 75 | // 设置消费模型,集群还是广播,默认为集群 76 | consumer.setMessageModel(MessageModel.CLUSTERING); 77 | // 设置一次消费消息的条数,默认为 1 条 78 | consumer.setConsumeMessageBatchMaxSize(consumeMessageBatchMaxSize); 79 | consumer.registerMessageListener((MessageListenerConcurrently) (msgs, context) -> { 80 | if (CollectionUtils.isEmpty(msgs)) { 81 | return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; 82 | } 83 | MessageExt messageExt = msgs.get(0); 84 | handler.consumer(messageExt); 85 | return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; 86 | }); 87 | 88 | try { 89 | consumer.subscribe(topics, "*"); 90 | // 启动消费 91 | consumer.start(); 92 | } catch (Exception e) { 93 | throw new RuntimeException(e); 94 | } 95 | return consumer; 96 | } 97 | 98 | } 99 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/alert/strategy/LarkAlarmStrategy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.alert.strategy; 19 | 20 | import java.io.IOException; 21 | import java.nio.charset.StandardCharsets; 22 | import java.util.concurrent.ExecutorService; 23 | import java.util.concurrent.LinkedBlockingQueue; 24 | import java.util.concurrent.ThreadPoolExecutor; 25 | import java.util.concurrent.TimeUnit; 26 | 27 | import com.mq.idempotent.core.alert.AlertDTO; 28 | import com.mq.idempotent.core.spi.Join; 29 | import com.mq.idempotent.core.utils.ThreadFactoryImpl; 30 | import okhttp3.MediaType; 31 | import okhttp3.OkHttpClient; 32 | import okhttp3.Request; 33 | import okhttp3.RequestBody; 34 | import org.apache.commons.lang3.exception.ExceptionUtils; 35 | 36 | import org.springframework.util.ObjectUtils; 37 | 38 | /** 39 | * @author : wh 40 | * @date : 2022/6/15 18:05 41 | * @description: 42 | */ 43 | @Join 44 | public class LarkAlarmStrategy implements AlertStrategy { 45 | 46 | private static final String TEMPLATE = 47 | "{\"msg_type\":\"interactive\",\"card\":{\"config\":{\"wide_screen_mode\":true},\"header\":{\"template\":\"greed\",\"title\":{\"content\":\"mq 幂等报警\",\"tag\":\"plain_text\"}},\"elements\":[{\"fields\":[{\"is_short\":true,\"text\":{\"content\":\"** 唯一key:** %s\",\"tag\":\"lark_md\"}},{\"is_short\":true,\"text\":{\"content\":\"** methodName:** %s\",\"tag\":\"lark_md\"}},{\"is_short\":true,\"text\":{\"content\":\"** 异常** %s\",\"tag\":\"lark_md\"}}],\"tag\":\"div\"},{\"tag\":\"hr\"}]}}"; 48 | 49 | private static final String ALL = ",{\"tag\":\"div\",\"text\":{\"content\":\" \",\"tag\":\"lark_md\"}}"; 50 | 51 | private static final int FEISHU_MESSAGE_HASH_MAX_LENGTH = 30 * 1024; 52 | 53 | private static final OkHttpClient OK_HTTP_CLIENT = new OkHttpClient() 54 | .newBuilder().connectTimeout(50L, TimeUnit.SECONDS) 55 | .readTimeout(60L, TimeUnit.SECONDS) 56 | .build(); 57 | 58 | private static final ExecutorService executor = new ThreadPoolExecutor(1, 3, 60, TimeUnit.SECONDS, 59 | new LinkedBlockingQueue<>(100), new ThreadFactoryImpl("feishu-"));; 60 | 61 | @Override 62 | public boolean sendMsg(AlertDTO alertDTO) { 63 | String stackTrace = ExceptionUtils.getStackTrace(alertDTO.getThrowable()); 64 | // 格式化异常 65 | if (!ObjectUtils.isEmpty(stackTrace)) { 66 | stackTrace = stackTrace.replaceAll("\n", "\\\\n"); 67 | stackTrace = stackTrace.replaceAll("\t", "\\\\t"); 68 | if (stackTrace.getBytes(StandardCharsets.UTF_8).length > FEISHU_MESSAGE_HASH_MAX_LENGTH) { 69 | stackTrace = stackTrace.substring(0, new String(new byte[FEISHU_MESSAGE_HASH_MAX_LENGTH]).length()); 70 | } 71 | } 72 | 73 | stackTrace = String.format(TEMPLATE, alertDTO.getKey(), alertDTO.getMethod().getName(), stackTrace); 74 | 75 | RequestBody body = RequestBody.create( 76 | MediaType.parse("application/json"), stackTrace); 77 | Request request = new Request.Builder() 78 | .url(alertDTO.getWebHook()) 79 | .post(body) 80 | .build(); 81 | try { 82 | OK_HTTP_CLIENT.newCall(request).execute(); 83 | } catch (IOException e) { 84 | throw new RuntimeException(e); 85 | } 86 | return false; 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/autoconfigure/MqIdempotentAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.autoconfigure; 19 | 20 | import com.mq.idempotent.core.alert.strategy.AlertStrategy; 21 | import com.mq.idempotent.core.alert.strategy.AlertStrategyFactory; 22 | import com.mq.idempotent.core.annotation.Idempotent; 23 | import com.mq.idempotent.core.aop.MessageConverter; 24 | import com.mq.idempotent.core.aop.MqIdempotentAnnotationAdvisor; 25 | import com.mq.idempotent.core.aop.MqIdempotentAnnotationInterceptor; 26 | import com.mq.idempotent.core.config.IdempotentProperties; 27 | import com.mq.idempotent.core.model.IdempotentConfig; 28 | import com.mq.idempotent.core.strategy.AbstractIdempotentStrategy; 29 | import com.mq.idempotent.core.strategy.impl.RedisIdempotentStrategy; 30 | import com.mq.idempotent.core.utils.TransactionUtil; 31 | import lombok.AllArgsConstructor; 32 | import org.redisson.api.RedissonClient; 33 | 34 | import org.springframework.beans.factory.annotation.Autowired; 35 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 36 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 37 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 38 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 39 | import org.springframework.context.annotation.Bean; 40 | import org.springframework.context.annotation.Configuration; 41 | import org.springframework.transaction.PlatformTransactionManager; 42 | 43 | /** 44 | * @author : wh 45 | * @date : 2021/12/30 18:13 46 | * @description: 47 | */ 48 | @Configuration 49 | @EnableConfigurationProperties({IdempotentProperties.class}) 50 | @AllArgsConstructor 51 | public class MqIdempotentAutoConfiguration { 52 | 53 | private IdempotentProperties properties; 54 | 55 | @Bean 56 | public MqIdempotentAnnotationAdvisor mqIdempotentAnnotationAdvisor(AbstractIdempotentStrategy idempotentStrategy, @Autowired(required = false) AlertStrategy alertStrategy, 57 | TransactionUtil transactionUtil) { 58 | MqIdempotentAnnotationInterceptor advisor = new MqIdempotentAnnotationInterceptor(idempotentStrategy, alertStrategy, transactionUtil); 59 | return new MqIdempotentAnnotationAdvisor(advisor, Idempotent.class); 60 | } 61 | 62 | @ConditionalOnMissingBean(IdempotentConfig.class) 63 | @Bean 64 | public IdempotentConfig idempotentConfig() { 65 | IdempotentConfig idempotentConfig = new IdempotentConfig(); 66 | idempotentConfig.initConfig(properties); 67 | return idempotentConfig; 68 | } 69 | 70 | @Bean 71 | @ConditionalOnProperty(prefix = IdempotentProperties.PREFIX + ".", value = "alertName", havingValue = "lark") 72 | public AlertStrategy alertStrategy(IdempotentConfig idempotentConfig) { 73 | return AlertStrategyFactory.newInstance(idempotentConfig.getAlertName()); 74 | } 75 | 76 | @Bean 77 | @ConditionalOnClass(RedissonClient.class) 78 | @ConditionalOnProperty(prefix = IdempotentProperties.PREFIX + ".strategy", value = "jdbc", havingValue = "true") 79 | public AbstractIdempotentStrategy jdbcIdempotentStrategy(RedissonClient redissonClient, IdempotentConfig idempotentConfig, MessageConverter messageConverter) { 80 | return new RedisIdempotentStrategy(idempotentConfig, messageConverter, redissonClient); 81 | } 82 | 83 | @Bean 84 | @ConditionalOnClass(AbstractIdempotentStrategy.class) 85 | public AbstractIdempotentStrategy redisIdempotentStrategy(RedissonClient redissonClient, IdempotentConfig idempotentConfig, MessageConverter messageConverter) { 86 | return new RedisIdempotentStrategy(idempotentConfig, messageConverter, redissonClient); 87 | } 88 | 89 | @Bean 90 | public TransactionUtil transactionUtil(PlatformTransactionManager transactionManager) { 91 | return new TransactionUtil(transactionManager); 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/aop/MqIdempotentAnnotationInterceptor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.aop; 19 | 20 | import java.lang.reflect.Method; 21 | import java.util.Arrays; 22 | 23 | import com.mq.idempotent.core.alert.AlertDTO; 24 | import com.mq.idempotent.core.alert.strategy.AlertStrategy; 25 | import com.mq.idempotent.core.annotation.Idempotent; 26 | import com.mq.idempotent.core.exception.MessageConcurrencyException; 27 | import com.mq.idempotent.core.strategy.AbstractIdempotentStrategy; 28 | import com.mq.idempotent.core.utils.TransactionUtil; 29 | import lombok.extern.slf4j.Slf4j; 30 | import org.aopalliance.intercept.MethodInterceptor; 31 | import org.aopalliance.intercept.MethodInvocation; 32 | 33 | import org.springframework.beans.factory.annotation.Autowired; 34 | import org.springframework.util.ObjectUtils; 35 | 36 | /** 37 | * @author : wh 38 | * @date : 2021/12/21 18:51 39 | * @description: 40 | */ 41 | @Slf4j 42 | public class MqIdempotentAnnotationInterceptor implements MethodInterceptor { 43 | 44 | private final AbstractIdempotentStrategy idempotentStrategy; 45 | 46 | private final AlertStrategy alertStrategy; 47 | 48 | private final TransactionUtil transactionUtil; 49 | 50 | public MqIdempotentAnnotationInterceptor(AbstractIdempotentStrategy idempotentStrategy, @Autowired(required = false) AlertStrategy alertStrategy, TransactionUtil transactionUtil) { 51 | this.idempotentStrategy = idempotentStrategy; 52 | this.alertStrategy = alertStrategy; 53 | this.transactionUtil = transactionUtil; 54 | } 55 | 56 | @Override 57 | public Object invoke(MethodInvocation methodInvocation) throws Throwable { 58 | Method method = methodInvocation.getMethod(); 59 | 60 | // 判断返回值是不是void或者boolean 61 | final boolean isReturnVoid = method.getReturnType().equals(Void.TYPE); 62 | final boolean isReturnBoolean = method.getReturnType().equals(Boolean.TYPE); 63 | if (!isReturnVoid && !isReturnBoolean) { 64 | throw new Exception("method returnType is not boolean or void"); 65 | } 66 | //方法参数 67 | Object[] args = methodInvocation.getArguments(); 68 | Idempotent annotation = method.getAnnotation(Idempotent.class); 69 | String key = idempotentStrategy.getUniqueKey(Arrays.stream(args).findFirst() 70 | .orElseThrow(() -> new Exception("去重第一个参数不能为空")), annotation.field(), method, args); 71 | if (!idempotentStrategy.lock(key)) { 72 | log.info("有消息正在消费"); 73 | // 抛出异常依赖mq自动重试 74 | throw new MessageConcurrencyException("有消息正在消费"); 75 | } 76 | if (log.isDebugEnabled()) { 77 | log.info("唯一key {}", key); 78 | } 79 | if (idempotentStrategy.exitKey(key)) { 80 | log.warn("重复消费 {}", key); 81 | return isReturnVoid ? null : true; 82 | } 83 | 84 | Object res; 85 | try { 86 | if (annotation.transactional()) { 87 | res = transactionUtil.transact(() -> proceed(methodInvocation, key)); 88 | } else { 89 | res = proceed(methodInvocation, key); 90 | } 91 | } finally { 92 | idempotentStrategy.unlock(key); 93 | } 94 | return res; 95 | } 96 | 97 | public Object proceed(MethodInvocation methodInvocation, String key) { 98 | try { 99 | Object proceed = methodInvocation.proceed(); 100 | idempotentStrategy.save(key); 101 | return proceed; 102 | } catch (Throwable throwable) { 103 | // 监控 104 | if (!ObjectUtils.isEmpty(alertStrategy)) { 105 | alertStrategy.sendMsg(new AlertDTO(key, methodInvocation.getMethod(), throwable, idempotentStrategy.getWebHook())); 106 | } 107 | log.error("throwable ", throwable); 108 | throw new RuntimeException(throwable); 109 | } 110 | } 111 | 112 | } 113 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![img.png](static/img/logo.png) 2 | 3 | ## mq-Idempotent 4 | 5 | 通用的mq消息幂等去重框架,开箱即用,支持主流所有mq
6 | General mq message idempotent deduplication framework, out of the box, supports all mainstream mq 7 | 8 | English 9 | 10 | 1. 需要项目是Springboot项目 11 | 2. 原理很简单基于Spring AOP + Redis做的 12 | 3. 现在暂时只支持aliyun ons-client、RocketMQ Client、Spring Boot RabbitMQ.不支持的可以自己实现MessageConverter去定义,非常轻量方便 13 | 4. 由于目前源代码非常轻量,所以不引用jar直接copy源代码到项目中使用也是可以的 14 | 15 | > 由于本地没有rabbitmq、Kafka等,但是需要支持也非常方便,只需要实现MessageConverter 接口,后面有详细说明 16 | 17 | ## 分支说明 18 | - master: 最新稳定代码 19 | - develop: 正在开发中的代码 20 | 21 | ## 使用 22 | 23 | #### 24 | 必要条件 25 | 1. redis redis使用的是redission 26 | 2. springboot 27 | 3. jdk8+ 28 | 29 | #### 1. 添加依赖 30 | ##### rocketmq-client 31 | - maven 32 | ```xml 33 | 34 | io.github.weihubeats 35 | mq-idempotent-spring-boot-starter 36 | 1.1.6 37 | 38 | ``` 39 | - gradle 40 | ```xml 41 | implementation 'io.github.weihubeats:mq-idempotent-spring-boot-starter:1.1.6' 42 | ``` 43 | 44 | ##### aliyun ons-client 45 | ```xml 46 | 47 | io.github.weihubeats 48 | mq-idempotent-spring-boot-starter-aliyun-rocketmq 49 | 1.1.6 50 | 51 | ``` 52 | 53 | - gradle 54 | ```xml 55 | implementation 'io.github.weihubeats:mq-idempotent-spring-boot-starter-aliyun-rocketmq:1.1.6' 56 | ``` 57 | #### 2. 在需要幂等的方法上添加注解 58 | ```java 59 | @Idempotent 60 | ``` 61 | 62 | ```java 63 | @Idempotent 64 | public void testConsumer(Message message) { 65 | String msg = new String(message.getBody()); 66 | System.out.println("消息id " + message.getMsgID()); 67 | System.out.println("消息key " + message.getKey()); 68 | System.out.println("消费成功, msg " + msg); 69 | } 70 | ``` 71 | 72 | > 注意事项,由于是基于AOP实现的,所以需要注意AOP失效场景导致的问题 73 | > 添加住的方法的参数必须是 Message,方法返回值必须是void 或者 boolean 因为aop要处理重复消费直接返回 true 或者 null 74 | > 更详细实用方式请参考模块 wh-mq-Idempotent-samples 75 | 76 | ## 例子参考 77 | 使用例子请参考 wh-mq-Idempotent-samples 模块 78 | 79 | 80 | ## 自定义配置 81 | 82 | - 基于配置文件自动配置 83 | ```yaml 84 | idempotent: 85 | redisKey: "mq:unique:" 86 | redisValue : "ss" 87 | tryLockTime: 2 88 | redisTimeOut: 3 89 | ``` 90 | - 基于JavaBean 91 | ```java 92 | @Bean 93 | public IdempotentConfig idempotentConfig() { 94 | IdempotentConfig idempotentConfig = new IdempotentConfig(); 95 | // 去重 redis key名 默认 mq::unique:: 96 | idempotentConfig.setRedisKey(redisKey); 97 | // 去重redis value 默认 s 98 | idempotentConfig.setRedisValue(redisValue); 99 | // 去重redis尝试获取锁等待时间 默认1s 单位秒 100 | idempotentConfig.setTryLockTime(tryLockTime); 101 | // 设置处理成功消息存放redis时间 默认 3天 102 | idempotentConfig.setRedisTimeOut(redisTimeOut); 103 | return idempotentConfig; 104 | 105 | } 106 | ``` 107 | 108 | ## 自定义mq去重 109 | 引入依赖 110 | ```java 111 | 112 | io.github.weihubeats 113 | mq-idempotent-core 114 | 1.1.6 115 | 116 | ``` 117 | 118 | 实现接口 MessageConverter.java 119 | 例如 支持rocketMQ 120 | ```java 121 | @Component 122 | public class RocketMQMessageConverter implements MessageConverter { 123 | 124 | 125 | @Override 126 | public String getUniqueKey(MessageExt messageExt, String field, Method method, Object[] args) { 127 | return StringUtils.isNoneBlank(messageExt.getKeys()) ? messageExt.getKeys() :messageExt.getMsgId(); 128 | } 129 | } 130 | 131 | ``` 132 | 133 | ## 模块说明 134 | - wh-core 核心实现 135 | - wh-mq-rocketmq rocketmq幂等核心实现 136 | - wh-mq-aliyun-rocketmq 阿里云client幂等核心实现 137 | - wh-mq-Idempotent-samples 使用例子 138 | 139 | ## 设计思路 140 | 141 | ![img.png](static/img/img.png) 142 | 143 | 目前redis实现的大致思路如下 144 | 1. 消费者获取到MQ消费信息 145 | 2. 基于配置的MQ消息中的业务唯一键去reids(Mysql) 判断是否已消费 146 | 3. 如果没有消费则加锁防止并发问题,加锁成功则消费,失败则返回消费失败让MQ重新投递,因为这里防止第一个抢到锁的线程执行失败,所以不能直接返回成功,需要后面的任务重新投递到MQ重新消费 147 | 4. 执行业务代码成功后写入消费成功(写入redis或更新Mysql) 148 | 5. 释放锁 149 | 150 | ## 发布版本 151 | 152 | - 1.0.4:支持阿里云RocketMQ Client 153 | - 1.0.5:新增支持开源RocketMQ Client,新增自动化配置 `IdempotentConfig.java` 154 | - 1.0.6:优化重复消费重复投递问题,优化代码结构 155 | - 1.0.7:优化redis key 过期时间 156 | - 1.0.8:优化AOP实现方式 157 | - 1.1.9:添加对Spring Boot RabbitMQ支持 158 | - 1.1.0-Release:优化消息存储细节,自定义消息转换策略添加methdo、args 159 | - 1.1.1-Release: 修复分布式锁异常问题 160 | - 1.1.2-Release: 修复分布式锁异常问题 161 | - 1.1.4-Release: 添加事务幂等处理,添加飞书报警,添加自定义spi异常报警 162 | 163 | ## 未来版本 164 | 1. 支持kafka 165 | 2. 支持Mysql去重 166 | 3. 网络两将军问题处理 167 | ## 正在开发中。。。。。。 168 | 169 | ## 期待你的加入 170 | 171 | 作者微信: 172 | ![作者微信](static/img/wx.jpg) 173 | 174 | ## 鸣谢 175 | 176 | 感谢 JetBrains 提供的免费开源 License: 177 | 178 |

179 | 图片引用自lets-mica 180 |

-------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/utils/ReflectUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.utils; 19 | 20 | import org.springframework.util.Assert; 21 | import org.springframework.util.StringUtils; 22 | 23 | import java.lang.reflect.AccessibleObject; 24 | import java.lang.reflect.Field; 25 | 26 | /** 27 | * @author : wh 28 | * @date : 2022/1/12 11:37 29 | * @description: 30 | */ 31 | public class ReflectUtil { 32 | 33 | /** 34 | * 字段缓存 35 | */ 36 | private static final SimpleCache, Field[]> FIELDS_CACHE = new SimpleCache<>(); 37 | 38 | /** 39 | * 获取字段值 40 | * 41 | * @param obj 对象,如果static字段,此处为类 42 | * @param fieldName 字段名 43 | * @return 字段值 44 | * @throws Exception 包装IllegalAccessException异常 45 | */ 46 | public static Object getFieldValue(Object obj, String fieldName) throws Exception { 47 | if (null == obj || StringUtils.isEmpty(fieldName)) { 48 | return null; 49 | } 50 | return getFieldValue(obj, getField(obj instanceof Class ? (Class) obj : obj.getClass(), fieldName)); 51 | } 52 | 53 | /** 54 | * 获取字段值 55 | * 56 | * @param obj 对象,static字段则此字段为null 57 | * @param field 字段 58 | * @return 字段值 59 | */ 60 | public static Object getFieldValue(Object obj, Field field) throws Exception { 61 | if (null == field) { 62 | return null; 63 | } 64 | if (obj instanceof Class) { 65 | // 静态字段获取时对象为null 66 | obj = null; 67 | } 68 | 69 | setAccessible(field); 70 | Object result; 71 | result = field.get(obj); 72 | return result; 73 | } 74 | 75 | /** 76 | * 设置方法为可访问(私有方法可以被外部调用) 77 | * 78 | * @param AccessibleObject的子类,比如Class、Method、Field等 79 | * @param accessibleObject 可设置访问权限的对象,比如Class、Method、Field等 80 | * @return 被设置可访问的对象 81 | */ 82 | public static T setAccessible(T accessibleObject) { 83 | if (null != accessibleObject && false == accessibleObject.isAccessible()) { 84 | accessibleObject.setAccessible(true); 85 | } 86 | return accessibleObject; 87 | } 88 | 89 | /** 90 | * 查找指定类中的指定name的字段(包括非public字段),也包括父类和Object类的字段, 字段不存在则返回{@code null} 91 | * 92 | * @param beanClass 被查找字段的类,不能为null 93 | * @param name 字段名 94 | * @return 字段 95 | * @throws SecurityException 安全异常 96 | */ 97 | public static Field getField(Class beanClass, String name) throws SecurityException { 98 | final Field[] fields = getFields(beanClass); 99 | return ArrayUtil.firstMatch((field) -> name.equals(getFieldName(field)), fields); 100 | } 101 | 102 | /** 103 | * 获取字段名 104 | * 105 | * @param field 字段 106 | * @return 字段名 107 | */ 108 | public static String getFieldName(Field field) { 109 | if (null == field) { 110 | return null; 111 | } 112 | return field.getName(); 113 | } 114 | 115 | /** 116 | * 获得一个类中所有字段列表,包括其父类中的字段
117 | * 如果子类与父类中存在同名字段,则这两个字段同时存在,子类字段在前,父类字段在后。 118 | * 119 | * @param beanClass 类 120 | * @return 字段列表 121 | * @throws SecurityException 安全检查异常 122 | */ 123 | public static Field[] getFields(Class beanClass) throws SecurityException { 124 | Assert.notNull(beanClass); 125 | return FIELDS_CACHE.get(beanClass, () -> getFieldsDirectly(beanClass, true)); 126 | } 127 | 128 | /** 129 | * 获得一个类中所有字段列表,直接反射获取,无缓存
130 | * 如果子类与父类中存在同名字段,则这两个字段同时存在,子类字段在前,父类字段在后。 131 | * 132 | * @param beanClass 类 133 | * @param withSuperClassFields 是否包括父类的字段列表 134 | * @return 字段列表 135 | * @throws SecurityException 安全检查异常 136 | */ 137 | public static Field[] getFieldsDirectly(Class beanClass, boolean withSuperClassFields) throws SecurityException { 138 | Assert.notNull(beanClass); 139 | 140 | Field[] allFields = null; 141 | Class searchType = beanClass; 142 | Field[] declaredFields; 143 | while (searchType != null) { 144 | declaredFields = searchType.getDeclaredFields(); 145 | if (null == allFields) { 146 | allFields = declaredFields; 147 | } else { 148 | allFields = ArrayUtil.append(allFields, declaredFields); 149 | } 150 | searchType = withSuperClassFields ? searchType.getSuperclass() : null; 151 | } 152 | 153 | return allFields; 154 | } 155 | 156 | } 157 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/aop/MqIdempotentAnnotationAdvisor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.aop; 19 | 20 | import lombok.NonNull; 21 | import org.aopalliance.aop.Advice; 22 | import org.aopalliance.intercept.MethodInterceptor; 23 | import org.springframework.aop.ClassFilter; 24 | import org.springframework.aop.MethodMatcher; 25 | import org.springframework.aop.Pointcut; 26 | import org.springframework.aop.support.AbstractPointcutAdvisor; 27 | import org.springframework.aop.support.AopUtils; 28 | import org.springframework.aop.support.ComposablePointcut; 29 | import org.springframework.aop.support.StaticMethodMatcher; 30 | import org.springframework.aop.support.annotation.AnnotationMatchingPointcut; 31 | import org.springframework.beans.BeansException; 32 | import org.springframework.beans.factory.BeanFactory; 33 | import org.springframework.beans.factory.BeanFactoryAware; 34 | import org.springframework.core.annotation.AnnotatedElementUtils; 35 | import org.springframework.util.Assert; 36 | 37 | import java.lang.annotation.Annotation; 38 | import java.lang.reflect.Method; 39 | import java.lang.reflect.Proxy; 40 | 41 | /** 42 | * @author : wh 43 | * @date : 2021/12/30 17:58 44 | * @description: 45 | */ 46 | public class MqIdempotentAnnotationAdvisor extends AbstractPointcutAdvisor implements BeanFactoryAware { 47 | 48 | private final Advice advice; 49 | 50 | private final Pointcut pointcut; 51 | 52 | private final Class annotation; 53 | 54 | public MqIdempotentAnnotationAdvisor(@NonNull MethodInterceptor advice, 55 | @NonNull Class annotation) { 56 | this.advice = advice; 57 | this.annotation = annotation; 58 | this.pointcut = buildPointcut(); 59 | } 60 | 61 | @Override 62 | public Pointcut getPointcut() { 63 | return this.pointcut; 64 | } 65 | 66 | @Override 67 | public Advice getAdvice() { 68 | return this.advice; 69 | } 70 | 71 | @Override 72 | public void setBeanFactory(BeanFactory beanFactory) throws BeansException { 73 | if (this.advice instanceof BeanFactoryAware) { 74 | ((BeanFactoryAware) this.advice).setBeanFactory(beanFactory); 75 | } 76 | } 77 | 78 | private Pointcut buildPointcut() { 79 | Pointcut cpc = new AnnotationMatchingPointcut(annotation, true); 80 | Pointcut mpc = new AnnotationMethodPoint(annotation); 81 | return new ComposablePointcut(cpc).union(mpc); 82 | } 83 | 84 | /** 85 | * In order to be compatible with the spring lower than 5.0 86 | */ 87 | private static class AnnotationMethodPoint implements Pointcut { 88 | 89 | private final Class annotationType; 90 | 91 | public AnnotationMethodPoint(Class annotationType) { 92 | Assert.notNull(annotationType, "Annotation type must not be null"); 93 | this.annotationType = annotationType; 94 | } 95 | 96 | @Override 97 | public ClassFilter getClassFilter() { 98 | return ClassFilter.TRUE; 99 | } 100 | 101 | @Override 102 | public MethodMatcher getMethodMatcher() { 103 | return new AnnotationMethodMatcher(annotationType); 104 | } 105 | 106 | private static class AnnotationMethodMatcher extends StaticMethodMatcher { 107 | 108 | private final Class annotationType; 109 | 110 | public AnnotationMethodMatcher(Class annotationType) { 111 | this.annotationType = annotationType; 112 | } 113 | 114 | @Override 115 | public boolean matches(Method method, Class targetClass) { 116 | if (matchesMethod(method)) { 117 | return true; 118 | } 119 | // Proxy classes never have annotations on their redeclared methods. 120 | if (Proxy.isProxyClass(targetClass)) { 121 | return false; 122 | } 123 | // The method may be on an interface, so let's check on the target class as well. 124 | Method specificMethod = AopUtils.getMostSpecificMethod(method, targetClass); 125 | return (specificMethod != method && matchesMethod(specificMethod)); 126 | } 127 | 128 | private boolean matchesMethod(Method method) { 129 | return AnnotatedElementUtils.hasAnnotation(method, this.annotationType); 130 | } 131 | } 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /README_en.md: -------------------------------------------------------------------------------- 1 | ## wh-mq-Idempotent 2 | 3 | 4 | 5 | 6 | General mq message idempotent deduplication framework, out of the box, supports all mainstream mq 7 | 1. The required project is a Springboot project 8 | 2. The principle is very simple, based on Spring AOP + Redis 9 | 3. For now, only aliyun ons-client and RocketMQ Client are supported. If not, you can implement MessageConverter to define it yourself, 10 | which is very lightweight and convenient 11 | 4. Since the current source code is very lightweight, it is also possible to copy the source code directly into the project without quoting the jar. 12 | 13 | > Since there are no rabbitmq, Kafka, etc. locally, it is also very convenient to support it. You only need to implement the MessageConverter interface, 14 | > which will be explained in detail later. 15 | 16 | 17 | 18 | ## use 19 | 20 | #### 21 | Necessary condition 22 | 1. redis(redission) 23 | 2. springboot 24 | 3. jdk8+ 25 | 26 | #### 1. Add dependency 27 | ##### rocketmq-client 28 | - maven 29 | ```xml 30 | 31 | io.github.weihubeats 32 | wh-mq-rocketmq 33 | 1.1.4-Release 34 | 35 | ``` 36 | - gradle 37 | ```xml 38 | implementation 'io.github.weihubeats:wh-mq-rocketmq:1.1.4-Release' 39 | ``` 40 | 41 | ##### aliyun ons-client 42 | ```xml 43 | 44 | io.github.weihubeats 45 | wh-mq-aliyun-rocketmq 46 | 1.1.4-Release 47 | 48 | ``` 49 | 50 | - gradle 51 | ```xml 52 | implementation 'io.github.weihubeats:wh-mq-aliyun-rocketmq' 53 | ``` 54 | #### 2. Add annotations to methods that need to be idempotent 55 | ```java 56 | @Idempotent 57 | ``` 58 | 59 | ```java 60 | @Idempotent 61 | public void testConsumer(Message message) { 62 | String msg = new String(message.getBody()); 63 | System.out.println("消息id " + message.getMsgID()); 64 | System.out.println("消息key " + message.getKey()); 65 | System.out.println("消费成功, msg " + msg); 66 | } 67 | ``` 68 | 69 | > Note, because it is implemented based on AOP, you need to pay attention to the problems caused by AOP failure scenarios 70 | > The parameter of the added method must be Message, and the return value of the method must be void or boolean, 71 | > because aop needs to handle repeated consumption and directly returns true or null 72 | > For more detailed practical methods, please refer to the module wh-mq-Idempotent-samples 73 | 74 | ## Example reference 75 | For usage examples, please refer to the wh-mq-Idempotent-samples module 76 | 77 | 78 | ## Custom configuration 79 | ```java 80 | @Bean 81 | public IdempotentConfig idempotentConfig() { 82 | IdempotentConfig idempotentConfig = new IdempotentConfig(); 83 | // 去重 redis key名 默认 mq::unique:: 84 | idempotentConfig.setRedisKey(redisKey); 85 | // 去重redis value 默认 s 86 | idempotentConfig.setRedisValue(redisValue); 87 | // 去重redis尝试获取锁等待时间 默认1s 单位秒 88 | idempotentConfig.setTryLockTime(tryLockTime); 89 | // 设置处理成功消息存放redis时间 默认 3天 90 | idempotentConfig.setRedisTimeOut(redisTimeOut); 91 | return idempotentConfig; 92 | 93 | } 94 | ``` 95 | 96 | ## Custom mq de-duplication 97 | Introduce dependencies 98 | ```java 99 | 100 | io.github.weihubeats 101 | wh-core 102 | 1.1.4-Release 103 | 104 | ``` 105 | 106 | Implement the interface MessageConverter.java 107 | For example, support rocketMQ 108 | ```java 109 | @Component 110 | public class RocketMQMessageConverter implements MessageConverter { 111 | 112 | 113 | @Override 114 | public String getUniqueKey(MessageExt messageExt) { 115 | return !StringUtils.isEmpty(messageExt.getKeys()) ? messageExt.getKeys() :messageExt.getMsgId(); 116 | } 117 | } 118 | 119 | ``` 120 | 121 | ## Module description 122 | - wh-core Core realization 123 | - wh-mq-rocketmq Rocketmq idempotent core implementation 124 | - wh-mq-aliyun-rocketmq aliyun client idempotent core implementation 125 | - wh-mq-Idempotent-samples Usage example 126 | 127 | ## Design ideas 128 | 129 | ![img.png](static/img/img.png) 130 | 131 | The general idea of the current redis implementation is as follows 132 | 1. Consumers get MQ consumption information 133 | 2. Based on the business unique key in the configured MQ message, go to reids (Mysql) to determine whether it has been consumed 134 | 3. If there is no consumption, then lock to prevent concurrency problems, if the lock is successful, then consume, if it fails, 135 | return the consumption failure to let MQ re-deliver, because this prevents the first thread that grabs the lock from failing to execute, 136 | so it cannot directly return to success, and the following tasks are required. Re-deliver to MQ for re-consumption 137 | 4. After successful execution of the business code, write consumption is successful (write redis or update Mysql) 138 | 5. Release lock 139 | 140 | ## release version 141 | 142 | - 1.0.4 : Support Alibaba Cloud RocketMQ Client 143 | - 1.0.5 : Added support for open source RocketMQ Client, and added automatic configuration `IdempotentConfig.java` 144 | - 1.0.6 : Optimize the problem of repeated consumption and repeated delivery, and optimize the code structure 145 | - 1.1.4-Release : Optimize the redis key expiration time 146 | 147 | ## Future version 148 | 149 | 1. Support RabbitMQ 150 | 2. Support kafka 151 | 3. Support Mysql de-duplication 152 | ## in development。。。。。。 153 | 154 | ## We hope you can join 155 | 156 | Author WeChat: 157 | ![Author WeChat](static/img/wx.jpg) -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/utils/SimpleCache.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.utils; 19 | 20 | import java.io.Serializable; 21 | import java.util.Iterator; 22 | import java.util.Map; 23 | import java.util.WeakHashMap; 24 | import java.util.concurrent.ConcurrentHashMap; 25 | import java.util.concurrent.locks.Lock; 26 | import java.util.concurrent.locks.ReentrantLock; 27 | import java.util.concurrent.locks.ReentrantReadWriteLock; 28 | import java.util.function.Predicate; 29 | 30 | /** 31 | * 简单缓存,无超时实现,默认使用{@link WeakHashMap}实现缓存自动清理 32 | * 33 | * @param 键类型 34 | * @param 值类型 35 | * @author Looly 36 | */ 37 | public class SimpleCache implements Iterable>, Serializable { 38 | 39 | private static final long serialVersionUID = 1L; 40 | 41 | /** 42 | * 池 43 | */ 44 | private final Map cache; 45 | // 乐观读写锁 46 | private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); 47 | /** 48 | * 写的时候每个key一把锁,降低锁的粒度 49 | */ 50 | protected final Map keyLockMap = new ConcurrentHashMap<>(); 51 | 52 | /** 53 | * 构造,默认使用{@link WeakHashMap}实现缓存自动清理 54 | */ 55 | public SimpleCache() { 56 | this(new WeakHashMap<>()); 57 | } 58 | 59 | /** 60 | * 构造 61 | *

62 | * 通过自定义Map初始化,可以自定义缓存实现。
63 | * 比如使用{@link WeakHashMap}则会自动清理key,使用HashMap则不会清理
64 | * 同时,传入的Map对象也可以自带初始化的键值对,防止在get时创建 65 | *

66 | * 67 | * @param initMap 初始Map,用于定义Map类型 68 | */ 69 | public SimpleCache(Map initMap) { 70 | this.cache = initMap; 71 | } 72 | 73 | /** 74 | * 从缓存池中查找值 75 | * 76 | * @param key 键 77 | * @return 值 78 | */ 79 | public V get(K key) { 80 | lock.readLock().lock(); 81 | try { 82 | return cache.get(key); 83 | } finally { 84 | lock.readLock().unlock(); 85 | } 86 | } 87 | 88 | /** 89 | * 从缓存中获得对象,当对象不在缓存中或已经过期返回Func0回调产生的对象 90 | * 91 | * @param key 键 92 | * @param supplier 如果不存在回调方法,用于生产值对象 93 | * @return 值对象 94 | */ 95 | public V get(K key, Func0 supplier) { 96 | return get(key, null, supplier); 97 | } 98 | 99 | /** 100 | * 从缓存中获得对象,当对象不在缓存中或已经过期返回Func0回调产生的对象 101 | * 102 | * @param key 键 103 | * @param validPredicate 检查结果对象是否可用,如是否断开连接等 104 | * @param supplier 如果不存在回调方法或结果不可用,用于生产值对象 105 | * @return 值对象 106 | */ 107 | public V get(K key, Predicate validPredicate, Func0 supplier) { 108 | V v = get(key); 109 | if (null == v && null != supplier) { 110 | // 每个key单独获取一把锁,降低锁的粒度提高并发能力,see pr#1385@Github 111 | final Lock keyLock = keyLockMap.computeIfAbsent(key, k -> new ReentrantLock()); 112 | keyLock.lock(); 113 | try { 114 | // 双重检查,防止在竞争锁的过程中已经有其它线程写入 115 | v = cache.get(key); 116 | if (null == v || (null != validPredicate && false == validPredicate.test(v))) { 117 | try { 118 | v = supplier.call(); 119 | } catch (Exception e) { 120 | throw new RuntimeException(e); 121 | } 122 | put(key, v); 123 | } 124 | } finally { 125 | keyLock.unlock(); 126 | keyLockMap.remove(key); 127 | } 128 | } 129 | 130 | return v; 131 | } 132 | 133 | /** 134 | * 放入缓存 135 | * 136 | * @param key 键 137 | * @param value 值 138 | * @return 值 139 | */ 140 | public V put(K key, V value) { 141 | // 独占写锁 142 | lock.writeLock().lock(); 143 | try { 144 | cache.put(key, value); 145 | } finally { 146 | lock.writeLock().unlock(); 147 | } 148 | return value; 149 | } 150 | 151 | /** 152 | * 移除缓存 153 | * 154 | * @param key 键 155 | * @return 移除的值 156 | */ 157 | public V remove(K key) { 158 | // 独占写锁 159 | lock.writeLock().lock(); 160 | try { 161 | return cache.remove(key); 162 | } finally { 163 | lock.writeLock().unlock(); 164 | } 165 | } 166 | 167 | /** 168 | * 清空缓存池 169 | */ 170 | public void clear() { 171 | // 独占写锁 172 | lock.writeLock().lock(); 173 | try { 174 | this.cache.clear(); 175 | } finally { 176 | lock.writeLock().unlock(); 177 | } 178 | } 179 | 180 | @Override 181 | public Iterator> iterator() { 182 | return this.cache.entrySet().iterator(); 183 | } 184 | } 185 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | io.github.weihubeats 8 | mq-idempotent 9 | pom 10 | ${revision} 11 | 12 | mq-idempotent 13 | mq-Idempotent 14 | https://github.com/weihubeats/mq-idempotent 15 | 16 | 17 | 18 | mq-Idempotent-core 19 | mq-idempotent-samples 20 | mq-idempotent-spring-boot-starter 21 | mq-idempotent-dependencies 22 | 23 | 24 | 25 | 1.1.6 26 | 1.2.7 27 | 3.0.1 28 | 29 | 30 | 31 | 32 | 33 | io.github.weihubeats 34 | mq-idempotent-dependencies 35 | ${revision} 36 | pom 37 | import 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | The Apache Software License, Version 2.0 47 | http://www.apache.org/licenses/LICENSE-2.0.txt 48 | repo 49 | 50 | 51 | 52 | 53 | 54 | weihubeats 55 | weihubeats@163.com 56 | https://weihubeats.blog.csdn.net/ 57 | 58 | 59 | 60 | 61 | scm:git@github.com:weihubeats/mq-idempotent.git 62 | scm:git@github.com:weihubeats/mq-idempotent.git 63 | git@github.com:weihubeats/mq-idempotent.git 64 | 65 | 66 | 67 | Github Issue 68 | https://github.com/weihubeats/mq-idempotent/issues 69 | 70 | 71 | 72 | 73 | 74 | 75 | org.apache.maven.plugins 76 | maven-compiler-plugin 77 | 3.5.1 78 | 79 | 1.8 80 | 1.8 81 | 82 | 83 | 84 | org.apache.maven.plugins 85 | maven-surefire-plugin 86 | 2.18.1 87 | 88 | true 89 | 90 | 91 | 92 | org.apache.maven.plugins 93 | maven-source-plugin 94 | 2.4 95 | 96 | true 97 | 98 | 99 | 100 | package 101 | 102 | jar-no-fork 103 | 104 | 105 | 106 | 107 | 108 | org.apache.maven.plugins 109 | maven-site-plugin 110 | 3.7.1 111 | 112 | 113 | 114 | org.apache.maven.plugins 115 | maven-javadoc-plugin 116 | 3.0.1 117 | 118 | ${java.home}/bin/javadoc 119 | 120 | -Xdoclint:none 121 | 122 | 123 | 124 | 125 | package 126 | 127 | jar 128 | 129 | 130 | 131 | 132 | 133 | 134 | org.apache.maven.plugins 135 | maven-gpg-plugin 136 | ${maven-gpg-plugin.version} 137 | 138 | 139 | sign-artifacts 140 | verify 141 | 142 | sign 143 | 144 | 145 | 146 | 147 | 148 | 149 | org.codehaus.mojo 150 | flatten-maven-plugin 151 | ${flatten-maven-plugin.version} 152 | 153 | true 154 | resolveCiFriendliesOnly 155 | 156 | 157 | 158 | flatten 159 | process-resources 160 | 161 | flatten 162 | 163 | 164 | 165 | flatten.clean 166 | clean 167 | 168 | clean 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | sonatype 179 | https://s01.oss.sonatype.org/content/repositories/snapshots/ 180 | 181 | 182 | sonatype 183 | https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ 184 | 185 | 186 | 187 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/utils/ArrayUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.utils; 19 | 20 | import org.springframework.util.Assert; 21 | 22 | import java.lang.reflect.Array; 23 | 24 | /** 25 | * @author : wh 26 | * @date : 2022/1/12 11:46 27 | * @description: 28 | */ 29 | public class ArrayUtil { 30 | 31 | /** 32 | * 数组中元素未找到的下标,值为-1 33 | */ 34 | public static final int INDEX_NOT_FOUND = -1; 35 | 36 | public static Object insert(Object array, int index, T... newElements) { 37 | if (isEmpty(newElements)) { 38 | return array; 39 | } 40 | if (isEmpty(array)) { 41 | return newElements; 42 | } 43 | 44 | final int len = length(array); 45 | if (index < 0) { 46 | index = (index % len) + len; 47 | } 48 | 49 | final T[] result = newArray(array.getClass().getComponentType(), Math.max(len, index) + newElements.length); 50 | System.arraycopy(array, 0, result, 0, Math.min(len, index)); 51 | System.arraycopy(newElements, 0, result, index, newElements.length); 52 | if (index < len) { 53 | System.arraycopy(array, index, result, index + newElements.length, len - index); 54 | } 55 | return result; 56 | } 57 | 58 | public static boolean isEmpty(T[] array) { 59 | return array == null || array.length == 0; 60 | } 61 | 62 | public static boolean isArray(Object obj) { 63 | return null != obj && obj.getClass().isArray(); 64 | } 65 | 66 | public static T[] newArray(Class componentType, int newSize) { 67 | return (T[]) Array.newInstance(componentType, newSize); 68 | } 69 | 70 | public static int length(Object array) throws IllegalArgumentException { 71 | if (null == array) { 72 | return 0; 73 | } 74 | return Array.getLength(array); 75 | } 76 | 77 | public static boolean isEmpty(Object array) { 78 | if (array != null) { 79 | if (isArray(array)) { 80 | return 0 == Array.getLength(array); 81 | } 82 | return false; 83 | } 84 | return true; 85 | } 86 | 87 | /** 88 | * 返回数组中第一个匹配规则的值 89 | * 90 | * @param 数组元素类型 91 | * @param matcher 匹配接口,实现此接口自定义匹配规则 92 | * @param array 数组 93 | * @return 匹配元素,如果不存在匹配元素或数组为空,返回 {@code null} 94 | */ 95 | @SuppressWarnings("unchecked") 96 | public static T firstMatch(Matcher matcher, T... array) { 97 | final int index = matchIndex(matcher, array); 98 | if (index < 0) { 99 | return null; 100 | } 101 | 102 | return array[index]; 103 | } 104 | 105 | /** 106 | * 返回数组中第一个匹配规则的值的位置 107 | * 108 | * @param 数组元素类型 109 | * @param matcher 匹配接口,实现此接口自定义匹配规则 110 | * @param array 数组 111 | * @return 匹配到元素的位置,-1表示未匹配到 112 | */ 113 | @SuppressWarnings("unchecked") 114 | public static int matchIndex(Matcher matcher, T... array) { 115 | return matchIndex(matcher, 0, array); 116 | } 117 | 118 | /** 119 | * 返回数组中第一个匹配规则的值的位置 120 | * 121 | * @param 数组元素类型 122 | * @param matcher 匹配接口,实现此接口自定义匹配规则 123 | * @param beginIndexInclude 检索开始的位置 124 | * @param array 数组 125 | * @return 匹配到元素的位置,-1表示未匹配到 126 | */ 127 | @SuppressWarnings("unchecked") 128 | public static int matchIndex(Matcher matcher, int beginIndexInclude, T... array) { 129 | Assert.notNull(matcher, "Matcher must be not null !"); 130 | if (isNotEmpty(array)) { 131 | for (int i = beginIndexInclude; i < array.length; i++) { 132 | if (matcher.match(array[i])) { 133 | return i; 134 | } 135 | } 136 | } 137 | 138 | return INDEX_NOT_FOUND; 139 | } 140 | 141 | /** 142 | * 数组是否为非空 143 | * 144 | * @param 数组元素类型 145 | * @param array 数组 146 | * @return 是否为非空 147 | */ 148 | public static boolean isNotEmpty(T[] array) { 149 | return (null != array && array.length != 0); 150 | } 151 | 152 | /** 153 | * 将新元素添加到已有数组中
154 | * 添加新元素会生成一个新的数组,不影响原数组 155 | * 156 | * @param 数组元素类型 157 | * @param buffer 已有数组 158 | * @param newElements 新元素 159 | * @return 新数组 160 | */ 161 | @SafeVarargs 162 | public static T[] append(T[] buffer, T... newElements) { 163 | if (isEmpty(buffer)) { 164 | return newElements; 165 | } 166 | return insert(buffer, buffer.length, newElements); 167 | } 168 | 169 | /** 170 | * 将新元素插入到到已有数组中的某个位置
171 | * 添加新元素会生成一个新的数组,不影响原数组
172 | * 如果插入位置为为负数,从原数组从后向前计数,若大于原数组长度,则空白处用null填充 173 | * 174 | * @param 数组元素类型 175 | * @param buffer 已有数组 176 | * @param index 插入位置,此位置为对应此位置元素之前的空档 177 | * @param newElements 新元素 178 | * @return 新数组 179 | */ 180 | @SuppressWarnings("unchecked") 181 | public static T[] insert(T[] buffer, int index, T... newElements) { 182 | return (T[]) insert((Object) buffer, index, newElements); 183 | } 184 | 185 | } 186 | -------------------------------------------------------------------------------- /mq-idempotent-dependencies/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 4.0.0 7 | pom 8 | ${revision} 9 | 10 | io.github.weihubeats 11 | mq-idempotent-dependencies 12 | 13 | mq-idempotent 14 | mq-Idempotent 15 | https://github.com/weihubeats/mq-idempotent 16 | 17 | 18 | 8 19 | 8 20 | UTF-8 21 | 1.8 22 | 2.6.8 23 | 1.0 24 | 1.18.4 25 | 3.17.3 26 | 3.12.0 27 | 3.5.15 28 | 5.8.2 29 | 3.7.0 30 | 1.8.8.1.Final 31 | 3.5.2 32 | 42.3.3 33 | 4.7.0 34 | 1.1.6 35 | 1.2.7 36 | 3.0.1 37 | 38 | 39 | 40 | 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-dependencies 45 | ${spring-boot.version} 46 | pom 47 | import 48 | 49 | 50 | 51 | org.projectlombok 52 | lombok 53 | ${lombok.version} 54 | 55 | 56 | org.redisson 57 | redisson 58 | ${redission.version} 59 | 60 | 61 | org.apache.commons 62 | commons-lang3 63 | ${common.lang3} 64 | 65 | 66 | 67 | com.squareup.okhttp3 68 | okhttp 69 | ${okhttp.version} 70 | 71 | 72 | 73 | org.junit.jupiter 74 | junit-jupiter 75 | ${junit-jupiter.version} 76 | test 77 | 78 | 79 | org.mockito 80 | mockito-junit-jupiter 81 | ${mockito.version} 82 | test 83 | 84 | 85 | org.mockito 86 | mockito-core 87 | ${mockito.version} 88 | test 89 | 90 | 91 | org.mockito 92 | mockito-inline 93 | ${mockito.version} 94 | test 95 | 96 | 97 | com.aliyun.openservices 98 | ons-client 99 | ${ons-client.version} 100 | 101 | 102 | 103 | org.postgresql 104 | postgresql 105 | ${org.postgresql.version} 106 | 107 | 108 | com.baomidou 109 | mybatis-plus-boot-starter 110 | ${mybatis-plus-boot-starter.version} 111 | 112 | 113 | org.apache.rocketmq 114 | rocketmq-client 115 | ${rocketmq-client.version} 116 | 117 | 118 | 119 | io.github.weihubeats 120 | mq-idempotent-core 121 | ${revision} 122 | 123 | 124 | io.github.weihubeats 125 | mq-idempotent-spring-boot-starter-aliyun-rocketmq 126 | ${revision} 127 | 128 | 129 | io.github.weihubeats 130 | mq-idempotent-spring-boot-starter-rabbitmq 131 | ${revision} 132 | 133 | 134 | io.github.weihubeats 135 | mq-idempotent-spring-boot-starter-rocketmq 136 | ${revision} 137 | 138 | 139 | 140 | 141 | 142 | scm:git@github.com:weihubeats/mq-idempotent.git 143 | scm:git@github.com:weihubeats/mq-idempotent.git 144 | git@github.com:weihubeats/mq-idempotent.git 145 | 146 | 147 | 148 | 149 | weihubeats 150 | weihubeats@163.com 151 | https://weihubeats.blog.csdn.net/ 152 | 153 | 154 | 155 | 156 | 157 | The Apache Software License, Version 2.0 158 | http://www.apache.org/licenses/LICENSE-2.0.txt 159 | repo 160 | 161 | 162 | 163 | 164 | 165 | 166 | org.codehaus.mojo 167 | flatten-maven-plugin 168 | ${flatten-maven-plugin.version} 169 | 170 | true 171 | resolveCiFriendliesOnly 172 | 173 | 174 | 175 | flatten 176 | process-resources 177 | 178 | flatten 179 | 180 | 181 | 182 | flatten.clean 183 | clean 184 | 185 | clean 186 | 187 | 188 | 189 | 190 | 191 | 192 | org.apache.maven.plugins 193 | maven-gpg-plugin 194 | ${maven-gpg-plugin.version} 195 | 196 | 197 | sign-artifacts 198 | verify 199 | 200 | sign 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | sonatype 213 | https://s01.oss.sonatype.org/content/repositories/snapshots/ 214 | 215 | 216 | sonatype 217 | https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ 218 | 219 | 220 | 221 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /mq-idempotent-core/src/main/java/com/mq/idempotent/core/spi/ExtensionLoader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.mq.idempotent.core.spi; 19 | 20 | import java.io.IOException; 21 | import java.io.InputStream; 22 | import java.net.URL; 23 | import java.util.ArrayList; 24 | import java.util.Collections; 25 | import java.util.Enumeration; 26 | import java.util.HashMap; 27 | import java.util.List; 28 | import java.util.Map; 29 | import java.util.Objects; 30 | import java.util.Properties; 31 | import java.util.concurrent.ConcurrentHashMap; 32 | import java.util.stream.Collectors; 33 | 34 | import lombok.extern.slf4j.Slf4j; 35 | import org.apache.commons.lang3.StringUtils; 36 | 37 | /** 38 | * @author : wh 39 | * @date : 2022/6/15 15:31 40 | * @description: 41 | * @see ExtensionLoader 42 | */ 43 | @Slf4j 44 | public class ExtensionLoader { 45 | 46 | private static final String WH_DIRECTORY = "META-INF/wh/"; 47 | 48 | private static final Map, ExtensionLoader> LOADERS = new ConcurrentHashMap<>(); 49 | 50 | private final Class clazz; 51 | 52 | private final ClassLoader classLoader; 53 | 54 | private final Holder>> cachedClasses = new Holder<>(); 55 | 56 | private final Map> cachedInstances = new ConcurrentHashMap<>(); 57 | 58 | private final Map, Object> joinInstances = new ConcurrentHashMap<>(); 59 | 60 | private String cachedDefaultName; 61 | 62 | /** 63 | * Instantiates a new Extension loader. 64 | * 65 | * @param clazz the clazz. 66 | */ 67 | private ExtensionLoader(final Class clazz, final ClassLoader cl) { 68 | this.clazz = clazz; 69 | this.classLoader = cl; 70 | if (!Objects.equals(clazz, ExtensionFactory.class)) { 71 | ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getExtensionClasses(); 72 | } 73 | } 74 | 75 | /** 76 | * Gets extension loader. 77 | * 78 | * @param the type parameter 79 | * @param clazz the clazz 80 | * @param cl the cl 81 | * @return the extension loader. 82 | */ 83 | public static ExtensionLoader getExtensionLoader(final Class clazz, final ClassLoader cl) { 84 | 85 | Objects.requireNonNull(clazz, "extension clazz is null"); 86 | 87 | if (!clazz.isInterface()) { 88 | throw new IllegalArgumentException("extension clazz (" + clazz + ") is not interface!"); 89 | } 90 | if (!clazz.isAnnotationPresent(SPI.class)) { 91 | throw new IllegalArgumentException("extension clazz (" + clazz + ") without @" + SPI.class + " Annotation"); 92 | } 93 | ExtensionLoader extensionLoader = (ExtensionLoader) LOADERS.get(clazz); 94 | if (Objects.nonNull(extensionLoader)) { 95 | return extensionLoader; 96 | } 97 | LOADERS.putIfAbsent(clazz, new ExtensionLoader<>(clazz, cl)); 98 | return (ExtensionLoader) LOADERS.get(clazz); 99 | } 100 | 101 | /** 102 | * Gets extension loader. 103 | * 104 | * @param the type parameter 105 | * @param clazz the clazz 106 | * @return the extension loader 107 | */ 108 | public static ExtensionLoader getExtensionLoader(final Class clazz) { 109 | return getExtensionLoader(clazz, ExtensionLoader.class.getClassLoader()); 110 | } 111 | 112 | /** 113 | * Gets default join. 114 | * 115 | * @return the default join. 116 | */ 117 | public T getDefaultJoin() { 118 | getExtensionClasses(); 119 | if (StringUtils.isBlank(cachedDefaultName)) { 120 | return null; 121 | } 122 | return getJoin(cachedDefaultName); 123 | } 124 | 125 | /** 126 | * Gets join. 127 | * 128 | * @param name the name 129 | * @return the join. 130 | */ 131 | public T getJoin(final String name) { 132 | if (StringUtils.isBlank(name)) { 133 | throw new NullPointerException("get join name is null"); 134 | } 135 | Holder objectHolder = cachedInstances.get(name); 136 | if (Objects.isNull(objectHolder)) { 137 | cachedInstances.putIfAbsent(name, new Holder<>()); 138 | objectHolder = cachedInstances.get(name); 139 | } 140 | Object value = objectHolder.getValue(); 141 | if (Objects.isNull(value)) { 142 | synchronized (cachedInstances) { 143 | value = objectHolder.getValue(); 144 | if (Objects.isNull(value)) { 145 | value = createExtension(name); 146 | objectHolder.setValue(value); 147 | } 148 | } 149 | } 150 | return (T) value; 151 | } 152 | 153 | /** 154 | * get all join spi. 155 | * 156 | * @return list. joins 157 | */ 158 | public List getJoins() { 159 | Map> extensionClasses = this.getExtensionClasses(); 160 | if (extensionClasses.isEmpty()) { 161 | return Collections.emptyList(); 162 | } 163 | if (Objects.equals(extensionClasses.size(), cachedInstances.size())) { 164 | return (List) this.cachedInstances.values().stream().map(e -> e.getValue()).collect(Collectors.toList()); 165 | } 166 | List joins = new ArrayList<>(); 167 | extensionClasses.forEach((name, v) -> { 168 | T join = this.getJoin(name); 169 | joins.add(join); 170 | }); 171 | return joins; 172 | } 173 | 174 | @SuppressWarnings("unchecked") 175 | private T createExtension(final String name) { 176 | Class aClass = getExtensionClasses().get(name); 177 | if (Objects.isNull(aClass)) { 178 | throw new IllegalArgumentException("name is error"); 179 | } 180 | Object o = joinInstances.get(aClass); 181 | if (Objects.isNull(o)) { 182 | try { 183 | joinInstances.putIfAbsent(aClass, aClass.newInstance()); 184 | o = joinInstances.get(aClass); 185 | } catch (InstantiationException | IllegalAccessException e) { 186 | throw new IllegalStateException("Extension instance(name: " + name + ", class: " 187 | + aClass + ") could not be instantiated: " + e.getMessage(), e); 188 | 189 | } 190 | } 191 | return (T) o; 192 | } 193 | 194 | /** 195 | * Gets extension classes. 196 | * 197 | * @return the extension classes 198 | */ 199 | public Map> getExtensionClasses() { 200 | Map> classes = cachedClasses.getValue(); 201 | if (Objects.isNull(classes)) { 202 | synchronized (cachedClasses) { 203 | classes = cachedClasses.getValue(); 204 | if (Objects.isNull(classes)) { 205 | classes = loadExtensionClass(); 206 | cachedClasses.setValue(classes); 207 | } 208 | } 209 | } 210 | return classes; 211 | } 212 | 213 | private Map> loadExtensionClass() { 214 | SPI annotation = clazz.getAnnotation(SPI.class); 215 | if (Objects.nonNull(annotation)) { 216 | String value = annotation.value(); 217 | if (StringUtils.isNotBlank(value)) { 218 | cachedDefaultName = value; 219 | } 220 | } 221 | Map> classes = new HashMap<>(16); 222 | loadDirectory(classes); 223 | return classes; 224 | } 225 | 226 | /** 227 | * Load files under WH_DIRECTORY. 228 | */ 229 | private void loadDirectory(final Map> classes) { 230 | String fileName = WH_DIRECTORY + clazz.getName(); 231 | try { 232 | Enumeration urls = Objects.nonNull(this.classLoader) ? classLoader.getResources(fileName) 233 | : ClassLoader.getSystemResources(fileName); 234 | if (Objects.nonNull(urls)) { 235 | while (urls.hasMoreElements()) { 236 | URL url = urls.nextElement(); 237 | loadResources(classes, url); 238 | } 239 | } 240 | } catch (IOException t) { 241 | log.error("load extension class error {}", fileName, t); 242 | } 243 | } 244 | 245 | private void loadResources(final Map> classes, final URL url) throws IOException { 246 | try (InputStream inputStream = url.openStream()) { 247 | Properties properties = new Properties(); 248 | properties.load(inputStream); 249 | properties.forEach((k, v) -> { 250 | String name = (String) k; 251 | String classPath = (String) v; 252 | if (StringUtils.isNotBlank(name) && StringUtils.isNotBlank(classPath)) { 253 | try { 254 | loadClass(classes, name, classPath); 255 | } catch (ClassNotFoundException e) { 256 | throw new IllegalStateException("load extension resources error", e); 257 | } 258 | } 259 | }); 260 | } catch (IOException e) { 261 | throw new IllegalStateException("load extension resources error", e); 262 | } 263 | } 264 | 265 | private void loadClass(final Map> classes, 266 | final String name, final String classPath) throws ClassNotFoundException { 267 | Class subClass = Objects.nonNull(this.classLoader) ? Class.forName(classPath, true, this.classLoader) : Class.forName(classPath); 268 | if (!clazz.isAssignableFrom(subClass)) { 269 | throw new IllegalStateException("load extension resources error," + subClass + " subtype is not of " + clazz); 270 | } 271 | if (!subClass.isAnnotationPresent(Join.class)) { 272 | throw new IllegalStateException("load extension resources error," + subClass + " without @" + Join.class + " annotation"); 273 | } 274 | Class oldClass = classes.get(name); 275 | if (Objects.isNull(oldClass)) { 276 | classes.put(name, subClass); 277 | } else if (!Objects.equals(oldClass, subClass)) { 278 | throw new IllegalStateException("load extension resources error,Duplicate class " + clazz.getName() + " name " + name + " on " + oldClass.getName() + " or " + subClass.getName()); 279 | } 280 | } 281 | 282 | /** 283 | * The type Holder. 284 | * 285 | * @param the type parameter. 286 | */ 287 | public static class Holder { 288 | 289 | private volatile T value; 290 | 291 | /** 292 | * Gets value. 293 | * 294 | * @return the value 295 | */ 296 | public T getValue() { 297 | return value; 298 | } 299 | 300 | /** 301 | * Sets value. 302 | * 303 | * @param value the value 304 | */ 305 | public void setValue(final T value) { 306 | this.value = value; 307 | } 308 | } 309 | } 310 | --------------------------------------------------------------------------------