Expanded dispatch so we pass in all types (WIP to share async executor)

master
nathan 2019-06-11 21:30:26 +02:00
parent 5674e46693
commit d4ac1915a1
4 changed files with 141 additions and 202 deletions

View File

@ -16,12 +16,16 @@
package dorkbox.messageBus.dispatch;
import dorkbox.messageBus.error.ErrorHandler;
import dorkbox.messageBus.publication.Publisher;
import dorkbox.messageBus.subscription.SubscriptionManager;
/**
* @author dorkbox, llc
* Date: 2/2/15
*/
public interface Dispatch {
void publish(Object message1);
void publish(Object message1, Object message2);
void publish(Object message1, Object message2, Object message3);
void publish(Publisher publisher, ErrorHandler errorHandler, SubscriptionManager subscriptionManager, Object message1);
void publish(Publisher publisher, ErrorHandler errorHandler, SubscriptionManager subscriptionManager, Object message1, Object message2);
void publish(Publisher publisher, ErrorHandler errorHandler, SubscriptionManager subscriptionManager, Object message1, Object message2, Object message3);
}

View File

@ -1,27 +0,0 @@
/*
* Copyright 2017 dorkbox, llc
*
* 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 dorkbox.messageBus.dispatch;
/**
* Cancels the publication of the message (or messages). Only applicable for the currently running thread. No more subscribers for
* this message will be called.
*/
public
class DispatchCancel extends RuntimeException {
public
DispatchCancel() {
}
}

View File

@ -17,34 +17,28 @@ package dorkbox.messageBus.dispatch;
import dorkbox.messageBus.error.DeadMessage;
import dorkbox.messageBus.error.ErrorHandler;
import dorkbox.messageBus.publication.Publisher;
import dorkbox.messageBus.subscription.Subscription;
import dorkbox.messageBus.subscription.SubscriptionManager;
/**
* By default, it is the calling thread that has to get the subscriptions, which the sync/async logic then uses.
*
* The exception to this rule is when checking/calling DeadMessage publication.
*
* @author dorkbox, llc
* Date: 2/2/15
*/
@SuppressWarnings("Duplicates")
public
class DispatchExact implements Dispatch {
private final ErrorHandler errorHandler;
private final SubscriptionManager subManager;
public
DispatchExact(final ErrorHandler errorHandler, final SubscriptionManager subManager) {
this.errorHandler = errorHandler;
this.subManager = subManager;
DispatchExact() {
}
@Override
public
void publish(final Object message1) {
final ErrorHandler errorHandler = this.errorHandler;
final SubscriptionManager subManager = this.subManager;
void publish(final Publisher publisher, final ErrorHandler errorHandler, final SubscriptionManager subManager,
final Object message1) {
final Class<?> messageClass1 = message1.getClass();
@ -54,38 +48,33 @@ class DispatchExact implements Dispatch {
int subLength;
boolean hasSubs = false;
try {
// Run subscriptions. if the subscriptions are NULL or length == 0, it means we don't have any that were ever subscribed.
if (subscriptions != null && (subLength = subscriptions.length) > 0) {
// even though they are non-null, and have length > 0 --- it is still possible the subscription was REMOVED at some point.
// so there won't be any object/method this publishes to AND there won't be any "dead messages" triggered
for (int i = 0; i < subLength; i++) {
sub = subscriptions[i];
hasSubs |= sub.publish(errorHandler, message1);
}
// Run subscriptions. if the subscriptions are NULL or length == 0, it means we don't have any that were ever subscribed.
if (subscriptions != null && (subLength = subscriptions.length) > 0) {
// even though they are non-null, and have length > 0 --- it is still possible the subscription was REMOVED at some point.
// so there won't be any object/method this publishes to AND there won't be any "dead messages" triggered
for (int i = 0; i < subLength; i++) {
sub = subscriptions[i];
hasSubs |= sub.publish(publisher, errorHandler, message1);
}
}
if (!hasSubs) {
// Dead Event must EXACTLY MATCH (no subclasses)
final Subscription[] deadSubscriptions = subManager.getSubs(DeadMessage.class); // can return null
if (deadSubscriptions != null) {
final DeadMessage deadMessage = new DeadMessage(message1);
for (int i = 0; i < deadSubscriptions.length; i++) {
sub = deadSubscriptions[i];
sub.publish(errorHandler, deadMessage);
}
if (!hasSubs) {
// Dead Event must EXACTLY MATCH (no subclasses)
final Subscription[] deadSubscriptions = subManager.getSubs(DeadMessage.class); // can return null
if (deadSubscriptions != null) {
final DeadMessage deadMessage = new DeadMessage(message1);
for (int i = 0; i < deadSubscriptions.length; i++) {
sub = deadSubscriptions[i];
sub.publish(publisher, errorHandler, deadMessage);
}
}
} catch (DispatchCancel ignored) {
// we wanted to cancel the dispatch for this specific message
}
}
@Override
public
void publish(final Object message1, final Object message2) {
final ErrorHandler errorHandler = this.errorHandler;
final SubscriptionManager subManager = this.subManager;
void publish(final Publisher publisher, final ErrorHandler errorHandler, final SubscriptionManager subManager,
final Object message1, final Object message2) {
final Class<?> messageClass1 = message1.getClass();
final Class<?> messageClass2 = message2.getClass();
@ -96,38 +85,33 @@ class DispatchExact implements Dispatch {
int subLength;
boolean hasSubs = false;
try {
// Run subscriptions. if the subscriptions are NULL or length == 0, it means we don't have any that were ever subscribed.
if (subscriptions != null && (subLength = subscriptions.length) > 0) {
// even though they are non-null, and have length > 0 --- it is still possible the subscription was REMOVED at some point.
// so there won't be any object/method this publishes to AND there won't be any "dead messages" triggered
for (int i = 0; i < subLength; i++) {
sub = subscriptions[i];
hasSubs |= sub.publish(errorHandler, message1, message2);
}
// Run subscriptions. if the subscriptions are NULL or length == 0, it means we don't have any that were ever subscribed.
if (subscriptions != null && (subLength = subscriptions.length) > 0) {
// even though they are non-null, and have length > 0 --- it is still possible the subscription was REMOVED at some point.
// so there won't be any object/method this publishes to AND there won't be any "dead messages" triggered
for (int i = 0; i < subLength; i++) {
sub = subscriptions[i];
hasSubs |= sub.publish(publisher, errorHandler, message1, message2);
}
}
if (!hasSubs) {
// Dead Event must EXACTLY MATCH (no subclasses)
final Subscription[] deadSubscriptions = subManager.getSubs(DeadMessage.class); // can return null
if (deadSubscriptions != null) {
final DeadMessage deadMessage = new DeadMessage(message1, message2);
for (int i = 0; i < deadSubscriptions.length; i++) {
sub = deadSubscriptions[i];
sub.publish(errorHandler, deadMessage);
}
if (!hasSubs) {
// Dead Event must EXACTLY MATCH (no subclasses)
final Subscription[] deadSubscriptions = subManager.getSubs(DeadMessage.class); // can return null
if (deadSubscriptions != null) {
final DeadMessage deadMessage = new DeadMessage(message1, message2);
for (int i = 0; i < deadSubscriptions.length; i++) {
sub = deadSubscriptions[i];
sub.publish(publisher, errorHandler, deadMessage);
}
}
} catch (DispatchCancel ignored) {
// we wanted to cancel the dispatch for these specific messages
}
}
@Override
public
void publish(final Object message1, final Object message2, final Object message3) {
final ErrorHandler errorHandler = this.errorHandler;
final SubscriptionManager subManager = this.subManager;
void publish(final Publisher publisher, final ErrorHandler errorHandler, final SubscriptionManager subManager,
final Object message1, final Object message2, final Object message3) {
final Class<?> messageClass1 = message1.getClass();
final Class<?> messageClass2 = message2.getClass();
@ -139,30 +123,26 @@ class DispatchExact implements Dispatch {
int subLength;
boolean hasSubs = false;
try {
// Run subscriptions. if the subscriptions are NULL or length == 0, it means we don't have any that were ever subscribed.
if (subscriptions != null && (subLength = subscriptions.length) > 0) {
// even though they are non-null, and have length > 0 --- it is still possible the subscription was REMOVED at some point.
// so there won't be any object/method this publishes to AND there won't be any "dead messages" triggered
for (int i = 0; i < subLength; i++) {
sub = subscriptions[i];
hasSubs |= sub.publish(errorHandler, message1, message2, message3);
}
// Run subscriptions. if the subscriptions are NULL or length == 0, it means we don't have any that were ever subscribed.
if (subscriptions != null && (subLength = subscriptions.length) > 0) {
// even though they are non-null, and have length > 0 --- it is still possible the subscription was REMOVED at some point.
// so there won't be any object/method this publishes to AND there won't be any "dead messages" triggered
for (int i = 0; i < subLength; i++) {
sub = subscriptions[i];
hasSubs |= sub.publish(publisher, errorHandler, message1, message2, message3);
}
}
if (!hasSubs) {
// Dead Event must EXACTLY MATCH (no subclasses)
final Subscription[] deadSubscriptions = subManager.getSubs(DeadMessage.class); // can return null
if (deadSubscriptions != null) {
final DeadMessage deadMessage = new DeadMessage(message1, message2, message3);
for (int i = 0; i < deadSubscriptions.length; i++) {
sub = deadSubscriptions[i];
sub.publish(errorHandler, deadMessage);
}
if (!hasSubs) {
// Dead Event must EXACTLY MATCH (no subclasses)
final Subscription[] deadSubscriptions = subManager.getSubs(DeadMessage.class); // can return null
if (deadSubscriptions != null) {
final DeadMessage deadMessage = new DeadMessage(message1, message2, message3);
for (int i = 0; i < deadSubscriptions.length; i++) {
sub = deadSubscriptions[i];
sub.publish(publisher, errorHandler, deadMessage);
}
}
} catch (DispatchCancel ignored) {
// we wanted to cancel the dispatch for these specific messages
}
}
}

View File

@ -17,6 +17,7 @@ package dorkbox.messageBus.dispatch;
import dorkbox.messageBus.error.DeadMessage;
import dorkbox.messageBus.error.ErrorHandler;
import dorkbox.messageBus.publication.Publisher;
import dorkbox.messageBus.subscription.Subscription;
import dorkbox.messageBus.subscription.SubscriptionManager;
@ -28,20 +29,14 @@ import dorkbox.messageBus.subscription.SubscriptionManager;
public
class DispatchExactWithSuperTypes implements Dispatch {
private final ErrorHandler errorHandler;
private final SubscriptionManager subManager;
public
DispatchExactWithSuperTypes(final ErrorHandler errorHandler, final SubscriptionManager subManager) {
this.errorHandler = errorHandler;
this.subManager = subManager;
DispatchExactWithSuperTypes() {
}
@Override
public
void publish(final Object message1) {
final ErrorHandler errorHandler = this.errorHandler;
final SubscriptionManager subManager = this.subManager;
void publish(final Publisher publisher, final ErrorHandler errorHandler, final SubscriptionManager subManager,
final Object message1) {
final Class<?> messageClass1 = message1.getClass();
@ -52,47 +47,43 @@ class DispatchExactWithSuperTypes implements Dispatch {
int subLength;
boolean hasSubs = false;
try {
// Run subscriptions. if the subscriptions are NULL or length == 0, it means we don't have any that were ever subscribed.
if (subscriptions != null && (subLength = subscriptions.length) > 0) {
// even though they are non-null, and have length > 0 --- it is still possible the subscription was REMOVED at some point.
// so there won't be any object/method this publishes to AND there won't be any "dead messages" triggered
for (int i = 0; i < subLength; i++) {
sub = subscriptions[i];
hasSubs |= sub.publish(errorHandler, message1);
}
// Run subscriptions. if the subscriptions are NULL or length == 0, it means we don't have any that were ever subscribed.
if (subscriptions != null && (subLength = subscriptions.length) > 0) {
// even though they are non-null, and have length > 0 --- it is still possible the subscription was REMOVED at some point.
// so there won't be any object/method this publishes to AND there won't be any "dead messages" triggered
for (int i = 0; i < subLength; i++) {
sub = subscriptions[i];
hasSubs |= sub.publish(publisher, errorHandler, message1);
}
}
if ((subLength = superSubscriptions.length) > 0) {
// even though they are non-null, and have length > 0 --- it is still possible the subscription was REMOVED at some point.
// so there won't be any object/method this publishes to AND there won't be any "dead messages" triggered
for (int i = 0; i < subLength; i++) {
sub = superSubscriptions[i];
hasSubs |= sub.publish(errorHandler, message1);
}
if ((subLength = superSubscriptions.length) > 0) {
// even though they are non-null, and have length > 0 --- it is still possible the subscription was REMOVED at some point.
// so there won't be any object/method this publishes to AND there won't be any "dead messages" triggered
for (int i = 0; i < subLength; i++) {
sub = superSubscriptions[i];
hasSubs |= sub.publish(publisher, errorHandler, message1);
}
}
if (!hasSubs) {
// Dead Event must EXACTLY MATCH (no subclasses)
final Subscription[] deadSubscriptions = subManager.getSubs(DeadMessage.class); // can return null
if (deadSubscriptions != null) {
final DeadMessage deadMessage = new DeadMessage(message1);
for (int i = 0; i < deadSubscriptions.length; i++) {
sub = deadSubscriptions[i];
sub.publish(errorHandler, deadMessage);
}
if (!hasSubs) {
// Dead Event must EXACTLY MATCH (no subclasses)
final Subscription[] deadSubscriptions = subManager.getSubs(DeadMessage.class); // can return null
if (deadSubscriptions != null) {
final DeadMessage deadMessage = new DeadMessage(message1);
for (int i = 0; i < deadSubscriptions.length; i++) {
sub = deadSubscriptions[i];
sub.publish(publisher, errorHandler, deadMessage);
}
}
} catch (DispatchCancel ignored) {
// we wanted to cancel the dispatch for this specific message
}
}
@Override
public
void publish(final Object message1, final Object message2) {
final ErrorHandler errorHandler = this.errorHandler;
final SubscriptionManager subManager = this.subManager;
void publish(final Publisher publisher, final ErrorHandler errorHandler, final SubscriptionManager subManager,
final Object message1, final Object message2) {
final Class<?> messageClass1 = message1.getClass();
final Class<?> messageClass2 = message2.getClass();
@ -104,47 +95,42 @@ class DispatchExactWithSuperTypes implements Dispatch {
int subLength;
boolean hasSubs = false;
try {
// Run subscriptions. if the subscriptions are NULL or length == 0, it means we don't have any that were ever subscribed.
if (subscriptions != null && (subLength = subscriptions.length) > 0) {
// even though they are non-null, and have length > 0 --- it is still possible the subscription was REMOVED at some point.
// so there won't be any object/method this publishes to AND there won't be any "dead messages" triggered
for (int i = 0; i < subLength; i++) {
sub = subscriptions[i];
hasSubs |= sub.publish(errorHandler, message1, message2);
}
// Run subscriptions. if the subscriptions are NULL or length == 0, it means we don't have any that were ever subscribed.
if (subscriptions != null && (subLength = subscriptions.length) > 0) {
// even though they are non-null, and have length > 0 --- it is still possible the subscription was REMOVED at some point.
// so there won't be any object/method this publishes to AND there won't be any "dead messages" triggered
for (int i = 0; i < subLength; i++) {
sub = subscriptions[i];
hasSubs |= sub.publish(publisher, errorHandler, message1, message2);
}
}
if ((subLength = superSubscriptions.length) > 0) {
// even though they are non-null, and have length > 0 --- it is still possible the subscription was REMOVED at some point.
// so there won't be any object/method this publishes to AND there won't be any "dead messages" triggered
for (int i = 0; i < subLength; i++) {
sub = superSubscriptions[i];
hasSubs |= sub.publish(errorHandler, message1, message2);
}
if ((subLength = superSubscriptions.length) > 0) {
// even though they are non-null, and have length > 0 --- it is still possible the subscription was REMOVED at some point.
// so there won't be any object/method this publishes to AND there won't be any "dead messages" triggered
for (int i = 0; i < subLength; i++) {
sub = superSubscriptions[i];
hasSubs |= sub.publish(publisher, errorHandler, message1, message2);
}
}
if (!hasSubs) {
// Dead Event must EXACTLY MATCH (no subclasses)
final Subscription[] deadSubscriptions = subManager.getSubs(DeadMessage.class); // can return null
if (deadSubscriptions != null) {
final DeadMessage deadMessage = new DeadMessage(message1, message2);
for (int i = 0; i < deadSubscriptions.length; i++) {
sub = deadSubscriptions[i];
sub.publish(errorHandler, deadMessage);
}
if (!hasSubs) {
// Dead Event must EXACTLY MATCH (no subclasses)
final Subscription[] deadSubscriptions = subManager.getSubs(DeadMessage.class); // can return null
if (deadSubscriptions != null) {
final DeadMessage deadMessage = new DeadMessage(message1, message2);
for (int i = 0; i < deadSubscriptions.length; i++) {
sub = deadSubscriptions[i];
sub.publish(publisher, errorHandler, deadMessage);
}
}
} catch (DispatchCancel ignored) {
// we wanted to cancel the dispatch for these specific messages
}
}
@Override
public
void publish(final Object message1, final Object message2, final Object message3) {
final ErrorHandler errorHandler = this.errorHandler;
final SubscriptionManager subManager = this.subManager;
void publish(final Publisher publisher, final ErrorHandler errorHandler, final SubscriptionManager subManager,
final Object message1, final Object message2, final Object message3) {
final Class<?> messageClass1 = message1.getClass();
final Class<?> messageClass2 = message2.getClass();
@ -157,39 +143,35 @@ class DispatchExactWithSuperTypes implements Dispatch {
int subLength;
boolean hasSubs = false;
try {
// Run subscriptions. if the subscriptions are NULL or length == 0, it means we don't have any that were ever subscribed.
if (subscriptions != null && (subLength = subscriptions.length) > 0) {
// even though they are non-null, and have length > 0 --- it is still possible the subscription was REMOVED at some point.
// so there won't be any object/method this publishes to AND there won't be any "dead messages" triggered
for (int i = 0; i < subLength; i++) {
sub = subscriptions[i];
hasSubs |= sub.publish(errorHandler, message1, message2, message3);
}
// Run subscriptions. if the subscriptions are NULL or length == 0, it means we don't have any that were ever subscribed.
if (subscriptions != null && (subLength = subscriptions.length) > 0) {
// even though they are non-null, and have length > 0 --- it is still possible the subscription was REMOVED at some point.
// so there won't be any object/method this publishes to AND there won't be any "dead messages" triggered
for (int i = 0; i < subLength; i++) {
sub = subscriptions[i];
hasSubs |= sub.publish(publisher, errorHandler, message1, message2, message3);
}
}
if ((subLength = superSubscriptions.length) > 0) {
// even though they are non-null, and have length > 0 --- it is still possible the subscription was REMOVED at some point.
// so there won't be any object/method this publishes to AND there won't be any "dead messages" triggered
for (int i = 0; i < subLength; i++) {
sub = superSubscriptions[i];
hasSubs |= sub.publish(errorHandler, message1, message2, message3);
}
if ((subLength = superSubscriptions.length) > 0) {
// even though they are non-null, and have length > 0 --- it is still possible the subscription was REMOVED at some point.
// so there won't be any object/method this publishes to AND there won't be any "dead messages" triggered
for (int i = 0; i < subLength; i++) {
sub = superSubscriptions[i];
hasSubs |= sub.publish(publisher, errorHandler, message1, message2, message3);
}
}
if (!hasSubs) {
// Dead Event must EXACTLY MATCH (no subclasses)
final Subscription[] deadSubscriptions = subManager.getSubs(DeadMessage.class); // can return null
if (deadSubscriptions != null) {
final DeadMessage deadMessage = new DeadMessage(message1, message2, message3);
for (int i = 0; i < deadSubscriptions.length; i++) {
sub = deadSubscriptions[i];
sub.publish(errorHandler, deadMessage);
}
if (!hasSubs) {
// Dead Event must EXACTLY MATCH (no subclasses)
final Subscription[] deadSubscriptions = subManager.getSubs(DeadMessage.class); // can return null
if (deadSubscriptions != null) {
final DeadMessage deadMessage = new DeadMessage(message1, message2, message3);
for (int i = 0; i < deadSubscriptions.length; i++) {
sub = deadSubscriptions[i];
sub.publish(publisher, errorHandler, deadMessage);
}
}
} catch (DispatchCancel ignored) {
// we wanted to cancel the dispatch for these specific messages
}
}
}