Added script to test issue 9, publish of a QoS2 message
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
@GrabResolver(name='Paho', root='https://repo.eclipse.org/content/repositories/paho-releases/')
|
||||
@Grab(group='org.eclipse.paho', module='mqtt-client', version='0.4.0')
|
||||
|
||||
import org.eclipse.paho.client.mqttv3.MqttClient
|
||||
import org.eclipse.paho.client.mqttv3.MqttMessage
|
||||
import org.eclipse.paho.client.mqttv3.MqttException
|
||||
import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence
|
||||
|
||||
String host = args[0]
|
||||
|
||||
String tmpDir = System.getProperty("java.io.tmpdir")
|
||||
MqttDefaultFilePersistence dataStore = new MqttDefaultFilePersistence(tmpDir)
|
||||
|
||||
MqttClient client = new MqttClient("tcp://${host}:1883", "PublisherClient", dataStore)
|
||||
client.connect()
|
||||
MqttMessage message = new MqttMessage('Hello world!!'.bytes)
|
||||
message.setQos(0)
|
||||
print "publishing.."
|
||||
client.publish('log', 'Hello world!!'.bytes, 0, false)
|
||||
|
||||
client.publish('/exit', 'Exit'.bytes, 0, false)
|
||||
client.disconnect()
|
||||
println "disconnected"
|
||||
@@ -6,16 +6,18 @@ import org.eclipse.paho.client.mqttv3.MqttMessage
|
||||
import org.eclipse.paho.client.mqttv3.MqttException
|
||||
import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence
|
||||
|
||||
String host = args[0]
|
||||
|
||||
String tmpDir = System.getProperty("java.io.tmpdir")
|
||||
MqttDefaultFilePersistence dataStore = new MqttDefaultFilePersistence(tmpDir)
|
||||
|
||||
MqttClient client = new MqttClient("tcp://localhost:1883", "PublisherClient", dataStore)
|
||||
MqttClient client = new MqttClient("tcp://${host}:1883", "PublisherClient", dataStore)
|
||||
client.connect()
|
||||
MqttMessage message = new MqttMessage('Hello world!!'.bytes)
|
||||
message.setQos(0)
|
||||
print "publishing.."
|
||||
long startTime = System.currentTimeMillis()
|
||||
(1..1000000).each {
|
||||
(1..100000).each {
|
||||
client.publish('topic', 'Hello world!!'.bytes, 0, false)
|
||||
}
|
||||
client.publish('/exit', 'Exit'.bytes, 0, false)
|
||||
|
||||
@@ -51,10 +51,11 @@ class SubscriberCallback implements MqttCallback {
|
||||
}
|
||||
}
|
||||
|
||||
String host = args[0]
|
||||
String tmpDir = System.getProperty("java.io.tmpdir")
|
||||
MqttDefaultFilePersistence dataStore = new MqttDefaultFilePersistence(tmpDir)
|
||||
|
||||
MqttClient client = new MqttClient("tcp://localhost:1883", "SubscriberClient", dataStore)
|
||||
MqttClient client = new MqttClient("tcp://${host}:1883", "SubscriberClient", dataStore)
|
||||
def callback = new SubscriberCallback()
|
||||
client.callback = callback
|
||||
client.connect()
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
@GrabResolver(name='Paho', root='https://repo.eclipse.org/content/repositories/paho-releases/')
|
||||
@Grab(group='org.eclipse.paho', module='mqtt-client', version='0.4.0')
|
||||
|
||||
import org.eclipse.paho.client.mqttv3.MqttClient
|
||||
import org.eclipse.paho.client.mqttv3.MqttMessage
|
||||
import org.eclipse.paho.client.mqttv3.MqttException
|
||||
import org.eclipse.paho.client.mqttv3.MqttCallback
|
||||
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken
|
||||
import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence
|
||||
import java.util.concurrent.CountDownLatch
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
|
||||
|
||||
class SubscriberCallback implements MqttCallback {
|
||||
|
||||
int m_numReceived = 0
|
||||
long m_startTime
|
||||
boolean firstMessageReceived = false
|
||||
|
||||
private CountDownLatch m_latch = new CountDownLatch(1)
|
||||
|
||||
void waitFinish() {
|
||||
m_latch.await()
|
||||
}
|
||||
|
||||
public void connectionLost(Throwable cause) {
|
||||
m_latch.countDown()
|
||||
}
|
||||
|
||||
public void messageArrived(String topic, MqttMessage message) throws Exception {
|
||||
//println "Received message ${new String(message.payload)} on topic [${topic}]"
|
||||
if (!firstMessageReceived) {
|
||||
m_startTime = System.currentTimeMillis()
|
||||
firstMessageReceived = true
|
||||
}
|
||||
if (topic == "/exit") {
|
||||
long stopTime = System.currentTimeMillis()
|
||||
long spentTime = stopTime - m_startTime
|
||||
println "/exit received"
|
||||
println "subscriber disconnected, received ${m_numReceived} messages in ${spentTime} ms"
|
||||
client.disconnect()
|
||||
m_latch.countDown()
|
||||
} else {
|
||||
println "Received $message on topic $topic"
|
||||
m_numReceived++
|
||||
}
|
||||
}
|
||||
|
||||
public void deliveryComplete(IMqttDeliveryToken token) {
|
||||
//To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
}
|
||||
|
||||
String host = args[0]
|
||||
String topicSubscription = args[1]
|
||||
String tmpDir = System.getProperty("java.io.tmpdir")
|
||||
MqttDefaultFilePersistence dataStore = new MqttDefaultFilePersistence(tmpDir)
|
||||
|
||||
MqttClient client = new MqttClient("tcp://${host}:1883", "SubscriberClient", dataStore)
|
||||
def callback = new SubscriberCallback()
|
||||
client.callback = callback
|
||||
client.connect()
|
||||
client.subscribe(topicSubscription, 0)
|
||||
println "subscribed to $topicSubscription"
|
||||
client.subscribe("/exit", 0)
|
||||
println "subscribed to /exit"
|
||||
|
||||
callback.waitFinish()
|
||||
Reference in New Issue
Block a user