Course Setup
Use Case
Messaging Basics
Consumer Subscription Types
API Internals for Message Integrity and Scaling
1 of 3

Hands-on Exercise: Publish Messages Asynchronously

Complete the following for your Hands-on Exercise:

  • Follow the tutorial, creating OrderProducer.java to write messages asynchronously to orderBackLogChina.
  • Add an if statement to your code.  If the message string contains “China”, publish the message to topic orderBackLogChina.  All other messages should be published to topic orderBackLogUS.
String topic = null;
if (message.getValue().contains("China")) {
     topic = "persistent://student31/developer/orderBackLogChina";
} else {
     topic = "persistent://student31/developer/orderBackLogUS";
}

Note: DynamicProducerFactory.java will write to the console each time a new producer is created.  You should see that exactly two producers are created. Example output:

Starting sn_training.dev_training.OrderProducer
Will publish order 1US asynchronously to topic persistent://student31/developer/orderBackLogUS
Creating new producer with topic persistent://student31/developer/orderBackLogUS
Will publish order 2China asynchronously to topic persistent://student31/developer/orderBackLogChina
Creating new producer with topic persistent://student31/developer/orderBackLogChina
Will publish order 3US asynchronously to topic persistent://student31/developer/orderBackLogUS
Will publish order 4China asynchronously to topic persistent://student31/developer/orderBackLogChina
Will publish order 5US asynchronously to topic persistent://student31/developer/orderBackLogUS
Will publish order 6China asynchronously to topic persistent://student31/developer/orderBackLogChina