本文整理了Java中org.fusesource.mqtt.client.MQTT.setKeepAlive()
方法的一些代码示例,展示了MQTT.setKeepAlive()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MQTT.setKeepAlive()
方法的具体详情如下:
包路径:org.fusesource.mqtt.client.MQTT
类名称:MQTT
方法名:setKeepAlive
暂无
代码示例来源:origin: fusesource/mqtt-client
main.mqtt.setHost(shift(argl));
} else if ("-k".equals(arg)) {
main.mqtt.setKeepAlive(Short.parseShort(shift(argl)));
} else if ("-c".equals(arg)) {
main.mqtt.setCleanSession(false);
代码示例来源:origin: fusesource/mqtt-client
main.mqtt.setHost(shift(argl));
} else if ("-k".equals(arg)) {
main.mqtt.setKeepAlive(Short.parseShort(shift(argl)));
} else if ("-c".equals(arg)) {
main.mqtt.setCleanSession(false);
代码示例来源:origin: apache/activemq-artemis
@Override
public void setKeepAlive(int keepAlive) throws Exception {
mqtt.setKeepAlive((short) keepAlive);
}
}
代码示例来源:origin: apache/activemq-artemis
@Test(timeout = 60 * 1000)
public void testRepeatedLinkStealing() throws Exception {
final String clientId = "duplicateClient";
final AtomicReference<BlockingConnection> oldConnection = new AtomicReference<>();
final String TOPICA = "TopicA";
for (int i = 1; i <= 10; ++i) {
LOG.info("Creating MQTT Connection {}", i);
MQTT mqtt = createMQTTConnection(clientId, false);
mqtt.setKeepAlive((short) 2);
final BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
connection.publish(TOPICA, TOPICA.getBytes(), QoS.EXACTLY_ONCE, true);
assertTrue("Client connect failed for attempt: " + i, Wait.waitFor(() -> connection.isConnected(), 3000, 200));
if (oldConnection.get() != null) {
assertTrue("Old client still connected on attempt: " + i, Wait.waitFor(() -> !oldConnection.get().isConnected(), 3000, 200));
}
oldConnection.set(connection);
}
oldConnection.get().publish(TOPICA, TOPICA.getBytes(), QoS.EXACTLY_ONCE, true);
oldConnection.get().disconnect();
}
代码示例来源:origin: apache/activemq-artemis
mqtt.setKeepAlive((short) 2);
final BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
代码示例来源:origin: apache/activemq-artemis
@Test(timeout = 60 * 1000)
public void testDuplicateClientId() throws Exception {
final String clientId = "duplicateClient";
MQTT mqtt = createMQTTConnection(clientId, false);
mqtt.setKeepAlive((short) 2);
final BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
final String TOPICA = "TopicA";
connection.publish(TOPICA, TOPICA.getBytes(), QoS.EXACTLY_ONCE, true);
MQTT mqtt1 = createMQTTConnection(clientId, false);
mqtt1.setKeepAlive((short) 2);
final BlockingConnection connection1 = mqtt1.blockingConnection();
connection1.connect();
assertTrue("Duplicate client disconnected", Wait.waitFor(() -> connection1.isConnected()));
assertTrue("Old client still connected", Wait.waitFor(() -> !connection.isConnected()));
connection1.publish(TOPICA, TOPICA.getBytes(), QoS.EXACTLY_ONCE, true);
connection1.disconnect();
}
代码示例来源:origin: apache/activemq-artemis
@Test(timeout = 60 * 1000)
public void testClientDisconnectedOnMaxConsumerLimitReached() throws Exception {
Exception peerDisconnectedException = null;
try {
String clientId = "test.client";
SimpleString coreAddress = new SimpleString("foo.bar");
Topic[] mqttSubscription = new Topic[]{new Topic("foo/bar", QoS.AT_LEAST_ONCE)};
getServer().createQueue(coreAddress, RoutingType.MULTICAST, new SimpleString(clientId + "." + coreAddress), null, false, true, 0, false, true);
MQTT mqtt = createMQTTConnection();
mqtt.setClientId(clientId);
mqtt.setKeepAlive((short) 2);
final BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
connection.subscribe(mqttSubscription);
} catch (EOFException e) {
peerDisconnectedException = e;
}
assertNotNull(peerDisconnectedException);
assertTrue(peerDisconnectedException.getMessage().contains("Peer disconnected"));
}
代码示例来源:origin: apache/activemq-artemis
@Test(timeout = 60 * 1000)
public void testPingKeepsInactivityMonitorAlive() throws Exception {
MQTT mqtt = createMQTTConnection();
mqtt.setClientId("foo");
mqtt.setKeepAlive((short) 2);
final BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
assertTrue("KeepAlive didn't work properly", Wait.waitFor(() -> connection.isConnected()));
connection.disconnect();
}
代码示例来源:origin: apache/activemq-artemis
@Test(timeout = 30 * 1000)
public void testDefaultKeepAliveWhenClientSpecifiesZero() throws Exception {
stopBroker();
protocolConfig = "transport.defaultKeepAlive=2000";
startBroker();
MQTT mqtt = createMQTTConnection();
mqtt.setClientId("foo");
mqtt.setKeepAlive((short) 0);
final BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
assertTrue("KeepAlive didn't work properly", Wait.waitFor(() -> connection.isConnected()));
}
代码示例来源:origin: apache/activemq-artemis
@Test(timeout = 60 * 1000)
public void testTurnOffInactivityMonitor() throws Exception {
stopBroker();
protocolConfig = "transport.useInactivityMonitor=false";
startBroker();
MQTT mqtt = createMQTTConnection();
mqtt.setClientId("foo3");
mqtt.setKeepAlive((short) 2);
final BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
assertTrue("KeepAlive didn't work properly", Wait.waitFor(() -> connection.isConnected()));
connection.disconnect();
}
代码示例来源:origin: apache/activemq-artemis
@Test(timeout = 60 * 1000)
public void testPingOnMQTT() throws Exception {
stopBroker();
protocolConfig = "maxInactivityDuration=-1";
startBroker();
MQTT mqtt = createMQTTConnection();
mqtt.setClientId("test-mqtt");
mqtt.setKeepAlive((short) 2);
final BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
assertTrue("KeepAlive didn't work properly", Wait.waitFor(() -> connection.isConnected()));
connection.disconnect();
}
代码示例来源:origin: apache/activemq-artemis
MQTT mqtt = createMQTTConnection();
mqtt.setClientId("foo");
mqtt.setKeepAlive((short) 20);
代码示例来源:origin: apache/activemq-artemis
final String clientId = "publishDollar";
mqtt.setClientId(clientId);
mqtt.setKeepAlive((short) 2);
BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
mqtt.setKeepAlive((short) 2);
connection = mqtt.blockingConnection();
connection.connect();
代码示例来源:origin: apache/activemq-artemis
mqtt.setKeepAlive((short) 2);
代码示例来源:origin: org.apache.apex/malhar-contrib
client.setConnectAttemptsMax(mqttClientConfig.getConnectAttemptsMax());
client.setHost(mqttClientConfig.getHost(), mqttClientConfig.getPort());
client.setKeepAlive(mqttClientConfig.getKeepAliveInterval());
if (mqttClientConfig.getPassword() != null) {
client.setPassword(mqttClientConfig.getPassword());
代码示例来源:origin: apache/activemq-artemis
@Test(timeout = 60 * 1000)
public void testClientConnectionFailure() throws Exception {
MQTT mqtt = createMQTTConnection("reconnect", false);
mqtt.setKeepAlive((short) 1);
final BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
Wait.waitFor(() -> connection.isConnected());
final String TOPIC = "TopicA";
final byte[] qos = connection.subscribe(new Topic[]{new Topic(TOPIC, QoS.EXACTLY_ONCE)});
assertEquals(QoS.EXACTLY_ONCE.ordinal(), qos[0]);
connection.publish(TOPIC, TOPIC.getBytes(), QoS.EXACTLY_ONCE, false);
// kill transport
connection.kill();
// FIXME Wait for the previous connection to timeout. This is not required in ActiveMQ. Needs investigating.
Thread.sleep(10000);
final BlockingConnection newConnection = mqtt.blockingConnection();
newConnection.connect();
Wait.waitFor(() -> newConnection.isConnected());
assertEquals(QoS.EXACTLY_ONCE.ordinal(), qos[0]);
Message msg = newConnection.receive(1000, TimeUnit.MILLISECONDS);
assertNotNull(msg);
assertEquals(TOPIC, new String(msg.getPayload()));
msg.ack();
newConnection.disconnect();
}
代码示例来源:origin: apache/apex-malhar
@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);
}
}
代码示例来源:origin: apache/activemq-artemis
@Test(timeout = 60 * 1000)
public void testClientConnectionFailureSendsWillMessage() throws Exception {
getServer().createQueue(SimpleString.toSimpleString("will"), RoutingType.MULTICAST, SimpleString.toSimpleString("will"), null, true, false);
MQTT mqtt = createMQTTConnection("1", false);
mqtt.setKeepAlive((short) 1);
mqtt.setWillMessage("test message");
mqtt.setWillTopic("will");
mqtt.setWillQos(QoS.AT_LEAST_ONCE);
final BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
Wait.waitFor(() -> connection.isConnected());
MQTT mqtt2 = createMQTTConnection("2", false);
BlockingConnection connection2 = mqtt2.blockingConnection();
connection2.connect();
connection2.subscribe(new Topic[]{new Topic("will", QoS.AT_LEAST_ONCE)});
// kill transport
connection.kill();
// FIXME Wait for the previous connection to timeout. This is not required in ActiveMQ. Needs investigating.
Thread.sleep(10000);
Message m = connection2.receive(1000, TimeUnit.MILLISECONDS);
assertEquals("test message", new String(m.getPayload()));
}
代码示例来源:origin: apache/activemq-artemis
public void testRetainedMessageOnVirtualTopics() throws Exception {
MQTT mqtt = createMQTTConnection();
mqtt.setKeepAlive((short) 60);
代码示例来源:origin: apache/activemq-artemis
@Test(timeout = 60 * 1000)
public void testWillMessageIsRetained() throws Exception {
getServer().createQueue(SimpleString.toSimpleString("will"), RoutingType.MULTICAST, SimpleString.toSimpleString("will"), null, true, false);
MQTT mqtt = createMQTTConnection("1", false);
mqtt.setKeepAlive((short) 1);
mqtt.setWillMessage("test message");
mqtt.setWillTopic("will");
mqtt.setWillQos(QoS.AT_LEAST_ONCE);
mqtt.setWillRetain(true);
final BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
Wait.waitFor(() -> connection.isConnected());
// kill transport
connection.kill();
Thread.sleep(10000);
MQTT mqtt2 = createMQTTConnection("2", false);
BlockingConnection connection2 = mqtt2.blockingConnection();
connection2.connect();
connection2.subscribe(new Topic[]{new Topic("will", QoS.AT_LEAST_ONCE)});
Message m = connection2.receive(1000, TimeUnit.MILLISECONDS);
assertNotNull(m);
m.ack();
assertEquals("test message", new String(m.getPayload()));
}
内容来源于网络,如有侵权,请联系作者删除!