use of com.tangosol.net.topic.Publisher in project coherence-spring by coherence-community.
the class CoherenceTopicListenerTest method shouldReceiveMessagesForMultipleSubscribersInSameGroup.
@Test
void shouldReceiveMessagesForMultipleSubscribersInSameGroup() {
AtomicInteger count = new AtomicInteger();
try (Publisher<String> publisher = getPublisher("Nineteen", Publisher.OrderByValue.value((v) -> count.getAndIncrement()))) {
for (int i = 0; i < publisher.getChannelCount() * 2; i++) {
publisher.publish("message-" + i);
}
int expected = count.get();
Eventually.assertDeferred(() -> this.listenerFive.received(), is(expected));
TreeSet<String> set = new TreeSet<>(this.listenerFive.listOne);
set.addAll(this.listenerFive.listTwo);
assertThat(set.size(), is(expected));
}
}
use of com.tangosol.net.topic.Publisher 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;
}
use of com.tangosol.net.topic.Publisher in project micronaut-coherence by micronaut-projects.
the class CoherenceTopicListenerTest method shouldReceiveElementBinderArguments.
@Test
public void shouldReceiveElementBinderArguments() throws Exception {
int channel = 1;
try (Publisher<String> publisher = getPublisher("Eighteen", Publisher.OrderBy.id(channel))) {
String message = "ABC";
Publisher.Status status = publisher.publish(message).get(1, TimeUnit.MINUTES);
listenerFour.latch.await(1, TimeUnit.MINUTES);
assertThat(listenerFour.lastElementOne, is(notNullValue()));
assertThat(listenerFour.lastElementOne.getChannel(), is(status.getChannel()));
assertThat(listenerFour.lastElementOne.getPosition(), is(status.getPosition()));
assertThat(listenerFour.lastElementOne.getValue(), is(message));
assertThat(listenerFour.lastElementTwo, is(notNullValue()));
assertThat(listenerFour.lastElementTwo.getChannel(), is(status.getChannel()));
assertThat(listenerFour.lastElementTwo.getPosition(), is(status.getPosition()));
assertThat(listenerFour.lastElementTwo.getValue(), is(message));
assertThat(listenerFour.lastSubscriberTwo, is(notNullValue()));
assertThat(listenerFour.lastValueThree, is(message));
assertThat(listenerFour.lastChannelFour, is(status.getChannel()));
assertThat(listenerFour.lastValueFour, is(message));
assertThat(listenerFour.lastPositionFive, is(status.getPosition()));
assertThat(listenerFour.lastValueFive, is(message));
assertThat(listenerFour.lastBinary, is(notNullValue()));
Serializer serializer = publisher.getNamedTopic().getService().getSerializer();
assertThat(ExternalizableHelper.fromBinary(listenerFour.lastBinary, serializer), is(message));
}
}
use of com.tangosol.net.topic.Publisher in project micronaut-coherence by micronaut-projects.
the class CoherenceTopicListenerTest method shouldReceiveMessagesForMultipleSubscribersInSameGroup.
@Test
public void shouldReceiveMessagesForMultipleSubscribersInSameGroup() {
AtomicInteger count = new AtomicInteger();
try (Publisher<String> publisher = getPublisher("Nineteen", Publisher.OrderByValue.value(v -> count.getAndIncrement()))) {
for (int i = 0; i < publisher.getChannelCount() * 2; i++) {
publisher.publish("message-" + i);
}
int expected = count.get();
Eventually.assertDeferred(() -> listenerFive.received(), is(expected));
TreeSet<String> set = new TreeSet<>(listenerFive.listOne);
set.addAll(listenerFive.listTwo);
assertThat(set.size(), is(expected));
}
}
use of com.tangosol.net.topic.Publisher 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;
}
Aggregations