Search in sources :

Example 1 with ConsumerSettings

use of com.meltwater.rxrabbit.ConsumerSettings in project rxrabbit by meltwater.

the class ExampleAppsTest method consumeDocumentsAndGetIds.

private Set<String> consumeDocumentsAndGetIds(int nrToPublish) throws InterruptedException {
    final SortedSet<String> out = Collections.synchronizedSortedSet(new TreeSet<String>());
    ConsumerFactory consumerFactory = new DefaultConsumerFactory(channelFactory, new ConsumerSettings());
    consumerFactory.createConsumer(prop.getProperty("out.queue")).doOnNext(message -> message.acknowledger.ack()).doOnNext(message -> out.add(message.basicProperties.getMessageId())).take(nrToPublish).timeout(100, TimeUnit.SECONDS).toBlocking().last();
    return out;
}
Also used : ConsumerSettings(com.meltwater.rxrabbit.ConsumerSettings) ConsumerFactory(com.meltwater.rxrabbit.ConsumerFactory) AfterClass(org.junit.AfterClass) Properties(java.util.Properties) Exchange(com.meltwater.rxrabbit.Exchange) SortedSet(java.util.SortedSet) ConsumerSettings(com.meltwater.rxrabbit.ConsumerSettings) DockerContainers(com.meltwater.rxrabbit.docker.DockerContainers) Set(java.util.Set) Test(org.junit.Test) IOException(java.io.IOException) ConnectionSettings(com.meltwater.rxrabbit.ConnectionSettings) TreeSet(java.util.TreeSet) Logger(com.meltwater.rxrabbit.util.Logger) TimeUnit(java.util.concurrent.TimeUnit) Assert.assertThat(org.junit.Assert.assertThat) DefaultConsumerFactory(com.meltwater.rxrabbit.DefaultConsumerFactory) BrokerAddresses(com.meltwater.rxrabbit.BrokerAddresses) DefaultChannelFactory(com.meltwater.rxrabbit.impl.DefaultChannelFactory) After(org.junit.After) Is.is(org.hamcrest.core.Is.is) RabbitTestUtils(com.meltwater.rxrabbit.RabbitTestUtils) Collections(java.util.Collections) Before(org.junit.Before) ConsumerFactory(com.meltwater.rxrabbit.ConsumerFactory) DefaultConsumerFactory(com.meltwater.rxrabbit.DefaultConsumerFactory) DefaultConsumerFactory(com.meltwater.rxrabbit.DefaultConsumerFactory)

Example 2 with ConsumerSettings

use of com.meltwater.rxrabbit.ConsumerSettings in project rxrabbit by meltwater.

the class ExampleCode method publishAndConsume.

public int publishAndConsume(int numMessages, long maxWaitMillis, String brokerHost, int brokerPort, String queue, String exchange) throws IOException, InterruptedException {
    // ---------- Set Up ----------
    Logger logger = new Logger(ExampleCode.class);
    final ConnectionSettings connectionSettings = new ConnectionSettings();
    BrokerAddresses brokers = new BrokerAddresses(Lists.newArrayList(new BrokerAddresses.BrokerAddressBuilder().withHost(brokerHost).withPort(brokerPort).build()));
    DefaultChannelFactory channelFactory = new DefaultChannelFactory(brokers, connectionSettings);
    // Create queue, exchange and bind together
    AdminChannel adminChannel = channelFactory.createAdminChannel();
    adminChannel.exchangeDeclare(exchange, "topic", true, false, false, new HashMap<>());
    adminChannel.queueDeclare(queue, true, false, false, new HashMap<>());
    adminChannel.queueBind(queue, exchange, "#", new HashMap<>());
    adminChannel.close();
    // ---------- Publish messages ----------
    final PublisherSettings publisherSettings = new PublisherSettings().withNumChannels(2).withPublisherConfirms(true).withRetryCount(3);
    RabbitPublisher publisher = new DefaultPublisherFactory(channelFactory, publisherSettings).createPublisher();
    Observable.range(0, numMessages).map(String::valueOf).flatMap((input) -> publisher.call(new Exchange(exchange), new RoutingKey("#"), new AMQP.BasicProperties(), new Payload(input.getBytes())).toObservable()).doOnError((e) -> logger.errorWithParams("Failed to publish message", e)).subscribe();
    // ---------- Consume messages ----------
    ConsumerSettings consumerSettings = new ConsumerSettings().withRetryCount(ConsumerSettings.RETRY_FOREVER).withNumChannels(1).withPreFetchCount(1024);
    Observable<Message> consumer = new DefaultConsumerFactory(channelFactory, consumerSettings).createConsumer(queue);
    final AtomicInteger consumedMessages = new AtomicInteger(0);
    // save the Subscription so you can stop consuming later
    Subscription consumeSubscription = consumer.doOnNext(message -> consumedMessages.incrementAndGet()).doOnNext(message -> message.acknowledger.ack()).subscribe();
    try {
        long startTime = System.currentTimeMillis();
        while (consumedMessages.get() < numMessages) {
            Thread.sleep(10);
            if ((System.currentTimeMillis() - startTime) > maxWaitMillis) {
                throw new RuntimeException("Did not receive all '" + numMessages + "' messages within '" + maxWaitMillis + "' millis.");
            }
        }
    } finally {
        // unsubscribe (closes consume channel, and the consume connection if there are no other consumers)
        consumeSubscription.unsubscribe();
        // closes publish channel, and the publish connection of there are no other publishers
        publisher.close();
    }
    return consumedMessages.get();
}
Also used : DefaultChannelFactory(com.meltwater.rxrabbit.impl.DefaultChannelFactory) RoutingKey(com.meltwater.rxrabbit.RoutingKey) Message(com.meltwater.rxrabbit.Message) Exchange(com.meltwater.rxrabbit.Exchange) ConsumerSettings(com.meltwater.rxrabbit.ConsumerSettings) Payload(com.meltwater.rxrabbit.Payload) IOException(java.io.IOException) HashMap(java.util.HashMap) ConnectionSettings(com.meltwater.rxrabbit.ConnectionSettings) PublisherSettings(com.meltwater.rxrabbit.PublisherSettings) Logger(com.meltwater.rxrabbit.util.Logger) Observable(rx.Observable) DefaultConsumerFactory(com.meltwater.rxrabbit.DefaultConsumerFactory) Lists(com.google.common.collect.Lists) BrokerAddresses(com.meltwater.rxrabbit.BrokerAddresses) RabbitPublisher(com.meltwater.rxrabbit.RabbitPublisher) DefaultChannelFactory(com.meltwater.rxrabbit.impl.DefaultChannelFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DefaultPublisherFactory(com.meltwater.rxrabbit.DefaultPublisherFactory) RoutingKey(com.meltwater.rxrabbit.RoutingKey) Subscription(rx.Subscription) AdminChannel(com.meltwater.rxrabbit.AdminChannel) AMQP(com.rabbitmq.client.AMQP) ConsumerSettings(com.meltwater.rxrabbit.ConsumerSettings) Message(com.meltwater.rxrabbit.Message) RabbitPublisher(com.meltwater.rxrabbit.RabbitPublisher) Logger(com.meltwater.rxrabbit.util.Logger) PublisherSettings(com.meltwater.rxrabbit.PublisherSettings) DefaultConsumerFactory(com.meltwater.rxrabbit.DefaultConsumerFactory) BrokerAddresses(com.meltwater.rxrabbit.BrokerAddresses) Exchange(com.meltwater.rxrabbit.Exchange) AMQP(com.rabbitmq.client.AMQP) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DefaultPublisherFactory(com.meltwater.rxrabbit.DefaultPublisherFactory) AdminChannel(com.meltwater.rxrabbit.AdminChannel) Payload(com.meltwater.rxrabbit.Payload) ConnectionSettings(com.meltwater.rxrabbit.ConnectionSettings)

Aggregations

BrokerAddresses (com.meltwater.rxrabbit.BrokerAddresses)2 ConnectionSettings (com.meltwater.rxrabbit.ConnectionSettings)2 ConsumerSettings (com.meltwater.rxrabbit.ConsumerSettings)2 DefaultConsumerFactory (com.meltwater.rxrabbit.DefaultConsumerFactory)2 Exchange (com.meltwater.rxrabbit.Exchange)2 DefaultChannelFactory (com.meltwater.rxrabbit.impl.DefaultChannelFactory)2 Logger (com.meltwater.rxrabbit.util.Logger)2 IOException (java.io.IOException)2 Lists (com.google.common.collect.Lists)1 AdminChannel (com.meltwater.rxrabbit.AdminChannel)1 ConsumerFactory (com.meltwater.rxrabbit.ConsumerFactory)1 DefaultPublisherFactory (com.meltwater.rxrabbit.DefaultPublisherFactory)1 Message (com.meltwater.rxrabbit.Message)1 Payload (com.meltwater.rxrabbit.Payload)1 PublisherSettings (com.meltwater.rxrabbit.PublisherSettings)1 RabbitPublisher (com.meltwater.rxrabbit.RabbitPublisher)1 RabbitTestUtils (com.meltwater.rxrabbit.RabbitTestUtils)1 RoutingKey (com.meltwater.rxrabbit.RoutingKey)1 DockerContainers (com.meltwater.rxrabbit.docker.DockerContainers)1 AMQP (com.rabbitmq.client.AMQP)1