Module it.rebirthproject.ufoeb
Class EventBusBuilder
java.lang.Object
it.rebirthproject.ufoeb.architecture.eventbus.EventBusBuilder
A builder which can be used to create an
EventBus-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate InheritancePolicyThe eventsInheritancePolicyused by the eventbus.private StringThe #inheritancePackageFrontierPath is used to stop the iteration over classes while using event inheritance.private booleanThis parameter should be used when you want to use inheritance over a listener and all its superclasses.private IntegerThe number of internal workers used by theEventBusbehind the scenes to process posted events and notify listeners.private IntegerThe length of the infrastructure internal queues used by theEventBusInfrastructure.private booleanThis parameter ensures that workers gets an unmodifiableRegistration's list.private booleanIf a registering listener does not have anyListenannotated method or, in case of event inheritance enabled, also its super classes or interfaces does not have anyListenannotated method, then anEventBusExceptionis thrown.private booleanSets the bus to log warnings when no registration is found for a specific event.private booleanif set then anEventBusExceptionis thrown when an invalidListenannotated method is found in a Listener.private booleanThis parameter should be used when you want to use spped up listener's methods call.private booleanBoolean attribute used to enable or disable verbose logging -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbuild()Build anEventBusconfigured byEventBusBuilder's properties eventually set or with pre-configured default valuesSets theeventInheritancePolicytoCompleteEventInheritancePolicySets theeventInheritancePolicytoClassEventInheritancePolicysetInheritancePackageFrontierPath(String inheritancePackageFrontierPath) Sets the package used to stop the iteration over classes while using event inheritance.if set this parameter enables event inheritance over listeners classes (and superclasses).setNumberOfWorkers(Integer numberOfWorkers) Sets the number of internal workers used by the eventbus behind the scenes to process posted events and notify listenersSets the number of internal workers used by the eventbus equal to the available processors of current device.setQueuesLength(Integer queueLength) Sets the length of internal queues used by the theEventBusInfrastructure.Sets thesafeRegistrationsListNeededattribute to trueIf a registering listener does not have anyListenannotated method or, in case of event inheritance enabled, also its super classes or interfaces does not have anyListenannotated method, then anEventBusExceptionis thrown.Sets the bus to log warnings when no registration is found for a specific event.if set then anEventBusExceptionis thrown when an invalidListenannotated method is found in a Listener.This set the usage of Lambdafactory instead of standard java reflectionIf this setter is set, the verbose logging will be enabled
-
Field Details
-
queueLength
The length of the infrastructure internal queues used by theEventBusInfrastructure. Basically the length is important for the commandQueryMessageQueue of theBusMemoryStateManagerto avoid the block of theBusMemoryStateManagerand should be calculated correctly depending on the pressure of the application that uses the bus (posts speed, number of posting threads, and so on). The default number of workers is 100. -
numberOfWorkers
The number of internal workers used by theEventBusbehind the scenes to process posted events and notify listeners. The default number of workers is 1. -
safeRegistrationsListNeeded
private boolean safeRegistrationsListNeededThis parameter ensures that workers gets an unmodifiableRegistration's list. For best performance leave this parameter to false (default) and avoid to register/unregister listeners at runtime time. Otherwise, set it to true. -
listenerSuperclassInheritance
private boolean listenerSuperclassInheritanceThis parameter should be used when you want to use inheritance over a listener and all its superclasses. Enabling it will let the bus look for all listeners' methods considering also all their superclasses methods. The default value is to look for only the listener's class methods. -
useLambdaFactoryInsteadOfStandardReflection
private boolean useLambdaFactoryInsteadOfStandardReflectionThis parameter should be used when you want to use spped up listener's methods call. Instead the standard reflection getMethod() workers will use the method handler created by the lambdafactory getMethodHandler(). Very useful when listener's methods are not time consuming. Anyway beware that MethodHandles.lookup() does not work with modules and with java 9> when a listener class is located in a different module than the eventbus's one. So basically this will work always with java 8. And with java 9> when you do not use modules, for example in an application. If you want to create a library with java 9> that uses the ufoeventbus then you must use the default method. For more informations see https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/invoke/MethodHandles.Lookup.html -
throwNoListenerAnnotationException
private boolean throwNoListenerAnnotationExceptionIf a registering listener does not have anyListenannotated method or, in case of event inheritance enabled, also its super classes or interfaces does not have anyListenannotated method, then anEventBusExceptionis thrown. -
throwNoRegistrationsWarning
private boolean throwNoRegistrationsWarningSets the bus to log warnings when no registration is found for a specific event. If not set, no warning will be printed to the log. This is useful to debug application.Example:
Event E is posted to the bus but no Listener is registered to listen to it.
-
throwNotValidMethodException
private boolean throwNotValidMethodExceptionif set then anEventBusExceptionis thrown when an invalidListenannotated method is found in a Listener.The method must be defined using the following rules:
- The method must be public and not static
- The method must have only one parameter that represents the listened event
-
eventInheritancePolicy
The eventsInheritancePolicyused by the eventbus. The default value for the inheritance policy isNoEventInheritancePolicy -
inheritancePackageFrontierPath
The #inheritancePackageFrontierPath is used to stop the iteration over classes while using event inheritance. If a class belongs to the set package then the iteration stops. This parameter must be used only when event inheritance policy is enabled otherwise it is useless even if set.Example:
Class A extends class B and is defined under x.y.z java package Class B is defined under x.y java package inheritancePackageFrontierPath is defined to x.y,z
Then the iteration will stop when it reaches class A without iterating on its parent class. -
verboseLogging
private boolean verboseLoggingBoolean attribute used to enable or disable verbose logging
-
-
Constructor Details
-
EventBusBuilder
public EventBusBuilder()
-
-
Method Details
-
setSafeRegistrationsListNeeded
Sets thesafeRegistrationsListNeededattribute to true- Returns:
- The
EventBusBuilderinstance configured with thesafeRegistrationsListNeededset to true - See Also:
-
setThrowNoListenerAnnotationException
If a registering listener does not have anyListenannotated method or, in case of event inheritance enabled, also its super classes or interfaces does not have anyListenannotated method, then anEventBusExceptionis thrown.- Returns:
- The
EventBusBuilderinstance configured to throw a no listener foundEventBusException.
-
setThrowNotValidMethodException
if set then anEventBusExceptionis thrown when an invalidListenannotated method is found in a Listener.The method must be defined using the following rules:
- The method must be public and not static
- The method must have only one parameter that represents the listened event
- Returns:
- The
EventBusBuilderinstance configured to throw a not valid methodEventBusException.
-
setThrowNoRegistrationsWarning
Sets the bus to log warnings when no registration is found for a specific event. If not set, no warning will be printed to the log. This is useful to debug application.Example:
Event E is posted to the bus but no Listener is registered to listen to it.
- Returns:
- The
EventBusBuilderinstance configured to log no registration found warnings.
-
setListenerSuperclassInheritance
if set this parameter enables event inheritance over listeners classes (and superclasses). Otherwise, the default is to not use listener inheritance at all.Example:
Class Cat extends class Animal and both listen to event E, but only class Cat is registered on the bus. if class event inheritance is set then event E is registered inside the bus with both Cat and Animal classes. When a post arrives carrying event E, then both classes will receive the event even if only class Cat is registered on the bus. Otherwise only Cat class will receive the event.
- Returns:
- The
EventBusBuilderinstance configured to iterate over listener classes and super classes.
-
setUseLambdaFactoryInsteadOfStandardReflection
This set the usage of Lambdafactory instead of standard java reflection- Returns:
- The
EventBusBuilderinstance configured to throw a no listener foundEventBusException. - See Also:
-
setEventSuperclassInheritance
Sets theeventInheritancePolicytoClassEventInheritancePolicy- Returns:
- The
EventBusBuilderconfigured with theClassEventInheritancePolicy - See Also:
-
setEventInterfaceInheritance
- Returns:
- The
EventBusBuilderconfigured with theInterfaceEventInheritancePolicy - See Also:
-
setCompleteEventInheritance
Sets theeventInheritancePolicytoCompleteEventInheritancePolicy- Returns:
- The
EventBusBuilderconfigured with theCompleteEventInheritancePolicy - See Also:
-
setQueuesLength
Sets the length of internal queues used by the theEventBusInfrastructure.- Parameters:
queueLength- The length of the infrastructure queues- Returns:
- The
EventBusBuilderinstance configured with the updated number of workers
-
setNumberOfWorkers
Sets the number of internal workers used by the eventbus behind the scenes to process posted events and notify listeners- Parameters:
numberOfWorkers- The number of workers used by the eventbus- Returns:
- The
EventBusBuilderinstance configured with the updated number of workers
-
setNumberOfWorkersAsAvailableProcessors
Sets the number of internal workers used by the eventbus equal to the available processors of current device.- Returns:
- The
EventBusBuilderinstance configured with the updated number of workers - See Also:
-
setInheritancePackageFrontierPath
Sets the package used to stop the iteration over classes while using event inheritance. If a class belongs to the set package then the iteration stops. This parameter must be used only when event inheritance policy is enabled otherwise it is useless even if set.Example:
Class A extends class B and is defined under x.y.z java package Class B is defined under x.y java package inheritancePackageFrontierPath is defined to x.y,z
Then the iteration will stop when it reaches class A without iterating on its parent class.- Parameters:
inheritancePackageFrontierPath- The package frontier path- Returns:
- The
EventBusBuilderinstance configured with the updated frontier path - See Also:
-
setVerboseLogging
If this setter is set, the verbose logging will be enabled- Returns:
- The
EventBusBuilderinstance configured with verbose logging
-
build
Build anEventBusconfigured byEventBusBuilder's properties eventually set or with pre-configured default values- Returns:
- an
EventBusinstance - Throws:
EventBusException- If some error occurs while building a new eventbus
-