2

I have a class that uses static methods to wrap remote API calls.

Generally speaking, I don't want my API server to "listen" for these calls all the time, but instead only listen when the class is being used by a program. So I need a way to tell the server to "wake up" when the class gets loaded (how I send the "wake up" message is irrelevant).

I know that I can wake the server up when the first class method is invoked, but I want it to be ready as soon as the class is loaded into a running program (even if it is loaded lazily).

Also, it would be nice to know when the class is no longer used, so I can tell the server to go back to sleep.

Basically, I'm looking for a "constructor" and a "finalizer" of an entire class. Can this be done?

EDIT: A very important thing I forgot to mention, I can't have the user manually initialize/finalize the class using public static methods or anything like that. The class needs to feel like a regular native class.

user3470440
  • 221
  • 1
  • 2
  • 6
  • 1
    you will need to meet 'meaningful use' in order for the class to be 'loaded' into the JVM. at that time a static initializer is probably what you want. I don't know about the finalizer part. – Mark W Mar 27 '14 at 21:29
  • Not sure I understand what you mean by 'meaningful use'. Do you mean that JVM only loads classes lazily when they are first used? I forgot to mention that I don't want to add static initialize/finalize methods to the class, from the user's perspective the whole thing needs to act like a regular class. – user3470440 Mar 27 '14 at 21:30
  • Pretty much, yes. Check out some literature : http://www.onjava.com/pub/a/onjava/2005/01/26/classloading.html – Mark W Mar 27 '14 at 21:30
  • 2
    Is this server in some kind of container? Could you bind onto web-app lifecycle or servlet context lifecycle? Class loading lifecycle is.... not an awesomely well defined thing to try to work with. – Affe Mar 27 '14 at 21:31
  • I'm afraid the class lifespan is the only thing I can tap into. This class is meant to be used in native environments and "feel" like a native class. It's because of that I choose not to add a static "initialize" method. – user3470440 Mar 27 '14 at 21:38
  • Can you please include your code? Preferably just the skeletons of the related classes, and the methods that interact between those classes. – The Guy with The Hat Mar 27 '14 at 21:48
  • In normal environments, classes aren't unloaded. What controls class unloading in your case? – erickson Mar 27 '14 at 21:53
  • What do you mean by "feel like a native class"? In what way is a static initializer not "native"? The only way to tap into class loading/unloading would be to write your own classloader. – Jim Garrison Mar 27 '14 at 21:59

1 Answers1

6

You can use a Static Initialization Block:

class YourClass {
    static {
        System.out.println("I got loaded!");
    }
}

This will be called at the moment the class gets loaded by the JVM.

For the unloading part, a way (neccessarilly not the best) is to start a timer in the static initializer in which you close the resources after a certain time of no usage. Usage would be indicated by a constructor being called or something like that, but it might be tricky to implement with concurrency issues.

Another way could be to write a custom Classloader.

skiwi
  • 66,971
  • 31
  • 131
  • 216
  • Thanks so much! A very simple solution. Also thanks to the other guy who wrote something similar before you, but ended up deleting it before I got a chance to respond... He probably thought I didn't want to initialize the class this way because I wasn't clear enough about what I don't want to do. Anyway, I edited my original post to clarify. Thanks again to both of you. – user3470440 Mar 27 '14 at 22:05
  • By the way, perhaps there's a way to call a "destructor" when the main program exits? – user3470440 Mar 27 '14 at 22:07
  • 1
    This code is subject to the meaningful use part of my original comment. If he doesn't reference a static field, method, or create an instance of the class first (well technically this code will run BEFORE the value from the field/method or construction of the class, but it happens JUST before then), he should not expect this code to run. This seems to be contrary to the OP's edit, where you state they cant cause the code to run by referencing a static field or whatever. – Mark W Mar 27 '14 at 22:28