Package com.netflix.eventbus.spi

Examples of com.netflix.eventbus.spi.Subscribe


     *
     * @return The {@link EventConsumer} instance. <code>null</code> if none found.
     */
    private EventConsumer findEventConsumerForSubscriberMethod(SubscriberInfo subscriberInfo, String callDescription) {
        Method subscriberMethod = subscriberInfo.getSubscriberMethod();
        Subscribe subscribeAnnotation = subscriberMethod.getAnnotation(Subscribe.class);
        if (null == subscribeAnnotation) {
            LOGGER.error(String.format("The subscriber method: %s is not annotated with @Subscribe. Ignoring %s call.",
                    subscriberMethod, callDescription));
            return null;
        }
View Full Code Here


     */
    @SuppressWarnings("fallthrough")
    static Map<Method, String> validate(Object subscriber, List<Method> subscriberMethods) {
        Map<Method, String> errors = new HashMap<Method, String>(subscriberMethods.size());
        for (Method method : subscriberMethods) {
            Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
            if (null != subscribeAnnotation) {
                issueWarningsIfPresent(subscribeAnnotation, subscriber, method);
                Class<?>[] parameterTypes = method.getParameterTypes();
                if (parameterTypes.length != 1) {
                    errors.put(method, String.format(
                            "Subscriber: %s's Method: %s is annotated as a subscriber but defines more that one arguments.",
                            subscriber.getClass(), method.toGenericString()));
                } else if (parameterTypes[0].equals(Object.class)
                           && !subscriber.getClass().equals(CatchAllSubscriber.class)
                           && !DynamicSubscriber.class.isAssignableFrom(subscriber.getClass())) {
                    errors.put(method, String.format(
                            "Subscriber: %s's Method: %s is a subscriber for java.lang.Object, that is too broad an interest.",
                            subscriber.getClass(), method.toGenericString()));
                } else if (DynamicSubscriber.class.isAssignableFrom(subscriber.getClass())
                           && !parameterTypes[0].equals(Object.class)) {
                    Class<?> targetedEventType = ((DynamicSubscriber) subscriber).getEventType();
                    if (!parameterTypes[0].isAssignableFrom(targetedEventType)) {
                        errors.put(method, String.format(
                                "Dynamic subscriber: %s's Method: %s's argument is not compatible with the interested event type %s.",
                                subscriber.getClass(), method.toGenericString(), targetedEventType.getName()));
                    }
                } else if (subscribeAnnotation.batchingStrategy() != Subscribe.BatchingStrategy.None) {
                    if (!(Iterable.class.isAssignableFrom(parameterTypes[0]))) {
                        errors.put(method, String.format(
                                "Subscriber: %s's Method: %s is annotated with batching strategy: %s but does not accept an Iterable argument.",
                                subscriber.getClass(), method.toGenericString(),
                                subscribeAnnotation.batchingStrategy()));
                    } else {
                        Type[] genericParameterTypes = method.getGenericParameterTypes();
                        if (!(genericParameterTypes[0] instanceof ParameterizedType)) {
                            errors.put(method, String.format(
                                    "Subscriber: %s's Method: %s is a subscriber for java.lang.Object, that is too broad an interest.",
                                    subscriber.getClass(), method.toGenericString()));
                        }
                    }

                    switch (subscribeAnnotation.batchingStrategy()) {
                        case SizeOrAge:
                            if (subscribeAnnotation.batchSize() <= 1) {
                                errors.put(method, String.format(
                                        "Subscriber: %s's Method: %s is annotated with batching strategy: %s but does define a batch size.",
                                        subscriber.getClass(), method.toGenericString(),
                                        subscribeAnnotation.batchingStrategy()));
                            }
                        case Age:
                            if (subscribeAnnotation.batchAge() <= 0) {
                                errors.put(method, String.format(
                                        "Subscriber: %s's Method: %s is annotated with batching strategy: %s but does define a batch age.",
                                        subscriber.getClass(), method.toGenericString(),
                                        subscribeAnnotation.batchingStrategy()));
                            }
                            break;
                    }
                }
            }
View Full Code Here

     */
    public static SubscriberConfigProvider.SubscriberConfig getSubscriberConfig(Method subMethod, Object subscriber) {
        Preconditions.checkNotNull(subscriber);
        Preconditions.checkNotNull(subMethod);

        Subscribe annotation = subMethod.getAnnotation(Subscribe.class);
        if (null == annotation) {
            throw new IllegalArgumentException(String.format("Subscriber method %s does not contain a subscriber annotation.", subMethod.toGenericString()));
        }
        SubscriberConfigProvider.SubscriberConfig config = null;
        if (SubscriberConfigProvider.class.isAssignableFrom(subscriber.getClass())) {
            config = ((SubscriberConfigProvider) subscriber).getConfigForName(annotation.name());
        }

        if (null == config) {
            config = new AnnotationBasedSubscriberConfig(annotation);
        }
View Full Code Here

     */
    public static Class<?> getInterestedEventType(Object subscriber, Method subMethod) {
        Class<?> interestedEventType = (DynamicSubscriber.class.isAssignableFrom(subscriber.getClass()))
                          ? ((DynamicSubscriber) subscriber).getEventType()
                          : subMethod.getParameterTypes()[0];/* The subscriber method must be valid here. */
        Subscribe annotation = subMethod.getAnnotation(Subscribe.class);
        if (annotation.batchingStrategy() != Subscribe.BatchingStrategy.None
            && Iterable.class.isAssignableFrom(interestedEventType)) {
            // Batch consumer, the parameter type of Iterable is the actual event type.
            Type[] genericMethodParams = subMethod.getGenericParameterTypes();
            ParameterizedType interestedEventParam = (ParameterizedType) genericMethodParams[0]; // Validation ensures that the argument is generic Iterable.
            Type[] iterableTypeParams = interestedEventParam.getActualTypeArguments();
View Full Code Here

TOP

Related Classes of com.netflix.eventbus.spi.Subscribe

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.