本文整理汇总了Java中org.fusesource.mqtt.client.MQTT.setCleanSession方法的典型用法代码示例。如果您正苦于以下问题:Java MQTT.setCleanSession方法的具体用法?Java MQTT.setCleanSession怎么用?Java MQTT.setCleanSession使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.fusesource.mqtt.client.MQTT
的用法示例。
在下文中一共展示了MQTT.setCleanSession方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createMQTTSslConnection
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
private MQTT createMQTTSslConnection(String clientId, boolean clean) throws Exception {
MQTT mqtt = new MQTT();
mqtt.setConnectAttemptsMax(1);
mqtt.setReconnectAttemptsMax(0);
mqtt.setTracer(createTracer());
mqtt.setHost("ssl://localhost:" + port);
if (clientId != null) {
mqtt.setClientId(clientId);
}
mqtt.setCleanSession(clean);
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(new KeyManager[0], new TrustManager[]{new DefaultTrustManager()}, new SecureRandom());
mqtt.setSslContext(ctx);
return mqtt;
}
示例2: mqttClient
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
protected static MQTT mqttClient(String host, int port) {
MQTT client = new MQTT();
client.setCleanSession(true);
client.setClientId("amc-" + Thread.currentThread().getId() + "-"
+ System.currentTimeMillis());
client.setHost(URIs.newURI("tcp://" + host + ":" + port));
return client;
}
示例3: setup
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
@Override
public void setup(OperatorContext context)
{
try {
client = new MQTT();
if (mqttClientConfig.getClientId() != null) {
client.setClientId(mqttClientConfig.getClientId());
}
client.setCleanSession(mqttClientConfig.isCleanSession());
client.setConnectAttemptsMax(mqttClientConfig.getConnectAttemptsMax());
client.setHost(mqttClientConfig.getHost(), mqttClientConfig.getPort());
client.setKeepAlive(mqttClientConfig.getKeepAliveInterval());
if (mqttClientConfig.getPassword() != null) {
client.setPassword(mqttClientConfig.getPassword());
}
if (mqttClientConfig.getUserName() != null) {
client.setUserName(mqttClientConfig.getUserName());
}
if (mqttClientConfig.getWillMessage() != null) {
client.setWillMessage(mqttClientConfig.getWillMessage());
client.setWillQos(mqttClientConfig.getWillQos());
client.setWillRetain(mqttClientConfig.isWillRetain());
client.setWillTopic(mqttClientConfig.getWillTopic());
}
connection = client.blockingConnection();
connection.connect();
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
示例4: retrieveMQTTConnection
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
private static BlockingConnection retrieveMQTTConnection(String host, String truststorePath, String truststorePass, String keystorePath, String keystorePass) throws Exception {
MQTT mqtt = new MQTT();
mqtt.setConnectAttemptsMax(0);
mqtt.setReconnectAttemptsMax(0);
mqtt.setHost(host);
mqtt.setSslContext(SSLSupport.createContext("JKS", keystorePath, keystorePass, "JKS", truststorePath, truststorePass));
mqtt.setCleanSession(true);
BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
return connection;
}
示例5: createMQTTTcpConnection
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
private MQTT createMQTTTcpConnection(String clientId, boolean clean) throws Exception {
MQTT mqtt = new MQTT();
mqtt.setConnectAttemptsMax(1);
mqtt.setReconnectAttemptsMax(0);
mqtt.setTracer(createTracer());
mqtt.setVersion("3.1.1");
if (clientId != null) {
mqtt.setClientId(clientId);
}
mqtt.setCleanSession(clean);
mqtt.setHost("localhost", port);
return mqtt;
}
示例6: testValidZeroLengthClientId
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
@Test(timeout = 30 * 1000)
public void testValidZeroLengthClientId() throws Exception {
MQTT mqtt = createMQTTConnection();
mqtt.setClientId("");
mqtt.setCleanSession(true);
BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
connection.disconnect();
}
示例7: testSubscribeMultipleTopics
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
@Test(timeout = 30 * 10000)
public void testSubscribeMultipleTopics() throws Exception {
byte[] payload = new byte[1024 * 32];
for (int i = 0; i < payload.length; i++) {
payload[i] = '2';
}
MQTT mqtt = createMQTTConnection();
mqtt.setClientId("MQTT-Client");
mqtt.setCleanSession(false);
final BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
Topic[] topics = {new Topic("Topic/A", QoS.EXACTLY_ONCE), new Topic("Topic/B", QoS.EXACTLY_ONCE)};
Topic[] wildcardTopic = {new Topic("Topic/#", QoS.AT_LEAST_ONCE)};
connection.subscribe(wildcardTopic);
for (Topic topic : topics) {
connection.publish(topic.name().toString(), payload, QoS.AT_LEAST_ONCE, false);
}
int received = 0;
for (int i = 0; i < topics.length; ++i) {
Message message = connection.receive();
assertNotNull(message);
received++;
payload = message.getPayload();
String messageContent = new String(payload);
LOG.info("Received message from topic: " + message.getTopic() + " Message content: " + messageContent);
message.ack();
}
assertEquals("Should have received " + topics.length + " messages", topics.length, received);
}
示例8: createMQTTTcpConnection
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
private MQTT createMQTTTcpConnection(String clientId, boolean clean, int port) throws Exception {
MQTT mqtt = new MQTT();
mqtt.setConnectAttemptsMax(1);
mqtt.setReconnectAttemptsMax(0);
mqtt.setTracer(createTracer());
if (clientId != null) {
mqtt.setClientId(clientId);
}
mqtt.setCleanSession(clean);
mqtt.setHost("localhost", port);
return mqtt;
}
示例9: provideMqttConnection
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
@Provides
@Singleton
public CallbackConnection provideMqttConnection(final MQTTProperties mqttProperties) throws Exception {
MQTT mqtt = new MQTT();
mqtt.setHost(mqttProperties.getBrokerHost(), mqttProperties.getBrokerPort());
mqtt.setCleanSession(mqttProperties.isMqttcleanSession());
mqtt.setClientId(mqttProperties.getMqttClientId());
mqtt.setKeepAlive((short) mqttProperties.getMqttKeepAlive());
mqtt.setUserName(mqttProperties.getMqttUsername());
mqtt.setPassword(mqttProperties.getMqttPassword());
return mqtt.callbackConnection();
}
示例10: MqttFactory
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
public MqttFactory() {
mqtt = new MQTT();
mqtt.setCleanSession(true);
}
示例11: activate
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
@Override
public void activate(OperatorContext context)
{
try {
client = new MQTT();
if (mqttClientConfig.getClientId() != null) {
client.setClientId(mqttClientConfig.getClientId());
}
client.setCleanSession(mqttClientConfig.isCleanSession());
client.setConnectAttemptsMax(mqttClientConfig.getConnectAttemptsMax());
client.setHost(mqttClientConfig.getHost(), mqttClientConfig.getPort());
client.setKeepAlive(mqttClientConfig.getKeepAliveInterval());
if (mqttClientConfig.getPassword() != null) {
client.setPassword(mqttClientConfig.getPassword());
}
if (mqttClientConfig.getUserName() != null) {
client.setUserName(mqttClientConfig.getUserName());
}
if (mqttClientConfig.getWillMessage() != null) {
client.setWillMessage(mqttClientConfig.getWillMessage());
client.setWillQos(mqttClientConfig.getWillQos());
client.setWillRetain(mqttClientConfig.isWillRetain());
}
if (mqttClientConfig.getWillTopic() != null) {
client.setWillTopic(mqttClientConfig.getWillTopic());
}
initializeConnection();
thread = new Thread(new Runnable()
{
@Override
public void run()
{
while (true) {
try {
Message msg = connection.receive();
holdingBuffer.add(msg);
} catch (Exception ex) {
LOG.error("Trouble receiving", ex);
}
}
}
});
thread.start();
} catch (Exception ex) {
LOG.error("Caught exception during activation: ", ex);
throw new RuntimeException(ex);
}
}
示例12: testRetainedMessage
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
@Test(timeout = 120 * 1000)
public void testRetainedMessage() throws Exception {
MQTT mqtt = createMQTTConnection();
mqtt.setKeepAlive((short) 60);
final String RETAIN = "RETAIN";
final String TOPICA = "TopicA";
final String[] clientIds = {null, "foo", "durable"};
for (String clientId : clientIds) {
LOG.info("Testing now with Client ID: {}", clientId);
mqtt.setClientId(clientId);
mqtt.setCleanSession(!"durable".equals(clientId));
BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
// set retained message and check
connection.publish(TOPICA, RETAIN.getBytes(), QoS.EXACTLY_ONCE, true);
connection.subscribe(new Topic[]{new Topic(TOPICA, QoS.AT_LEAST_ONCE)});
Message msg = connection.receive(5000, TimeUnit.MILLISECONDS);
assertNotNull("No retained message for " + clientId, msg);
assertEquals(RETAIN, new String(msg.getPayload()));
msg.ack();
assertNull(connection.receive(500, TimeUnit.MILLISECONDS));
// test duplicate subscription
connection.subscribe(new Topic[]{new Topic(TOPICA, QoS.AT_LEAST_ONCE)});
msg = connection.receive(15000, TimeUnit.MILLISECONDS);
assertNotNull("No retained message on duplicate subscription for " + clientId, msg);
assertEquals(RETAIN, new String(msg.getPayload()));
msg.ack();
assertNull(connection.receive(500, TimeUnit.MILLISECONDS));
connection.unsubscribe(new String[]{TOPICA});
// clear retained message and check that we don't receive it
connection.publish(TOPICA, "".getBytes(), QoS.AT_MOST_ONCE, true);
connection.subscribe(new Topic[]{new Topic(TOPICA, QoS.AT_LEAST_ONCE)});
msg = connection.receive(500, TimeUnit.MILLISECONDS);
assertNull("Retained message not cleared for " + clientId, msg);
connection.unsubscribe(new String[]{TOPICA});
// set retained message again and check
connection.publish(TOPICA, RETAIN.getBytes(), QoS.EXACTLY_ONCE, true);
connection.subscribe(new Topic[]{new Topic(TOPICA, QoS.AT_LEAST_ONCE)});
msg = connection.receive(5000, TimeUnit.MILLISECONDS);
assertNotNull("No reset retained message for " + clientId, msg);
assertEquals(RETAIN, new String(msg.getPayload()));
msg.ack();
assertNull(connection.receive(500, TimeUnit.MILLISECONDS));
// re-connect and check
connection.disconnect();
connection = mqtt.blockingConnection();
connection.connect();
connection.subscribe(new Topic[]{new Topic(TOPICA, QoS.AT_LEAST_ONCE)});
msg = connection.receive(5000, TimeUnit.MILLISECONDS);
assertNotNull("No reset retained message for " + clientId, msg);
assertEquals(RETAIN, new String(msg.getPayload()));
msg.ack();
assertNull(connection.receive(500, TimeUnit.MILLISECONDS));
connection.unsubscribe(new String[]{TOPICA});
connection.disconnect();
}
}
示例13: testRetainedMessageOnVirtualTopics
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
@Ignore
@Test(timeout = 120 * 1000)
public void testRetainedMessageOnVirtualTopics() throws Exception {
MQTT mqtt = createMQTTConnection();
mqtt.setKeepAlive((short) 60);
final String RETAIN = "RETAIN";
final String TOPICA = "VirtualTopic/TopicA";
final String[] clientIds = {null, "foo", "durable"};
for (String clientId : clientIds) {
LOG.info("Testing now with Client ID: {}", clientId);
mqtt.setClientId(clientId);
mqtt.setCleanSession(!"durable".equals(clientId));
BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
// set retained message and check
connection.publish(TOPICA, RETAIN.getBytes(), QoS.EXACTLY_ONCE, true);
connection.subscribe(new Topic[]{new Topic(TOPICA, QoS.AT_LEAST_ONCE)});
Message msg = connection.receive(5000, TimeUnit.MILLISECONDS);
assertNotNull("No retained message for " + clientId, msg);
assertEquals(RETAIN, new String(msg.getPayload()));
msg.ack();
assertNull(connection.receive(500, TimeUnit.MILLISECONDS));
// test duplicate subscription
connection.subscribe(new Topic[]{new Topic(TOPICA, QoS.AT_LEAST_ONCE)});
msg = connection.receive(15000, TimeUnit.MILLISECONDS);
assertNotNull("No retained message on duplicate subscription for " + clientId, msg);
assertEquals(RETAIN, new String(msg.getPayload()));
msg.ack();
assertNull(connection.receive(500, TimeUnit.MILLISECONDS));
connection.unsubscribe(new String[]{TOPICA});
// clear retained message and check that we don't receive it
connection.publish(TOPICA, "".getBytes(), QoS.AT_MOST_ONCE, true);
connection.subscribe(new Topic[]{new Topic(TOPICA, QoS.AT_LEAST_ONCE)});
msg = connection.receive(500, TimeUnit.MILLISECONDS);
assertNull("Retained message not cleared for " + clientId, msg);
connection.unsubscribe(new String[]{TOPICA});
// set retained message again and check
connection.publish(TOPICA, RETAIN.getBytes(), QoS.EXACTLY_ONCE, true);
connection.subscribe(new Topic[]{new Topic(TOPICA, QoS.AT_LEAST_ONCE)});
msg = connection.receive(5000, TimeUnit.MILLISECONDS);
assertNotNull("No reset retained message for " + clientId, msg);
assertEquals(RETAIN, new String(msg.getPayload()));
msg.ack();
assertNull(connection.receive(500, TimeUnit.MILLISECONDS));
// re-connect and check
connection.disconnect();
connection = mqtt.blockingConnection();
connection.connect();
connection.subscribe(new Topic[]{new Topic(TOPICA, QoS.AT_LEAST_ONCE)});
msg = connection.receive(5000, TimeUnit.MILLISECONDS);
assertNotNull("No reset retained message for " + clientId, msg);
assertEquals(RETAIN, new String(msg.getPayload()));
msg.ack();
assertNull(connection.receive(500, TimeUnit.MILLISECONDS));
LOG.info("Test now unsubscribing from: {} for the last time", TOPICA);
connection.unsubscribe(new String[]{TOPICA});
connection.disconnect();
}
}
示例14: sendSampleMQTTMessage
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
/**
* Seinding message ot type MQTT do not work properly with version 3.1.5 of RabbitMQ, See https://github.com/rabbitmq/rabbitmq-mqtt/issues/5
*
* @throws Exception
*/
private void sendSampleMQTTMessage() throws Exception {
final String topic = "public";
final String quote = "The force of mind is only as great as its expression; its depth only as deep as its power to expand and lose itself";
// MQTT Client
MQTT mqtt = new MQTT();
mqtt.setHost("localhost", 1883);
final BlockingConnection connection = mqtt.blockingConnection();
mqtt.setCleanSession(false);
connection.connect();
Thread.sleep(1000);
assertThat(connection.isConnected()).isTrue();
LOG.info("<eventadmin type='outbound'>");
LOG.info("\tTOPIC: {}", topic);
LOG.info("\tQuote: {}", quote);
LOG.info("</eventadmin>\n");
connection.publish(topic, quote.getBytes(), QoS.AT_MOST_ONCE, false);
}