简体   繁体   English

Java和事件驱动的编程

[英]Java and event driven programming

I am using javaeventing to write an even driven shell to access a database. 我正在使用javaeventing编写一个甚至驱动的外壳程序来访问数据库。 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. 现在,shell连接到数据库并侦听传入的命令。
  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? 现在,如何避免while(!exit){ //do stuff }的循环? How to use Java eventing correctly? 如何正确使用Java事件?

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. 但是,我正在寻找java事件是否可以提供更好的解决方案。

Update1: 更新1:

Here is what I am trying to implement: 这是我想要实现的:

1 My application is a shell (like mongodb shell) to try out a key-value database. 1我的应用程序是一个外壳(如mongodb外壳),用于试用键值数据库。
2 The simple code: 2简单的代码:

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? 现在,在这里我看不到Java事件的使用,并且解决了我的问题,请您告诉我, 如何在这里出现Java事件? I want the user to trigger the event. 我希望用户触发事件。

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. 您的其余代码都可以完全基于事件。

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. 我发现很难理解您到底想做什么,但是我有javaEventing的经验,我将尽力为您提供帮助。 Will Hartung is correct, you need to create your events somewhere. 请问Hartung是正确的,您需要在某个地方创建事件。 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. 因此,如果我对您的理解正确,则希望从命令行启动java-app,然后连接到数据库并监视要插入的命令,并在插入时创建一个事件。 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. 这意味着您可能需要while {}子句,在其中对数据库执行查询,以等待包含要查找的命令的结果集。 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. 现在,任何其他监听您事件的线程(ReceivedCommandEvent)都将接收该事件,并可以从事件有效负载中检索命令。

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. 如果您的其他应用程序也是用Java编写的,则可以考虑使用分布式事件,从而允许一个Java应用程序将事件发送到在网络中其他计算机上的其他JVM中运行的Java应用程序。 You might want to look at JED ( http://code.google.com/p/jed-java-event-distribution ), which does exactly that. 您可能想看看JED( http://code.google.com/p/jed-java-event-distribution ),它正是这样做的。

I hope this helps, Bob 希望对您有所帮助,鲍勃

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM