David
David

Reputation: 185

How can I share memory between two Java web services?

I got two Java web services, hosted in Tomcat on the same server.

Is there any way to share memory (objects) between them?

I can turn the sharing into some kind of web methods calls, however

  1. this is complicated, a lot of changes are required.
  2. this is not really sharing, objects are duplicated, although it should work for my case.
  3. this will expose methods that should not be called by the clients.

Upvotes: 1

Views: 2987

Answers (5)

lasanthasuresh
lasanthasuresh

Reputation: 1

How about using a shared library.

You can refactor your logic, move them to a separate library, and build as a separate jar.

The jar should be place in tomcat_home/lib directory. And in your web apps the library dependency should be set as provided ( in maven )

You store create and store the objects you need to be shared in the shared memory, and access them from any web

Upvotes: 0

Try using JCS:

http://commons.apache.org/proper/commons-jcs/

Hope it helps! ;)

Upvotes: 1

Stephen C
Stephen C

Reputation: 719229

Is there any way to share memory (objects) between them?

You can create a shared memory region that is shared by two JVMs. You can do this using native code, or (in theory) by mapping a file into the address-space of two apps.

But you can't put Java objects in that region. The JVM doesn't support this, either in Java code or in native code. (And even if you could, synchronization would be a big problem.)


So could you use shared memory to share data between two JVMs?

Maybe. But you'd need to treat the share memory segment as a kind of database, and implement a scheme for copying object state between the segment and each JVM's heap. And you'd need to implement a robust synchronization scheme, probably using semaphores.

In short, it would be a significant amount of work to implement, and it wouldn't "feel" like the JVMs were sharing objects. It would be easier to use an existing database or distributed caching solution.

Upvotes: 2

GETah
GETah

Reputation: 21439

On Inter process communications, Java says:

To facilitate communication between processes, most operating systems support Inter Process Communication (IPC) resources, such as pipes and sockets. IPC is used not just for communication between processes on the same system, but processes on different systems.

I would rather go for pipes or sockets. This will make your life a lot easier and your web services more flexible, as they can run on two separate machines still with the ability to talk to each other as if they were setting side by side.

This is being said, back to practice. Say for example you have a set of objects {a,b,c} you want to share between your services. Create a data store class that holds {a,b,c} objects and whenever there is an update, do it in the data store dataStore.setA(A new_a). Behind the scene, and for every update, the local data store will notify the remote data store sitting in the other application and transmit all the updates that have just been made. The following DTO can be used to transmit all changes from one data store to another:

public class ObjectUpdateEvent<Source> implements Serializable {
    private String fieldName;
    private Object previousValue;
    private Object newValue;
    private Source source;
    // Constructor...
}

Updating an the object "a" can be done the following way

public class DataStore{
    // .....

    public setA(A new_a){
      ObjectUpdateEvent<DataStore> updateDto = new ObjectUpdateEvent<DataStore>();
      updateDto.setPreviousValue(a);
      updateDto.setNewValue(new_a);
      sendUpdateDto();
      a = new_a;
    }
}

EDIT: This is exactly what @duffymo mentioned above.

Upvotes: 0

duffymo
duffymo

Reputation: 308928

Not that I know of. Sounds like it's fraught with peril. It's hard enough to synchronize objects in one app; you have no hope with two. What good could this possibly do?

If it's common methods you need, put them into a service that both can call. If it's common data, put it in a database.

Upvotes: 5

Related Questions