1

I've inherited a Struts2 project which needs some functionality addition. When I ran into de code to guess how the previous guy did things, I found out that if he wants a class to instantiate only once when the Tomcat server starts (because it has to read heavy loads of data from disk, but only once to get its config, for instance), he did this in the following way:

public class ExampleClass {
    public ExampleClass(){//Read files and stuff to initialize}
    public Object method(Object[] args){//The job to do}
}

And then, in the struts action which uses it he instantiates it this way:

public class SomeAction extends ActionSupport {
    ExampleClass example = new ExampleClass()
    public String execute() {
        //Do stuff every time the action is called
        Object result = example.method(args);
        // Do stuff with results
    }
}

I know from servlet times that this does the trick, however, I feel like the guy who handled this before was as inexperienced in Struts2 as I am, so here comes my question:

Is this the proper way to do so according to style recommendations and best practices? Does struts2 provide a more controlled way to do so?

I found some answers related to simple parameters here, but I'm not sure if this is the proper way for objects like those? What would happen if ExampleClass instance is really heavy? I don't want them to be copied around:

How to set a value in application scope in struts2?

Some background about ExampleClass: When the constructor is called, it reads large sets of files and extracts it's configurations from them, creating complex internal representations. When method() is called, it analyzes it's parameters using the rules, and outputs results to the user. This process usually takes seconds, and doesn't modify the previously initialized rule values.

This is running in Tomcat 7, however, I'm planning to upgrade to Tomcat 8.5 when everything is in place. I'd like to know if there are known issues about this regarding to this setup aswell (there are no other incompatibilities in the code).

BTW: He's not checking if ExampleClass is broken or anything like that, this definetly looks like a recipe to disaster xD. In fact, If I remove the source files, it is still trying to execute the method()... Poor soul...

Ideally, I need a way to instantiate all my application-level objects on start-up (they're the application itself, the rest is just a mere interface) in a way that if they fail Struts2 will tell Tomcat not to start that war, with the corresponding error logging and so on.

If Struts2 doesn't support this, which is the commonly accepted work-around? Maybe some Interceptor to check the object status and return to a error page if it hasn't been correctly instantiated? Execute a partial stop of tomcat from within?

All the objects of this project are thread safe (the only write operation inside them is performed on initialization), but I'd like to know best practices for Struts2 when objects are not so simple. What happens if a user can actually break one? (I know I should by any means avoid that, and I do, but mistakes happen, so I need a secure way to get through them, and get properly alerted, and of course I need a way to reinstantiate it safelly or to stop the whole service).

Right now, I can manually execute something like:

public class SomeAction extends ActionSupport {
    ExampleClass example = new ExampleClass();
    private boolean otherIsBuildingExample = false;
    public String execute() {
        if(otherIsBuildingExample) return '500 error';
        if(example==null || example.isBroken()){
            otherIsBuildingExample = true;
            example = new ExampleClass();
            otherIsBuildingExample = false;
        }
        Object result = example.method(args);
        // Do stuff with results
    }
}

Indeed, this would be cleaner with Interceptors, or so, however, this sounds like a pain in the *** for concurrency, specially taking into consideration thay example takes several seconds to start, and that more requests can come, so more concerns to take into consideration, like: what if two people call if(otherIsBuildingExample) and the second one gets the value before the first one performs otherIsBuildingExample=true? Nothing good... If the class is simple enough, both will instantiate and the slower one will prevail, but if one instantiation blocks the other's resources... well, more problems.

The only clean solution I can think of is to make ExampleClass robust enough so you can repare it using its own methods (not reinstantiating) and make those thread safe in the common way (if 10 people try to repair it, only one will proceed, while the others are just waiting for the first to end to continue, for instance).

Or maybe everytime you call execute() you get a copy of example, so no worries at all about this?

I'm digging into struts documentation Thanks in advance.

DGoiko
  • 346
  • 2
  • 19
  • There are any number of ways this could be handled; I generally did it with Spring since most every project used Spring. In any case, all you *really* need is a singleton that caches the response, so only the first hit incurs the cost. This isn't very tricky. – Dave Newton Nov 13 '18 at 15:37
  • yeah, thats what I thought, thanks @DaveNewton. – DGoiko Nov 13 '18 at 23:31

0 Answers0