Skip to content

Commit

Permalink
feature: add relevant hints of RocketMQ starter (#3371)
Browse files Browse the repository at this point in the history
feature: add relevant hints of RocketMQ starter & fix files format problem
  • Loading branch information
steverao authored Jul 10, 2023
1 parent 0d7dfc9 commit 6e02b51
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@

<properties>
<!-- Project revision -->
<revision>2022.0.0.0-RC2</revision>
<revision>2022.0.0.0</revision>

<!-- Spring Cloud -->
<spring.cloud.version>2022.0.0</spring.cloud.version>
Expand Down
2 changes: 1 addition & 1 deletion spring-cloud-alibaba-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<description>Spring Cloud Alibaba Dependencies</description>

<properties>
<revision>2022.0.0.0-RC2</revision>
<revision>2022.0.0.0</revision>
<sentinel.version>1.8.6</sentinel.version>
<seata.version>1.7.0-native-rc2</seata.version>
<nacos.client.version>2.2.1</nacos.client.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,18 @@
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-acl</artifactId>
<exclusions>
<exclusion>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.22</version>
</dependency>

<!-- Testing -->
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2013-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.cloud.stream.binder.rocketmq.aot.hint;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import com.alibaba.cloud.stream.binder.rocketmq.properties.RocketMQConsumerProperties;

import org.springframework.aot.hint.ExecutableMode;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.util.ReflectionUtils;

/**
* @author ChengPu raozihao
*/
public class RocketMQConsumerPropertiesHints implements RuntimeHintsRegistrar {

@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
Constructor<RocketMQConsumerProperties> constructor;
try {
constructor = RocketMQConsumerProperties.class.getConstructor();
}
catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
hints.reflection().registerConstructor(constructor, ExecutableMode.INVOKE);
// setMessageModel
Method setMessageModel = ReflectionUtils.findMethod(RocketMQConsumerProperties.class, "setMessageModel", String.class);
hints.reflection().registerMethod(setMessageModel, ExecutableMode.INVOKE);
// getPush
Method getPush = ReflectionUtils.findMethod(RocketMQConsumerProperties.class, "getPush");
hints.reflection().registerMethod(getPush, ExecutableMode.INVOKE);
// setSubscription
Method setSubscription = ReflectionUtils.findMethod(RocketMQConsumerProperties.class, "setSubscription", String.class);
hints.reflection().registerMethod(setSubscription, ExecutableMode.INVOKE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2013-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.cloud.stream.binder.rocketmq.aot.hint;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import com.alibaba.cloud.stream.binder.rocketmq.properties.RocketMQConsumerProperties;
import com.alibaba.cloud.stream.binder.rocketmq.properties.RocketMQProducerProperties;
import com.alibaba.cloud.stream.binder.rocketmq.properties.RocketMQSpecificPropertiesProvider;

import org.springframework.aot.hint.ExecutableMode;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.util.ReflectionUtils;

/**
* @author ChengPu raozihao
*/
public class RocketMQSpecificPropertiesProviderHints implements RuntimeHintsRegistrar {

@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
Constructor<RocketMQSpecificPropertiesProvider> constructor;
try {
constructor = RocketMQSpecificPropertiesProvider.class.getConstructor();
}
catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
hints.reflection().registerConstructor(constructor, ExecutableMode.INVOKE);
// getConsumer
Method getConsumer = ReflectionUtils.findMethod(RocketMQSpecificPropertiesProvider.class, "getConsumer");
hints.reflection().registerMethod(getConsumer, ExecutableMode.INVOKE);
// setConsumer
Method setConsumer = ReflectionUtils.findMethod(RocketMQSpecificPropertiesProvider.class, "setConsumer", RocketMQConsumerProperties.class);
hints.reflection().registerMethod(setConsumer, ExecutableMode.INVOKE);
// getProducer
Method getProducer = ReflectionUtils.findMethod(RocketMQSpecificPropertiesProvider.class, "getProducer");
hints.reflection().registerMethod(getProducer, ExecutableMode.INVOKE);
// setProducer
Method setProducer = ReflectionUtils.findMethod(RocketMQSpecificPropertiesProvider.class, "setProducer", RocketMQProducerProperties.class);
hints.reflection().registerMethod(setProducer, ExecutableMode.INVOKE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
org.springframework.aot.hint.RuntimeHintsRegistrar=\
com.alibaba.cloud.stream.binder.rocketmq.aot.hint.RocketMQSpecificPropertiesProviderHints,\
com.alibaba.cloud.stream.binder.rocketmq.aot.hint.RocketMQConsumerPropertiesHints
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2013-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.cloud.stream.binder.rocketmq.aot.hint;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import com.alibaba.cloud.stream.binder.rocketmq.properties.RocketMQConsumerProperties;
import org.junit.jupiter.api.Test;

import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.util.ReflectionUtils;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author ChengPu raozihao
*/
public class RocketMQConsumerPropertiesHintsTests {

@Test
public void shouldRegisterHints() {
Constructor<RocketMQConsumerProperties> constructor;
try {
constructor = RocketMQConsumerProperties.class.getConstructor();
}
catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
Method setMessageModel = ReflectionUtils.findMethod(RocketMQConsumerProperties.class, "setMessageModel", String.class);
Method getPush = ReflectionUtils.findMethod(RocketMQConsumerProperties.class, "getPush");
Method setSubscription = ReflectionUtils.findMethod(RocketMQConsumerProperties.class, "setSubscription", String.class);
RuntimeHints hints = new RuntimeHints();
new RocketMQConsumerPropertiesHints().registerHints(hints, getClass().getClassLoader());
assertThat(RuntimeHintsPredicates.reflection().onConstructor(constructor)).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(setMessageModel)).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(getPush)).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(setSubscription)).accepts(hints);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2013-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.cloud.stream.binder.rocketmq.aot.hint;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import com.alibaba.cloud.stream.binder.rocketmq.properties.RocketMQConsumerProperties;
import com.alibaba.cloud.stream.binder.rocketmq.properties.RocketMQProducerProperties;
import com.alibaba.cloud.stream.binder.rocketmq.properties.RocketMQSpecificPropertiesProvider;
import org.junit.jupiter.api.Test;

import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.util.ReflectionUtils;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author ChengPu raozihao
*/
public class RocketMQSpecificPropertiesProviderHintsTests {

@Test
public void shouldRegisterHints() {
Constructor<RocketMQSpecificPropertiesProvider> constructor;
try {
constructor = RocketMQSpecificPropertiesProvider.class.getConstructor();
}
catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
Method getConsumer = ReflectionUtils.findMethod(RocketMQSpecificPropertiesProvider.class, "getConsumer");
Method setConsumer = ReflectionUtils.findMethod(RocketMQSpecificPropertiesProvider.class, "setConsumer", RocketMQConsumerProperties.class);
Method getProducer = ReflectionUtils.findMethod(RocketMQSpecificPropertiesProvider.class, "getProducer");
Method setProducer = ReflectionUtils.findMethod(RocketMQSpecificPropertiesProvider.class, "setProducer", RocketMQProducerProperties.class);
RuntimeHints hints = new RuntimeHints();
new RocketMQSpecificPropertiesProviderHints().registerHints(hints, getClass().getClassLoader());
assertThat(RuntimeHintsPredicates.reflection().onConstructor(constructor)).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(getConsumer)).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(setConsumer)).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(getProducer)).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(setProducer)).accepts(hints);
}
}

0 comments on commit 6e02b51

Please sign in to comment.