Search in sources :

Example 1 with Outbound

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);
}
Also used : Outbound(fish.payara.micro.cdi.Outbound) PayaraClusteredCDIEventImpl(fish.payara.appserver.micro.services.PayaraClusteredCDIEventImpl) PayaraClusteredCDIEvent(fish.payara.micro.event.PayaraClusteredCDIEvent) Annotation(java.lang.annotation.Annotation)

Example 2 with Outbound

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);
                }
            }
        });
    }
}
Also used : Context(org.glassfish.internal.api.JavaEEContextUtil.Context) InitialContext(javax.naming.InitialContext) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) Serializable(java.io.Serializable) HashSet(java.util.HashSet) Set(java.util.Set) Inbound(fish.payara.micro.cdi.Inbound) Annotation(java.lang.annotation.Annotation) Outbound(fish.payara.micro.cdi.Outbound)

Aggregations

Outbound (fish.payara.micro.cdi.Outbound)2 Annotation (java.lang.annotation.Annotation)2 PayaraClusteredCDIEventImpl (fish.payara.appserver.micro.services.PayaraClusteredCDIEventImpl)1 Inbound (fish.payara.micro.cdi.Inbound)1 PayaraClusteredCDIEvent (fish.payara.micro.event.PayaraClusteredCDIEvent)1 Serializable (java.io.Serializable)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 InitialContext (javax.naming.InitialContext)1 Context (org.glassfish.internal.api.JavaEEContextUtil.Context)1 ExtendedDeploymentContext (org.glassfish.internal.deployment.ExtendedDeploymentContext)1