本文整理汇总了Java中com.microsoft.azure.iothub.Message类的典型用法代码示例。如果您正苦于以下问题:Java Message类的具体用法?Java Message怎么用?Java Message使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Message类属于com.microsoft.azure.iothub包,在下文中一共展示了Message类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sendMessage
import com.microsoft.azure.iothub.Message; //导入依赖的package包/类
public boolean sendMessage(byte[] messagePayload) {
logger.debug("Starting IoT Hub distro...");
logger.debug("Beginning IoT Hub setup.");
try {
logger.debug("Azure connect with: " + connectionString);
client = new DeviceClient(connectionString.toString(), IotHubClientProtocol.MQTT);
logger.debug("Successfully created an IoT Hub client.");
client.open();
logger.debug("Opened connection to IoT Hub.");
Message msg = new Message(messagePayload);
msg.setExpiryTime(5000);
Object lockobj = new Object();
EventCallback callback = new EventCallback();
client.sendEventAsync(msg, callback, lockobj);
synchronized (lockobj) {
lockobj.wait();
}
client.close();
logger.debug("Shutting down...");
return true;
} catch (Exception e) {
logger.error("Failure: " + e.toString());
}
return false;
}
示例2: parseHttpsMessage
import com.microsoft.azure.iothub.Message; //导入依赖的package包/类
/**
* Returns the HTTPS message represented by the service-bound message.
*
* @param message the service-bound message to be mapped to its HTTPS message
* equivalent.
*
* @return the HTTPS message represented by the service-bound message.
*/
public static HttpsSingleMessage parseHttpsMessage(Message message) {
HttpsSingleMessage httpsMsg = new HttpsSingleMessage();
// Codes_SRS_HTTPSSINGLEMESSAGE_11_001: [The parsed HttpsSingleMessage shall have a copy of the original message body as its body.]
byte[] msgBody = message.getBytes();
httpsMsg.body = Arrays.copyOf(msgBody, msgBody.length);
// Codes_SRS_HTTPSSINGLEMESSAGE_11_003: [The parsed HttpsSingleMessage shall add the prefix 'iothub-app-' to each of the message properties.]
MessageProperty[] msgProperties = message.getProperties();
httpsMsg.properties = new MessageProperty[msgProperties.length];
for (int i = 0; i < msgProperties.length; ++i)
{
MessageProperty property = msgProperties[i];
httpsMsg.properties[i] = new MessageProperty(
HTTPS_APP_PROPERTY_PREFIX + property.getName(),
property.getValue());
}
return httpsMsg;
}
示例3: receiveMessage
import com.microsoft.azure.iothub.Message; //导入依赖的package包/类
/**
* Receives a message, if one exists.
*
* @return the message received, or null if none exists.
*
* @throws IllegalStateException if the connection state is currently closed.
*/
public Message receiveMessage() throws IllegalStateException
{
// Codes_SRS_MQTTIOTHUBCONNECTION_15_015: [If the MQTT connection is closed,
// the function shall throw an IllegalStateException.]
if (this.state == ConnectionState.CLOSED)
{
throw new IllegalStateException("The MQTT connection is currently closed. Call open() before attempting" +
"to receive a message.");
}
Message message = null;
// Codes_SRS_MQTTIOTHUBCONNECTION_15_014: [The function shall attempt to consume a message
// from the received messages queue.]
if (this.receivedMessagesQueue.size() > 0)
{
message = this.receivedMessagesQueue.remove();
}
return message;
}
示例4: execute
import com.microsoft.azure.iothub.Message; //导入依赖的package包/类
public IotHubMessageResult execute(Message msg, Object context) {
IotHubMessageResult result = IotHubMessageResult.REJECT;
try {
String commandJson = new String(msg.getBytes(), Message.DEFAULT_IOTHUB_MESSAGE_CHARSET);
//DeviceCommand command = DeviceCommand.fromJSON(commandJson);
org.json.JSONObject jsonObject = new JSONObject(commandJson);
Log.d("IoTHubSvc", "Received message with content: " + commandJson);
String commandName = jsonObject.getString("Name");
if (MESSAGE_START_TELEMETRY.equalsIgnoreCase(commandName)) {
//SendEvent.startTelemetry();
onStartTelemetry();
result = IotHubMessageResult.COMPLETE;
} else if (MESSAGE_STOP_TELEMETRY.equalsIgnoreCase(commandName)) {
//SendEvent.stopTelemetry();
onStopTelemetry();
result = IotHubMessageResult.COMPLETE;
}
} catch (Throwable e) {
e.printStackTrace();
}
return result;
}
示例5: onStartTelemetry
import com.microsoft.azure.iothub.Message; //导入依赖的package包/类
private void onStartTelemetry() {
isTelemetryRunning = true;
new Thread(new Runnable() {
public void run() {
Log.d("IoTHubSvc", "Telemetry thread running");
broadcastStatus(STATUS_TELEMETRY_STARTED);
Random rand = new Random();
while(isTelemetryRunning) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("DeviceId", "AndroidDemo2");
jsonObject.addProperty("Temperature", rand.nextInt(120));
jsonObject.addProperty("Humidity", rand.nextInt(100));
jsonObject.addProperty("ExternalTemperature", rand.nextInt(120));
Message msg = new Message(jsonObject.toString());
EventCallback callback = new EventCallback();
client.sendEventAsync(msg, callback, null);
broadcastStatus(STATUS_TELEMETRY_RECEIVED, jsonObject.toString());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Log.d("IoTHubSvc", "Telemetry thread exited");
broadcastStatus(STATUS_TELEMETRY_STOPPED);
}
}).start();
}
示例6: toMessage
import com.microsoft.azure.iothub.Message; //导入依赖的package包/类
/**
* Returns the Iot Hub message represented by the HTTPS message.
*
* @return the IoT Hub message represented by the HTTPS message.
*/
public Message toMessage()
{
// Codes_SRS_HTTPSSINGLEMESSAGE_11_007: [The function shall return an IoT Hub message with a copy of the message body as its body.]
Message msg = new Message(this.getBody());
// Codes_SRS_HTTPSSINGLEMESSAGE_11_008: [The function shall return an IoT Hub message with application-defined properties that have the prefix 'iothub-app' removed.]
for (MessageProperty property : this.properties)
{
String propertyName =
httpsAppPropertyToAppProperty(property.getName());
msg.setProperty(propertyName, property.getValue());
}
return msg;
}
示例7: IotHubOutboundPacket
import com.microsoft.azure.iothub.Message; //导入依赖的package包/类
/**
* Constructor.
*
* @param message the message to be sent.
* @param callback the callback to be invoked when a response from the IoT
* Hub is received.
* @param callbackContext the context to be passed to the callback.
*/
public IotHubOutboundPacket(Message message,
IotHubEventCallback callback,
Object callbackContext)
{
// Codes_SRS_IOTHUBOUTBOUNDPACKET_11_001: [The constructor shall save the message, callback, and callback context.]
this.message = message;
this.callback = callback;
this.callbackContext = callbackContext;
}
示例8: messageArrived
import com.microsoft.azure.iothub.Message; //导入依赖的package包/类
@Override
public void messageArrived(String topic, MqttMessage mqttMessage)
{
// Codes_SRS_MQTTIOTHUBCONNECTION_15_019: [A new message is created using the payload
// from the received MqttMessage]
Message message = new Message(mqttMessage.getPayload());
// Codes_SRS_MQTTIOTHUBCONNECTION_15_020: [The newly created message is added to the received messages queue.]
this.receivedMessagesQueue.add(message);
}
示例9: sendEvents
import com.microsoft.azure.iothub.Message; //导入依赖的package包/类
@Override
public int sendEvents(int index, int numEventsToSend)
{
int eventIndex = index;
for (int i = 0; i < numEventsToSend; i++)
{
if (eventIndex == events.size())
eventIndex = 0;
String msgStr = augmentMessage(events.get(eventIndex++));
try
{
msgStr = getCurrentTime() + "," + msgStr;
byte[] bytes = msgStr.getBytes();
Message msg = new Message(bytes);
msg.setProperty("messageCount", Integer.toString(i));
clients[0].sendEventAsync(msg, callback, i);
System.out.println("S sent message - " + msgStr.trim());
if (running.get() == false)
break;
}
catch (Exception error)
{
error.printStackTrace();
}
}
return eventIndex;
}
示例10: getMessage
import com.microsoft.azure.iothub.Message; //导入依赖的package包/类
/**
* Getter for the message to be sent.
*
* @return the message to be sent.
*/
public Message getMessage()
{
// Codes_SRS_IOTHUBOUTBOUNDPACKET_11_002: [The function shall return the message given in the constructor.]
return message;
}
示例11: sendEvent
import com.microsoft.azure.iothub.Message; //导入依赖的package包/类
/**
* Sends an event message.
*
* @param message the event message.
*
* @return the status code from sending the event message.
*
* @throws IllegalStateException if the MqttIotHubConnection is not open
*/
public IotHubStatusCode sendEvent(Message message) throws IllegalStateException
{
synchronized (MQTT_CONNECTION_LOCK)
{
// Codes_SRS_MQTTIOTHUBCONNECTION_15_010: [If the message is null or empty,
// the function shall return status code BAD_FORMAT.]
if (message == null || message.getBytes() == null || message.getBytes().length == 0)
{
return IotHubStatusCode.BAD_FORMAT;
}
// Codes_SRS_MQTTIOTHUBCONNECTION_15_013: [If the MQTT connection is closed,
// the function shall throw an IllegalStateException.]
if (this.state == ConnectionState.CLOSED)
{
throw new IllegalStateException("Cannot send event using a closed MQTT connection");
}
// Codes_SRS_MQTTIOTHUBCONNECTION_15_008: [The function shall send an event message
// to the IoT Hub given in the configuration.]
// Codes_SRS_MQTTIOTHUBCONNECTION_15_009: [The function shall send the message payload.]
// Codes_SRS_MQTTIOTHUBCONNECTION_15_011: [If the message was successfully received by the service,
// the function shall return status code OK_EMPTY.]
IotHubStatusCode result = IotHubStatusCode.OK_EMPTY;
try
{
inFlightCount++;
while (inFlightCount >= maxInFlightCount)
{
Thread.sleep(10);
}
MqttMessage mqttMessage = new MqttMessage(message.getBytes());
mqttMessage.setQos(qos);
IMqttDeliveryToken publishToken = asyncClient.publish(publishTopic, mqttMessage);
}
// Codes_SRS_MQTTIOTHUBCONNECTION_15_012: [If the message was not successfully
// received by the service, the function shall return status code ERROR.]
catch(Exception e)
{
result = IotHubStatusCode.ERROR;
}
return result;
}
}
示例12: getBodyAsString
import com.microsoft.azure.iothub.Message; //导入依赖的package包/类
/**
* Returns the message body as a string. The body is encoded using charset
* UTF-8.
*
* @return the message body as a string.
*/
public String getBodyAsString() {
// Codes_SRS_HTTPSSINGLEMESSAGE_11_010: [The function shall return the message body as a string encoded using charset UTF-8.]
return new String(this.body, Message.DEFAULT_IOTHUB_MESSAGE_CHARSET);
}
示例13: addMessage
import com.microsoft.azure.iothub.Message; //导入依赖的package包/类
/**
* Adds a message to the transport queue.
*
* @param message the message to be sent.
* @param callback the callback to be invoked when a response for the
* message is received.
* @param callbackContext the context to be passed in when the callback is
* invoked.
*/
void addMessage(Message message,
IotHubEventCallback callback,
Object callbackContext);