检测代码是否在应用程序服务器 java 中运行的最佳方法
对于 J2EE bean,我重用了为 java swing 应用程序开发的代码。 不幸的是,JOptionPane.showMessageDialog()
很常用。 幸运的是,大多数情况发生在 J2EE 应用程序不重用的代码段中,但在某些情况下,较低级别的代码具有 JOptionPane.showMessageDialog()
的实例。 显然,这会导致服务器上弹出对话框,这是我想避免的。
作为第一步,我想以某种方式确保服务器上不会出现任何对话框。
有人建议查看某些事件或绘画队列(我不记得是哪一个): 那就是:
// old code: JOptionPane.showMessageDialog(msg);
if ( someEventQueue.size() == 0 ) // <== consider this pseudo-code
Log.log(msg); // I am running on a server. Tell the log.
else
JOptionPane.showMessageDialog(msg); // I have a user made of meat. Tell him!
我从来没有真正做到这一点。 你会怎么办?
For a J2EE bean I am reusing code that was developed for a java swing application. JOptionPane.showMessageDialog()
is unfortunately commonly used. Most occurences luckily in code sections that are not reused by the J2EE application, but in some cases lower levels of the code has instances of JOptionPane.showMessageDialog()
. Obviously this it results in dialog boxes popping up on the server, which is what I want to avoid.
As a first step I'd like to somehow assure that no dialog boxes will ever occur on the server.
Someone suggested peeking in some event or paint queue (I do not recall which one):
That would be:
// old code: JOptionPane.showMessageDialog(msg);
if ( someEventQueue.size() == 0 ) // <== consider this pseudo-code
Log.log(msg); // I am running on a server. Tell the log.
else
JOptionPane.showMessageDialog(msg); // I have a user made of meat. Tell him!
I never really got that working. What would you do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保服务器
以默认方式启动 大多数服务器应该以这种方式启动。 然后您可以查看:
有关 headless 的更多详细信息,请访问此处:
Make sure the server is started with
Most servers should be started that way by default. Then you can check:
More details on headless available here:
我刚刚在 Linux 上使用 OpenJDK 运行时环境 (IcedTea 2.4.3) (Gentoo build 1.7.0_45-b31) 对此进行了测试,我发现
unset DISPLAY
也足以使isHeadless()
(和isHeadlessInstance()
)返回true
。因此,此方法不仅告诉您 AWT 是否被迫忽略系统的图形功能,还告诉您您的进程是否有权访问图形功能。
示例:
该代码正在某些没有显卡的机器上运行(这可能是您想知道的),因此如果由于某种原因您想知道无头模式是否为,您可以随时检查系统属性>被迫。
I just tested this with OpenJDK Runtime Environment (IcedTea 2.4.3) (Gentoo build 1.7.0_45-b31) on Linux, and I find that
unset DISPLAY
was also sufficient to makeisHeadless()
(andisHeadlessInstance()
) returntrue
.So, this method doesn't just tell you whether AWT was forced to ignore the system's graphics capabilities, but rather whether your process has access to graphics capabilities at all.
Example:
The code is running on some box that has no display card (that's probably what you want to know) so you can always check the system property if for some reason you want to know whether headless mode was forced.