简体   繁体   English

如何从命令行初始化应用程序的Java对象

[英]How to initialize an application's java object from command line

I have an ear application (myApp) that runs on a Websphere Application Server (WAS). 我有一个在Websphere Application Server(WAS)上运行的Ear应用程序(myApp)。 I have a jar (myJar) that is loaded into the classpath of myApp when the WAS server is started. 我有一个jar(myJar),它在启动WAS服务器时加载到myApp的类路径中。 myJar has a class (MyInitClass) that reads from a db and loads a set of data (myData) into memory. myJar有一个类(MyInitClass),该类从db中读取并将一组数据(myData)加载到内存中。 This data gets read many times by myApp. myApp会多次读取此数据。 The point is to get myData into memory to prevent doing a db call every time this data is used. 关键是要使myData进入内存,以防止每次使用此数据时进行db调用。 This part works great! 这部分效果很好!
The solution I am trying to provide is a manual initialization of MyInitClass. 我尝试提供的解决方案是MyInitClass的手动初始化。 myData gets changed from time to time and I would like to be able to reinitialize MyInitClass from a command line so I don't have to restart the application. myData会不时更改,我希望能够从命令行重新初始化MyInitClass,因此不必重新启动应用程序。 Is this possible? 这可能吗?
myApp calls a class (MyClass) that has something like this: myApp调用具有以下内容的类(MyClass):

public static MyInitClass initClass;

public boolean doStuff()
{
    if (initClass == null)
    {
        // this method loads the data into initClass.myData array
        initClass.dataInitializer();
    }
    else
        // no need to reload initClass.myData
}

I have created code similar to this in another class (MyManualInit): 我在另一个类(MyManualInit)中创建了与此类似的代码:

public static void main(String[] args)
{
    MyClass.initClass = new MyInitClass();
    MyClass.initClass.dataInitializer();
}

When I run MyManualInit from command line it prints all the same debug info that gets printed during the initialization from myApp. 当我从命令行运行MyManualInit时,它将打印所有与在myApp初始化期间打印的相同调试信息。 But myApp does not recognize that MyInitClass has been reinitialized. 但是myApp无法识别MyInitClass已被重新初始化。 I have printed System.out.println(System.getProperty("java.home")) from both processes to validate that I am using the same JRE to run both. 我已经从两个进程中打印了System.out.println(System.getProperty(“ java.home”)),以验证我使用相同的JRE来运行这两个进程。 Am I doing something obviously wrong here or does it just not work like that? 我是在这里做错了明显的事情,还是那样行不通? I assumed if I ran MyManualInit on the same JRE it would work. 我以为如果我在同一JRE上运行MyManualInit,它将起作用。 MyClass, MyInitClass and MyManualInit are all in myJar. MyClass,MyInitClass和MyManualInit都在myJar中。
Please let me know if you need more info. 如果您需要更多信息,请告诉我。

You are mixing things. 您正在混合事物。 Websphere runs in an instance of the JVM and your command line program instantiates a new one, and objects do not communicate between different JVMs (at least without some effort, bringing up sockets, etc.) Websphere在JVM实例中运行,并且您的命令行程序实例化了一个新实例,并且对象无法在不同的JVM之间进行通信(至少不费吹灰之力,打开套接字等)。

Actually your code does a lazy initialization of your initClass object, and it should be enough without any command line interaction. 实际上,您的代码对initClass对象进行了延迟的初始化,并且在没有任何命令行交互的情况下就足够了。 Why is it not enough for you? 为什么对您来说还不够?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM