本文整理匯總了Java中org.eclipse.paho.client.mqttv3.MqttConnectOptions類的典型用法代碼示例。如果您正苦於以下問題:Java MqttConnectOptions類的具體用法?Java MqttConnectOptions怎麽用?Java MqttConnectOptions使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
MqttConnectOptions類屬於org.eclipse.paho.client.mqttv3包,在下文中一共展示了MqttConnectOptions類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getMqttClient
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入依賴的package包/類
/**
* get MqttClient by clientKey
* @param clientKey
* @return
* @throws MqttException
*/
public static MqttClient getMqttClient(String serverURI, String clientId,StringRedisTemplate redisTemplate)
throws MqttException{
String clientKey=serverURI.concat(clientId);
if(clientMap.get(clientKey)==null){
lock.lock();
if(clientMap.get(clientKey)==null){
MqttClientPersistence persistence = new MemoryPersistence();
MqttClient client = new MqttClient(serverURI, clientId, persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
MqttCallback callback = new IMMqttCallBack(client,redisTemplate);
client.setCallback(callback);
connOpts.setCleanSession(true);
client.connect(connOpts);
clientMap.put(clientKey, client);
}
lock.unlock();
}
return clientMap.get(clientKey);
}
示例2: init
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入依賴的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();
}
示例3: init
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入依賴的package包/類
public void init() throws MqttException {
try {
String url = mqttProperties.getHostname() + ":" + mqttProperties.getPort();
LOGGER.info("Opening MQTT connection: '{}'", url);
LOGGER.info("properties: {}", mqttProperties);
MqttConnectOptions connectOptions = new MqttConnectOptions();
connectOptions.setUserName(mqttProperties.getUsername());
connectOptions.setPassword(mqttProperties.getPassword().toCharArray());
connectOptions.setCleanSession(false);
client = new MqttClient(url, mqttProperties.getClientName(), new MemoryPersistence());
client.setCallback(onMessageArrived);
client.connect(connectOptions);
client.subscribe(mqttProperties.getTopic());
} catch (MqttException e) {
LOGGER.error(e.getMessage(), e);
throw e;
}
}
示例4: connect
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入依賴的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);
}
}
示例5: getMqttConnectOptions
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入依賴的package包/類
public MqttConnectOptions getMqttConnectOptions() {
MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(isCleanSession());
options.setConnectionTimeout(getTimeout());
options.setKeepAliveInterval(getKeepAlive());
if (!getUsername().isEmpty()) {
options.setUserName(getUsername());
}
if (!getPassword().isEmpty()) {
options.setPassword(getPassword().toCharArray());
}
if (!getLwtTopic().isEmpty() && !getLwtPayload().isEmpty()) {
options.setWill(getLwtTopic(), getLwtPayload().getBytes(), getLwtQos(), isLwtRetained());
}
return options;
}
示例6: connectAndSubscribe
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入依賴的package包/類
/*********************************************************************************************************************************************************************
*
*/
private void connectAndSubscribe() throws Exception {
ConfigHandler configHandler = serialMqttBridge.getConfigHandler();
mqttClient = new MqttClient(configHandler.getMqttBrokerUrl(), configHandler.getMqttClientId(), null);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
connOpts.setAutomaticReconnect(true);
// Authentication
if (configHandler.getMqttBrokerUsername() != null && configHandler.getMqttBrokerPassword() != null) {
connOpts.setUserName(configHandler.getMqttBrokerUsername());
connOpts.setPassword(configHandler.getMqttBrokerPassword().toCharArray());
}
// MqttCallback
mqttCallback = new MqttSubscriptionCallback(this);
mqttClient.setCallback(mqttCallback);
mqttClient.connect(connOpts);
// Subscribe to defined inbound topic
mqttClient.subscribe(configHandler.getMqttTopicSubscribe(), configHandler.getMqttQosSubscribe());
}
示例7: initializeMqttClient
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入依賴的package包/類
private void initializeMqttClient()
throws MqttException, IOException, NoSuchAlgorithmException, InvalidKeySpecException {
mqttClient = new MqttClient(cloudIotOptions.getBrokerUrl(),
cloudIotOptions.getClientId(), new MemoryPersistence());
MqttConnectOptions options = new MqttConnectOptions();
// Note that the the Google Cloud IoT only supports MQTT 3.1.1, and Paho requires that we
// explicitly set this. If you don't set MQTT version, the server will immediately close its
// connection to your device.
options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
options.setUserName(CloudIotOptions.UNUSED_ACCOUNT_NAME);
// generate the jwt password
options.setPassword(mqttAuth.createJwt(cloudIotOptions.getProjectId()));
mqttClient.connect(options);
mReady.set(true);
}
示例8: connectClient
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入依賴的package包/類
private void connectClient() {
try {
client = new MqttClient(broker, clientId);
client.setCallback(this);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setUserName(user);
connOpts.setPassword(password.toCharArray());
connOpts.setCleanSession(true);
connOpts.setKeepAliveInterval(OUTGOING_MQTT_KEEP_ALIVE);
logger.debug("Connecting to broker: " + broker);
client.connect(connOpts);
logger.debug("Connected");
} catch (MqttException e) {
logger.error("Failed to connect to MQTT client ( " + broker + "/" + clientId
+ ") for outbound messages");
logger.error(e.getLocalizedMessage());
e.printStackTrace();
}
}
示例9: startListening
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入依賴的package包/類
private void startListening() {
logger.debug("Starting listening for response traffic");
try {
String url =
cmdrespMqttBrokerProtocol + "://" + cmdrespMqttBroker + ":" + cmdrespMqttBrokerPort;
client = new MqttClient(url, cmdrespMqttClientId);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setUserName(cmdrespMqttUser);
connOpts.setPassword(cmdrespMqttPassword.toCharArray());
connOpts.setCleanSession(true);
connOpts.setKeepAliveInterval(cmdrespMqttKeepAlive);
logger.debug("Connecting to response message broker: " + cmdrespMqttBroker);
client.connect(connOpts);
logger.debug("Connected to response message broker");
client.setCallback(this);
client.subscribe(cmdrespMqttTopic, cmdrespMqttQos);
} catch (MqttException e) {
logger.error("Unable to connect to response message queue. "
+ "Unable to respond to command requests.");
e.printStackTrace();
client = null;
}
}
示例10: startListening
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入依賴的package包/類
private void startListening() {
logger.debug("Starting listening for incoming traffic");
try {
String url =
incomingMqttBrokerProtocol + "://" + incomingMqttBroker + ":" + incomingMqttBrokerPort;
client = new MqttClient(url, incomingMqttClientId);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setUserName(incomingMqttUser);
connOpts.setPassword(incomingMqttPassword.toCharArray());
connOpts.setCleanSession(true);
connOpts.setKeepAliveInterval(incomingMqttKeepAlive);
logger.debug("Connecting to incoming message broker: " + incomingMqttBroker);
client.connect(connOpts);
logger.debug("Connected to incoming message broker");
client.setCallback(this);
client.subscribe(incomingMqttTopic, incomingMqttQos);
} catch (MqttException e) {
logger.error("Unable to connect to incoming message queue.");
e.printStackTrace();
client = null;
}
}
示例11: connect
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入依賴的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();
}
}
示例12: buildMqttConnectOptions
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入依賴的package包/類
private MqttConnectOptions buildMqttConnectOptions(AbstractAwsIotClient client, SocketFactory socketFactory) {
MqttConnectOptions options = new MqttConnectOptions();
options.setSocketFactory(socketFactory);
options.setCleanSession(true);
options.setConnectionTimeout(client.getConnectionTimeout() / 1000);
options.setKeepAliveInterval(client.getKeepAliveInterval() / 1000);
Set<String> serverUris = getServerUris();
if (serverUris != null && !serverUris.isEmpty()) {
String[] uriArray = new String[serverUris.size()];
serverUris.toArray(uriArray);
options.setServerURIs(uriArray);
}
if (client.getWillMessage() != null) {
AWSIotMessage message = client.getWillMessage();
options.setWill(message.getTopic(), message.getPayload(), message.getQos().getValue(), false);
}
return options;
}
示例13: refusedBadUsernamePassword
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入依賴的package包/類
@Test
public void refusedBadUsernamePassword(TestContext context) {
this.expectedReturnCode = MqttConnectReturnCode.CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD;
try {
MemoryPersistence persistence = new MemoryPersistence();
MqttConnectOptions options = new MqttConnectOptions();
options.setUserName("wrong_username");
options.setPassword("wrong_password".toCharArray());
MqttClient client = new MqttClient(String.format("tcp://%s:%d", MQTT_SERVER_HOST, MQTT_SERVER_PORT), "12345", persistence);
client.connect(options);
context.fail();
} catch (MqttException e) {
context.assertTrue(e.getReasonCode() == MqttException.REASON_CODE_FAILED_AUTHENTICATION);
}
}
示例14: refusedUnacceptableProtocolVersion
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入依賴的package包/類
@Test
public void refusedUnacceptableProtocolVersion(TestContext context) {
this.expectedReturnCode = MqttConnectReturnCode.CONNECTION_REFUSED_UNACCEPTABLE_PROTOCOL_VERSION;
try {
MemoryPersistence persistence = new MemoryPersistence();
MqttConnectOptions options = new MqttConnectOptions();
// trying the old 3.1
options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1);
MqttClient client = new MqttClient(String.format("tcp://%s:%d", MQTT_SERVER_HOST, MQTT_SERVER_PORT), "12345", persistence);
client.connect(options);
context.fail();
} catch (MqttException e) {
context.assertTrue(e.getReasonCode() == MqttException.REASON_CODE_INVALID_PROTOCOL_VERSION);
}
}
示例15: refusedClientIdZeroBytes
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入依賴的package包/類
@Test
public void refusedClientIdZeroBytes(TestContext context) {
this.expectedReturnCode = MqttConnectReturnCode.CONNECTION_REFUSED_IDENTIFIER_REJECTED;
try {
MemoryPersistence persistence = new MemoryPersistence();
MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(false);
options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
MqttClient client = new MqttClient(String.format("tcp://%s:%d", MQTT_SERVER_HOST, MQTT_SERVER_PORT), "", persistence);
client.connect(options);
context.fail();
} catch (MqttException e) {
context.assertTrue(e.getReasonCode() == MqttException.REASON_CODE_INVALID_CLIENT_ID);
context.assertNotNull(rejection);
}
}