Created
April 21, 2020 08:32
-
-
Save maxiwu/e7204c3cb6e00e33e64ed5570889611a to your computer and use it in GitHub Desktop.
Java sample code for AWS GGC client
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
package com.umedia.awsiotclient.service; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import javax.annotation.PostConstruct; | |
import com.amazonaws.services.iot.client.AWSIotException; | |
import com.amazonaws.services.iot.client.AWSIotMessage; | |
import com.amazonaws.services.iot.client.AWSIotMqttClient; | |
import com.amazonaws.services.iot.client.AWSIotQos; | |
import com.amazonaws.services.iot.client.sample.sampleUtil.SampleUtil; | |
import com.amazonaws.services.iot.client.sample.sampleUtil.SampleUtil.KeyStorePasswordPair; | |
import org.springframework.core.io.ClassPathResource; | |
import org.springframework.core.io.Resource; | |
import org.springframework.stereotype.Service; | |
@Service | |
public class MqttSampleClient { | |
String clientEndpoint = "greengrass-ats.iot.us-east-2.amazonaws.com"; // replace <prefix> and <region> with your own | |
String clientId = "random_client_id_FG446GG22"; // replace with your own client ID. Use unique client IDs for concurrent | |
// connections. | |
String certificateFile = "/certs/cert.pem"; // X.509 based certificate file | |
String privateKeyFile = "/certs/private.key"; // PKCS#1 or PKCS#8 PEM encoded private key file | |
@PostConstruct | |
public void connectAWS() throws IOException { | |
Resource cert = new ClassPathResource(certificateFile); | |
Resource prik = new ClassPathResource(privateKeyFile); | |
if (!cert.getFile().exists()) { | |
System.out.println("cert file not found"); | |
return; | |
} | |
// SampleUtil.java and its dependency PrivateKeyReader.java can be copied from | |
// the sample source code. | |
// Alternatively, you could load key store directly from a file - see the | |
// example included in this README. | |
KeyStorePasswordPair pair = SampleUtil.getKeyStorePasswordPair(cert.getFile().getAbsolutePath(), prik.getFile().getAbsolutePath()); | |
AWSIotMqttClient client = new AWSIotMqttClient(clientEndpoint, clientId, pair.keyStore, pair.keyPassword); | |
// optional parameters can be set before connect() | |
try { | |
client.setKeepAliveInterval(15*1000); | |
AWSIotMessage willMessage = new AWSIotMessage("spBv1.0/steven_core/LWT/"+clientId, AWSIotQos.QOS1, "{\"status\":\"offline\"}"); | |
client.setWillMessage(willMessage); | |
client.connect(); | |
AWSIotMessage birth = new AWSIotMessage("spBv1.0/steven_core/LWT/"+clientId, AWSIotQos.QOS1, "{\"status\":\"online\"}"); | |
client.publish(birth); | |
// System.in.read(); | |
client.disconnect(); | |
} catch (AWSIotException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment