Skip to content
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

Allow for custom dispatcher #1001

Merged
merged 4 commits into from
Oct 6, 2023
Merged

Allow for custom dispatcher #1001

merged 4 commits into from
Oct 6, 2023

Conversation

scottf
Copy link
Contributor

@scottf scottf commented Oct 6, 2023

Provide for the developer to provide a custom dispatcher. This is generally discouraged, but there was a specific use case for the thin vertx wrapper to which needed this.

* which should not be started on construction.
* @param id the assigned id of the dispatcher
*/
void start(String id);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed to add this so impl could override it. The connection sometimes creates a dispatcher and does not start it - it's created lazy - and wanted to ensure the slim edge that 2 threads try to create at the same time.

@@ -46,7 +54,7 @@ public interface Dispatcher extends Consumer {
* @return The Dispatcher, so calls can be chained.
* @throws IllegalStateException if the dispatcher was previously closed
*/
public Dispatcher subscribe(String subject);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public is redundant in interfaces

* Property used to set class name for the Dispatcher Factory
* {@link Builder#dispatcherFactory(DispatcherFactory) dispatcherFactory}.
*/
public static final String PROP_DISPATCHER_FACTORY_CLASS = "dispatcher.factory.class";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for options, there is a build, but you can also set factory class names via property

NatsDispatcher createDispatcher(NatsConnection conn, MessageHandler handler) {
return new NatsDispatcher(conn, handler);
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was made a class, not an interface. I'm trying to limit exposure of public things, note the warning, and a public interface required making NatsDispatcher and NatsConnection public


// Poison pill is a graphic, but common term for an item that breaks loops or stop something.
// In this class the poisonPill is used to break out of timed waits on the blocking queue.
// A simple == is used to check if any message in the queue is this message.
private final NatsMessage poisonPill;
protected final NatsMessage poisonPill;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intentionally did not make this public or an interface. The dev will have to extend this class in code in a matching package.

@@ -165,7 +167,7 @@ class NatsConnection implements Connection {

// Connect is only called after creation
void connect(boolean reconnectOnConnect) throws InterruptedException, IOException {
if (options.getServers().size() == 0) {
if (options.getServers().isEmpty()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intellij incessantly annoys me about these so I fix them when I'm in a source file.

@@ -1136,8 +1138,11 @@ CompletableFuture<Message> requestFutureInternal(String subject, Headers headers
}

if (inboxDispatcher.get() == null) {
NatsDispatcher d = new NatsDispatcher(this, this::deliverReply);
NatsDispatcher d = dispatcherFactory.createDispatcher(this, this::deliverReply);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the factory instead of just newing

private MessageQueue incoming;
private MessageHandler defaultHandler;
protected final MessageQueue incoming;
protected final MessageHandler defaultHandler;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

protected since this will be subclasses. final because Intellij says so, probably a good practice

@scottf scottf merged commit 0542422 into main Oct 6, 2023
2 checks passed
@scottf scottf deleted the custom-dispatcher branch October 6, 2023 20:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants