Example #1
0
  @SuppressWarnings("Convert2Lambda")
  private void extractListenerForRegularObject(
      final Object listener,
      final MethodAccess methodAccess,
      final String channel,
      final boolean consume) {

    logger.info(
        "EventManager {}:: {} is listening with method {} on channel {} and is consuming? {}",
        name,
        listener.getClass().getSimpleName(),
        methodAccess.name(),
        channel,
        consume);
    if (consume) {

      /* Do not use Lambda, this has to be a consume! */
      this.register(
          channel,
          new EventConsumer<Object>() {
            @Override
            public void listen(Event<Object> event) {

              invokeEventMethod(event, methodAccess, listener);
            }
          });
    } else {
      /* Do not use Lambda, this has to be a subscriber! */
      this.register(
          channel,
          new EventSubscriber<Object>() {
            @Override
            public void listen(Event<Object> event) {

              invokeEventMethod(event, methodAccess, listener);
            }
          });
    }
  }
Example #2
0
  private void extractEventListenerFromMethod(
      final Object listener,
      final MethodAccess methodAccess,
      final AnnotationData listen,
      final ServiceQueue serviceQueue) {

    logger.info(
        "EventManager {} ::extractEventListenerFromMethod  :: "
            + "{} is listening with method {} using annotation data {} ",
        name,
        serviceQueue,
        methodAccess.name(),
        listen.getValues());

    final String channel = listen.getValues().get("value").toString();
    final boolean consume = (boolean) listen.getValues().get("consume");

    if (serviceQueue == null) {
      extractListenerForRegularObject(listener, methodAccess, channel, consume);
    } else {
      extractListenerForService(serviceQueue, channel, consume);
    }
  }
Example #3
0
  private void doListen(final Object listener, final ServiceQueue serviceQueue) {

    if (debug) {
      logger.debug(
          "EventManager {}  registering listener {} with serviceQueue {}",
          name,
          listener,
          serviceQueue);
    }
    final ClassMeta<?> listenerClassMeta = ClassMeta.classMeta(listener.getClass());
    final Iterable<MethodAccess> listenerMethods = listenerClassMeta.methods();

    /* Add methods as listeners if they have a listen annotation. */
    for (final MethodAccess methodAccess : listenerMethods) {
      AnnotationData listenAnnotationData = getListenAnnotation(methodAccess);
      if (listenAnnotationData == null) continue;
      extractEventListenerFromMethod(listener, methodAccess, listenAnnotationData, serviceQueue);
    }

    /* Look for listener channel implementations. */
    final Class<?>[] interfacesFromListener = listenerClassMeta.cls().getInterfaces();

    /* Iterate through interfaces and see if any are marked with the event channel annotation. */
    for (Class<?> interfaceClass : interfacesFromListener) {
      final ClassMeta<?> metaFromListenerInterface = classMeta(interfaceClass);

      final AnnotationData eventChannelAnnotation =
          metaFromListenerInterface.annotation(AnnotationUtils.EVENT_CHANNEL_ANNOTATION_NAME);
      if (eventChannelAnnotation == null) {
        continue;
      }

      /* If we got this far, then we are dealing with an event channel interface
      so register the methods from this interface as channel listeners.
       */
      final Iterable<MethodAccess> interfaceMethods = metaFromListenerInterface.methods();

      final String classEventBusName =
          getClassEventChannelName(metaFromListenerInterface, eventChannelAnnotation);

      for (MethodAccess methodAccess : interfaceMethods) {

        /* By default the method name forms part of the event bus name,
        but this can be overridden by the EVENT_CHANNEL_ANNOTATION_NAME annotation on the method.
         */
        final AnnotationData methodAnnotation =
            methodAccess.annotation(AnnotationUtils.EVENT_CHANNEL_ANNOTATION_NAME);

        String methodEventBusName =
            methodAnnotation != null && methodAnnotation.getValues().get("value") != null
                ? methodAnnotation.getValues().get("value").toString()
                : null;

        if (Str.isEmpty(methodEventBusName)) {
          methodEventBusName = methodAccess.name();
        }

        final String channelName = createChannelName(null, classEventBusName, methodEventBusName);

        if (serviceQueue == null) {
          extractListenerForRegularObject(listener, methodAccess, channelName, false);
        } else {
          extractListenerForService(serviceQueue, channelName, false);
        }
      }
    }
  }