Java 和事件驱动编程

发布于 2024-10-16 02:46:40 字数 1016 浏览 2 评论 0原文

我正在使用 javaeventing 编写一个均匀驱动的 shell 来访问数据库。所以,我的用例是:

  1. 在命令行中打开 shell。
  2. 现在,shell 连接到数据库并侦听传入的命令。
  3. 当它收到命令时,它会执行该命令并返回所需的输出。

现在,我怎样才能避免这里出现 while(!exit){ //do stuff } 类型的循环?如何正确使用Java事件处理?

直接的方法可以是:

while(!exit)
{
   exit = myClass.execute("command"); //when command is exit, return true.
}

但是,我正在寻找 java eventing 是否可以提供更好的解决方案。

更新1:

这是我想要实现的:

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
}

现在,我在这里没有看到java事件的使用,我解决了我的问题,你能告诉我,java事件将如何出现在这里? 我希望用户触发该事件。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

来日方长 2024-10-23 02:46:40

我发现很难准确理解您想要做什么,但我有 javaEventing 的经验,并且我会尽力帮助您。威尔·哈同(Will Hartung)是对的,你需要在某个地方创建你的活动。因此,如果我理解正确的话,您希望从命令行启动 java-app,然后您想要连接到数据库并观察要插入的某些命令,并且在插入时创建一个事件。这是正确的吗?

在这种情况下,您可能需要对数据库进行一些轮询,因为普通数据库无法在某些条件成立时通知您的应用程序。这意味着您可能需要 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

现在,侦听您的事件(ReceivedCommandEvent)的任何其他线程都将接收该事件,并且可以从事件负载中检索该命令。

现在的问题是,为什么要使用数据库来传达命令?您只是用它来在应用程序之间进行通信吗?如果您的其他应用程序也是用 Java 编写的,您可以考虑使用分布式事件,允许一个 Java 应用程序将事件发送到网络中其他计算机上的其他 JVM 中运行的 Java 应用程序。您可能想查看 JED (http://code.google.com/p /jed-java-event-distribution),它正是这样做的。

我希望这有帮助,
鲍勃

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

虫児飞 2024-10-23 02:46:40

事件库所做的就是将事件分派给侦听器。除此之外,您还需要一些东西来实际创建事件。

在这种情况下,您需要代码来读取控制台并生成事件,您可能需要其他东西来“监听”数据库并从中生成事件(假设您有正在寻找的异步数据库事件,例如表或行更改)。

该代码仍然需要编写。

除非该框架向您提供此类实用程序类,否则您必须自己编写这些实用程序类,并且这些类可能就像您所描述的那样。

但这些类都位于系统的边缘,驱动着数据。其余代码都可以完全基于事件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文