将应用程序迁移到服务模式 [java/groovy]

发布于 2024-11-14 00:37:20 字数 536 浏览 4 评论 0原文

我有一个用 groovy 编写的应用程序。它需要一些 cmd 参数并返回先前格式化的响应。随着系统的增长,似乎需要极其频繁地运行该应用程序(例如 5 分钟内运行 80 次),这会导致某些性能问题。特别是,它一遍又一遍地创建所有对象,这导致一次运行最多填充 60MB RAM(可以轻松计算 ROM/交换的使用程度)。

我想将其迁移到服务运行模式,该模式将仅接受某些参数并返回格式化输出。但是:

  1. 应用程序总是由bat/sh脚本触发(这是无法更改的)
  2. 脚本和应用程序都在同一主机服务器上

所以,我想知道如何更好地执行脚本和应用程序的通信服务?

PS:抱歉,我没有提到,它是一个独立的应用程序,它永远不会使用服务器或类似的东西,因为它看起来是多余的。解决方案应该尽可能简单并且极其轻量。

示例:我现在能想到的最简单的事情就是永远不要迁移它(我知道这是矛盾的;))并简单地引入一个数据库,其中将存储所有结果,并且应用程序将有自己的触发时间安排。每当它被任何参数触发时,它应该简单地搜索数据库中的最新结果并返回它。简单、轻便、快速且有效。 :)

I've got an application written in groovy. It takes some cmd args and returns previously formatted response. As system grew, it appeared that it is required to run this app extremely frequently (like 80 times in 5 mins) which leads to certain performance issues. In particular it creates all its objects over and over again which leads to filling up to 60MB RAM in one run (can be easily calculated how severely ROM/swap is used).

I want to migrate it to a service running mode which will simply take certain params and return formatted output. But:

  1. App is always triggered by a bat/sh script (this can't be changed)
  2. Both script and app are on the same host server

So, I'm wondering how it would be better to perform the communication of a script and a service?

P.S.: Sorry that I didn't mention, it's a standalone app, it will never use a server or anything like that as it appears to be redundant. Solution should be as simple as possible and extremely lightweight.

Example: The simplest thing I can think of by now is never to migrate it (I know it's contradictory ;)) and simply introduce a DB where all thee result will be stored and an app will have it's own schedule of when to trigger. Whenever it is triggered with any params, it should simply search the latest result in DB and return it. Easy, light, fast, and working. :)

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

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

发布评论

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

评论(1

百思不得你姐 2024-11-21 00:37:20

对于企业环境,我建议在应用程序服务器中运行带有 EJB 的 JavaEE 应用程序。对于您的要求,这可能有点过分了。一个简单的解决方案可以是:

  • 服务:使用本地 RMI 注册表实现 RMI 服务器。计算将在这里完成。
  • 脚本:连接到 RMI 服务器,调用 RMI 服务器上的方法并显示结果。

RMI 服务器

public class RmiServer extends UnicastRemoteObject implements RmiInterface
{
    private static final long serialVersionUID = 1L;

    public RmiServer() throws RemoteException
    {
        super();
    }

    public String random() throws RemoteException
    {
        return "Helo World! "+(new Random()).nextInt(100);
    }

    public static void main(String[] args) throws RemoteException, MalformedURLException
    {
        LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
        Naming.rebind("myServer", new RmiServer());
    }
}

RMI 客户端

RmiInterface server = (RmiInterface)Naming.lookup("//127.0.0.1/myServer");
System.out.println(server.random());

RMI 接口

public interface RmiInterface extends Remote
{
    public String random() throws RemoteException;
}

For enterprise environments I would suggest a JavaEE application with EJB running in an application server. For your requirements this might be an overkill. A simple solution can be:

  • Service: Implement a RMI server with a local RMI registry. Calculations will be done here.
  • Script: Connect to the RMI server, invoke a method at the RMI server and display the result.

RMI Server

public class RmiServer extends UnicastRemoteObject implements RmiInterface
{
    private static final long serialVersionUID = 1L;

    public RmiServer() throws RemoteException
    {
        super();
    }

    public String random() throws RemoteException
    {
        return "Helo World! "+(new Random()).nextInt(100);
    }

    public static void main(String[] args) throws RemoteException, MalformedURLException
    {
        LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
        Naming.rebind("myServer", new RmiServer());
    }
}

RMI Client

RmiInterface server = (RmiInterface)Naming.lookup("//127.0.0.1/myServer");
System.out.println(server.random());

RMI Interface

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