Java Reference
In-Depth Information
In Listing 11‐6, you start with the code that disambiguates your
Strings
.
LISTING 11‐6:
The annotation Qualii er interface
package com.devchronicles.observer;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD,ElementType.PARAMETER})
public @interface MessageEvent {
Type value();
enum Type{ SERVICE, PARAMETER }
The interface preceding class dei nes a
MessageEvent
qualii er and two enum types (
SERVICE
and
PARAMETER
) that you will use to act as annotation to mark the strings to be i red by the event instances.
import com.devchronicles.observer.MessageEvent.Type;
@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class EventService {
@Inject
private String message;
@Inject
@MessageEvent(Type.SERVICE)
Event<String> serviceEvent;
@Inject
@MessageEvent(Type.PARAMETER)
Event<String> parameterEvent;
public void startService(){
serviceEvent.fire("Starting service "+message);
parameterEvent.fire("‐d ‐p");
}
To use the
Qualii ers
, you just add the
MyEvent
annotation to the relevant injected instance with
the desired enum type in parenthesis. Then you later i re the events from within the
startService
method, just as you did before in the previous example. The bold parts code lines are all you have
added to the previous example in the previous listing.
Now you'll add the annotations to the observer part. As you did before, you just have to add the
Qualii ers
to the relevant
@Observes
annotation.