Search in sources :

Example 1 with PublisherBuilder

use of org.eclipse.microprofile.reactive.streams.operators.PublisherBuilder in project smallrye-reactive-messaging by smallrye.

the class KafkaSourceWithLegacyMetadataTest method testBroadcastWithPartitions.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testBroadcastWithPartitions() {
    companion.topics().create(topic, 2);
    MapBasedConfig config = newCommonConfigForSource().with("value.deserializer", IntegerDeserializer.class.getName()).with("broadcast", true).with("partitions", 2);
    connector = new KafkaConnector();
    connector.executionHolder = new ExecutionHolder(vertx);
    connector.configurations = UnsatisfiedInstance.instance();
    connector.consumerRebalanceListeners = UnsatisfiedInstance.instance();
    connector.kafkaCDIEvents = new CountKafkaCdiEvents();
    connector.init();
    PublisherBuilder<? extends KafkaRecord> builder = (PublisherBuilder<? extends KafkaRecord>) connector.getPublisherBuilder(config);
    List<KafkaRecord> messages1 = new ArrayList<>();
    List<KafkaRecord> messages2 = new ArrayList<>();
    builder.forEach(messages1::add).run();
    builder.forEach(messages2::add).run();
    companion.produceIntegers().usingGenerator(i -> new ProducerRecord<>(topic, i), 10);
    await().atMost(2, TimeUnit.MINUTES).until(() -> messages1.size() >= 10);
    await().atMost(2, TimeUnit.MINUTES).until(() -> messages2.size() >= 10);
    assertThat(messages1.stream().map(KafkaRecord::getPayload).collect(Collectors.toList())).containsExactlyInAnyOrder(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
    assertThat(messages2.stream().map(KafkaRecord::getPayload).collect(Collectors.toList())).containsExactlyInAnyOrder(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
}
Also used : IntegerDeserializer(org.apache.kafka.common.serialization.IntegerDeserializer) PublisherBuilder(org.eclipse.microprofile.reactive.streams.operators.PublisherBuilder) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) ExecutionHolder(io.smallrye.reactive.messaging.providers.connectors.ExecutionHolder) Test(org.junit.jupiter.api.Test)

Example 2 with PublisherBuilder

use of org.eclipse.microprofile.reactive.streams.operators.PublisherBuilder in project smallrye-reactive-messaging by smallrye.

the class KafkaSourceWithLegacyMetadataTest method testBroadcast.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testBroadcast() {
    MapBasedConfig config = newCommonConfigForSource().with("value.deserializer", IntegerDeserializer.class.getName()).with("broadcast", true);
    CountKafkaCdiEvents testEvents = new CountKafkaCdiEvents();
    connector = new KafkaConnector();
    connector.executionHolder = new ExecutionHolder(vertx);
    connector.configurations = UnsatisfiedInstance.instance();
    connector.consumerRebalanceListeners = UnsatisfiedInstance.instance();
    connector.kafkaCDIEvents = testEvents;
    connector.init();
    PublisherBuilder<? extends KafkaRecord> builder = (PublisherBuilder<? extends KafkaRecord>) connector.getPublisherBuilder(config);
    List<KafkaRecord> messages1 = new ArrayList<>();
    List<KafkaRecord> messages2 = new ArrayList<>();
    builder.forEach(messages1::add).run();
    builder.forEach(messages2::add).run();
    companion.produceIntegers().usingGenerator(i -> new ProducerRecord<>(topic, i), 10);
    await().atMost(2, TimeUnit.MINUTES).until(() -> messages1.size() >= 10);
    await().atMost(2, TimeUnit.MINUTES).until(() -> messages2.size() >= 10);
    assertThat(messages1.stream().map(KafkaRecord::getPayload).collect(Collectors.toList())).containsExactly(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
    assertThat(messages2.stream().map(KafkaRecord::getPayload).collect(Collectors.toList())).containsExactly(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
    assertThat(testEvents.firedConsumerEvents.sum()).isEqualTo(1);
    assertThat(testEvents.firedProducerEvents.sum()).isEqualTo(0);
}
Also used : PublisherBuilder(org.eclipse.microprofile.reactive.streams.operators.PublisherBuilder) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) ExecutionHolder(io.smallrye.reactive.messaging.providers.connectors.ExecutionHolder) Test(org.junit.jupiter.api.Test)

Example 3 with PublisherBuilder

use of org.eclipse.microprofile.reactive.streams.operators.PublisherBuilder in project smallrye-reactive-messaging by smallrye.

the class AmqpConnector method getPublisherBuilder.

@Override
public PublisherBuilder<? extends Message<?>> getPublisherBuilder(Config config) {
    AmqpConnectorIncomingConfiguration ic = new AmqpConnectorIncomingConfiguration(config);
    String address = ic.getAddress().orElseGet(ic::getChannel);
    opened.put(ic.getChannel(), false);
    boolean broadcast = ic.getBroadcast();
    boolean durable = ic.getDurable();
    boolean autoAck = ic.getAutoAcknowledgement();
    AmqpClient client = AmqpClientHelper.createClient(this, ic, clientOptions);
    String link = ic.getLinkName().orElseGet(ic::getChannel);
    ConnectionHolder holder = new ConnectionHolder(client, ic, getVertx());
    holders.put(ic.getChannel(), holder);
    AmqpFailureHandler onNack = createFailureHandler(ic);
    Multi<? extends Message<?>> multi = holder.getOrEstablishConnection().onItem().transformToUni(connection -> connection.createReceiver(address, new AmqpReceiverOptions().setAutoAcknowledgement(autoAck).setDurable(durable).setLinkName(link).setCapabilities(getClientCapabilities(ic)))).onItem().invoke(r -> opened.put(ic.getChannel(), true)).onItem().transformToMulti(r -> getStreamOfMessages(r, holder, address, ic.getChannel(), onNack, ic.getCloudEvents(), ic.getTracingEnabled()));
    Integer interval = ic.getReconnectInterval();
    Integer attempts = ic.getReconnectAttempts();
    multi = multi.onFailure().invoke(log::retrieveMessagesRetrying).onFailure().retry().withBackOff(ofSeconds(1), ofSeconds(interval)).atMost(attempts).onFailure().invoke(t -> {
        opened.put(ic.getChannel(), false);
        log.retrieveMessagesNoMoreRetrying(t);
    });
    if (broadcast) {
        multi = multi.broadcast().toAllSubscribers();
    }
    return ReactiveStreams.fromPublisher(multi);
}
Also used : AMQPLogging.log(io.smallrye.reactive.messaging.amqp.i18n.AMQPLogging.log) Arrays(java.util.Arrays) AmqpModifiedFailedAndUndeliverableHere(io.smallrye.reactive.messaging.amqp.fault.AmqpModifiedFailedAndUndeliverableHere) SemanticAttributes(io.opentelemetry.semconv.trace.attributes.SemanticAttributes) HealthReport(io.smallrye.reactive.messaging.health.HealthReport) INCOMING(io.smallrye.reactive.messaging.annotations.ConnectorAttribute.Direction.INCOMING) INCOMING_AND_OUTGOING(io.smallrye.reactive.messaging.annotations.ConnectorAttribute.Direction.INCOMING_AND_OUTGOING) Reception(javax.enterprise.event.Reception) AmqpFailStop(io.smallrye.reactive.messaging.amqp.fault.AmqpFailStop) Duration(java.time.Duration) Map(java.util.Map) Observes(javax.enterprise.event.Observes) Connector(org.eclipse.microprofile.reactive.messaging.spi.Connector) SpanBuilder(io.opentelemetry.api.trace.SpanBuilder) Any(javax.enterprise.inject.Any) Instance(javax.enterprise.inject.Instance) AmqpFailureHandler(io.smallrye.reactive.messaging.amqp.fault.AmqpFailureHandler) Context(io.opentelemetry.context.Context) ExecutionHolder(io.smallrye.reactive.messaging.providers.connectors.ExecutionHolder) ConnectorAttribute(io.smallrye.reactive.messaging.annotations.ConnectorAttribute) Span(io.opentelemetry.api.trace.Span) AmqpAccept(io.smallrye.reactive.messaging.amqp.fault.AmqpAccept) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SubscriberBuilder(org.eclipse.microprofile.reactive.streams.operators.SubscriberBuilder) Config(org.eclipse.microprofile.config.Config) Collectors(java.util.stream.Collectors) SpanKind(io.opentelemetry.api.trace.SpanKind) AmqpClient(io.vertx.mutiny.amqp.AmqpClient) Priority(javax.annotation.Priority) List(java.util.List) AmqpSenderOptions(io.vertx.amqp.AmqpSenderOptions) PostConstruct(javax.annotation.PostConstruct) ProtonSender(io.vertx.proton.ProtonSender) ApplicationScoped(javax.enterprise.context.ApplicationScoped) PublisherBuilder(org.eclipse.microprofile.reactive.streams.operators.PublisherBuilder) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) OutgoingConnectorFactory(org.eclipse.microprofile.reactive.messaging.spi.OutgoingConnectorFactory) BroadcastProcessor(io.smallrye.mutiny.operators.multi.processors.BroadcastProcessor) AmqpReceiverOptions(io.vertx.amqp.AmqpReceiverOptions) AmqpModifiedFailed(io.smallrye.reactive.messaging.amqp.fault.AmqpModifiedFailed) Duration.ofSeconds(java.time.Duration.ofSeconds) Tracer(io.opentelemetry.api.trace.Tracer) AtomicReference(java.util.concurrent.atomic.AtomicReference) HealthReporter(io.smallrye.reactive.messaging.health.HealthReporter) Multi(io.smallrye.mutiny.Multi) Uni(io.smallrye.mutiny.Uni) Inject(javax.inject.Inject) AmqpClientOptions(io.vertx.amqp.AmqpClientOptions) BeforeDestroyed(javax.enterprise.context.BeforeDestroyed) OUTGOING(io.smallrye.reactive.messaging.annotations.ConnectorAttribute.Direction.OUTGOING) IncomingConnectorFactory(org.eclipse.microprofile.reactive.messaging.spi.IncomingConnectorFactory) ReactiveStreams(org.eclipse.microprofile.reactive.streams.operators.ReactiveStreams) AMQPExceptions.ex(io.smallrye.reactive.messaging.amqp.i18n.AMQPExceptions.ex) TracingMetadata(io.smallrye.reactive.messaging.TracingMetadata) AmqpReceiver(io.vertx.mutiny.amqp.AmqpReceiver) GlobalOpenTelemetry(io.opentelemetry.api.GlobalOpenTelemetry) AmqpRelease(io.smallrye.reactive.messaging.amqp.fault.AmqpRelease) AmqpReject(io.smallrye.reactive.messaging.amqp.fault.AmqpReject) Message(org.eclipse.microprofile.reactive.messaging.Message) AmqpSender(io.vertx.mutiny.amqp.AmqpSender) Vertx(io.vertx.mutiny.core.Vertx) Collections(java.util.Collections) AmqpReceiverOptions(io.vertx.amqp.AmqpReceiverOptions) AmqpClient(io.vertx.mutiny.amqp.AmqpClient) AmqpFailureHandler(io.smallrye.reactive.messaging.amqp.fault.AmqpFailureHandler)

Example 4 with PublisherBuilder

use of org.eclipse.microprofile.reactive.streams.operators.PublisherBuilder in project smallrye-reactive-messaging by smallrye.

the class MqttSourceTest method testBroadcast.

@Test
public void testBroadcast() {
    String topic = UUID.randomUUID().toString();
    Map<String, Object> config = new HashMap<>();
    config.put("topic", topic);
    config.put("host", address);
    config.put("port", port);
    config.put("channel-name", topic);
    config.put("broadcast", true);
    MqttSource source = new MqttSource(vertx, new MqttConnectorIncomingConfiguration(new MapBasedConfig(config)));
    List<MqttMessage<?>> messages1 = new ArrayList<>();
    List<MqttMessage<?>> messages2 = new ArrayList<>();
    PublisherBuilder<MqttMessage<?>> stream = source.getSource();
    stream.forEach(messages1::add).run();
    stream.forEach(messages2::add).run();
    await().until(source::isReady);
    AtomicInteger counter = new AtomicInteger();
    new Thread(() -> usage.produceIntegers(topic, 10, null, counter::getAndIncrement)).start();
    await().atMost(2, TimeUnit.MINUTES).until(() -> messages1.size() >= 10);
    await().atMost(2, TimeUnit.MINUTES).until(() -> messages2.size() >= 10);
    assertThat(messages1.stream().map(Message::getPayload).map(x -> (byte[]) x).map(bytes -> Integer.valueOf(new String(bytes))).collect(Collectors.toList())).containsExactly(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
    assertThat(messages2.stream().map(Message::getPayload).map(x -> (byte[]) x).map(bytes -> Integer.valueOf(new String(bytes))).collect(Collectors.toList())).containsExactly(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
}
Also used : Awaitility.await(org.awaitility.Awaitility.await) java.util(java.util) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ConnectorLiteral(org.eclipse.microprofile.reactive.messaging.spi.ConnectorLiteral) WeldContainer(org.jboss.weld.environment.se.WeldContainer) Collectors(java.util.stream.Collectors) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Message(org.eclipse.microprofile.reactive.messaging.Message) AfterEach(org.junit.jupiter.api.AfterEach) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PublisherBuilder(org.eclipse.microprofile.reactive.streams.operators.PublisherBuilder) Weld(org.jboss.weld.environment.se.Weld) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) Test(org.junit.jupiter.api.Test)

Example 5 with PublisherBuilder

use of org.eclipse.microprofile.reactive.streams.operators.PublisherBuilder in project smallrye-reactive-messaging by smallrye.

the class MqttServerConnectorTest method test.

@Test
void test(@Any MqttServerConnector connector, VertxTestContext testContext) {
    final AtomicBoolean opened = new AtomicBoolean();
    final Map<String, String> configMap = new HashMap<>();
    configMap.put("port", "0");
    final List<TestMqttMessage> testMessages = new CopyOnWriteArrayList<>();
    testMessages.add(new TestMqttMessage("hello/topic", 1, "Hello world!", EXACTLY_ONCE.value(), false));
    testMessages.add(new TestMqttMessage("foo/bar", 2, "dkufhdspkjfosdjfs;", AT_LEAST_ONCE.value(), true));
    testMessages.add(new TestMqttMessage("foo/bar", -1, "Hello world!", AT_MOST_ONCE.value(), false));
    testMessages.add(new TestMqttMessage("sa/srt/tgvbc", 3, "Yeah", EXACTLY_ONCE.value(), true));
    final PublisherBuilder<MqttMessage> builder = (PublisherBuilder<MqttMessage>) connector.getPublisherBuilder(TestUtils.config(configMap));
    // The source is the same for every call
    assertEquals(builder, connector.getPublisherBuilder(TestUtils.config(configMap)));
    builder.buildRs().subscribe(createSubscriber(testContext, opened, testMessages));
    TestUtils.sendMqttMessages(testMessages, CompletableFuture.supplyAsync(() -> {
        await().until(opened::get);
        await().until(() -> connector.port() != 0);
        return connector.port();
    }), testContext);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PublisherBuilder(org.eclipse.microprofile.reactive.streams.operators.PublisherBuilder) HashMap(java.util.HashMap) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Aggregations

PublisherBuilder (org.eclipse.microprofile.reactive.streams.operators.PublisherBuilder)17 Test (org.junit.jupiter.api.Test)13 MapBasedConfig (io.smallrye.reactive.messaging.test.common.config.MapBasedConfig)10 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 Collectors (java.util.stream.Collectors)8 Awaitility.await (org.awaitility.Awaitility.await)8 Message (org.eclipse.microprofile.reactive.messaging.Message)8 ExecutionHolder (io.smallrye.reactive.messaging.providers.connectors.ExecutionHolder)6 java.util (java.util)5 TimeUnit (java.util.concurrent.TimeUnit)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 WeldContainer (org.jboss.weld.environment.se.WeldContainer)5 AfterEach (org.junit.jupiter.api.AfterEach)5 KafkaMapBasedConfig (io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig)4 Vertx (io.vertx.mutiny.core.Vertx)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)4