use of com.meltwater.rxrabbit.ChannelFactory 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();
}
Aggregations