Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java and event driven programming

I am using javaeventing to write an even driven shell to access a database. So, my use case is:

  1. open up the shell in command line.
  2. Now, the shell connects to database and listens for the command coming in.
  3. 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.

like image 778
zengr Avatar asked Feb 08 '11 01:02

zengr


2 Answers

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:

Class ReceivedCommandEvent extends EventManager.EventObject {}   // define your event   

while (command==null) {
  command = pollDataBaseForCommand();  //poll your databae for commands
  waitForSomePeriod(); 
}

EventManager.triggerEvent(this, new ReceivedCommandEvent(command));     //trigger your event, with the command as payload

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

like image 177
Bob Avatar answered Oct 14 '22 02:10

Bob


All that eventing library does is dispatch events to listeners. Beyond that you need something to actually CREATE the events.

In this case, you would need code to read the console and generate events, you would need potentially something else to "listen" to the DB and generate events from that (assuming you have asynchronous DB events you're looking for, such as table or row changes).

That code still needs to be written.

And unless that framework provides to you such utility classes, you'll have to write that yourself, and those classes will likely be just like you describe.

But those classes are all at the edge of the system, driving the data. The rest of your code can all be completely event based.

like image 2
Will Hartung Avatar answered Oct 14 '22 02:10

Will Hartung