Interface Listener

All Superinterfaces:
NamedObject, Receiver<Transmittable>, Transceiver
All Known Subinterfaces:
Component, ComponentMixin, Converter<From,​To>, FailureReporter, FileService, Logger, MessageCounter, NetworkResource, Repeater, RepeaterMixin, Resource, SerializationSession, StringConverter<Value>, TwoWayConverter<From,​To>, WritableResource
All Known Implementing Classes:
Application, AssertingFailureReporter, AttachmentConverter, AttachmentListConverter, BaseCollectionChangeWatcher, BaseCollectionConverter, BaseComponent, BaseConverter, BaseExtractor, BaseFailureReporter, BaseFormattedConverter, BaseFormattedLocalTimeConverter, BaseHttpResource, BaseListConverter, BaseLogger, BaseNetworkResource, BaseReadableResource, BaseRepeater, BaseSetConverter, BaseStringConverter, BaseWritableResource, Batcher, BatcherTest, BatcherTest.TestBatcher, BodyConverter, BooleanConverter, Bytes.Converter, ClassConverter, ConcurrentMutableCount, Confidence.Converter, ConnectionListener, ConsoleLogger, ConsoleWriter, CoreApplicationProject, CoreCollectionsProject, CoreCollectionsUnitTest, CoreCommandLineProject, CoreConfigurationProject, CoreNetworkEmailProject, CoreNetworkFtpProject, CoreNetworkHttpProject, CoreNetworkSocketProject, CoreSecurityProject, CoreTestProject, Count.Converter, DateTimeConverter, Deployment, Deployment.Converter, DeploymentSet, DoubleConverter, Duration.Converter, Duration.MillisecondsConverter, Duration.SecondsConverter, EmailAddress.Converter, EmailAddressConverter, EmailAddressSetConverter, EmailSender, EnumConverter, EnumListConverter, EnumSetConverter, FieldArchive, File, File.Converter, FileCache, FileList.Converter, FilePath.Converter, FloatConverter, Folder, Folder.Converter, FolderChangeWatcher, FolderList.Converter, FormattedDoubleConverter, FormattedIntegerConverter, FormattedLongConverter, Frequency.Converter, FtpResource, HexadecimalLongConverter, Host.Converter, HttpDateTimeConverter, HttpGetResource, HttpNetworkLocation.Converter, HttpPostResource, HttpPutResource, HumanizedLocalDateTimeConverter, Identifier.Converter, IdentityConverter, InputResource, IntegerConverter, IntegerIdentifier.Converter, IntersectionTest, IsoLocalDateTimeConverter, IteratorTest, JarLauncher, JUnitFailureReporter, KernelProject, KivaKit, KivaKitThread, KryoSerializationSession, KryoUnitTest, LanguageIsoCode.Converter, Level.Converter, LocalDateConverter, LocalDateTimeConverter, LocalDateTimeWithMillisecondsConverter, LocalDateTimeWithSecondsConverter, LocalTimeConverter, LogFailureReporter, LogServiceLogger, LongConverter, Maximum.Converter, MessageChecker, MessageList, Minimum.Converter, MutableCount, NetworkCoreProject, NetworkLocation.Converter, NetworkPath.Converter, NullFailureReporter, NullListener, NullLogger, NullResource, OutputResource, PackageResource, PatternConverter, Percent.Converter, PeriodicCollectionChangeWatcher, PlainTextPassword.Converter, Port.Converter, Port.ListConverter, Project, Quantizable.Converter, RepeatingKivaKitThread, Resource.Converter, ResourceFolder.Converter, ResourceList.Converter, ResourcePath.Converter, ResourceProject, ResourceSection, Retry, SecureFtpResource, SecureHttpGetResource, SecureHttpPostResource, SerializationCoreProject, SerializationJerseyJsonProject, SerializationJsonProject, SerializationKryoProject, Server, SetDifferencerTest, SetOperationTest, Settings, SettingsFolder, SettingsPackage, SftpFolderWatcher, SmtpEmailSender, StateMachine, StringList.Converter, StringResource, ThrowableConverter, ThrowingFailureReporter, ThrowingListener, UnionTest, UnitTest, UserName.Converter, UtcDateTimeConverter, ValidationIssues, VersionConverter, VirtualReferenceTracker, WithoutTest, ZipEntry, ZoneIdConverter

public interface Listener extends Transceiver, Receiver<Transmittable>, NamedObject
Handles messages through Transceiver.onHandle(Transmittable) by receiving them through the receive(Transmittable) method unless the listener isDeaf(), meaning that it is discarding messages it receives.

Listening to Broadcasters

A listener can listen to a particular Broadcaster with listenTo(Broadcaster). Conversely, a listener can be added to a broadcaster with Broadcaster.addListener(Listener). Both methods achieve the same result.

Convenience Methods and Logging

A number of convenience methods in the Transceiver superinterface make it easy to transmit specific common messages to listeners. These convenience methods are particularly useful in logging because Loggers are Listeners which log the messages they receive. For example:

 private static final Logger LOGGER = LoggerFactory.newLogger();

     [...]

 LOGGER.warning("Unable to read $", file);
 

Repeater Chains

These convenience methods are even easier to access when an object implements Repeater by extending BaseRepeater. In the example below, EmployeeLoader is a Repeater which transmits a warning to all of its registered listeners. The PayrollProcessor class is also a Repeater which listens to messages transmitted by the EmployeeLoader and re-transmits them to its own listeners. Clients of the PayrollProcessor can listen to it in turn and they will receive the warning transmitted EmployeeLoader, when it is repeated by the PayrollProcessor. This pattern creates a chain of repeaters that terminates in one or more listeners. The final listener is often, but not always a logger. The base Application class in kivakit-application, for example, is a Repeater which logs the messages it receives by default.

Repeater Example

 class EmployeeLoader extends BaseRepeater
 {

     [...]

     warning("Unable to load $", employee);

     [...]
 }

 class PayrollProcessor extends BaseRepeater
 {

     [...]

     var loader = listenTo(new EmployeeLoader());

     [...]

 }

 var processor = LOGGER.listenTo(new PayrollProcessor());

 
Author:
jonathanl (shibo)
See Also:
Broadcaster, State(Art) Blog Article
  • Method Details

    • none

      static Listener none()
      Listener that does nothing with messages. Useful only when you want to discard output from something
    • isDeaf

      default boolean isDeaf()
      Returns:
      True if this listener doesn't do anything with the messages it gets
    • listenTo

      default <T extends Broadcaster> T listenTo(T broadcaster, MessageFilter filter)
      Registers this listener with the given broadcaster in being interested in transmitted messages
    • listenTo

      default <T extends Broadcaster> T listenTo(T broadcaster)
      Registers this listener with the given broadcaster in being interested in transmitted messages
    • message

      default void message(Message message)
      Not public API
    • onHandle

      default void onHandle(Transmittable message)
      Not public API
      Specified by:
      onHandle in interface Transceiver
    • onMessage

      void onMessage(Message message)
    • onReceive

      default void onReceive(Transmittable transmittable)
      Not public API
    • receive

      default void receive(Transmittable message)
      Not public API
      Specified by:
      receive in interface Receiver<Transmittable>