Search in sources :

Example 1 with Subscriber

use of com.tangosol.net.topic.Subscriber in project coherence-spring by coherence-community.

the class NamedTopicConfigurationTest method shouldInjectPublisher.

@Test
public void shouldInjectPublisher() throws Exception {
    NamedTopicPublisherFieldsBean publisherBean = this.ctx.getBean(NamedTopicPublisherFieldsBean.class);
    NamedTopicSubscriberFieldsBean subscriberBean = this.ctx.getBean(NamedTopicSubscriberFieldsBean.class);
    Publisher<Integer> numbersPublisher = publisherBean.getIntegers();
    assertThat(numbersPublisher, is(notNullValue()));
    Publisher<Person> peoplePublisher = publisherBean.getPeople();
    assertThat(peoplePublisher, is(notNullValue()));
    Subscriber<Integer> numbersSubscriber = subscriberBean.getIntegers();
    assertThat(numbersSubscriber, is(notNullValue()));
    Subscriber<Person> peopleSubscriber = subscriberBean.getPeople();
    assertThat(peopleSubscriber, is(notNullValue()));
    Subscriber<String> peopleFirstNamesSubscriber = subscriberBean.getPeopleFirstNames();
    assertThat(peopleFirstNamesSubscriber, is(notNullValue()));
    Subscriber<Person> peopleFilteredSubscriber = subscriberBean.getPeopleFiltered();
    assertThat(peopleFilteredSubscriber, is(notNullValue()));
    CompletableFuture<Subscriber.Element<Integer>> receiveNumber = numbersSubscriber.receive();
    numbersPublisher.publish(19).join();
    Subscriber.Element<Integer> element = receiveNumber.get(1, TimeUnit.MINUTES);
    assertThat(element.getValue(), is(19));
    Person homer = new Person("Homer", "Simpson", LocalDate.now(), new PhoneNumber(1, "555-123-9999"));
    Person bart = new Person("Bart", "Simpson", LocalDate.now(), new PhoneNumber(1, "555-123-9999"));
    CompletableFuture<Subscriber.Element<Person>> receivePerson = peopleSubscriber.receive();
    CompletableFuture<Subscriber.Element<String>> receiveName = peopleFirstNamesSubscriber.receive();
    CompletableFuture<Subscriber.Element<Person>> receiveFiltered = peopleFilteredSubscriber.receive();
    peoplePublisher.publish(homer).join();
    Subscriber.Element<Person> personElement = receivePerson.get(1, TimeUnit.MINUTES);
    Subscriber.Element<String> nameElement = receiveName.get(1, TimeUnit.MINUTES);
    assertThat(personElement.getValue(), is(homer));
    assertThat(nameElement.getValue(), is(homer.getFirstName()));
    assertThat(receiveFiltered.isDone(), is(false));
    peoplePublisher.publish(bart).join();
    personElement = receiveFiltered.get(1, TimeUnit.MINUTES);
    assertThat(personElement.getValue(), is(bart));
}
Also used : Subscriber(com.tangosol.net.topic.Subscriber) PhoneNumber(data.PhoneNumber) Person(data.Person) Test(org.junit.jupiter.api.Test)

Example 2 with Subscriber

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

the class CoherenceSubscriberExceptionTest method testFourArgCauseCtor.

@Test
void testFourArgCauseCtor() {
    Subscriber<?> subscriber = mock(Subscriber.class);
    Subscriber.Element<?> element = mock(Subscriber.Element.class);
    Throwable cause = new RuntimeException();
    CoherenceSubscriberException exception = new CoherenceSubscriberException(cause, Integer.MAX_VALUE, subscriber, element);
    assertThat(exception.getMessage(), nullValue());
    assertThat(exception.getCause(), is(cause));
    assertThat(exception.getKafkaListener(), is(Integer.MAX_VALUE));
    assertThat(exception.getKafkaConsumer(), is(subscriber));
    Optional<Subscriber.Element<?>> optElement = exception.getElement();
    assertThat(optElement, notNullValue());
    assertThat(optElement.orElseThrow(AssertionFailedError::new), is(element));
}
Also used : CoherenceSubscriberException(io.micronaut.coherence.messaging.exceptions.CoherenceSubscriberException) Subscriber(com.tangosol.net.topic.Subscriber) Test(org.junit.jupiter.api.Test)

Example 3 with Subscriber

use of com.tangosol.net.topic.Subscriber 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 4 with Subscriber

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

the class NamedTopicFactories method getSubscriber.

@Bean(preDestroy = "close")
@Prototype
@Type(Subscriber.class)
@SuppressWarnings({ "unchecked", "rawtypes" })
<V> Subscriber<V> getSubscriber(InjectionPoint<?> injectionPoint) {
    AnnotationMetadata metadata = injectionPoint.getAnnotationMetadata();
    List<Subscriber.Option> options = new ArrayList<>();
    String groupName = metadata.getValue(SubscriberGroup.class, String.class).orElse(null);
    if (StringUtils.isNotEmpty(groupName)) {
        options.add(Subscriber.Name.of(groupName));
    }
    if (metadata.hasStereotype(FilterBinding.class)) {
        Filter filter = filterFactory.filter(injectionPoint);
        options.add(Subscriber.Filtered.by(filter));
    }
    if (metadata.hasStereotype(ExtractorBinding.class)) {
        ValueExtractor extractor = extractorFactory.extractor(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 : Filter(com.tangosol.util.Filter) Subscriber(com.tangosol.net.topic.Subscriber) ArrayList(java.util.ArrayList) ValueExtractor(com.tangosol.util.ValueExtractor) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) Type(io.micronaut.context.annotation.Type) Prototype(io.micronaut.context.annotation.Prototype) Bean(io.micronaut.context.annotation.Bean)

Example 5 with Subscriber

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

the class NamedTopicFactoriesTest method shouldInjectPublisher.

@Test
public void shouldInjectPublisher() throws Exception {
    NamedTopicPublisherFieldsBean publisherBean = ctx.getBean(NamedTopicPublisherFieldsBean.class);
    NamedTopicSubscriberFieldsBean subscriberBean = ctx.getBean(NamedTopicSubscriberFieldsBean.class);
    Publisher<Integer> numbersPublisher = publisherBean.getNumbers();
    assertThat(numbersPublisher, is(notNullValue()));
    Publisher<Person> peoplePublisher = publisherBean.getPeople();
    assertThat(peoplePublisher, is(notNullValue()));
    Subscriber<Integer> numbersSubscriber = subscriberBean.getNumbers();
    assertThat(numbersSubscriber, is(notNullValue()));
    Subscriber<Person> peopleSubscriber = subscriberBean.getPeople();
    assertThat(peopleSubscriber, is(notNullValue()));
    Subscriber<String> peopleFirstNamesSubscriber = subscriberBean.getPeopleFirstNames();
    assertThat(peopleFirstNamesSubscriber, is(notNullValue()));
    Subscriber<Person> peopleFilteredSubscriber = subscriberBean.getPeopleFiltered();
    assertThat(peopleFilteredSubscriber, is(notNullValue()));
    CompletableFuture<Subscriber.Element<Integer>> receiveNumber = numbersSubscriber.receive();
    numbersPublisher.send(19).join();
    Subscriber.Element<Integer> element = receiveNumber.get(1, TimeUnit.MINUTES);
    assertThat(element.getValue(), is(19));
    Person homer = new Person("Homer", "Simpson", LocalDate.now(), new PhoneNumber(1, "555-123-9999"));
    Person bart = new Person("Bart", "Simpson", LocalDate.now(), new PhoneNumber(1, "555-123-9999"));
    CompletableFuture<Subscriber.Element<Person>> receivePerson = peopleSubscriber.receive();
    CompletableFuture<Subscriber.Element<String>> receiveName = peopleFirstNamesSubscriber.receive();
    CompletableFuture<Subscriber.Element<Person>> receiveFiltered = peopleFilteredSubscriber.receive();
    peoplePublisher.send(homer).join();
    Subscriber.Element<Person> personElement = receivePerson.get(1, TimeUnit.MINUTES);
    Subscriber.Element<String> nameElement = receiveName.get(1, TimeUnit.MINUTES);
    assertThat(personElement.getValue(), is(homer));
    assertThat(nameElement.getValue(), is(homer.getFirstName()));
    assertThat(receiveFiltered.isDone(), is(false));
    peoplePublisher.send(bart).join();
    personElement = receiveFiltered.get(1, TimeUnit.MINUTES);
    assertThat(personElement.getValue(), is(bart));
}
Also used : Subscriber(com.tangosol.net.topic.Subscriber) PhoneNumber(data.PhoneNumber) Person(data.Person) Test(org.junit.jupiter.api.Test) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest)

Aggregations

Subscriber (com.tangosol.net.topic.Subscriber)8 Test (org.junit.jupiter.api.Test)5 CoherenceSubscriberException (io.micronaut.coherence.messaging.exceptions.CoherenceSubscriberException)4 Filter (com.tangosol.util.Filter)3 ValueExtractor (com.tangosol.util.ValueExtractor)3 Person (data.Person)2 PhoneNumber (data.PhoneNumber)2 ArrayList (java.util.ArrayList)2 ExtractorBinding (com.oracle.coherence.spring.annotation.ExtractorBinding)1 FilterBinding (com.oracle.coherence.spring.annotation.FilterBinding)1 SubscriberGroup (com.oracle.coherence.spring.annotation.SubscriberGroup)1 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 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