本文整理汇总了Java中org.eclipse.paho.client.mqttv3.MqttAsyncClient类的典型用法代码示例。如果您正苦于以下问题:Java MqttAsyncClient类的具体用法?Java MqttAsyncClient怎么用?Java MqttAsyncClient使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MqttAsyncClient类属于org.eclipse.paho.client.mqttv3包,在下文中一共展示了MqttAsyncClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: init
import org.eclipse.paho.client.mqttv3.MqttAsyncClient; //导入依赖的package包/类
@Override
public void init(MqttPluginConfiguration configuration) {
retryInterval = configuration.getRetryInterval();
mqttClientOptions = new MqttConnectOptions();
mqttClientOptions.setCleanSession(false);
mqttClientOptions.setMaxInflight(configuration.getMaxInFlight());
mqttClientOptions.setAutomaticReconnect(true);
String clientId = configuration.getClientId();
if (StringUtils.isEmpty(clientId)) {
clientId = UUID.randomUUID().toString();
}
if (!StringUtils.isEmpty(configuration.getAccessToken())) {
mqttClientOptions.setUserName(configuration.getAccessToken());
}
try {
mqttClient = new MqttAsyncClient("tcp://" + configuration.getHost() + ":" + configuration.getPort(), clientId);
} catch (Exception e) {
log.error("Failed to create mqtt client", e);
throw new RuntimeException(e);
}
// connect();
}
示例2: connect
import org.eclipse.paho.client.mqttv3.MqttAsyncClient; //导入依赖的package包/类
public void connect() {
try {
client = new MqttAsyncClient((configuration.isSsl() ? "ssl" : "tcp") + "://" + configuration.getHost() + ":" + configuration.getPort(),
getClientId(), new MemoryPersistence());
client.setCallback(this);
clientOptions = new MqttConnectOptions();
clientOptions.setCleanSession(true);
if (configuration.isSsl() && !StringUtils.isEmpty(configuration.getTruststore())) {
Properties sslProperties = new Properties();
sslProperties.put(SSLSocketFactoryFactory.TRUSTSTORE, configuration.getTruststore());
sslProperties.put(SSLSocketFactoryFactory.TRUSTSTOREPWD, configuration.getTruststorePassword());
sslProperties.put(SSLSocketFactoryFactory.TRUSTSTORETYPE, "JKS");
sslProperties.put(SSLSocketFactoryFactory.CLIENTAUTH, false);
clientOptions.setSSLProperties(sslProperties);
}
configuration.getCredentials().configure(clientOptions);
checkConnection();
if (configuration.getAttributeUpdates() != null) {
configuration.getAttributeUpdates().forEach(mapping ->
gateway.subscribe(new AttributesUpdateSubscription(mapping.getDeviceNameFilter(), this))
);
}
if (configuration.getServerSideRpc() != null) {
configuration.getServerSideRpc().forEach(mapping ->
gateway.subscribe(new RpcCommandSubscription(mapping.getDeviceNameFilter(), this))
);
}
} catch (MqttException e) {
log.error("[{}:{}] MQTT broker connection failed!", configuration.getHost(), configuration.getPort(), e);
throw new RuntimeException("MQTT broker connection failed!", e);
}
}
示例3: connect
import org.eclipse.paho.client.mqttv3.MqttAsyncClient; //导入依赖的package包/类
private void connect(String serverURI, String clientId, String zkConnect) throws MqttException {
mqtt = new MqttAsyncClient(serverURI, clientId);
mqtt.setCallback(this);
IMqttToken token = mqtt.connect();
Properties props = new Properties();
//Updated based on Kafka v0.8.1.1
props.put("metadata.broker.list", "localhost:9092");
props.put("serializer.class", "kafka.serializer.StringEncoder");
props.put("partitioner.class", "example.producer.SimplePartitioner");
props.put("request.required.acks", "1");
ProducerConfig config = new ProducerConfig(props);
kafkaProducer = new Producer<String, String>(config);
token.waitForCompletion();
logger.info("Connected to MQTT and Kafka");
}
示例4: connect
import org.eclipse.paho.client.mqttv3.MqttAsyncClient; //导入依赖的package包/类
private void connect() {
logger.debug(MessageFormat.format("Connecting to {0} as {1}", mqttConfig.getBroker(), mqttConfig.getClientid()));
try {
mqttSession = new MqttAsyncClient(mqttConfig.getBroker(),
mqttConfig.getClientid(),
new MemoryPersistence());
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
mqttSession.connect(connOpts, new TetradMQTTConnectionListener(this));
} catch (MqttException e) {
logger.error(MessageFormat.format("Error connecting to {0} as {1}. Message: {2}, ReasonCode: {3}",
mqttConfig.getBroker(),
mqttConfig.getClientid(),
e.getMessage(),
e.getReasonCode()
));
e.printStackTrace();
}
}
示例5: MqttSession
import org.eclipse.paho.client.mqttv3.MqttAsyncClient; //导入依赖的package包/类
/**
*
* @param deviceId ARTIK Cloud device ID
* @param deviceToken ARTIK Cloud device token
* @param msgCallback callback handling events such as receiving message from the topic subscribing to
* @param userCallback callback handling mqtt operations such as connect/disconnect/publish/subscribe et al.
* @throws ArtikCloudMqttException
*/
public MqttSession(String deviceId,
String deviceToken,
ArtikCloudMqttCallback callback
) throws ArtikCloudMqttException {
this.operationListener = new OperationListener(callback);
this.deviceId = deviceId;
this.deviceToken = deviceToken;
this.brokerUri = SCHEME + "://" + HOST + ":" + PORT;
this.publishMessageTopicPath = PUBLISH_TOPIC_MESSAGES_BASE + deviceId;
this.subscribeActionsTopicPath = SUBSCRIBE_TOPIC_ACTIONS_BASE + deviceId;
this.subscribeErrorTopicPath = SUBSCRIBE_TOPIC_ERRORS_BASE + deviceId;
try {
mqttClient = new MqttAsyncClient(brokerUri, deviceId, new MemoryPersistence());
msgListener = new MessageListener(callback);
mqttClient.setCallback(msgListener);
} catch (MqttException e) {
throw new ArtikCloudMqttException(e);
}
}
示例6: AwsIotMqttConnection
import org.eclipse.paho.client.mqttv3.MqttAsyncClient; //导入依赖的package包/类
public AwsIotMqttConnection(AbstractAwsIotClient client, SocketFactory socketFactory, String serverUri)
throws AWSIotException {
super(client);
this.socketFactory = socketFactory;
messageListener = new AwsIotMqttMessageListener(client);
clientListener = new AwsIotMqttClientListener(client);
try {
mqttClient = new MqttAsyncClient(serverUri, client.getClientId(), new MemoryPersistence());
mqttClient.setCallback(clientListener);
} catch (MqttException e) {
throw new AWSIotException(e);
}
}
示例7: startClient
import org.eclipse.paho.client.mqttv3.MqttAsyncClient; //导入依赖的package包/类
public void startClient() throws MqttException {
MemoryPersistence persistence = new MemoryPersistence();
try {
conOpt = new MqttConnectOptions();
conOpt.setCleanSession(true);
// Construct the MqttClient instance
client = new MqttAsyncClient(this.brokerURL, clientId, persistence);
// Set this wrapper as the callback handler
client.setCallback(this);
} catch (MqttException e) {
log.info("Unable to set up client: " + e.toString());
}
}
示例8: AllDataEvent
import org.eclipse.paho.client.mqttv3.MqttAsyncClient; //导入依赖的package包/类
public AllDataEvent(AHandler deviceHandler, String eventTopic, MqttAsyncClient mqttClient) {
super(deviceHandler, eventTopic, "allData", mqttClient);
super.addDescription(IMUV2.VALUE, short[][].class, "JSON",
"[[" + Short.MIN_VALUE + "," + Short.MIN_VALUE + "," + Short.MIN_VALUE + "],"
+ "[" + Short.MIN_VALUE + "," + Short.MIN_VALUE + "," + Short.MIN_VALUE + "],"
+ "[" + Short.MIN_VALUE + "," + Short.MIN_VALUE + "," + Short.MIN_VALUE + "],"
+ "[" + Short.MIN_VALUE + "," + Short.MIN_VALUE + "," + Short.MIN_VALUE + "],"
+ "[" + Short.MIN_VALUE + "," + Short.MIN_VALUE + "," + Short.MIN_VALUE + "," + Short.MIN_VALUE + "],"
+ "[" + Short.MIN_VALUE + "," + Short.MIN_VALUE + "," + Short.MIN_VALUE + "],"
+ "[" + Short.MIN_VALUE + "," + Short.MIN_VALUE + "," + Short.MIN_VALUE + "]]",
"...",
"[[" + Short.MAX_VALUE + "," + Short.MAX_VALUE + "," + Short.MAX_VALUE + "],"
+ "[" + Short.MAX_VALUE + "," + Short.MAX_VALUE + "," + Short.MAX_VALUE + "],"
+ "[" + Short.MAX_VALUE + "," + Short.MAX_VALUE + "," + Short.MAX_VALUE + "],"
+ "[" + Short.MAX_VALUE + "," + Short.MAX_VALUE + "," + Short.MAX_VALUE + "],"
+ "[" + Short.MAX_VALUE + "," + Short.MAX_VALUE + "," + Short.MAX_VALUE + "," + Short.MAX_VALUE + "],"
+ "[" + Short.MAX_VALUE + "," + Short.MAX_VALUE + "," + Short.MAX_VALUE + "],"
+ "[" + Short.MAX_VALUE + "," + Short.MAX_VALUE + "," + Short.MAX_VALUE + "]]"
);
}
示例9: AHandler
import org.eclipse.paho.client.mqttv3.MqttAsyncClient; //导入依赖的package包/类
public AHandler(URI mqttURI, String deviceNameRootTopic, String identityString) throws Throwable {
this.mqttURI = mqttURI;
this.identityString = identityString;
this.deviceNameTopic = deviceNameRootTopic + "/" + getApplicationName() + "/" + identityString;;
this.intentTopic = getDeviceBaseTopic() + "/intent";
this.eventTopic = getDeviceBaseTopic() + "/event";
this.statusTopic = getDeviceBaseTopic() + "/status";
this.mqttClient = new MqttAsyncClient(mqttURI.toString(), identityString, new MemoryPersistence());
this.intentSet = new HashSet<>();
this.intentsMap = new HashMap<>();
this.eventMap = new HashMap<>();
this.statusMap = new HashMap<>();
this.addStatusClass(HandlerReadyStatus.class);
connectToMQTT();
}
示例10: addEventClass
import org.eclipse.paho.client.mqttv3.MqttAsyncClient; //导入依赖的package包/类
public void addEventClass(Class... classes) {
for (Class eventClass : classes) {
try {
eventMap.put(eventClass, (AnEvent) eventClass.getConstructor(AHandler.class, String.class, MqttAsyncClient.class
).newInstance(this, eventTopic, mqttClient));
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
Logger.getLogger(AHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (mqttClient != null && mqttClient.isConnected()) {
for (AnEvent event : eventMap.values()) {
event.publishDescriptions();
}
}
}
示例11: addStatusClass
import org.eclipse.paho.client.mqttv3.MqttAsyncClient; //导入依赖的package包/类
public void addStatusClass(Class... classes) {
for (Class statusClass : classes) {
try {
statusMap.put(statusClass, (AStatus) statusClass.getConstructor(AHandler.class, String.class, MqttAsyncClient.class
).newInstance(this, statusTopic, mqttClient));
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
Logger.getLogger(AHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (mqttClient != null && mqttClient.isConnected()) {
for (AStatus status : statusMap.values()) {
status.publishDescriptions();
}
getStatus(HandlerReadyStatus.class).update(REACHABLE, true);
}
}
示例12: init
import org.eclipse.paho.client.mqttv3.MqttAsyncClient; //导入依赖的package包/类
@PostConstruct
public void init() throws Exception {
scheduler = Executors.newSingleThreadScheduledExecutor();
tbClientOptions = new MqttConnectOptions();
tbClientOptions.setCleanSession(false);
tbClientOptions.setMaxInflight(connection.getMaxInFlight());
tbClientOptions.setAutomaticReconnect(true);
MqttGatewaySecurityConfiguration security = connection.getSecurity();
security.setupSecurityOptions(tbClientOptions);
tbClient = new MqttAsyncClient((security.isSsl() ? "ssl" : "tcp") + "://" + connection.getHost() + ":" + connection.getPort(),
security.getClientId(), persistence.getPersistence());
tbClient.setCallback(this);
if (persistence.getBufferSize() > 0) {
DisconnectedBufferOptions options = new DisconnectedBufferOptions();
options.setBufferSize(persistence.getBufferSize());
options.setBufferEnabled(true);
options.setPersistBuffer(true);
tbClient.setBufferOpts(options);
}
connect();
scheduler.scheduleAtFixedRate(this::reportStats, 0, reporting.getInterval(), TimeUnit.MILLISECONDS);
}
示例13: MqttStressTestClient
import org.eclipse.paho.client.mqttv3.MqttAsyncClient; //导入依赖的package包/类
public MqttStressTestClient(ResultAccumulator results, String brokerUri, String deviceToken) throws MqttException {
this.results = results;
this.clientId = MqttAsyncClient.generateClientId();
this.deviceToken = deviceToken;
this.persistence = new MemoryPersistence();
this.client = new MqttAsyncClient(brokerUri, clientId, persistence);
}
示例14: makeClientId
import org.eclipse.paho.client.mqttv3.MqttAsyncClient; //导入依赖的package包/类
private String makeClientId ( final String clientId )
{
if ( clientId != null )
{
return clientId;
}
return MqttAsyncClient.generateClientId ();
}
示例15: SensorsManager
import org.eclipse.paho.client.mqttv3.MqttAsyncClient; //导入依赖的package包/类
public SensorsManager(final MqttAsyncClient asyncClient,
final String boardCommandsTopic, final String boardDataBaseTopic, final String encoding) {
this.boardCommandsTopic = boardCommandsTopic;
this.boardDataBaseTopic = boardDataBaseTopic;
this.encoding = encoding;
this.asyncClient = asyncClient;
// Build and save the topic names that we will use to publish the data from the sensors
this.earthHumidityTopic = this.boardDataBaseTopic.concat(SENSOR_EARTH_HUMIDITY);
final String sunlightDataBaseTopic = boardDataBaseTopic.concat(SENSOR_SUNLIGHT);
this.visibleLightTopic = String.join(TOPIC_SEPARATOR, sunlightDataBaseTopic, "visiblelight");
this.infraredLightTopic = String.join(TOPIC_SEPARATOR, sunlightDataBaseTopic, "ir");
this.ultraVioletIndexTopic = String.join(TOPIC_SEPARATOR, sunlightDataBaseTopic, "uv");
}
开发者ID:PacktPublishing,项目名称:MQTT-Essentials-A-Lightweight-IoT-Protocol,代码行数:14,代码来源:SensorsManager.java