I am using javaeventing to write an even driven shell to access a database. So, my use case is:
- open up the shell in command line.
- Now, the shell connects to database and listens for the command coming in.
- when it gets a command, it executes it and gives back the required output.
Now, how can I avoid while(!exit){ //do stuff }
kind of loop here? How to use Java eventing correctly?
The straight forward way can be:
while(!exit)
{
exit = myClass.execute("command"); //when command is exit, return true.
}
But, I am looking if java eventing can give a better solution.
Update1:
Here is what I am trying to implement:
1 My application is a shell (like mongodb shell) to try out a key-value database.
2 The simple code:
init(); //Initialize the connection with database
while(!exit)
{
//exit = myClass.execute("put(5)"); //This returns false
exit = myClass.execute("exit"); //returns true, i.e. the user wants to exit the shell
}
Now, here I do not see the use of java eventing and I solved my problem, can you please tell me, HOW java eventing will come in picture here? I want the user to trigger the event.
I find it difficult to understand what you're trying to do exactly, but I have experience with javaEventing, and I will try to help you as best I can. Will Hartung is correct, you need to create your events somewhere. So if I understand you correctly, you wish to start your java-app from the command line, then you want to connect to a database and watch for some command to be inserted, and when inserted, you want to create an event. Is this correct?
In that case, you will probably need to do some polling against the database, because ordinary databases have no way of notifying your application when some condition is true. This means you would probably need while{} clause, in which you perform queries against the database, waiting for a result set containing the command you are looking for. When found, you could create an event like this:
Now, any other threads listening for your event (the ReceivedCommandEvent) will receive the event, and can retrieve the command from the event payload.
Now, the question is, why do you want to use the database to communicate commands anyways? Do you simply use it to communicate between applications? If your other application is also written in Java, you could consider using distributed events, allowing one java app to send events to java apps running in other JVMs on other machines in the network. You might want to look at JED (http://code.google.com/p/jed-java-event-distribution), which does exactly that.
I hope this helps, Bob