Event Admin
Contents
- Utilise la notion de topic (une string)
- envoie des properties : Map<String, Object> properties = new HashMap<>();
Event Publisher Example
@Component(
property = {
"osgi.command.scope=fipro",
"osgi.command.function=boss" },
service = BossCommand.class)
public class BossCommand {
@Reference
EventAdmin eventAdmin;
@Descriptor("As a mafia boss you want something to be done")
public void boss(
@Descriptor("the command that should be executed. "
+ "possible values are: convince, encash, solve")
String command,
@Descriptor("who should be 'convinced', "
+ "'asked for protection money' or 'finally solved'")
String target) {
// create the event properties object
Map<String, Object> properties = new HashMap<>();
properties.put(MafiaBossConstants.PROPERTY_KEY_TARGET, target);
Event event = null;
switch (command) {
case "convince":
event = new Event(MafiaBossConstants.TOPIC_CONVINCE, properties);
break;
case "encash":
event = new Event(MafiaBossConstants.TOPIC_ENCASH, properties);
break;
case "solve":
event = new Event(MafiaBossConstants.TOPIC_SOLVE, properties);
break;
default:
System.out.println("Such a command is not known!");
}
if (event != null) {
eventAdmin.postEvent(event);
}
}
}
Event Consumer Example
@Component(
property = EventConstants.EVENT_TOPIC
+ "=" + MafiaBossConstants.TOPIC_CONVINCE)
public class Luigi implements EventHandler {
@Override
public void handleEvent(Event event) {
System.out.println("Luigi: "
+ event.getProperty(MafiaBossConstants.PROPERTY_KEY_TARGET)
+ " was 'convinced' to support our family");
}
}