检测代码是否在应用程序服务器 java 中运行的最佳方法

发布于 2024-07-25 09:10:34 字数 638 浏览 5 评论 0原文

对于 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 技术交流群。

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

发布评论

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

评论(2

等数载,海棠开 2024-08-01 09:10:34

确保服务器

java -Djava.awt.headless=true

以默认方式启动 大多数服务器应该以这种方式启动。 然后您可以查看:

boolean headless_check = GraphicsEnvironment.isHeadless();

有关 headless 的更多详细信息,请访问此处

Make sure the server is started with

java -Djava.awt.headless=true

Most servers should be started that way by default. Then you can check:

boolean headless_check = GraphicsEnvironment.isHeadless();

More details on headless available here:

暖阳 2024-08-01 09:10:34

我刚刚在 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 make isHeadless() (and isHeadlessInstance()) return true.

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.

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