本文整理汇总了Java中org.eclipse.paho.client.mqttv3.persist.MemoryPersistence类的典型用法代码示例。如果您正苦于以下问题:Java MemoryPersistence类的具体用法?Java MemoryPersistence怎么用?Java MemoryPersistence使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MemoryPersistence类属于org.eclipse.paho.client.mqttv3.persist包,在下文中一共展示了MemoryPersistence类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getMqttClient
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; //导入依赖的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.persist.MemoryPersistence; //导入依赖的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;
}
}
示例3: connect
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; //导入依赖的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);
}
}
示例4: getPersistence
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; //导入依赖的package包/类
public MqttClientPersistence getPersistence() {
if (StringUtils.isEmpty(type) || type.equals("memory")) {
log.info("Initializing default memory persistence!");
return new MemoryPersistence();
} else if (type.equals("file")) {
if (StringUtils.isEmpty(path)) {
log.info("Initializing default file persistence!");
return new MqttDefaultFilePersistence();
} else {
log.info("Initializing file persistence using directory: {}", path);
return new MqttDefaultFilePersistence(path);
}
} else {
log.error("Unknown persistence option: {}. Only 'memory' and 'file' are supported at the moment!", type);
throw new IllegalArgumentException("Unknown persistence option: " + type + "!");
}
}
示例5: initializeMqttClient
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; //导入依赖的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);
}
示例6: mqttClientFactory
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; //导入依赖的package包/类
@Bean
public MqttPahoClientFactory mqttClientFactory() {
DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
factory.setServerURIs(mqttProperties.getUrl());
factory.setUserName(mqttProperties.getUsername());
factory.setPassword(mqttProperties.getPassword());
factory.setCleanSession(mqttProperties.isCleanSession());
factory.setConnectionTimeout(mqttProperties.getConnectionTimeout());
factory.setKeepAliveInterval(mqttProperties.getKeepAliveInterval());
if (ObjectUtils.nullSafeEquals(mqttProperties.getPersistence(), "file")) {
factory.setPersistence(new MqttDefaultFilePersistence(mqttProperties.getPersistenceDirectory()));
}
else if (ObjectUtils.nullSafeEquals(mqttProperties.getPersistence(), "memory")) {
factory.setPersistence(new MemoryPersistence());
}
return factory;
}
示例7: getPersistence
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; //导入依赖的package包/类
public MqttClientPersistence getPersistence() {
if (StringUtils.isEmpty(type) || type.equals("memory")) {
log.info("Initializing default memory persistence!");
return new MemoryPersistence();
} else if (type.equals("file")) {
if (StringUtils.isEmpty(path)) {
log.info("Initializing default file persistence!");
return new MqttDefaultFilePersistence();
} else {
log.info("Initializing file persistence using directory: {}", path);
return new MqttDefaultFilePersistence(path);
}
} else {
log.error("Unknown persistence option: {}. Only 'memory' and 'file' are supported at the moment!", type);
throw new IllegalArgumentException("Unknown persistence option: " + type + "!");
}
}
示例8: testMQtt
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; //导入依赖的package包/类
@Test
public void testMQtt() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
MqttClient client = new MqttClient("tcp://localhost:" + MQTT_PORT, MqttClient.generateClientId(), new MemoryPersistence());
client.connect();
MqttComponent mqtt = new MqttComponent();
mqtt.client = client;
Publisher<byte[]> fromTopic = mqtt.from("input", byte[].class);
Subscriber<byte[]> toTopic = mqtt.to("output", byte[].class);
Flux.from(fromTopic)
.log()
.subscribe(toTopic);
client.subscribe("output", (topic, message) -> {
result = new Integer(new String(message.getPayload()));
latch.countDown();
});
client.publish("input", new MqttMessage(new Integer(2).toString().getBytes()));
client.publish("input", new MqttMessage(new Integer(2).toString().getBytes()));
latch.await(100, TimeUnit.SECONDS);
Assert.assertEquals(2, result, 0.1);
client.disconnect();
client.close();
}
示例9: connect
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; //导入依赖的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();
}
}
示例10: MqttSession
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; //导入依赖的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);
}
}
示例11: mqttClientFactory
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; //导入依赖的package包/类
@Bean
public MqttPahoClientFactory mqttClientFactory() {
AuthConfiguration.AdminUser defaultUser = raptorConfiguration.getAuth().getServiceUser();
if (defaultUser == null) {
throw new RuntimeException("Missing service user. Review raptor.yml configuration file under auth.users section");
}
DispatcherConfiguration dispatcherConfig = raptorConfiguration.getDispatcher();
DefaultMqttPahoClientFactory f = new DefaultMqttPahoClientFactory();
log.debug("Using local broker user {}", defaultUser.getUsername());
f.setUserName(defaultUser.getUsername());
f.setPassword(defaultUser.getPassword());
f.setServerURIs(dispatcherConfig.getUri());
f.setCleanSession(true);
f.setPersistence(new MemoryPersistence());
return f;
}
示例12: AwsIotMqttConnection
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; //导入依赖的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);
}
}
示例13: disconnectedByClient
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; //导入依赖的package包/类
@Test
public void disconnectedByClient(TestContext context) {
Async async = context.async();
try {
MemoryPersistence persistence = new MemoryPersistence();
MqttClient client = new MqttClient(String.format("tcp://%s:%d", MQTT_SERVER_HOST, MQTT_SERVER_PORT), "12345", persistence);
client.connect();
client.disconnect();
// give more time to the MqttClient to update its connection state
this.vertx.setTimer(1000, t1 -> {
async.complete();
});
async.await();
context.assertTrue(!client.isConnected() && !this.endpoint.isConnected());
} catch (MqttException e) {
context.assertTrue(false);
e.printStackTrace();
}
}
示例14: refusedBadUsernamePassword
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; //导入依赖的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);
}
}
示例15: refusedUnacceptableProtocolVersion
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; //导入依赖的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);
}
}