-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optional dispatching with executor instead of blocking (#1083)
- Loading branch information
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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
76 changes: 76 additions & 0 deletions
76
src/main/java/io/nats/client/impl/NatsDispatcherWithExecutor.java
This file contains 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,76 @@ | ||
// Copyright 2024 The NATS 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: | ||
// | ||
// http://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 io.nats.client.impl; | ||
|
||
import io.nats.client.MessageHandler; | ||
|
||
class NatsDispatcherWithExecutor extends NatsDispatcher { | ||
|
||
NatsDispatcherWithExecutor(NatsConnection conn, MessageHandler handler) { | ||
super(conn, handler); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
while (this.running.get()) { // start | ||
|
||
NatsMessage msg = this.incoming.pop(this.waitForMessage); | ||
|
||
if (msg != null) { | ||
NatsSubscription sub = msg.getNatsSubscription(); | ||
if (sub != null && sub.isActive()) { | ||
MessageHandler handler = subscriptionHandlers.get(sub.getSID()); | ||
if (handler == null) { | ||
handler = defaultHandler; | ||
} | ||
// A dispatcher can have a null defaultHandler. You can't subscribe without a handler, | ||
// but messages might come in while the dispatcher is being closed or after unsubscribe | ||
// and the [non-default] handler has already been removed from subscriptionHandlers | ||
if (handler != null) { | ||
sub.incrementDeliveredCount(); | ||
this.incrementDeliveredCount(); | ||
|
||
MessageHandler finalHandler = handler; | ||
connection.getExecutor().execute(() -> { | ||
try { | ||
finalHandler.onMessage(msg); | ||
} catch (Exception exp) { | ||
connection.processException(exp); | ||
} | ||
|
||
if (sub.reachedUnsubLimit()) { | ||
connection.invalidate(sub); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
|
||
if (breakRunLoop()) { | ||
return; | ||
} | ||
} | ||
} | ||
catch (InterruptedException exp) { | ||
if (this.running.get()){ | ||
this.connection.processException(exp); | ||
} //otherwise we did it | ||
} | ||
finally { | ||
this.running.set(false); | ||
this.thread = null; | ||
} | ||
} | ||
} |