2

I've been playing around with the idea of creating a program that can perform multiple different tasks while being monitored by a central thread. The idea is to create a core program that will look for and load separate modules when it runs/while running. These modules will perform their own tasks before passing the finished object(s) off to another module which will handle something such as uploading the finished product to a web server or organizing the resulting data into a pre-determined file/folder structure (see image for basic concept). enter image description here I've never really worked with the modular design in Java before; all my past programs have been fully self contained, so I have almost no clue where to even start.

My goal is to store external libraries in the core program (for example, AWS SDK for Java, which can shoot the .jar up to about 18MB itself) so that the separate modules can be distributed almost instantly when an update is made, rather than having to re-distribute the entire, 18MB+ self-contained program just to fix a 1-line bug. Additionally, I'd like to be able to update individual modules, when necessary, without having to restart the entire program, allowing other modules to continue working while one is being updated, reducing overall downtime so that the only time the program is required to shut down entirely is when an update to the core is made.

What I want to know is, is this possible (I'm assuming so), and if so; what should I look into/how would I go about setting it up? (Am I trying to reinvent the wheel? If so, where can I find said wheel?)

I looked into using a ServiceLoader and played around with the example here, but I'm not sure how I could go about pointing one module to another with it or if it can retrieve the library classes from the core, nor if it can do the "active updating" mentioned. All I've seen, so far, is that (in my example) the core could access the module, but I am not sure if/how the module might access the core.

Not sure if it's relevant to the post, but I do all my work inside of Eclipse (figured worth mentioning in case there is an IDE-specific tool available).

Community
  • 1
  • 1
DGolberg
  • 2,109
  • 4
  • 23
  • 31
  • I think you just need to load an additional jar at runtime. See this question on how to do that http://stackoverflow.com/questions/10624907/loading-jars-at-runtime – bhspencer Mar 13 '15 at 17:57
  • Asking for Eclipse plugin recommendations is off topic. You might want to consider editing the question with this in mind, since it doesn't seem to be your focal point. http://stackoverflow.com/help/on-topic – Radiodef Mar 13 '15 at 18:02

2 Answers2

4

Take a look into OSGi, the Bundle Structure is basically what you are looking for. With OSGi, you create "bundles" which are jars with ➕ Metadata to tell the runtime (which can e.g. be Apache Felix or Eclipse Equinox) what dependedependencies your bundle has and what it offers. With OSGi you can start and stop and update each Bundle independently from all the others and it even stops depending other functionality and restarts them after bundle update (given that you configured it correctly ;))

@bhspencer: just loading new jars doesn't allow you to update properly because the standard java classloader does not allow unloading of classes.

DJGummikuh
  • 108
  • 6
  • That sounds very much like what I'm looking for, though I do have an additional question... Does this also work in a "client" environment where I do not have access to the client, instead relying on the program to check for and install updates on its own? For example; a client program on a remote user's machine that communicates with a database to create/submit work to be processed. – DGolberg Mar 13 '15 at 18:06
  • 1
    Yes, Eclipse has this functionality built on OSGi. (Though most people leave it disabled.) – Alex Fitzpatrick Mar 13 '15 at 18:11
  • Nice! I'll have to start looking for some OSGi tutorials it sounds like! Thanks guys! (links are welcome if you know of any good ones). – DGolberg Mar 13 '15 at 18:15
0

You can't unload a Class from a ClassLoader, but you can unload an entire ClassLoader from the JVM by nulling all references to it and to all instances of classes it has loaded. URLClassLoader makes a good starting point.

Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82
  • Interesting, but it sounds like that would deactivate the other modules, which I'd like to keep running to reduce downtime. – DGolberg Mar 13 '15 at 18:11