Class StateBean
- All Implemented Interfaces:
HandleAware
,Cloneable
The StateService
is used to broadcast changes to the
bean. It will only send the bean if it has changed from the
previous broadcast. This is determined by cloning the bean
and then comparing the new version to the previously cloned
bean and only broadcasting / updating if it is not equal.
This allows equality to determine what triggers the broadcast.
For example, it's possible to have a state bean with data that
does not trigger a broadcast, but will be included when
equality related changes are made.
In general, StateBean
instances must be registered
with StateService
. This allows clients to query the
current state of beans before any changes have been made.
Any StateBean
instances placed in the
BeanContext
will automatically be registered. This
also means it's possible to use BeanContext.connect()
and BeanContext.disconnect()
to manage registrations
or simply register them manually. Once registered, the
broadcast()
method can be used to trigger the service
to check for changes in the bean and send any changes.
- Since:
- 1.0
- Version:
- 2023-10-07
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addListener
(StateBeanListener listener) Add a listener to the list.boolean
Causes the bean to be examined for changes relative to the last broadcast attempt and if any changes are found the bean will be sent via theMessageBroker
.boolean
Returns the handle for this bean.getPath()
Return the path of the bean.Class<?>
getView()
Returns the optional view class to use when converting this bean to json before being broadcast.int
hashCode()
void
removeListener
(StateBeanListener listener) Remove a listener from the list.void
Set the optional view class to use when converting this bean to json before being broadcast.Methods inherited from class java.lang.Object
getClass, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.tccc.kos.commons.core.service.handle.HandleAware
addHandleChild, getName
-
Constructor Details
-
StateBean
public StateBean() -
StateBean
-
-
Method Details
-
getPath
Return the path of the bean. This will be used as part of the topic used to broadcast changes in bean state via the message broker. It should be aMessageBroker
topic friendly string. If the bean is associated with a service or something that has aHandlePath
then this can simply return the path of the associated object.This must be a unique string relative to all other state beans as the
MessageBroker
topic used to send state changes is the same base topic for all beans, with just the final segment of the topic containing this string.This string must not change over time.
- Specified by:
getPath
in interfaceHandleAware
- Returns:
- this bean's handle path
-
broadcast
public boolean broadcast()Causes the bean to be examined for changes relative to the last broadcast attempt and if any changes are found the bean will be sent via theMessageBroker
.- Returns:
- true if the bean has changed
-
getView
Returns the optional view class to use when converting this bean to json before being broadcast. -
setView
Set the optional view class to use when converting this bean to json before being broadcast. This allows various parts of the bean to be filtered out when sending. -
addListener
Add a listener to the list.- Parameters:
listener
- the listener to add
-
removeListener
Remove a listener from the list.- Parameters:
listener
- the listener to remove
-
equals
-
hashCode
public int hashCode() -
getHandle
Description copied from interface:HandleAware
Returns the handle for this bean. This is used by the context to compute the opaque handle for the object.- Specified by:
getHandle
in interfaceHandleAware
- Returns:
- the handle node for the bean
-