How is the following java code useful in autonomic computing?
In the book I'm learning, they show this java code:
Class c = ClassLoader.getSystemClassLoader().loadClass(name); Class type = this.getClass().getClassLoader().loadClass(name); Object obj = type.newInstance();
This code is used to dynamically load Java classes The book continues:
What are the benefits of using such code? The concept of autonomic computing is usually "autonomic system management" How does this relate to how Java programs are controlled by the JVM?
Source: automatic calculation principle design (by lalanda)
Solution
The advantage is that you can decide what classes to actually load and use at run time For simple java programs, you have a single implementation of a class without any benefit
Complex environments like OSGi (the foundation of eclipse) use separate classloaders for each module This provides flexibility and the possibility of replacing modules at runtime
Another "classic" use case is to load the database driver at run time You may want to connect to MySQL database or Oracle, and both use different implementations of jdbc driver
Bonus:
A very good article by Alex blewitt discussing the concept of eclipse / OSGi class loading can be found here
In my own coding experience, I use eclipse plug-ins for enterprise web monitoring projects Monitoring basically focuses on the constantly scraping resources on the network Each such resource is monitored by the implementation of the monitor plug - in Not all resources are under our control, so when they change, we must adjust the plug-in that handles the resource When we uninstall the old plug-in module, the whole monitoring application can continue to run Of course, I use the eclipse rich client platform (RCP) to hide the class loader of each plug-in You only need to specify which plug-in depends on which and the actual class loading, and then it is completed by the RCP platform
Web servers like Tomcat use the same method, although I don't have much Tomcat experience
Implementing a dynamic class loading system directly may be a good exercise, but for real-world applications, I will explicitly study production level implementations, such as eclipse RCP or Apache karaf
If you want to organize the whole thing further and need to run the plug-in in the cluster, you may need to check GYREX
I can't share my code here, but here are some good starting points and code examples:
> http://www.vogella.com/tutorials/EclipsePlugIn/article.html > https://karaf.apache.org/manual/latest-2.3.x/quick-start.html > http://www.javaworld.com/article/2077837/java-se/hello –osgi–part-1–bundles-for-beginners. html