Example #1
0
  @Override
  public void stopListening(Object listener) {
    final ClassMeta<?> classMeta = ClassMeta.classMeta(listener.getClass());
    final Iterable<MethodAccess> methods = classMeta.methods();

    for (final MethodAccess methodAccess : methods) {
      final AnnotationData listen = getListenAnnotation(methodAccess);
      if (listen == null) continue;
      stopListeningToMethodEventListeners(listener, methodAccess, listen);
    }
  }
Example #2
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);
        }
      }
    }
  }