简体   繁体   中英

Retain Non-Serializable Object's Values After Tomcat Reloaded

My program has to go through a learning step that takes around 15 mins to complete. The result of this learning is two Models stored into two public objects which will be then used in other classes. I put this learning step in the following method:

public void init()

So as to be performed at the start of the server. The problem is, every time the server reloads, it re-does the learning step. I have to wait another 15 minutes just to see the effects of a small change. I was wondering if there is a way to retain the value of some objects throughout the running of the program and the server. Here is my code:

public static Model model1;
public static Model model2;

@Override
public void init()
{
    model1= readModel(source1)
    model2= readModel(source2)
}

PS. I am using Servlets with JSP pages and Tomcat Server.

As a general solution, I would suggest for you to keep the learning part and the model out of service container. Possibly a different VM / process. This way you will be able to retain the model for as long as the process is required to run, independent of the state of client process that is your tomcat.

DETAILED

You can achieve this in few steps

  • First, you need to migrate model preparation and caching to a different program. This program will run as a daemon and you can use Daemon by Apache to run it as a background service
  • Second, Assuming your daemon is up and running, your model consumer can communicate with the model VM using standard protocols. The selection of protocol depends on your exact requirements. It can be an API exposed over TCP/HTTP or RMI or anything else.

ALTERNATIVELY

As I suggested in comments, you can also dump the model binary to file system once the model is trained. Cache the model on tomcat startup. The io will be much faster than learning phase.

视情况将其设为会话或应用程序范围的 bean,并将其作为 bean 而不是普通对象访问。

You could have a look here ? The Idea is either save session somewhere and put your model objects there or just use Hazelcast (overkill probably :))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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