use of fish.payara.micro.cdi.Outbound in project Payara by payara.
the class ClusteredCDIEventBusImpl method onOutboundEvent.
void onOutboundEvent(@Observes @Outbound Serializable event, EventMetadata meta) throws IOException {
PayaraClusteredCDIEvent clusteredEvent;
// read the metadata on the Outbound Annotation to set data into the event
boolean loopBack = false;
String eventName = "";
String[] instanceName = new String[0];
for (Annotation annotation : meta.getQualifiers()) {
if (annotation instanceof Outbound) {
Outbound outboundattn = (Outbound) annotation;
eventName = outboundattn.eventName();
loopBack = outboundattn.loopBack();
instanceName = outboundattn.instanceName();
}
}
clusteredEvent = new PayaraClusteredCDIEventImpl(runtime.getLocalDescriptor(), event);
clusteredEvent.setLoopBack(loopBack);
clusteredEvent.setProperty(EVENT_PROPERTY, eventName);
clusteredEvent.setProperty(INSTANCE_PROPERTY, serializeArray(instanceName));
Set<Annotation> qualifiers = meta.getQualifiers();
if (qualifiers != null && !qualifiers.isEmpty()) {
clusteredEvent.addQualifiers(qualifiers);
}
runtime.publishCDIEvent(clusteredEvent);
}
use of fish.payara.micro.cdi.Outbound in project Payara by payara.
the class ClusteredCDIEventBusImpl method eventReceived.
@Override
public void eventReceived(final PayaraClusteredCDIEvent event) {
// first check if the event is targetted at a specific instance
String instanceName = event.getProperty(INSTANCE_PROPERTY);
if (!(instanceName == null) && !(instanceName.length() == 0)) {
// there is an instance name filter
String[] names = deserializeToArray(instanceName);
boolean forUs = false;
String thisInstance = runtime.getInstanceName();
for (String name : names) {
if (name.equals(thisInstance)) {
forUs = true;
break;
}
}
if (!forUs)
return;
}
try (Context ctx = ctxUtil.pushContext()) {
managedExecutorService.submit(new Runnable() {
@Override
public void run() {
ClassLoader oldCL = Utility.getClassLoader();
try {
Utility.setContextClassLoader(ctxUtil.getInvocationClassLoader());
// create the set of qualifiers for the event
// first add Inbound qualifier with the correct properties
Set<Annotation> qualifiers = new HashSet<>();
Serializable eventPayload = event.getPayload();
Inbound inbound = new Inbound() {
@Override
public String eventName() {
return event.getProperty(EVENT_PROPERTY);
}
@Override
public Class<? extends Annotation> annotationType() {
return Inbound.class;
}
};
qualifiers.add(inbound);
// Now create Qualifiers for the sent event qualifiers
Set<Annotation> receivedQualifiers = event.getQualifiers();
for (Annotation receivedQualifier : receivedQualifiers) {
// strip out OutBound as we don't want it even though it was sent over
if (!(receivedQualifier instanceof Outbound)) {
qualifiers.add(receivedQualifier);
}
}
Annotation[] annotations = qualifiers.toArray(new Annotation[0]);
bm.fireEvent(eventPayload, annotations);
} catch (IOException | ClassNotFoundException ex) {
Logger.getLogger(ClusteredCDIEventBusImpl.class.getName()).log(ex.getCause() instanceof IllegalStateException ? Level.FINE : Level.INFO, "Received Event but could not process it", ex);
} finally {
Utility.setContextClassLoader(oldCL);
}
}
});
}
}
Aggregations