Search in sources :

Example 1 with NamedTopic

use of com.tangosol.net.topic.NamedTopic in project micronaut-coherence by micronaut-projects.

the class CoherenceTopicListenerProcessor method createSubscribers.

@SuppressWarnings({ "unchecked", "rawtypes" })
void createSubscribers(Coherence coherence) {
    for (MethodHolder holder : methods) {
        List<Subscriber.Option> options = new ArrayList<>();
        ExecutableMethod<?, ?> method = holder.getMethod();
        String topicName = Utils.getFirstTopicName(method).orElse(method.getMethodName());
        String sessionName = method.stringValue(SessionName.class).orElse(Coherence.DEFAULT_NAME);
        if (!coherence.hasSession(sessionName)) {
            LOG.info("Skipping @CoherenceTopicListener annotated method subscription {} Session {} does not exist on Coherence instance {}", method, sessionName, coherence.getName());
            return;
        }
        Session session = coherence.getSession(sessionName);
        Publisher[] sendToPublishers;
        String[] sendToTopics = Utils.getSendToTopicNames(method);
        if (sendToTopics.length > 0) {
            if (method.getReturnType().isVoid()) {
                LOG.info("Skipping @SendTo annotations for @CoherenceTopicListener annotated method {} - 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];
        }
        method.stringValue(SubscriberGroup.class).ifPresent(name -> options.add(Subscriber.Name.of(name)));
        List<String> filterBindings = method.getAnnotationNamesByStereotype(FilterBinding.class);
        if (!filterBindings.isEmpty()) {
            Set<Annotation> annotations = filterBindings.stream().map(s -> method.getAnnotationType(s).orElse(null)).filter(Objects::nonNull).map(method::synthesize).collect(Collectors.toSet());
            Filter filter = filterFactories.resolve(annotations);
            if (filter != null) {
                options.add(Subscriber.Filtered.by(filter));
            }
        }
        List<String> extractorBindings = method.getAnnotationNamesByStereotype(ExtractorBinding.class);
        if (!extractorBindings.isEmpty()) {
            Set<Annotation> annotations = extractorBindings.stream().map(s -> method.getAnnotationType(s).orElse(null)).filter(Objects::nonNull).map(method::synthesize).collect(Collectors.toSet());
            ValueExtractor extractor = extractorFactories.resolve(annotations);
            if (extractor != null) {
                options.add(Subscriber.Convert.using(extractor));
            }
        }
        BeanDefinition<?> beanDefinition = holder.getBeanDefinition();
        Class<?> clsBeanType = beanDefinition.getBeanType();
        Object bean = context.getBean(clsBeanType);
        NamedTopic<?> topic = session.getTopic(topicName);
        Subscriber<?> subscriber = topic.createSubscriber(options.toArray(options.toArray(new Subscriber.Option[0])));
        TopicSubscriber<?, ?, ?> topicSubscriber = new TopicSubscriber(topicName, subscriber, sendToPublishers, bean, method, registry, scheduler);
        subscribers.add(topicSubscriber);
        topicSubscriber.nextMessage();
    }
    subscribed = true;
}
Also used : Filter(com.tangosol.util.Filter) Coherence(com.tangosol.net.Coherence) java.util(java.util) Publishers(io.micronaut.core.async.publisher.Publishers) NamedTopic(com.tangosol.net.topic.NamedTopic) Publisher(com.tangosol.net.topic.Publisher) FluxSink(reactor.core.publisher.FluxSink) ArrayUtils(io.micronaut.core.util.ArrayUtils) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) ElementArgumentBinderRegistry(io.micronaut.coherence.messaging.binders.ElementArgumentBinderRegistry) io.micronaut.coherence.annotation(io.micronaut.coherence.annotation) Session(com.tangosol.net.Session) Singleton(javax.inject.Singleton) Scheduler(reactor.core.scheduler.Scheduler) Function(java.util.function.Function) ExecutableMethod(io.micronaut.inject.ExecutableMethod) Inject(javax.inject.Inject) PreDestroy(javax.annotation.PreDestroy) ValueExtractor(com.tangosol.util.ValueExtractor) TaskExecutors(io.micronaut.scheduling.TaskExecutors) ApplicationContext(io.micronaut.context.ApplicationContext) ExecutableBinder(io.micronaut.core.bind.ExecutableBinder) Subscriber(com.tangosol.net.topic.Subscriber) Schedulers(reactor.core.scheduler.Schedulers) Named(javax.inject.Named) Argument(io.micronaut.core.type.Argument) BoundExecutable(io.micronaut.core.bind.BoundExecutable) FilterFactories(io.micronaut.coherence.FilterFactories) ExecutorService(java.util.concurrent.ExecutorService) DefaultExecutableBinder(io.micronaut.core.bind.DefaultExecutableBinder) Blocking(io.micronaut.core.annotation.Blocking) Logger(org.slf4j.Logger) CancellationException(java.util.concurrent.CancellationException) Collectors(java.util.stream.Collectors) ExecutableMethodProcessor(io.micronaut.context.processor.ExecutableMethodProcessor) CoherenceLifecycleEvent(com.tangosol.net.events.CoherenceLifecycleEvent) Flux(reactor.core.publisher.Flux) CompletionStage(java.util.concurrent.CompletionStage) Annotation(java.lang.annotation.Annotation) BeanDefinition(io.micronaut.inject.BeanDefinition) ExtractorFactories(io.micronaut.coherence.ExtractorFactories) CoherenceSubscriberException(io.micronaut.coherence.messaging.exceptions.CoherenceSubscriberException) Subscriber(com.tangosol.net.topic.Subscriber) Publisher(com.tangosol.net.topic.Publisher) ValueExtractor(com.tangosol.util.ValueExtractor) Annotation(java.lang.annotation.Annotation) Filter(com.tangosol.util.Filter) Session(com.tangosol.net.Session)

Aggregations

Coherence (com.tangosol.net.Coherence)1 Session (com.tangosol.net.Session)1 CoherenceLifecycleEvent (com.tangosol.net.events.CoherenceLifecycleEvent)1 NamedTopic (com.tangosol.net.topic.NamedTopic)1 Publisher (com.tangosol.net.topic.Publisher)1 Subscriber (com.tangosol.net.topic.Subscriber)1 Filter (com.tangosol.util.Filter)1 ValueExtractor (com.tangosol.util.ValueExtractor)1 ExtractorFactories (io.micronaut.coherence.ExtractorFactories)1 FilterFactories (io.micronaut.coherence.FilterFactories)1 io.micronaut.coherence.annotation (io.micronaut.coherence.annotation)1 ElementArgumentBinderRegistry (io.micronaut.coherence.messaging.binders.ElementArgumentBinderRegistry)1 CoherenceSubscriberException (io.micronaut.coherence.messaging.exceptions.CoherenceSubscriberException)1 ApplicationContext (io.micronaut.context.ApplicationContext)1 ExecutableMethodProcessor (io.micronaut.context.processor.ExecutableMethodProcessor)1 Blocking (io.micronaut.core.annotation.Blocking)1 Publishers (io.micronaut.core.async.publisher.Publishers)1 BoundExecutable (io.micronaut.core.bind.BoundExecutable)1 DefaultExecutableBinder (io.micronaut.core.bind.DefaultExecutableBinder)1 ExecutableBinder (io.micronaut.core.bind.ExecutableBinder)1