4.2. 接收信息
此客户端程序使用 < connection-url > 连接到服务器,为源 <address& gt; 创建一个接收器,并接收信息直到终止或到达 < count> 信息。
示例:接收信息
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;
public class Receive {
private static final int MESSAGE_COUNT = 100;
public static void main(String[] args) throws Exception {
final String serverHost = System.getProperty("HOST", "localhost");
final int serverPort = Integer.getInteger("PORT", 5672);
final String address = System.getProperty("ADDRESS", "send-receive-example");
final Client client = Client.create();
final ConnectionOptions options = new ConnectionOptions();
options.user(System.getProperty("USER"));
options.password(System.getProperty("PASSWORD"));
try (Connection connection = client.connect(serverHost, serverPort, options);
Receiver receiver = connection.openReceiver(address)) {
for (int i = 0; i < MESSAGE_COUNT; ++i) {
Delivery delivery = receiver.receive();
Message<String> message = delivery.message();
System.out.println("Received message with body: " + message.body());
}
}
}
}
运行示例
要运行示例程序,请参阅 第 3 章 开始使用。