【发布时间】:2011-06-23 03:47:24
【问题描述】:
我正在使用javaeventing 编写一个偶数驱动的shell 来访问数据库。所以,我的用例是:
- 在命令行中打开 shell。
- 现在,shell 连接到数据库并监听传入的命令。
- 当它收到一个命令时,它会执行它并返回所需的输出。
现在,我怎样才能避免while(!exit){ //do stuff } 在这里出现循环?如何正确使用 Java eventing?
直接的方式可以是:
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 eventing 的使用,我解决了我的问题,你能告诉我,java eventing 将如何出现在这里?我希望用户触发事件。
【问题讨论】:
-
也许我误解了你的问题,但这不正是 eventing 的用途吗?你注册你的事件,在这里退出,当它发生时,你的代码被调用。因此,您应该不需要编写这样的循环。如果我不在左侧字段中,请纠正我。
-
是的,你是对的。但我无法弄清楚,如何实现这一目标?