use of com.meltwater.rxrabbit.BrokerAddresses in project rxrabbit by meltwater.
the class ExampleAppsTest method can_consume_and_publish.
@Test
public void can_consume_and_publish() throws InterruptedException, IOException {
BrokerAddresses broker = new BrokerAddresses(prop.getProperty("rabbit.broker.uris"));
final ExampleAppShovel exampleApp = new ExampleAppShovel(prop.getProperty("in.queue"), new Exchange(prop.getProperty("out.exchange")), new ConnectionSettings(), broker);
try {
exampleApp.start();
int nrToPublish = 5_000;
LoadGenerator.publishTestMessages(broker, prop.getProperty("in.exchange"), nrToPublish);
Set<String> idSet = consumeDocumentsAndGetIds(nrToPublish);
assertThat(idSet.size(), is(nrToPublish));
} finally {
exampleApp.stop();
}
}
use of com.meltwater.rxrabbit.BrokerAddresses 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();
}
use of com.meltwater.rxrabbit.BrokerAddresses in project rxrabbit by meltwater.
the class ExampleAppShovel method main.
public static void main(String[] args) throws IOException {
Properties prop = new Properties();
prop.load(ExampleAppShovel.class.getResourceAsStream("/example_app_shovel.properties"));
prop.putAll(System.getProperties());
// Create and start the app
final ExampleAppShovel exampleAppShovel = new ExampleAppShovel(prop.getProperty("rabbit.input.queue"), new Exchange(prop.getProperty("rabbit.output.exchange")), new ConnectionSettings(), new BrokerAddresses(prop.getProperty("rabbit.broker.uris")));
exampleAppShovel.start();
// On shutdown call stop
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
log.infoWithParams("Closing app ...");
exampleAppShovel.stop();
}
});
// Wait for Ctrl+C
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
break;
}
}
}
use of com.meltwater.rxrabbit.BrokerAddresses in project rxrabbit by meltwater.
the class LoadGenerator method main.
public static void main(String[] args) throws IOException {
Properties prop = new Properties();
prop.load(LoadGenerator.class.getResourceAsStream("/load_generator.properties"));
prop.putAll(System.getProperties());
publishTestMessages(new BrokerAddresses(prop.getProperty("rabbit.broker.uris")), prop.getProperty("publish.to.exchange"), Long.parseLong(prop.getProperty("publish.message.count")));
}
use of com.meltwater.rxrabbit.BrokerAddresses 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