Java: Strong code mobility How?

Does anyone know how to use Java for strong code portability ? Have you ever done this before?

Here I am trying to achieve.

Suppose we have two separate Java applications that communicate over the network. Appendix A and Appendix B.

Appendix A has an x ​​class created as an object and uses it. Appendix B does not know about this class x.

Application A must transfer the instance of class x to application B. Application B must be able to dynamically load class x and save the state of class x.

I have googled around and found a bunch of resources on how to dynamically load a class at runtime. However, I am not sure if the mechanism for transmitting an instance of an object over the network is covered, along with its state, for a dynamic call.

Any pointers would be very helpful, and thanks in advance!

NOTE: I am more interested in how (for example, approach, way of thinking) this problem is solved, and not what is used to solve it; this is because I have been instructed to develop my own solution to solve this problem. Although the indication of the libraries / frameworks is great, it would be ideal if the answers were sent from people who previously did something similar (as if rare).

+6
java dynamic-class-loaders dynamic-class-creation
source share
6 answers

You ask about strong mobility, but your requirements are met with poor mobility, which with some restrictions is provided by the RMI protocol. RMI not only supports RPC, serialization of objects and graphs of distributed objects, but also usually uses code sharing so that the client can load the bytecode of classes known only to the server and execute this bytecode locally.

Strong mobility is not supported by Java, and I cannot figure out how to implement it without introducing proprietary extensions into the virtual machine. Essentially, strong mobility in the Java context means that you pause or pause a thread in one virtual machine, transfer all instances of objects accessible from this thread, potentially the bytecode needed to execute, and the internal thread st (call stack, etc.) e.) On another virtual machine and make it recreate the state of the stream and continue execution at the point where the stream was suspended in the original virtual machine.

+2
source share

I wrote an open source library that supports Code Mobility in exactly the same way as above. In fact, it supports the use of the RPC type.

See: Mobility-RPC - Complete Code Mobility and RPC for the Java Platform

In terms of where it stands for poor mobility versus strong mobility: in the middle .

When you transfer an object to a remote computer, you can control which method calls the object and with what arguments it arrives at the remote machine. Therefore, if you write a mobile agent, then he can decide how he wants to resume execution on the next machine, before he goes on his own, he does not need to resume work from the same place.

+1
source share

There is a project called cajo that can dynamically move objects across the network. However, I am not sure about the execution status.

0
source share

In normal Java, you need some way for application B to load the class for the object, then you need to serialize the object from application A to application B. This can be done if the classes are available in some central places, such as the HTTP server . But in the general case, when you want to transfer a completely new object to application B, you need to implement your own class loader (or find a library that does this).

If all of your objects are Serializable and you have a central place to store classes, this should be fairly easy to implement. You can use URLClassLoader to load classes from an http server, and then regular Java Serialization to transfer over a serialized object. Some coordination between applications A and B is required so that B knows which class is loading, A knows when to send the object, and B knows how to continue executing the methods of the object. With this approach, there is probably no way for object X to be in the process of executing the method; he would have to stop and then resume his execution in collaboration with App A.

0
source share

To move an instance of Class I, you need to do two things from A to B.

First, the definition of class C (usually in the form of a list of byte codes), and then the serialized form I. Note that you must use the XML serialization setting of the old binary serialization.

Actually, the hard part is passing C dependencies, since you essentially need to pass all C superclasses, plus all return data types and field types, as well as their superclasses, return field types, etc. etc. etc.

If you really need to do this for all possible C values, the best option would be to use a frame designed for this, like a grid or Terracotta. If you can restrain yourself, such as a given and narrow interface, you are likely to be much better. Also consider using properties for this, as they are very simple and can give you far.

0
source share

If you use JDK6, you can send the source code, compile it, so now the class loader can find this new class, because it was compiled dynamically.

The trick is that you may need to use some DI to load it into your application, so you need a DI infrastructure that can work dynamically.

Then, when it is introduced into your class, you set the properties at that point.

They must have a well-known interface. There should be a contract that both can depend on.

I have not tried, and I do not think Java is for this language. If you should have this functionality, you can look at something like one of the jvm or Clojure scripting languages, since I expect it to work with this as well, and then your Java application will interact with this other dynamic structure , since everything will work on jvm.

UPDATE:

Here is a link to compile on the fly, with JDK5, as this is something that many people may not know about.

http://fivedots.coe.psu.ac.th/~ad/jg/javaArt1/index.html

-2
source share

All Articles