-
Notifications
You must be signed in to change notification settings - Fork 167
Rename resolveReference to resolve and add <T> List<T> getBeans(Class<T> clazz)
#2492
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a85f573
split BeanRegistry interface
rrayst 465b93b
Rename `resolveReference` to `resolve` and add `<T> List<T> getBeans(…
christiangoerdes d82b45b
Merge branch 'master' into beanregistry-improvements
christiangoerdes 8aea5e8
Merge branch 'master' into beanregistry-improvements
christiangoerdes 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
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
44 changes: 44 additions & 0 deletions
44
annot/src/main/java/com/predic8/membrane/annot/beanregistry/AsyncBeanCollector.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,44 @@ | ||
| package com.predic8.membrane.annot.beanregistry; | ||
|
|
||
| import javax.annotation.concurrent.GuardedBy; | ||
| import java.util.concurrent.BlockingQueue; | ||
| import java.util.concurrent.LinkedBlockingDeque; | ||
|
|
||
| /** | ||
| * Thread-safe, asynchronous wrapper for {@link BeanCollector}. | ||
| */ | ||
| public class AsyncBeanCollector implements BeanCollector { | ||
|
|
||
| private final BlockingQueue<ChangeEvent> changeEvents = new LinkedBlockingDeque<>(); | ||
| private final BeanCollector delegate; | ||
|
|
||
| @GuardedBy("this") | ||
| Thread t; | ||
|
|
||
| public AsyncBeanCollector(BeanCollector delegate) { | ||
| this.delegate = delegate; | ||
| } | ||
|
|
||
| @Override | ||
| public void handle(ChangeEvent changeEvent, boolean isLast) { | ||
| changeEvents.add(changeEvent); | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized void start() { | ||
| if (t != null) | ||
| return; | ||
| t = new Thread(() -> { | ||
| while (true) { | ||
| try { | ||
| ChangeEvent changeEvent = changeEvents.take(); | ||
| delegate.handle(changeEvent, changeEvents.isEmpty()); | ||
| } catch (InterruptedException e) { | ||
| break; | ||
| } | ||
| } | ||
| }); | ||
| t.start(); | ||
| } | ||
|
|
||
| } | ||
41 changes: 41 additions & 0 deletions
41
annot/src/main/java/com/predic8/membrane/annot/beanregistry/BeanCollector.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,41 @@ | ||
| package com.predic8.membrane.annot.beanregistry; | ||
|
|
||
| import com.predic8.membrane.annot.Grammar; | ||
| import com.predic8.membrane.annot.yaml.GenericYamlParser; | ||
| import com.predic8.membrane.annot.yaml.WatchAction; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * This is the definition side of a {@link BeanRegistryImplementation}. You can start the bean registry | ||
| * and send it a series of change events. | ||
| */ | ||
| public interface BeanCollector { | ||
| /** | ||
| * Utility method to ingest a stream of YAML objects as a static configuration and then | ||
| * start the bean registry. | ||
| * @param yamls stream of YAML objects | ||
| * @param grammar the grammar to use for parsing | ||
| */ | ||
| default void parseYamls(InputStream yamls, Grammar grammar) throws IOException { | ||
| List<BeanDefinition> bds = GenericYamlParser.parseMembraneResources(yamls, grammar); | ||
| for (int i = 0; i < bds.size(); i++) { | ||
| handle(new BeanDefinitionChanged(WatchAction.ADDED, bds.get(i)), i == bds.size() - 1); | ||
| } | ||
| handle(new StaticConfigurationLoaded(), true); | ||
| start(); | ||
| } | ||
|
|
||
| /** | ||
| * @param changeEvent the change event | ||
| * @param isLast indicates whether this is the last change event for this batch of changes | ||
| */ | ||
| void handle(ChangeEvent changeEvent, boolean isLast); | ||
|
|
||
| /** | ||
| * Starts the bean registry. | ||
| */ | ||
| void start(); | ||
| } |
26 changes: 26 additions & 0 deletions
26
annot/src/main/java/com/predic8/membrane/annot/beanregistry/BeanContainer.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,26 @@ | ||
| package com.predic8.membrane.annot.beanregistry; | ||
|
|
||
| public class BeanContainer { | ||
| private final BeanDefinition definition; | ||
| /** | ||
| * Constructed bean after initialization. | ||
| */ | ||
| private volatile Object singleton; | ||
|
|
||
| public BeanContainer(BeanDefinition definition) { | ||
| this.definition = definition; | ||
| } | ||
|
|
||
|
|
||
| public Object getSingleton() { | ||
| return singleton; | ||
| } | ||
|
|
||
| public void setSingleton(Object singleton) { | ||
| this.singleton = singleton; | ||
| } | ||
|
|
||
| public BeanDefinition getDefinition() { | ||
| return definition; | ||
| } | ||
| } |
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
11 changes: 11 additions & 0 deletions
11
annot/src/main/java/com/predic8/membrane/annot/beanregistry/BeanDefinitionChanged.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,11 @@ | ||
| package com.predic8.membrane.annot.beanregistry; | ||
|
|
||
| import com.predic8.membrane.annot.yaml.WatchAction; | ||
|
|
||
| /** | ||
| * Signals that a BeanDefinition has changed (=was added, modified, or deleted). | ||
| */ | ||
| public record BeanDefinitionChanged( | ||
| WatchAction action, | ||
| BeanDefinition bd) implements ChangeEvent { | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thread lifecycle and configuration issues.
Several concerns with the background thread:
No shutdown mechanism: There's no
stop()method to gracefully terminate the thread. Consider adding one that interrupts the thread and awaits termination.Non-daemon thread: The thread will prevent JVM shutdown. Consider setting
t.setDaemon(true)or providing explicit shutdown.Thread naming: Add a descriptive name for debugging:
t.setName("AsyncBeanCollector-worker").Exception handling: If
delegate.handle()throws an unchecked exception, the thread will terminate silently.🔎 Proposed improvements
@Override public synchronized void start() { if (t != null) return; t = new Thread(() -> { while (true) { try { ChangeEvent changeEvent = changeEvents.take(); delegate.handle(changeEvent, changeEvents.isEmpty()); } catch (InterruptedException e) { break; + } catch (Exception e) { + // Log and continue processing + e.printStackTrace(); } } }); + t.setName("AsyncBeanCollector-worker"); + t.setDaemon(true); t.start(); } + + public synchronized void stop() { + if (t != null) { + t.interrupt(); + try { + t.join(5000); + } catch (InterruptedException ignored) { + Thread.currentThread().interrupt(); + } + t = null; + } + }🤖 Prompt for AI Agents