0

I am novice in OSGI technology. Idea of dynamic adding modules to working program seems very attractive for me. I know that OSGI uses own classloader per bundle. As I understand due to this we have "hot deploy". But I have misunderstanding how this works.

I want to to understand how this technology works. Therefore I want to write own "hot deployer" with hot .class file replace.

For example in my mind was born following idea:

Write application with 2 threads:

first thread outputes some messsage (old message)in the eternal cycle

In second thread we wait some time then replace .class file of class which outputes old message. now our thread1 outputes new message. not necessarily that new message outputes immediately after execution of second thread. I know that we cannot unload class force.

I just want to see that it is possible to write custom "hot deployer".

I cannot find information in google which can helps me.

Please help me realize my idea.

P.S.

I really have not enough information for where can I search information and how to realize. I have read all answers - but I am not able to realize it.

P.S. I know that familiar issue was resolved in jsp and servlets. Specification obliges hot replace jsp without server restart. But I know that jsp ultimately compiles to .class file.

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • Take a look here: http://stackoverflow.com/questions/148681/unloading-classes-in-java – Alexandre Santos May 06 '14 at 08:32
  • 1
    I think you should start with basic understanding about how classloading works in java before going further with this. It's essential to understand the concept of classloaders first. After that, you can experiment with writing a custom classloader. Threads are not relevant in hot deployments, classloaders are. – eis May 06 '14 at 08:32
  • @Alexandre Santos I have alerady read it – gstackoverflow May 06 '14 at 08:34
  • @eis I think that I similar with basic concepts of classloading. I know that threads and hot deploy doesn't connect but this experiments can show result of hot replace. – gstackoverflow May 06 '14 at 08:36
  • @gstackoverflow well, if you have an idea how to replace a class within a thread, you should elaborate it in your question. Or are you asking for someone to figure out how to do it for you, just looking for ideas? – eis May 06 '14 at 09:33
  • @eis I don't understand how to start. – gstackoverflow May 06 '14 at 10:37

2 Answers2

1

In OSGi, you can deploy or update bundles at runtime, not classes in the bundles. Probably you are looking for a similar solution that JRebel gives to you.

Instead of class replacement, You should concentrate of designing your bundles to be simple. In that case a bundle deployment will be as fast as if you just replaced a class (a moment). Also, you should design your architecture in the way that if you update a bundle, it will not cause the restart of all of your technologies.

I suggest, that you should read about patterns that others use within the OSGi world to keep their bundles lazily coupled: whiteboard pattern, extender model

Balazs Zsoldos
  • 6,036
  • 2
  • 23
  • 31
  • I can simplistically viewed as a class bundle – gstackoverflow May 06 '14 at 09:13
  • I am sorry, but I do not get what you mean – Balazs Zsoldos May 06 '14 at 10:03
  • in my simple situation I can consider class as bundle P.S. sorry for bad English – gstackoverflow May 06 '14 at 10:36
  • You will have a lot of bundles than :). If you have stateless solutions / technologies, it is not such a big problem to have 10-100 classes in a bundle and re-deploy them together. It will still be a moment. However, you will have to restart your test each time you deploy the class (not like after hot-code-replacement, where the debug jumps back to the beginning of the function) – Balazs Zsoldos May 06 '14 at 11:22
  • I don't want use OSGI I want to write custom hello world realization. – gstackoverflow May 06 '14 at 11:51
  • So you don't want to use OSGi, but you do want to study it. Then just do so. There are several framework implementations available in open source. Study the code and you will understand how they work. Pretty soon you will discover that writing such a system from scratch is by no means simple, and requires more work than posting such a broad question here. – Marcel Offermans May 06 '14 at 23:18
1

I want to learn how to drive a car.... please explain to me how to build my own gas turbine engine. Yes I know that cars don't use gas turbine engines but still I want to build one because I believe this will help me to understand driving better.

OSGi module reloading does not work at the level of classes, it works at the level of bundles (modules) which are aggregations of classes. It does this by disposing and recreating whole classloaders.

There's nothing wrong with wanting to know the low-level details but this should usually come after a broad understanding.

Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77