Search in sources :

Example 1 with PublisherFactory

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

Aggregations

Charsets (com.google.common.base.Charsets)1 BrokerAddresses (com.meltwater.rxrabbit.BrokerAddresses)1 ChannelFactory (com.meltwater.rxrabbit.ChannelFactory)1 ConnectionSettings (com.meltwater.rxrabbit.ConnectionSettings)1 DefaultPublisherFactory (com.meltwater.rxrabbit.DefaultPublisherFactory)1 DeliveryMode (com.meltwater.rxrabbit.DeliveryMode)1 Exchange (com.meltwater.rxrabbit.Exchange)1 Payload (com.meltwater.rxrabbit.Payload)1 PublisherFactory (com.meltwater.rxrabbit.PublisherFactory)1 PublisherSettings (com.meltwater.rxrabbit.PublisherSettings)1 RabbitPublisher (com.meltwater.rxrabbit.RabbitPublisher)1 RoutingKey (com.meltwater.rxrabbit.RoutingKey)1 DefaultChannelFactory (com.meltwater.rxrabbit.impl.DefaultChannelFactory)1 FibonacciBackoffAlgorithm (com.meltwater.rxrabbit.util.FibonacciBackoffAlgorithm)1 Logger (com.meltwater.rxrabbit.util.Logger)1 AMQP (com.rabbitmq.client.AMQP)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Properties (java.util.Properties)1