Chapter 4. Examples This chapter demonstrates the use of Red Hat build of Apache Qpid ProtonJ2 through example programs. For more examples, see the Apache Qpid ProtonJ2 examples . 4.1. Sending messages This client program connects to a server using <serverHost> and <serverPort> , creates a sender for target <address> , sends 100 messages containing String and exits. Example: Sending messages package org.apache.qpid.protonj2.client.examples; import org.apache.qpid.protonj2.client.Client; import org.apache.qpid.protonj2.client.Connection; import org.apache.qpid.protonj2.client.ConnectionOptions; import org.apache.qpid.protonj2.client.Message; import org.apache.qpid.protonj2.client.Sender; import org.apache.qpid.protonj2.client.Tracker; 1 public class Send { private static final int MESSAGE_COUNT = 100; 2 public static void main(String[] argv) throws Exception { final String serverHost = System.getProperty("HOST", "localhost"); 3 final int serverPort = Integer.getInteger("PORT", 5672); 4 final String address = System.getProperty("ADDRESS", "send-receive-example"); final Client client = Client.create(); 5 final ConnectionOptions options = new ConnectionOptions(); 6 options.user(System.getProperty("USER")); options.password(System.getProperty("PASSWORD")); try (Connection connection = client.connect(serverHost, serverPort, options); 7 Sender sender = connection.openSender(address)) { 8 for (int i = 0; i < MESSAGE_COUNT; ++i) { Message<String> message = Message.create(String.format("Hello World! [%s]", i)); 9 Tracker tracker = sender.send(message); 10 tracker.awaitSettlement(); 11 System.out.println(String.format("Sent message to %s: %s", sender.address(), message.body())); } } } } <.> packages required by Red Hat build of Apache Qpid ProtonJ2. <.> The number of messages to send. <.> serverHost is the network address of the host or virtual host for the AMQP connection and can be configured by setting the Environment variable HOST . <.> serverPort is the port on the host that the broker is accepting connections and can be configured by setting the environment variable PORT . <.> Client is the container that can create multiple Connections to a broker. <.> options is used for various setting, including User and Password . See Section 5.1, "Connection Options" for more information. <.> connection is the AMQP Connection to a broker. <.> Create a sender for transferring messages to the broker. <.> In the message send loop a new message is created. <.> The message is sent to the broker. <.> Wait for the broker to settle the message. Running the example To run the example program, see Chapter 3, Getting started . 4.2. Receiving messages This client program connects to a server using <connection-url> , creates a receiver for source <address> , and receives messages until it is terminated or it reaches <count> messages. Example: Receiving messages package org.apache.qpid.protonj2.client.examples; import org.apache.qpid.protonj2.client.Client; import org.apache.qpid.protonj2.client.Connection; import org.apache.qpid.protonj2.client.ConnectionOptions; import org.apache.qpid.protonj2.client.Delivery; import org.apache.qpid.protonj2.client.Message; import org.apache.qpid.protonj2.client.Receiver; 1 public class Receive { private static final int MESSAGE_COUNT = 100; 2 public static void main(String[] args) throws Exception { final String serverHost = System.getProperty("HOST", "localhost"); 3 final int serverPort = Integer.getInteger("PORT", 5672); 4 final String address = System.getProperty("ADDRESS", "send-receive-example"); 5 final Client client = Client.create(); 6 final ConnectionOptions options = new ConnectionOptions(); 7 options.user(System.getProperty("USER")); options.password(System.getProperty("PASSWORD")); try (Connection connection = client.connect(serverHost, serverPort, options); 8 Receiver receiver = connection.openReceiver(address)) { 9 for (int i = 0; i < MESSAGE_COUNT; ++i) { Delivery delivery = receiver.receive(); 10 Message<String> message = delivery.message(); 11 System.out.println("Received message with body: " + message.body()); } } } } 1 1 required packages for Red Hat build of Apache Qpid ProtonJ2. 2 2 The number of messages to receive. 3 3 serverHost is the network address of the host or virtual host for the AMQP connection and can be configured by setting the Environment variable HOST . 4 4 serverPort is the port on the host that the broker is accepting connections and can be configured by setting the environment variable PORT . 5 5 6 Client is the container that can create multiple Connections to a broker. 6 7 options is used for various setting, including User and Password . See Section 5.1, "Connection Options" for more information. 7 8 connection is the AMQP Connection to a broker. 8 9 Create a receiver for receiving messages from the broker. 9 10 In the message receive loop a new delivery is received. 10 11 The message is obtained from the delivery . Running the example To run the example program, see Chapter 3, Getting started .

Commands:
package org.apache.qpid.protonj2.client.examples; import org.apache.qpid.protonj2.client.Client; import org.apache.qpid.protonj2.client.Connection; import org.apache.qpid.protonj2.client.ConnectionOptions; import org.apache.qpid.protonj2.client.Message; import org.apache.qpid.protonj2.client.Sender; import org.apache.qpid.protonj2.client.Tracker; 1 public class Send { private static final int MESSAGE_COUNT = 100; 2 public static void main(String[] argv) throws Exception { final String serverHost = System.getProperty("HOST", "localhost"); 3 final int serverPort = Integer.getInteger("PORT", 5672); 4 final String address = System.getProperty("ADDRESS", "send-receive-example"); final Client client = Client.create(); 5 final ConnectionOptions options = new ConnectionOptions(); 6 options.user(System.getProperty("USER")); options.password(System.getProperty("PASSWORD")); try (Connection connection = client.connect(serverHost, serverPort, options); 7 Sender sender = connection.openSender(address)) { 8 for (int i = 0; i < MESSAGE_COUNT; ++i) { Message<String> message = Message.create(String.format("Hello World! [%s]", i)); 9 Tracker tracker = sender.send(message); 10 tracker.awaitSettlement(); 11 System.out.println(String.format("Sent message to %s: %s", sender.address(), message.body())); } } } }
package org.apache.qpid.protonj2.client.examples; import org.apache.qpid.protonj2.client.Client; import org.apache.qpid.protonj2.client.Connection; import org.apache.qpid.protonj2.client.ConnectionOptions; import org.apache.qpid.protonj2.client.Delivery; import org.apache.qpid.protonj2.client.Message; import org.apache.qpid.protonj2.client.Receiver; 1 public class Receive { private static final int MESSAGE_COUNT = 100; 2 public static void main(String[] args) throws Exception { final String serverHost = System.getProperty("HOST", "localhost"); 3 final int serverPort = Integer.getInteger("PORT", 5672); 4 final String address = System.getProperty("ADDRESS", "send-receive-example"); 5 final Client client = Client.create(); 6 final ConnectionOptions options = new ConnectionOptions(); 7 options.user(System.getProperty("USER")); options.password(System.getProperty("PASSWORD")); try (Connection connection = client.connect(serverHost, serverPort, options); 8 Receiver receiver = connection.openReceiver(address)) { 9 for (int i = 0; i < MESSAGE_COUNT; ++i) { Delivery delivery = receiver.receive(); 10 Message<String> message = delivery.message(); 11 System.out.println("Received message with body: " + message.body()); } } } }