Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Paho » How to check if a client has a continued persistent session with the broker?
How to check if a client has a continued persistent session with the broker? [message #1710539] Wed, 07 October 2015 12:29 Go to next message
prem singhania is currently offline prem singhaniaFriend
Messages: 2
Registered: October 2015
Junior Member
Hi,

I am writing a java client program that connects to mqtt apollo broker with clean session set to false and subscribes to some topic, but before connecting to the broker I want to check if some persistent session with the broker is already active. Is there some way to know this?

Thanks in advance
Re: How to check if a client has a continued persistent session with the broker? [message #1710552 is a reply to message #1710539] Wed, 07 October 2015 13:37 Go to previous messageGo to next message
James Sutton is currently offline James SuttonFriend
Messages: 71
Registered: July 2015
Member
Hi,

Yes, this is possible in the Java Client with MQTT Brokers that are 3.1.1 compliant.

Here is how you can do it:
public static void main(String[] args) {

        String topic        = "/test";
        String content      = "Message from MqttPublishSample";
        int qos             = 2;
        String broker       = "tcp://iot.eclipse.org:1883";
        String clientId     = "JavaSample";
        MemoryPersistence persistence = new MemoryPersistence();

        try {
            MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
            MqttConnectOptions connOpts = new MqttConnectOptions();
            connOpts.setCleanSession(false);
            System.out.println("Connecting to broker: "+broker);
            
            // Old method: sampleClient.connect(connOpts);
            // Change to:         
            IMqttToken result = sampleClient.connectWithResult(connOpts);
            // Returns true if session is present, false if it is not.
            System.out.println("Session Present: " + result.getSessionPresent());
            
            
            System.out.println("Connected");
            System.out.println("Publishing message: "+content);
            MqttMessage message = new MqttMessage(content.getBytes());
            message.setQos(qos);
            sampleClient.publish(topic, message);
            System.out.println("Message published");
            sampleClient.disconnect();
            System.out.println("Disconnected");
            System.exit(0);
        } catch(MqttException me) {
            System.out.println("reason "+me.getReasonCode());
            System.out.println("msg "+me.getMessage());
            System.out.println("loc "+me.getLocalizedMessage());
            System.out.println("cause "+me.getCause());
            System.out.println("excep "+me);
            me.printStackTrace();
        }
    }
Re: How to check if a client has a continued persistent session with the broker? [message #1710633 is a reply to message #1710552] Thu, 08 October 2015 05:21 Go to previous message
prem singhania is currently offline prem singhaniaFriend
Messages: 2
Registered: October 2015
Junior Member
Thanks for the quick reply james, I tried running your program as it is and I got the following errors
persistence.java:25: error: cannot find symbol
IMqttToken result = sampleClient.connectWithResult(connOpts);

symbol: method connectWithResult(MqttConnectOptions)
location: variable sampleClient of type MqttClient
persistence.java:27: error: cannot find symbol
System.out.println("Session Present: " + result.getSessionPresent());

symbol: method getSessionPresent()
location: variable result of type IMqttToken
2 errors

I think the jar that I am using is not correct, in that case can u please state the link from where I can download the latest paho jar?
Previous Topic:Link DLL to C project for an embedded system
Next Topic:WebSocket network error: The operation couldn't be completed. (OSStatus error -9807.)
Goto Forum:
  


Current Time: Wed Apr 24 18:24:53 GMT 2024

Powered by FUDForum. Page generated in 0.03384 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top