Search in sources :

Example 1 with SessionName

use of io.micronaut.coherence.annotation.SessionName in project micronaut-coherence by micronaut-projects.

the class EventObserverSupportTest method testCreateObserverForSessionLifecycleEvent.

@Test
void testCreateObserverForSessionLifecycleEvent() {
    ExecutableMethodEventObserver observer = mock(ExecutableMethodEventObserver.class);
    @Starting
    @Started
    @Stopping
    @Stopped
    @Name("test")
    @SessionName("test")
    final class c {
    }
    Starting starting = c.class.getAnnotation(Starting.class);
    Started started = c.class.getAnnotation(Started.class);
    Stopping stopping = c.class.getAnnotation(Stopping.class);
    Stopped stopped = c.class.getAnnotation(Stopped.class);
    Name name = c.class.getAnnotation(Name.class);
    SessionName sessionName = c.class.getAnnotation(SessionName.class);
    Set<java.lang.annotation.Annotation> value = new java.util.HashSet<>();
    value.add(starting);
    value.add(started);
    value.add(stopping);
    value.add(stopped);
    value.add(name);
    value.add(sessionName);
    when(observer.getObservedQualifiers()).thenReturn(value);
    EventObserverSupport.EventHandler handler = EventObserverSupport.createObserver(SessionLifecycleEvent.class, observer);
    EnumSet<?> actual = handler.eventTypes();
    EnumSet<?> expected = EnumSet.allOf(SessionLifecycleEvent.Type.class);
    assertThat(actual, is(expected));
    assertThat(((EventObserverSupport.SessionLifecycleEventHandler) handler).name, is("test"));
}
Also used : Started(io.micronaut.coherence.annotation.Started) Stopped(io.micronaut.coherence.annotation.Stopped) SessionLifecycleEvent(com.tangosol.net.events.SessionLifecycleEvent) Stopping(io.micronaut.coherence.annotation.Stopping) SessionName(io.micronaut.coherence.annotation.SessionName) SessionName(io.micronaut.coherence.annotation.SessionName) Name(io.micronaut.coherence.annotation.Name) Starting(io.micronaut.coherence.annotation.Starting) Test(org.junit.jupiter.api.Test)

Example 2 with SessionName

use of io.micronaut.coherence.annotation.SessionName in project micronaut-coherence by micronaut-projects.

the class NamedCacheFactories method getCacheInternal.

private <K, V> NamedCache<K, V> getCacheInternal(InjectionPoint<?> injectionPoint, boolean isCQC) {
    AnnotationMetadata metadata = injectionPoint.getAnnotationMetadata();
    String sessionName = metadata.getValue(SessionName.class, String.class).orElse(Coherence.DEFAULT_NAME);
    String name = metadata.getValue(Name.class, String.class).orElse(getName(injectionPoint));
    isCQC = isCQC || injectionPoint.isAnnotationPresent(View.class);
    if (StringUtils.isEmpty(name)) {
        throw new IllegalArgumentException("Cannot determine cache/map name. No @Name qualifier and injection point is not named");
    }
    Session session = beanContext.createBean(Session.class, sessionName);
    NamedCache<K, V> cache = session.getCache(name);
    if (isCQC || metadata.hasAnnotation(View.class)) {
        boolean hasValues = metadata.booleanValue(View.class, "cacheValues").orElse(true);
        Filter filter = filterFactory.filter(injectionPoint);
        ValueExtractor extractor = getExtractor(injectionPoint);
        ViewId id = new ViewId(name, sessionName, filter, hasValues, extractor);
        WeakReference<ContinuousQueryCache> refCQC = views.compute(id, (key, ref) -> {
            ContinuousQueryCache cqc = ref == null ? null : ref.get();
            if (cqc == null || !cqc.isActive()) {
                cqc = new ContinuousQueryCache<>(cache, filter, hasValues, null, extractor);
                return new WeakReference<>(cqc);
            } else {
                return ref;
            }
        });
        return refCQC.get();
    } else {
        return cache;
    }
}
Also used : ValueExtractor(com.tangosol.util.ValueExtractor) View(io.micronaut.coherence.annotation.View) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) SessionName(io.micronaut.coherence.annotation.SessionName) Name(io.micronaut.coherence.annotation.Name) SessionName(io.micronaut.coherence.annotation.SessionName) Filter(com.tangosol.util.Filter) WeakReference(java.lang.ref.WeakReference) ContinuousQueryCache(com.tangosol.net.cache.ContinuousQueryCache)

Example 3 with SessionName

use of io.micronaut.coherence.annotation.SessionName in project micronaut-coherence by micronaut-projects.

the class CoherencePublisherIntroductionAdvice method intercept.

@Override
public Object intercept(MethodInvocationContext<Object, Object> context) {
    if (context.hasAnnotation(CoherencePublisher.class)) {
        // Make sure the CoherencePublisher annotation is present
        context.findAnnotation(CoherencePublisher.class).orElseThrow(() -> new IllegalStateException("No @CoherencePublisher annotation present on method: " + context));
        String topicName = Utils.getFirstTopicName(context).orElse(null);
        String sessionName = context.stringValue(SessionName.class).orElse(Coherence.DEFAULT_NAME);
        Duration maxBlock = context.getValue(CoherencePublisher.class, "maxBlock", Duration.class).orElse(null);
        Argument<?> bodyArgument = null;
        Argument<?>[] arguments = context.getArguments();
        Object[] parameterValues = context.getParameterValues();
        int valueIndex = -1;
        for (int i = 0; i < arguments.length; i++) {
            Argument<?> argument = arguments[i];
            if (argument.isAnnotationPresent(MessageBody.class)) {
                bodyArgument = argument;
                valueIndex = i;
            } else if (argument.isAnnotationPresent(Topics.class) || argument.isAnnotationPresent(Topic.class)) {
                Object o = parameterValues[i];
                if (o != null) {
                    topicName = o.toString();
                }
            }
        }
        if (StringUtils.isEmpty(topicName)) {
            throw new MessagingClientException("No topic specified for method: " + context);
        }
        if (bodyArgument == null) {
            for (int i = 0; i < arguments.length; i++) {
                Argument<?> argument = arguments[i];
                if (!argument.getAnnotationMetadata().hasStereotype(Bindable.class)) {
                    bodyArgument = argument;
                    valueIndex = i;
                    break;
                }
            }
        }
        if (bodyArgument == null) {
            throw new MessagingClientException("No valid message body argument found for method: " + context);
        }
        Object value = parameterValues[valueIndex];
        ReturnType<Object> returnType = context.getReturnType();
        Class<?> javaReturnType = returnType.getType();
        Publisher<Object> publisher = getPublisher(topicName, sessionName);
        boolean isReactiveReturnType = Publishers.isConvertibleToPublisher(javaReturnType);
        boolean isReactiveValue = value != null && Publishers.isConvertibleToPublisher(value.getClass());
        InterceptedMethod interceptedMethod = InterceptedMethod.of(context);
        if (isReactiveReturnType) {
            // return type is a reactive type
            Flux<?> flux = buildSendFlux(context, publisher, Argument.OBJECT_ARGUMENT, maxBlock, value);
            return Publishers.convertPublisher(flux, javaReturnType);
        } else {
            // return type is a future - must be future of Void
            Argument<?> returnArg = returnType.getFirstTypeVariable().orElse(Argument.of(Void.class));
            if (returnArg.getType() != Void.class) {
                throw new MessagingClientException("Generic return type for method must be Void, i.e. CompletableFuture<Void> - " + context);
            }
            CompletableFuture<Void> completableFuture = new CompletableFuture<>();
            if (isReactiveValue) {
                // return type is a future and value is reactive
                Flux<?> sendFlux = buildSendFlux(context, publisher, returnArg, maxBlock, value);
                if (!Publishers.isSingle(value.getClass())) {
                    sendFlux = sendFlux.collectList().flux();
                }
                // noinspection ReactiveStreamsSubscriberImplementation
                sendFlux.subscribe(new Subscriber<Object>() {

                    @Override
                    public void onSubscribe(Subscription s) {
                        s.request(1);
                    }

                    @Override
                    public void onNext(Object o) {
                    }

                    @Override
                    public void onError(Throwable t) {
                        completableFuture.completeExceptionally(wrapException(context, t));
                    }

                    @Override
                    public void onComplete() {
                        completableFuture.complete(null);
                    }
                });
            } else {
                // return type is a future and value is single message
                publisher.publish(value).handle((status, exception) -> {
                    if (exception != null) {
                        completableFuture.completeExceptionally(wrapException(context, exception));
                    } else {
                        completableFuture.complete(null);
                    }
                    return null;
                });
            }
            return interceptedMethod.handleResult(completableFuture);
        }
    } else {
        // can't be implemented so proceed
        return context.proceed();
    }
}
Also used : Argument(io.micronaut.core.type.Argument) CompletableFuture(java.util.concurrent.CompletableFuture) MessagingClientException(io.micronaut.messaging.exceptions.MessagingClientException) Subscription(org.reactivestreams.Subscription) CoherencePublisher(io.micronaut.coherence.annotation.CoherencePublisher) InterceptedMethod(io.micronaut.aop.InterceptedMethod) Bindable(io.micronaut.core.bind.annotation.Bindable) Duration(java.time.Duration) SessionName(io.micronaut.coherence.annotation.SessionName)

Aggregations

SessionName (io.micronaut.coherence.annotation.SessionName)3 Name (io.micronaut.coherence.annotation.Name)2 ContinuousQueryCache (com.tangosol.net.cache.ContinuousQueryCache)1 SessionLifecycleEvent (com.tangosol.net.events.SessionLifecycleEvent)1 Filter (com.tangosol.util.Filter)1 ValueExtractor (com.tangosol.util.ValueExtractor)1 InterceptedMethod (io.micronaut.aop.InterceptedMethod)1 CoherencePublisher (io.micronaut.coherence.annotation.CoherencePublisher)1 Started (io.micronaut.coherence.annotation.Started)1 Starting (io.micronaut.coherence.annotation.Starting)1 Stopped (io.micronaut.coherence.annotation.Stopped)1 Stopping (io.micronaut.coherence.annotation.Stopping)1 View (io.micronaut.coherence.annotation.View)1 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)1 Bindable (io.micronaut.core.bind.annotation.Bindable)1 Argument (io.micronaut.core.type.Argument)1 MessagingClientException (io.micronaut.messaging.exceptions.MessagingClientException)1 WeakReference (java.lang.ref.WeakReference)1 Duration (java.time.Duration)1 CompletableFuture (java.util.concurrent.CompletableFuture)1