Java 和事件驱动编程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4928713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java and event driven programming
提问by zengr
I am using javaeventingto write an even driven shell to access a database. So, my use case is:
我正在使用javaeventing编写一个偶数驱动的 shell 来访问数据库。所以,我的用例是:
- 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.
- 在命令行中打开shell。
- 现在,shell 连接到数据库并侦听传入的命令。
- 当它得到一个命令时,它会执行它并返回所需的输出。
Now, how can I avoid while(!exit){ //do stuff }
kind of loop here? How to use Java eventing correctly?
现在,我怎样才能避免while(!exit){ //do stuff }
这种循环?如何正确使用 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 eventing 是否可以提供更好的解决方案。
Update1:
更新1:
Here is what I am trying to implement:
这是我正在尝试实施的内容:
1My application is a shell (like mongodb shell) to try out a key-value database.
2The simple code:
1我的应用程序是一个 shell(如 mongodb shell)来尝试键值数据库。
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?I want the user to trigger the event.
现在,在这里我没有看到 java 事件的使用,我解决了我的问题,你能告诉我,java 事件如何在这里出现?我希望用户触发事件。
采纳答案by Bob
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?
我发现很难理解您到底要做什么,但我有 javaEventing 的经验,我会尽力帮助您。Will Hartung 是正确的,您需要在某处创建您的活动。因此,如果我理解正确的话,您希望从命令行启动您的 java-app,然后您希望连接到数据库并观察要插入的某些命令,并且在插入时,您希望创建一个事件。这个对吗?
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:
在这种情况下,您可能需要对数据库进行一些轮询,因为当某些条件为真时,普通数据库无法通知您的应用程序。这意味着您可能需要 while{} 子句,您可以在其中对数据库执行查询,等待包含您正在查找的命令的结果集。找到后,您可以创建这样的事件:
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. You might want to look at JED (http://code.google.com/p/jed-java-event-distribution), which does exactly that.
现在的问题是,你为什么要使用数据库来传达命令?您是否只是使用它在应用程序之间进行通信?如果您的其他应用程序也是用 Java 编写的,您可以考虑使用分布式事件,允许一个 Java 应用程序将事件发送到在网络中其他机器上的其他 JVM 中运行的 Java 应用程序。您可能想查看 JED ( http://code.google.com/p/jed-java-event-distribution),它正是这样做的。
I hope this helps, Bob
我希望这会有所帮助,鲍勃
回答by Will Hartung
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.
但这些类都在系统的边缘,驱动着数据。您的其余代码都可以完全基于事件。