Search in sources :

Example 1 with SubscriberGroup

use of com.oracle.coherence.spring.annotation.SubscriberGroup in project coherence-spring by coherence-community.

the class NamedTopicConfiguration method getSubscriber.

@SuppressWarnings("unchecked")
@Bean(destroyMethod = "release")
@DependsOn(CoherenceSpringConfiguration.COHERENCE_SERVER_BEAN_NAME)
@Primary
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
<V> Subscriber<V> getSubscriber(InjectionPoint injectionPoint) {
    List<Subscriber.Option> options = new ArrayList<>();
    final MergedAnnotations mergedAnnotations = MergedAnnotations.from(injectionPoint.getAnnotatedElement());
    final MergedAnnotation<SubscriberGroup> mergedSubscribedGroupAnnotation = mergedAnnotations.get(SubscriberGroup.class);
    if (mergedSubscribedGroupAnnotation.isPresent()) {
        String subscribedGroupName = mergedSubscribedGroupAnnotation.synthesize().value();
        if (StringUtils.hasLength(subscribedGroupName)) {
            options.add(Subscriber.Name.of(subscribedGroupName));
        }
    }
    final MergedAnnotation<FilterBinding> mergedFilterBindingAnnotation = mergedAnnotations.get(FilterBinding.class);
    if (mergedFilterBindingAnnotation.isPresent()) {
        Filter filter = this.filterService.getFilter(injectionPoint);
        options.add(Subscriber.Filtered.by(filter));
    }
    final MergedAnnotation<ExtractorBinding> mergedExtractorBindingAnnotation = mergedAnnotations.get(ExtractorBinding.class);
    if (mergedExtractorBindingAnnotation.isPresent()) {
        ValueExtractor extractor = this.extractorService.getExtractor(injectionPoint);
        options.add(Subscriber.Convert.using(extractor));
    }
    NamedTopic<V> topic = getTopicInternal(injectionPoint);
    return options.isEmpty() ? topic.createSubscriber() : topic.createSubscriber(options.toArray(new Subscriber.Option[0]));
}
Also used : ExtractorBinding(com.oracle.coherence.spring.annotation.ExtractorBinding) ArrayList(java.util.ArrayList) SubscriberGroup(com.oracle.coherence.spring.annotation.SubscriberGroup) ValueExtractor(com.tangosol.util.ValueExtractor) FilterBinding(com.oracle.coherence.spring.annotation.FilterBinding) Filter(com.tangosol.util.Filter) Subscriber(com.tangosol.net.topic.Subscriber) MergedAnnotations(org.springframework.core.annotation.MergedAnnotations) DependsOn(org.springframework.context.annotation.DependsOn) Scope(org.springframework.context.annotation.Scope) Primary(org.springframework.context.annotation.Primary) Bean(org.springframework.context.annotation.Bean)

Example 2 with SubscriberGroup

use of com.oracle.coherence.spring.annotation.SubscriberGroup in project coherence-spring by coherence-community.

the class CoherenceTopicListenerSubscribers method createSubscribers.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void createSubscribers(Coherence coherence) {
    final Map<String, List<Method>> candidates = this.candidates.getCoherenceTopicListenerCandidateMethods();
    for (Map.Entry<String, List<Method>> entry : candidates.entrySet()) {
        final String beanName = entry.getKey();
        final List<Method> methods = entry.getValue();
        for (Method method : methods) {
            final Class<?> argumentClassType = method.getParameters()[0].getType();
            if (logger.isDebugEnabled()) {
                logger.debug(String.format("Handling Coherence %s - Bean: %s, method: %s", argumentClassType.getName(), beanName, method.getName()));
            }
            String topicName = Utils.getFirstTopicName(method).orElse(method.getName());
            SessionName sessionNameAnn = AnnotatedElementUtils.findMergedAnnotation(method, SessionName.class);
            String sessionName = (sessionNameAnn != null) ? sessionNameAnn.value() : Coherence.DEFAULT_NAME;
            if (!coherence.hasSession(sessionName)) {
                if (logger.isInfoEnabled()) {
                    logger.info(String.format("Skipping @CoherenceTopicListener annotated method subscription %s Session %s does not exist on Coherence instance %s", method, sessionName, coherence.getName()));
                }
                continue;
            }
            Session session = coherence.getSession(sessionName);
            Publisher[] sendToPublishers;
            String[] sendToTopics = getSendToTopicNames(method);
            if (sendToTopics.length > 0) {
                if (method.getReturnType().equals(Void.class)) {
                    if (logger.isInfoEnabled()) {
                        logger.info(String.format("Skipping @SendTo annotations for @CoherenceTopicListener annotated method %s - method return type is void", method));
                    }
                    sendToPublishers = new Publisher[0];
                } else {
                    sendToPublishers = new Publisher[sendToTopics.length];
                    for (int i = 0; i < sendToTopics.length; i++) {
                        NamedTopic<?> topic = session.getTopic(sendToTopics[i]);
                        sendToPublishers[i] = topic.createPublisher();
                    }
                }
            } else {
                sendToPublishers = new Publisher[0];
            }
            List<Subscriber.Option> options = new ArrayList<>();
            MergedAnnotation<SubscriberGroup> subscriberGroupAnn = MergedAnnotations.from(method).get(SubscriberGroup.class);
            subscriberGroupAnn.getValue("value", String.class).ifPresent((name) -> options.add(Subscriber.Name.of(name)));
            Set<Annotation> filterAnnotations = MergedAnnotations.from(method).stream().filter((mergedAnnotation) -> mergedAnnotation.getType().isAnnotationPresent(FilterBinding.class)).map(MergedAnnotation::synthesize).collect(Collectors.toSet());
            if (!filterAnnotations.isEmpty()) {
                Filter filter = this.filterService.resolve(filterAnnotations);
                if (filter != null) {
                    options.add(Subscriber.Filtered.by(filter));
                }
            }
            Set<Annotation> extractorAnnotations = MergedAnnotations.from(method).stream().filter((mergedAnnotation) -> mergedAnnotation.getType().isAnnotationPresent(ExtractorBinding.class)).map(MergedAnnotation::synthesize).collect(Collectors.toSet());
            if (!extractorAnnotations.isEmpty()) {
                ValueExtractor extractor = this.extractorService.resolve(extractorAnnotations);
                if (extractor != null) {
                    options.add(Subscriber.Convert.using(extractor));
                }
            }
            NamedTopic<?> topic = session.getTopic(topicName);
            Subscriber<?> subscriber = topic.createSubscriber(options.toArray(new Subscriber.Option[0]));
            Object bean = this.applicationContext.getBean(beanName);
            TopicSubscriber<?, ?, ?> topicSubscriber = new TopicSubscriber<>(topicName, subscriber, sendToPublishers, bean, method, this.scheduler);
            this.subscribers.add(topicSubscriber);
            topicSubscriber.nextMessage();
        }
    }
    this.subscribed = true;
}
Also used : ArrayList(java.util.ArrayList) SubscriberGroup(com.oracle.coherence.spring.annotation.SubscriberGroup) List(java.util.List) ArrayList(java.util.ArrayList) ExtractorBinding(com.oracle.coherence.spring.annotation.ExtractorBinding) Method(java.lang.reflect.Method) Publisher(com.tangosol.net.topic.Publisher) ValueExtractor(com.tangosol.util.ValueExtractor) SessionName(com.oracle.coherence.spring.annotation.SessionName) Annotation(java.lang.annotation.Annotation) MergedAnnotation(org.springframework.core.annotation.MergedAnnotation) FilterBinding(com.oracle.coherence.spring.annotation.FilterBinding) Filter(com.tangosol.util.Filter) Map(java.util.Map) Session(com.tangosol.net.Session)

Aggregations

ExtractorBinding (com.oracle.coherence.spring.annotation.ExtractorBinding)2 FilterBinding (com.oracle.coherence.spring.annotation.FilterBinding)2 SubscriberGroup (com.oracle.coherence.spring.annotation.SubscriberGroup)2 Filter (com.tangosol.util.Filter)2 ValueExtractor (com.tangosol.util.ValueExtractor)2 ArrayList (java.util.ArrayList)2 SessionName (com.oracle.coherence.spring.annotation.SessionName)1 Session (com.tangosol.net.Session)1 Publisher (com.tangosol.net.topic.Publisher)1 Subscriber (com.tangosol.net.topic.Subscriber)1 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 Map (java.util.Map)1 Bean (org.springframework.context.annotation.Bean)1 DependsOn (org.springframework.context.annotation.DependsOn)1 Primary (org.springframework.context.annotation.Primary)1 Scope (org.springframework.context.annotation.Scope)1 MergedAnnotation (org.springframework.core.annotation.MergedAnnotation)1 MergedAnnotations (org.springframework.core.annotation.MergedAnnotations)1