I am trying to find the clients subscribed to a particulair channel. On git I found an example which used a function here_now() with 2 parameters. Something like this:
pubnub.hereNow(channel, new Callback() {
public void successCallback(String channel,Object message) {
notifyUser("HERE NOW : " + message);
}
public void errorCallback(String channel,
Object message) {
notifyUser("HERE NOW : " + message);
}
});
But in my company's application the here_now() function takes only one parameter i.e a String. I am relatively new to pubnub. Can anyone explain me what this parameter is? is it a channel name? how do I specify the call back functions? I am not able to find any documentation related to here_now(String arg)
Thanks
Please run the PubNubDemoConsole.java... you can walk through how all the API calls work.
Specifically, for here_now():
https://github.com/pubnub/java/blob/master/java/examples/src/com/pubnub/examples/PubnubDemoConsole.java#L157
(Looks a lot like above :)
You are indeed passing it two arguments. The first is the channel name you want stats for, the second parameter is the callback function to deliver the success or error results on.
Be sure you are on the latest PubNub for Java version as well:
https://github.com/pubnub/java/blob/master/java
If there are additional questions, let us know here, or via support at pubnub.com
geremy
Related
I am writing in Java on the Vertx framework, and I have an architecture question regarding blocking code.
I have a JsonObject which consists of 10 objects, like so:
{
"system":"CD0",
"system":"CD1",
"system":"CD2",
"system":"CD3",
"system":"CD4",
"system":"CD5",
"system":"CD6",
"system":"CD7",
"system":"CD8",
"system":"CD9"
}
I also have a synchronous function which gets an object from the JsonObject, and consumes a SOAP web service, while sending the object to it.
the SOAP Web service gets the content (e.g. CD0), and after a few seconds returns an Enum.
I then want to take that enum value returned, and save it in some sort of data variable(like hash table).
What I ultimately want is a function that will iterate over all the JsonObject's objects, and for each one, run the blocking code, in parallel.
I want it to run in parallel so even if one of the calls to the function needs to wait 20 seconds, it won't stuck the other calls.
how can I do such a thing in vertx?
p.s: I will appreciate if you will correct mistakes I wrote.
Why not to use rxJava and "zip" separate calls? Vertx has great support for rxJava too. Assuming that you are calling 10 times same method with different String argument and returning another String you could do something like this:
private Single<String> callWs(String arg) {
return Single.fromCallable(() -> {
//DO CALL WS
return "yourResult";
});
}
and then just use it with some array of arguments:
String[] array = new String[10]; //get your arguments
List<Single<String>> wsCalls = new ArrayList<>();
for (String s : array) {
wsCalls.add(callWs(s));
}
Single.zip(wsCalls, r -> r).subscribe(allYourResults -> {
// do whatever you like with resutls
});
More about zip function and reactive programming in general: reactivex.io
I have been battling this code the whole day without luck. I started by following this code sample from Google.
The problem is that the folder gets created successfully but inside onResult(), I always get a DriveId or resourceId that is invalid or incomplete. That means I cannot create a file inside the folder I created. Here is the code I am using:
public class CreateFolderActivity extends BaseDemoActivity {
#Override
public void onConnected(Bundle connectionHint) {
super.onConnected(connectionHint);
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("MyAppFolder").build();
Drive.DriveApi.getRootFolder(getGoogleApiClient()).createFolder(
getGoogleApiClient(), changeSet).setResultCallback(callback);
}
final ResultCallback<DriveFolderResult> callback = new ResultCallback<DriveFolderResult>() {
#Override
public void onResult(DriveFolderResult result) {
if (!result.getStatus().isSuccess()) {
showMessage("Error while trying to create the folder");
return;
}
// this value is always invalid with ending == operators
Log.d("DRIVEID", "Created a folder: " + result.getDriveFolder().getDriveId());
}
};
}
Whenever I run this code, I get the following id which appears incomplete:
CAESABi2VyCCzdWOuVMoAQ==
I don't know what is happening here!
I have Google'd around and read of adding listeners to listen for completion events but none of them seem to work.
I have seen nearly similar questions on SO on this but none of them work for me.
I manually copied the FolderId through my browser after the app created it and then pasted to my android code and the app created a file successfully. But this is not how things should work.
Am I suppose to wait for the sync to complete and if so, how?
Thank you in advance!
The answer to your problem can probably be found here. The DriveId you're getting is OK, but you should not handle it directly. It is a 'preliminary' DriveId that changes after the object has been committed (again, see SO 22874657). You can test it comparing DriveId you're getting vs. DriveId you'll get in 'onCompletion(CompletionEvent event)'.
This is just one of the side effects of GDAA's logic, shielding you from on-line / off-line network state resulting in unpredictable delays. You just have to rely on callbacks.
But I am surprised that you can't use this 'preliminary' DriveId (in case of a folder) immediately as a parent of another object (folder/file). I have never experienced it, passing the 'preliminary' DriveId immediately to another GDAA method.
It is different in case of the ResourceId. That one is secondary in the GDAA and is used only if you go outside of the device. It is not known to the GDAA until the object is committed (uploaded).
I used similar logic (creating folder / file tree) in this demo (see MainActivity.createTree() method). You're welcome to dig in it.
There is a related problem discussed in SO 34318220.
Good Luck
I'm trying to make dialog that will display an error message whenever I make wrong move in my scrabble game. So in Problem.java, I make it like this
class Problem
{
Problem(String s)
{
message = s;
}
}
So I write code to display the warning like this :
void displayProblem(Problem p)
{
JOptionPane.showMessageDialog(this,p, "WARNING!",JOptionPane.WARNING_MESSAGE);
}
I expect error message when I don't put tile something like this :
"no tiles placed"
just like what's in the code but it ended up like this :
What's wrong with my code anyway?
You either need to pass p.message to the dialog or override Problem's toString() method and return message there. What you're seeing is the output of standard toString(), i.e. class name + instance id.
Btw, you posted a lot of irrelevant code, which might make a lot of people want to either close the question or prevent them from trying to answer. When asking questions you should try and boil it down to the relevant parts, which in your case would be how you display the dialog and what the parameters look like. For more information, have a look here: https://stackoverflow.com/help/how-to-ask
I'm currently working on a project that need to communicate with java from air without the use of a server like Tomcat. For this i found and use Flerry.
Communicating between Java and Air is no problem, as long as I try to send a message from the class that I initially instantiated from Air.
What I try to do is subscript to messages from a Message Class in java and use that class to send info and errors to Air from Java.
MessageController.java:
public class MessageController
{
public MessageController()
{
}
public static void sendErrorMessage(String errorMessage)
{
NativeObject.sendMessage(errorMessage, "error");
}
public static void sendInfoMessage(String infoMessage)
{
NativeObject.sendMessage(infoMessage, "info");
}
}
In Air I create a NativeObject on the messaging class and subscribe to the messages:
var messageController:NativeObject = new NativeObject();
messageController.source = "controller.MessageController" ;
messageController.singleton = true;
messageController.debug = false;
messageController.addEventListener(FaultEvent.FAULT, onFileControllerFault, false, 0, true);
messageController.subscribe("info", infoMessageHandler);
messageController.subscribe("error", errorMessageHandler);
As you would have guessed, this doesn't work.
It seems that I am only able to dispatch messages from the class that I subscribe to directly, for example if I do this:
messageController.start();
and in my MessageController.java i put this:
public void start()
{
NativeObject.sendMessage("test message", "info");
}
the infoMessageHandler receives an message containing test message, as it should.
How can I dispatch messages from whatever class in Java and catch them on the Air side?
I'm not sure I fully understand the issue yet, but is there a reason you can't simply send and receive all messages through a Java 'communication' class? If that's working, I'd just set up public methods in Java interfacing/communications class and be done with it.
(It's been awhile since I've dug into how Flerry works so I can't recall enough to point out the likely cause of the behavior you're seeing.) Also Flerry is open source, and not very big at all. If you really want to know why it's behaving in a certain way, I'd wager you could figure it out by looking at the source. (Won't take 6 months to learn like some Spring or Hibernate code base, or even BlazeDS, I promise.)
I do use Flerry in a small app, but now that I think about it, I only have one class which dispatches anything to Flex! But I feel like you may need to describe the problem you're facing differently, because it sounds like expected behavior to me.
I'm using the Smack API in Java to connect to my XMPP server.
I want to send a customized message packet like this:
<message to="you#MyServer.com" type="chat" MYFIELD="custom stuff">
<body> hi </body>
<CUSTOM_STANZA A="..." B="...">
C="..."
D="..."
</CUSTOM_STANZA>
</message>
I'm guessing that I create implement my own Packet with that returns this XML in it's toXML() method. But that doesn't seem to work.
Any help would be appreciated.
i don't know why you want to add custom attributes to the message. This will be problematic on the client and may cause issues on the server as well since it will not match the schema for the message stanza.
The message content, on the other hand is easily handled as #Femi said with a packet extension. You need to create a MyExtension which extends PacketExtension, and the toXML() in that class will return your custom stanza.
You can create and send your custom message by:
Message message = new Message();
message.addExtension(new MyExtension());
chat.sendMessage(message);
To read the stanza, you will want to register a provider, which will create and return your custom PacketExtension. You should take a look at the EmbeddedExtensionProvider for this as it handles the tag parsing for you, thus simplifying the process.
I recently found out how to add custom stanza to your message. Its was quite easy once I figured it out. I just needed to extend the standard Message Class with my custom message class.
public class CustomMessage extends org.jivesoftware.smack.packet.Message {
public CustomMessage() {
super();
}
private String customStanza;
/**
* #param customStanza
* the customStanza to set
*/
public void setCustomStanza(String customStanza) {
this.customStanza = customStanza;
}
#Override
public String toXML() {
String XMLMessage = super.toXML();
String XMLMessage1 = XMLMessage.substring(0, XMLMessage.indexOf(">"));
String XMLMessage2 = XMLMessage.substring(XMLMessage.indexOf(">"));
if (this.customStanza != null) {
XMLMessage1 += " CustomStanza=\"" + this.customStanza + "\"";
}
return XMLMessage1 + XMLMessage2;
}
}
Then use the custom class to send messages like this:
CustomMessage message = new CustomMessage();
message.setCustomStanza("my data here");
System.out.println(message.toXML());
muc.sendMessage(message);
Your XML message would then look like this:
<message id="ee7Y7-8" CustomStanza="my data here"></message>
You can use a packet extension for this: unfortunately there is no good documentation or examples for using packet extensions. I've previously looked at this unresolved question which has example code but I was unable to get it working: I got no exceptions but it simply didn't work as my extension wasn't called and I moved on to just encoding my data in the body of a Message.
EDIT: for posterity, I managed to get the following code working. It uses the DOM4J classes DocumentHelper and Element.
Presence np, packet = new Presence();
packet.setID(sessionManager.nextStreamID().toString());
packet.setFrom(server.createJID(operator, null));
if(!available) packet.setType(Presence.Type.unavailable);
else packet.setType(null);
// add the custom XML
Element xml = DocumentHelper.createElement(QName.get("custom", "http://www.custom.com/xmpp"));
xml.addAttribute("type", "presenceupdate");
packet.addExtension(new PacketExtension(xml));
Mildly humorous: I ran into my own answer a year later while actually trying to solve this problem for a real project (as opposed to tinkering like I did before) and since I couldn't just abandon it I had to figure it out. I figure I'll need this answer again so here it is. SO: my memory in the sky.
EDIT: found an even simpler way of doing this:
Element xml = packet.addChildElement("custom", "http://www.custom.com/xmpp");
xml.addAttribute("type", "presenceupdate");
Thing to note: trying to add certain things (in my case, trying to add a delay element) resulted in the packet not being routed. Something in Openfire swallowed it, so this is something to watch for.
You need to define a custom class that should implements ExtensionElement (as menitioned by #Flow)
A very detailed explanation that produces the following stanza is available in this answer
<message id='923442621149' type='chat'><body>shanraisshan</body>
<reply xmlns='shayan:reply' rText='this is custom attribute'/>
</message>
where reply is a custom extension, which contains
Element (reply)
Namespace (shayan:reply)
the list of default xmpp namespaces are available at Official XMPP website