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
Modifier and TypeFieldDescriptionprivate InheritancePolicy
The eventsInheritancePolicy
used by the eventbus.private String
The #inheritancePackageFrontierPath is used to stop the iteration over classes while using event inheritance.private boolean
This parameter should be used when you want to use inheritance over a listener and all its superclasses.private Integer
The number of internal workers used by theEventBus
behind the scenes to process posted events and notify listeners.private Integer
The length of the infrastructure internal queues used by theEventBusInfrastructure
.private boolean
This parameter ensures that workers gets an unmodifiableRegistration
's list.private boolean
If a registering listener does not have anyListen
annotated method or, in case of event inheritance enabled, also its super classes or interfaces does not have anyListen
annotated method, then anEventBusException
is thrown.private boolean
Sets the bus to log warnings when no registration is found for a specific event.private boolean
if set then anEventBusException
is thrown when an invalidListen
annotated method is found in a Listener.private boolean
This parameter should be used when you want to use spped up listener's methods call.private boolean
Boolean attribute used to enable or disable verbose logging -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionbuild()
Build anEventBus
configured byEventBusBuilder
's properties eventually set or with pre-configured default valuesSets theeventInheritancePolicy
toCompleteEventInheritancePolicy
Sets theeventInheritancePolicy
toClassEventInheritancePolicy
setInheritancePackageFrontierPath
(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 thesafeRegistrationsListNeeded
attribute to trueIf a registering listener does not have anyListen
annotated method or, in case of event inheritance enabled, also its super classes or interfaces does not have anyListen
annotated method, then anEventBusException
is thrown.Sets the bus to log warnings when no registration is found for a specific event.if set then anEventBusException
is thrown when an invalidListen
annotated 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 theBusMemoryStateManager
to avoid the block of theBusMemoryStateManager
and 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 theEventBus
behind 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 anyListen
annotated method or, in case of event inheritance enabled, also its super classes or interfaces does not have anyListen
annotated method, then anEventBusException
is 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 anEventBusException
is thrown when an invalidListen
annotated 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 eventsInheritancePolicy
used 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 thesafeRegistrationsListNeeded
attribute to true- Returns:
- The
EventBusBuilder
instance configured with thesafeRegistrationsListNeeded
set to true - See Also:
-
setThrowNoListenerAnnotationException
If a registering listener does not have anyListen
annotated method or, in case of event inheritance enabled, also its super classes or interfaces does not have anyListen
annotated method, then anEventBusException
is thrown.- Returns:
- The
EventBusBuilder
instance configured to throw a no listener foundEventBusException
.
-
setThrowNotValidMethodException
if set then anEventBusException
is thrown when an invalidListen
annotated 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
EventBusBuilder
instance 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
EventBusBuilder
instance 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
EventBusBuilder
instance configured to iterate over listener classes and super classes.
-
setUseLambdaFactoryInsteadOfStandardReflection
This set the usage of Lambdafactory instead of standard java reflection- Returns:
- The
EventBusBuilder
instance configured to throw a no listener foundEventBusException
. - See Also:
-
setEventSuperclassInheritance
Sets theeventInheritancePolicy
toClassEventInheritancePolicy
- Returns:
- The
EventBusBuilder
configured with theClassEventInheritancePolicy
- See Also:
-
setEventInterfaceInheritance
- Returns:
- The
EventBusBuilder
configured with theInterfaceEventInheritancePolicy
- See Also:
-
setCompleteEventInheritance
Sets theeventInheritancePolicy
toCompleteEventInheritancePolicy
- Returns:
- The
EventBusBuilder
configured 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
EventBusBuilder
instance 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
EventBusBuilder
instance 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
EventBusBuilder
instance 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
EventBusBuilder
instance configured with the updated frontier path - See Also:
-
setVerboseLogging
If this setter is set, the verbose logging will be enabled- Returns:
- The
EventBusBuilder
instance configured with verbose logging
-
build
Build anEventBus
configured byEventBusBuilder
's properties eventually set or with pre-configured default values- Returns:
- an
EventBus
instance - Throws:
EventBusException
- If some error occurs while building a new eventbus
-