我试图将这个类用于GUI应用程序,其中receiveDeviceToCloudMessage返回一个包含消息的字符串值,以便在我的gui代码中实现,我该怎么办呢? 有没有办法可以将此代码转换为GUI应用程序? 由于抱怨,我不得不删除catch块
public class AzureIotHub
{
//The class variables
static String deviceConnectionString = "connectionString";
static String message;
public static EventHubClient receiveDeviceToCloudMessage(final String partitionId)
{
EventHubClient client = null;
try
{
client = EventHubClient.createFromConnectionStringSync(deviceConnectionString);
}
try
{
client.createReceiver(EventHubClient.DEFAULT_CONSUMER_GROUP_NAME,partitionId, Instant.now()).thenAccept(receiver ->
{
System.out.println("** Created receiver on partition " +partitionId);
try
{
while (true)
{ //While loop
Iterable<EventData> receivedEvents = receiver.receive(100).get();
int batchSize = 0;
if (receivedEvents != null)
{
System.out.println("Got some events");
for (EventData receivedEvent : receivedEvents)
{
// used a String variable to save the message
message = new String(receivedEvent.getBytes(), Charset.defaultCharset());
batchSize++;
}
}
System.out.println(String.format("Partition:%s,ReceivedBatch Size: %s", partitionId, batchSize));
}
}
});
}
return client;
}
public static void main(String[] args) throws IOException,NoSuchAlgorithmException
{
// Create receivers for partitions 0 and 1.
EventHubClient client0 = receiveDeviceToCloudMessage("0");
System.out.println("Press ENTER to exit.");
try
{
System.in.read();
System.out.println(message);
}
try
{
client0.closeSync();
System.exit(0);
}
}
}