Created
November 29, 2016 20:49
-
-
Save dluc/ac6a8cce6be45b0da5b882f60d3629a5 to your computer and use it in GitHub Desktop.
Azure IoT issue 968 - synchronous
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Dependencies: | |
iothub-java-service-client 1.0.10 | |
iothub-java-device-client 1.0.15 | |
*/ | |
import com.microsoft.azure.iot.service.sdk.*; | |
import java.nio.charset.StandardCharsets; | |
import java.text.DateFormat; | |
import java.text.SimpleDateFormat; | |
import java.time.Instant; | |
import java.util.Date; | |
import java.util.UUID; | |
/** | |
* Service client example for: | |
* - sending message to the device | |
* - waiting and receive feedback from the device | |
*/ | |
public class TestSync | |
{ | |
static String hub = System.getenv("SDK_HUB_NAME"); | |
static String serviceKey = System.getenv("SDK_SERVICE_KEY"); | |
static String device = System.getenv("SDK_DEVICE_ID"); | |
static long timeoutMs = 50; | |
static ServiceClient client; | |
public static void main(String[] args) throws Exception | |
{ | |
String cs = "HostName=" + hub + ".azure-devices.net;SharedAccessKeyName=service;SharedAccessKey=" + serviceKey; | |
log("Creating service client"); | |
client = ServiceClient.createFromConnectionString(cs, IotHubServiceClientProtocol.AMQPS); | |
log("Open service client connection"); | |
client.open(); | |
log("Creating feedback receiver"); | |
FeedbackReceiver feedbackReceiver = client.getFeedbackReceiver(device); | |
log("Opening feedback receiver connection"); | |
feedbackReceiver.open(); | |
for (int i = 1; i <= 10; i++) send(i); | |
log("Receiving, timeout " + timeoutMs + " milliseconds"); | |
FeedbackBatch batch = feedbackReceiver.receive(timeoutMs); | |
log("Batch size: " + batch.getRecords().size()); | |
log("Receiving, timeout " + timeoutMs + " milliseconds"); | |
batch = feedbackReceiver.receive(timeoutMs); | |
log("Batch size: " + batch.getRecords().size()); | |
log("Receiving, timeout " + timeoutMs + " milliseconds"); | |
batch = feedbackReceiver.receive(timeoutMs); | |
log("Batch size: " + batch.getRecords().size()); | |
log("Close service client connection"); | |
client.close(); | |
log("Done"); | |
} | |
static void send(Integer i) throws Exception | |
{ | |
Message msg = new Message("test".getBytes(StandardCharsets.UTF_8)); | |
msg.setTo(device); | |
msg.setDeliveryAcknowledgement(DeliveryAcknowledgement.Full); | |
msg.setExpiryTimeUtc(Date.from(Instant.now().plusSeconds(3600))); | |
msg.setMessageId(UUID.randomUUID().toString()); | |
log("Sending msg #" + i + ", To: '" + device + "', ID: " + msg.getMessageId()); | |
client.send(device, msg); | |
} | |
static void log(String text) | |
{ | |
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS"); | |
System.out.println("[" + dateFormat.format(new Date()) + "] " + text); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment