Annotation Interface ElementEventConsumer
@Target(METHOD)
@Retention(RUNTIME)
@Repeatable(ElementEventConsumers.class)
public @interface ElementEventConsumer
Annotates a method capable of receiving an instance of
Event. When an event is published,
the runtime will invoke this method with the event data. If the method's parameters match the types
of Event.getEventArguments(), the method will receive the unpacked arguments directly.
Direct Event Consumption
For classes annotated withElementServiceExport, event consumer methods are automatically discovered
and invoked when matching events are published. The service locator will find the service instance and
dispatch the event to it.
@ElementServiceExport
public class MyService {
@ElementEventConsumer("my.event")
public void onMyEvent(String arg1, int arg2) {
// Called when "my.event" is published with matching arguments
}
}
Routed Event Consumption
For implementation classes that are not directly exposed as services, use thevia() field to
route events through an exported service interface. This allows internal implementation classes to receive
events without needing to be part of the public API.
// Public service interface
@ElementPublic
public interface MyService {
// Service methods
}
// Private implementation that receives events
public class MyServiceImpl implements MyService {
@ElementEventConsumer(
value = "my.event",
via = @ElementServiceReference(MyService.class)
)
public void onMyEvent(String arg1, int arg2) {
// Called when "my.event" is published
// The service locator finds this instance via MyService interface
}
}
When using via(), the service locator will:
- Look up the service instance using the specified service interface
- Cast the instance to the declaring class of the annotated method
- Invoke the method on that instance
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classPlaceholder type used as the default value forvia()to indicate that no service routing should be used. -
Required Element Summary
Required Elements -
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionSpecifies a service interface through which this event consumer should be invoked.
-
Element Details
-
value
String valueThe name of theEvent.- Returns:
- the name of the event
-
via
Specifies a service interface through which this event consumer should be invoked. When specified, theServiceLocatorwill look up the service instance using the provided service reference, then invoke this method on that instance.This allows implementation classes that are not directly exported to receive events by routing through their public service interface. The implementation class must still be annotated with
ElementServiceExportand implement the interface specified in this field.If not specified (defaults to
ElementEventConsumer.None), the event consumer is invoked directly on the service instance, which requires the declaring class to be annotated withElementServiceExport.Example:
@ElementServiceExport public class MyServiceImpl implements MyPublicService { @ElementEventConsumer( value = "my.event", via = @ElementServiceReference(MyPublicService.class) ) public void onEvent(String arg) { // Invoked via lookup of MyPublicService } }- Returns:
- the service reference to use for event routing, or
@ElementServiceReference(None.class)to use direct invocation - Since:
- 3.7
- See Also:
- Default:
@dev.getelements.elements.sdk.annotation.ElementServiceReference(dev.getelements.elements.sdk.annotation.ElementEventConsumer.None.class)
-