-
Notifications
You must be signed in to change notification settings - Fork 13
feat: add Sequencer for spring boot #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fft0518
wants to merge
3
commits into
layotto:main
Choose a base branch
from
fft0518:tmp1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
examples/src/main/java/io/mosn/layotto/examples/pubsub/springboot/SequencerMethod.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package io.mosn.layotto.examples.pubsub.springboot; | ||
|
|
||
| import io.mosn.layotto.v1.Sequencer; | ||
| import org.springframework.stereotype.Component; | ||
| import spec.sdk.runtime.v1.domain.sequencer.GetNextIdRequest; | ||
| @Component | ||
| public class SequencerMethod { | ||
| @Sequencer(store_name="sequencer_demo", key = "examples", options ="STRONG" ) | ||
| public void method1(long nextId) { | ||
| System.out.println("NextID: "+ nextId); | ||
| } | ||
|
|
||
| @Sequencer(store_name="sequencer_demo", key = "examples", options ="WEAK" ) | ||
| public void method2(long nextId) { | ||
| System.out.println("NextID: "+ nextId); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
...pringboot/src/main/java/io/mosn/layotto/springboot/LayottoBeanPostProcessorSequencer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package io.mosn.layotto.springboot; | ||
|
|
||
| import io.mosn.layotto.v1.RuntimeClientBuilder; | ||
| import io.mosn.layotto.v1.Sequencer; | ||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.beans.BeansException; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.beans.factory.config.BeanPostProcessor; | ||
| import org.springframework.beans.factory.config.ConfigurableBeanFactory; | ||
| import org.springframework.beans.factory.config.EmbeddedValueResolver; | ||
| import spec.sdk.runtime.v1.client.RuntimeClient; | ||
| import spec.sdk.runtime.v1.domain.sequencer.GetNextIdRequest; | ||
| import spec.sdk.runtime.v1.domain.sequencer.SequencerOptions; | ||
|
|
||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.lang.reflect.Method; | ||
|
|
||
|
|
||
| public class LayottoBeanPostProcessorSequencer implements BeanPostProcessor { | ||
| private static final Logger LOGGER = LoggerFactory.getLogger(LayottoBeanPostProcessorSequencer.class.getName()); | ||
|
|
||
| private final EmbeddedValueResolver embeddedValueResolver; | ||
| @Autowired | ||
| private static LayottoProperties layottoConfig; | ||
|
|
||
|
|
||
| LayottoBeanPostProcessorSequencer(ConfigurableBeanFactory beanFactory) { | ||
| embeddedValueResolver = new EmbeddedValueResolver(beanFactory); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { | ||
|
|
||
| try { | ||
| sequencer(bean.getClass(), bean, embeddedValueResolver); | ||
| } catch (InvocationTargetException e) { | ||
| e.printStackTrace(); | ||
| } catch (IllegalAccessException e) { | ||
| e.printStackTrace(); | ||
| } | ||
|
|
||
| return bean; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { | ||
|
|
||
| return bean; | ||
| } | ||
|
|
||
| private static void sequencer(Class clazz, Object bean, EmbeddedValueResolver embeddedValueResolver) throws InvocationTargetException, IllegalAccessException { | ||
| if (clazz == null) { | ||
| return; | ||
| } | ||
| sequencer(clazz.getSuperclass(), bean, embeddedValueResolver); | ||
| for (Method method : clazz.getDeclaredMethods()) { | ||
| Sequencer getsequencer = method.getAnnotation(Sequencer.class); | ||
| if (getsequencer == null) { | ||
| continue; | ||
| } | ||
| String storeName = embeddedValueResolver.resolveStringValue(getsequencer.store_name()); | ||
| String key = embeddedValueResolver.resolveStringValue(getsequencer.key()); | ||
| String options = embeddedValueResolver.resolveStringValue(getsequencer.options()); | ||
| if (StringUtils.isNotEmpty(storeName) && StringUtils.isNotEmpty(key)&& StringUtils.isNotEmpty(options)) { | ||
|
|
||
| RuntimeClient layottoRuntime = new RuntimeClientBuilder().withIp(layottoConfig.DEFAULT_IP). | ||
| withPort(layottoConfig.DEFAULT_PORT) | ||
| .build(); | ||
| GetNextIdRequest getNextIdRequest = new GetNextIdRequest(); | ||
| getNextIdRequest.setStoreName(storeName); | ||
| getNextIdRequest.setKey(key); | ||
|
|
||
| SequencerOptions anoptions = new SequencerOptions(); | ||
| if(options.equals("STRONG") ) | ||
| anoptions.setOption(SequencerOptions.AutoIncrement.STRONG); | ||
| else if(options.equals("WEAK")) | ||
| anoptions.setOption(SequencerOptions.AutoIncrement.WEAK); | ||
| getNextIdRequest.setOptions(anoptions); | ||
|
|
||
| long nextId = layottoRuntime.getNextId(getNextIdRequest).getNextId(); | ||
| try { | ||
| method.invoke(bean, nextId); | ||
| } catch (Exception e) { | ||
| LOGGER.error("layotto sequencer method [{}] err:{ }", method.getName(), e.getMessage()); | ||
| throw e; | ||
| } | ||
| LOGGER.info("NextID:{},options:{}", nextId,options); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
|
|
||
| package io.mosn.layotto.v1; | ||
|
|
||
| import java.lang.annotation.*; | ||
|
|
||
| @Documented | ||
| @Target(ElementType.METHOD) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| public @interface Sequencer { | ||
|
|
||
| String store_name(); | ||
| String key(); | ||
| String options(); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.