I wrote a simple program below to recieve messages using pubnub in Java. The problem I am facing is I am unable to retrieve timetoken
of the received message. This callback is never called although it exists in the API
@Override
public void successCallback(String channel, Object message, String timeToken){
System.out.println("SUBSCRIBE : " + channel + " : " + message.getClass() + " : " + message.toString());
System.out.println(timeToken);
}
The callback without timetoken
is called though. Can anyone tell me how can I retrieve the timetoken of the message.
import java.io.IOException;
import com.pubnub.api.*;
import org.json.*;
public class Main{
public static void main(String[] args){
Pubnub pubnub =
new Pubnub("demo", "demo");
pubnub.setUUID("1");
try{
pubnub.subscribe("test", new Callback(){
@Override
public void connectCallback(String channel, Object message){
System.out.println("SUBSCRIBE : CONNECT on channel:" + channel + " : " + message.getClass() + " : "
+ message.toString());
}
@Override
public void disconnectCallback(String channel, Object message){
System.out.println("SUBSCRIBE : DISCONNECT on channel:" + channel + " : " + message.getClass() + " : "
+ message.toString());
}
public void reconnectCallback(String channel, Object message){
System.out.println("SUBSCRIBE : RECONNECT on channel:" + channel + " : " + message.getClass() + " : "
+ message.toString());
}
@Override
public void successCallback(String channel, Object message){
System.out.println("SUBSCRIBE : " + channel + " : " + message);
}
@Override
public void successCallback(String channel, Object message, String timeToken){
System.out.println("SUBSCRIBE : " + channel + " : " + message.getClass() + " : " + message.toString());
System.out.println(timeToken);
}
@Override
public void errorCallback(String channel, PubnubError error){
System.out.println("SUBSCRIBE : ERROR on channel " + channel + " : " + error.toString());
}
});
pubnub.history("akosha1", 100, new Callback(){
public void successCallback(String channel, Object response){
System.out.println(response.toString());
}
public void errorCallback(String channel, PubnubError error){
System.out.println(error.toString());
}
});
try{
System.in.read();
} catch (IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (PubnubException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
You can get the timetoken easily by building the java sdk jar yourself and altering the file PubnubCore.java and altering the line number 2617 to this :
if (!isWorkerDead(hreq))
callback.successWrapperCallback(channel, PubnubUtil.parseJSON(message), timeToken);
and build the pubnub jar again.
and then you can call
@Override
public void successCallback(String channel, Object message, String timetoken) {
System.out.println("PUBNUB:"+channel + " : "
+ message.getClass() + " : " + timetoken+" : "+message.toString());
}
I can share the updated jar if you want to skip this process.