Search in sources :

Example 1 with DefaultChannelFactory

use of com.meltwater.rxrabbit.impl.DefaultChannelFactory in project rxrabbit by meltwater.

the class RxRabbitMultiNodeTest method setup.

@Before
public void setup() throws Exception {
    dockerContainers.resetAll(false);
    dockerContainers.rabbit(RABBIT_1).assertUp();
    dockerContainers.rabbit(RABBIT_2).assertUp();
    String rabbitTcpPort = dockerContainers.rabbit(RABBIT_1).tcpPort();
    rabbitAdminPort = dockerContainers.rabbit(RABBIT_1).adminPort();
    String rabbit2TcpPort = dockerContainers.rabbit(RABBIT_2).tcpPort();
    log.infoWithParams("****** Rabbit brokers are up and running *****");
    BrokerAddresses addresses = new BrokerAddresses("amqp://localhost:" + rabbitTcpPort + "," + "amqp://localhost:" + rabbit2TcpPort);
    channelFactory = new DefaultChannelFactory(addresses, connectionSettings);
    consumerFactory = new DefaultConsumerFactory(channelFactory, consumeSettings);
    DefaultPublisherFactory publisherFactory = new DefaultPublisherFactory(channelFactory, publishSettings);
    httpClient = new AsyncHttpClient();
    messagesSeen.clear();
    createQueues(channelFactory, inputQueue, new Exchange(inputExchange));
    publisher = publisherFactory.createPublisher();
}
Also used : DefaultChannelFactory(com.meltwater.rxrabbit.impl.DefaultChannelFactory) AsyncHttpClient(com.ning.http.client.AsyncHttpClient) Before(org.junit.Before)

Example 2 with DefaultChannelFactory

use of com.meltwater.rxrabbit.impl.DefaultChannelFactory 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)

Example 3 with DefaultChannelFactory

use of com.meltwater.rxrabbit.impl.DefaultChannelFactory in project rxrabbit by meltwater.

the class RxRabbitTests method setup.

@Before
public void setup() throws Exception {
    dockerContainers.rabbit().assertUp();
    rabbitTcpPort = dockerContainers.rabbit().tcpPort();
    rabbitAdminPort = dockerContainers.rabbit().adminPort();
    log.infoWithParams("****** Rabbit broker is up and running *****");
    BrokerAddresses addresses = new BrokerAddresses("amqp://localhost:" + rabbitTcpPort);
    channelFactory = new DefaultChannelFactory(addresses, connectionSettings);
    consumerFactory = new DefaultConsumerFactory(channelFactory, consumeSettings);
    publisherFactory = new DefaultPublisherFactory(channelFactory, publishSettings);
    httpClient = new AsyncHttpClient();
    messagesSeen.clear();
    createQueues(channelFactory, inputQueue, new Exchange(inputExchange));
    publisher = publisherFactory.createPublisher();
    RxJavaHooks.setOnIOScheduler(null);
}
Also used : DefaultChannelFactory(com.meltwater.rxrabbit.impl.DefaultChannelFactory) AsyncHttpClient(com.ning.http.client.AsyncHttpClient) Before(org.junit.Before)

Example 4 with DefaultChannelFactory

use of com.meltwater.rxrabbit.impl.DefaultChannelFactory in project rxrabbit by meltwater.

the class LoadGenerator method publishTestMessages.

public static void publishTestMessages(BrokerAddresses addresses, String outputExchange, long nrToPublish) throws IOException {
    ConnectionSettings connectionSettings = new ConnectionSettings();
    connectionSettings.withHeartbeatSecs(5);
    connectionSettings.withShutdownTimeoutMillis(10_000);
    PublisherSettings publisherSettings = new PublisherSettings();
    publisherSettings.withPublisherConfirms(true);
    publisherSettings.withBackoffAlgorithm(new FibonacciBackoffAlgorithm());
    publisherSettings.withRetryCount(10);
    ChannelFactory channelFactory = new DefaultChannelFactory(addresses, connectionSettings);
    PublisherFactory publisherFactory = new DefaultPublisherFactory(channelFactory, publisherSettings);
    final RabbitPublisher publish = publisherFactory.createPublisher();
    List<Long> ids = new ArrayList<>();
    for (long i = 1; i <= nrToPublish; i++) {
        ids.add(i);
    }
    log.infoWithParams("Publishing messages to exchange.", "numToPublish", nrToPublish, "exchange", outputExchange);
    from(ids).flatMap(id -> {
        AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties().builder();
        builder.messageId(String.valueOf(id));
        builder.deliveryMode(DeliveryMode.persistent.code);
        builder.appId("load-generator");
        String msgPayload = "Message nr " + id;
        return publish.call(new Exchange(outputExchange), new RoutingKey("routing.key"), builder.build(), new Payload(msgPayload.getBytes(Charsets.UTF_8))).toObservable();
    }).doOnError(throwable -> log.errorWithParams("Unexpected error when publishing.", throwable)).timeout(30, TimeUnit.SECONDS).toBlocking().last();
    log.infoWithParams("All messages sent to exchange.", "numSent", nrToPublish, "exchange", outputExchange);
    publish.close();
}
Also used : DefaultChannelFactory(com.meltwater.rxrabbit.impl.DefaultChannelFactory) RoutingKey(com.meltwater.rxrabbit.RoutingKey) DeliveryMode(com.meltwater.rxrabbit.DeliveryMode) Charsets(com.google.common.base.Charsets) Properties(java.util.Properties) Exchange(com.meltwater.rxrabbit.Exchange) PublisherFactory(com.meltwater.rxrabbit.PublisherFactory) Payload(com.meltwater.rxrabbit.Payload) IOException(java.io.IOException) Observable.from(rx.Observable.from) ConnectionSettings(com.meltwater.rxrabbit.ConnectionSettings) PublisherSettings(com.meltwater.rxrabbit.PublisherSettings) Logger(com.meltwater.rxrabbit.util.Logger) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) BrokerAddresses(com.meltwater.rxrabbit.BrokerAddresses) RabbitPublisher(com.meltwater.rxrabbit.RabbitPublisher) ChannelFactory(com.meltwater.rxrabbit.ChannelFactory) DefaultChannelFactory(com.meltwater.rxrabbit.impl.DefaultChannelFactory) DefaultPublisherFactory(com.meltwater.rxrabbit.DefaultPublisherFactory) RoutingKey(com.meltwater.rxrabbit.RoutingKey) FibonacciBackoffAlgorithm(com.meltwater.rxrabbit.util.FibonacciBackoffAlgorithm) AMQP(com.rabbitmq.client.AMQP) RabbitPublisher(com.meltwater.rxrabbit.RabbitPublisher) FibonacciBackoffAlgorithm(com.meltwater.rxrabbit.util.FibonacciBackoffAlgorithm) ArrayList(java.util.ArrayList) PublisherSettings(com.meltwater.rxrabbit.PublisherSettings) ChannelFactory(com.meltwater.rxrabbit.ChannelFactory) DefaultChannelFactory(com.meltwater.rxrabbit.impl.DefaultChannelFactory) PublisherFactory(com.meltwater.rxrabbit.PublisherFactory) DefaultPublisherFactory(com.meltwater.rxrabbit.DefaultPublisherFactory) Exchange(com.meltwater.rxrabbit.Exchange) AMQP(com.rabbitmq.client.AMQP) DefaultPublisherFactory(com.meltwater.rxrabbit.DefaultPublisherFactory) Payload(com.meltwater.rxrabbit.Payload) ConnectionSettings(com.meltwater.rxrabbit.ConnectionSettings)

Example 5 with DefaultChannelFactory

use of com.meltwater.rxrabbit.impl.DefaultChannelFactory in project rxrabbit by meltwater.

the class ExampleAppsTest method setup.

@Before
public void setup() throws Exception {
    killAndRestartRabbitDocker();
    prop = new Properties();
    prop.load(ExampleAppsTest.class.getResourceAsStream("/test.properties"));
    prop.putAll(System.getProperties());
    addresses = new BrokerAddresses(prop.getProperty("rabbit.broker.uris"));
    channelFactory = new DefaultChannelFactory(addresses, new ConnectionSettings());
    RabbitTestUtils.createQueues(channelFactory, prop.getProperty("in.queue"), new Exchange(prop.getProperty("in.exchange")));
    RabbitTestUtils.createQueues(channelFactory, prop.getProperty("out.queue"), new Exchange(prop.getProperty("out.exchange")));
}
Also used : DefaultChannelFactory(com.meltwater.rxrabbit.impl.DefaultChannelFactory) Exchange(com.meltwater.rxrabbit.Exchange) Properties(java.util.Properties) ConnectionSettings(com.meltwater.rxrabbit.ConnectionSettings) BrokerAddresses(com.meltwater.rxrabbit.BrokerAddresses) Before(org.junit.Before)

Aggregations

DefaultChannelFactory (com.meltwater.rxrabbit.impl.DefaultChannelFactory)5 BrokerAddresses (com.meltwater.rxrabbit.BrokerAddresses)3 ConnectionSettings (com.meltwater.rxrabbit.ConnectionSettings)3 Exchange (com.meltwater.rxrabbit.Exchange)3 Before (org.junit.Before)3 DefaultPublisherFactory (com.meltwater.rxrabbit.DefaultPublisherFactory)2 Payload (com.meltwater.rxrabbit.Payload)2 PublisherSettings (com.meltwater.rxrabbit.PublisherSettings)2 RabbitPublisher (com.meltwater.rxrabbit.RabbitPublisher)2 RoutingKey (com.meltwater.rxrabbit.RoutingKey)2 Logger (com.meltwater.rxrabbit.util.Logger)2 AsyncHttpClient (com.ning.http.client.AsyncHttpClient)2 AMQP (com.rabbitmq.client.AMQP)2 IOException (java.io.IOException)2 Properties (java.util.Properties)2 Charsets (com.google.common.base.Charsets)1 Lists (com.google.common.collect.Lists)1 AdminChannel (com.meltwater.rxrabbit.AdminChannel)1 ChannelFactory (com.meltwater.rxrabbit.ChannelFactory)1 ConsumerSettings (com.meltwater.rxrabbit.ConsumerSettings)1