1

I am using Mappedbus for IPC between two JVMs (oracle jdk 1.8, linux arm); I would like to see the IPC work "forever", but as the memory mapped (or shared memory) file is finite, before or later the writer gets an EOFException.

So I try to reset the writer (and the reader), but after closing and reopening the writer, the reader is not able to read the new records.

So, the question is, how to make the following test case work i.e. go on forever with both writer and reader working?

 // intentionally low, for this test case
 final long FILE_SIZE = 2000L;

 private final String fileName = "/dev/shm/ipc-message";

 @Test
 public void hello() throws IOException {

    MappedBusReader reader;

    MappedBusWriter writer;
    writer = new MappedBusWriter(fileName, FILE_SIZE, 128, false);

    writer.open();  

    reader = new MappedBusReader(fileName, FILE_SIZE, 128);

    reader.open();        

    int writeCounter = 0;
    int readCounter = 0;

    while (true) {

        IpcMessage message = new IpcMessage();

        message.source = 1; 

        message.value = 1;
        message.destination = 2;

        message.body = new byte [32];

        try {

            writer.write(message);       

        } catch (EOFException ex) {
            System.out.println("write EXCEPTION");

            writer.close();

            writer = new MappedBusWriter(fileName, FILE_SIZE, 128, false);

            writer.open();  
        }

        System.out.println("write: " + writeCounter++);

        try {
            if (reader.next()) {

                    boolean recovered = reader.hasRecovered();
                    int type = reader.readType();

                    System.out.println("read: " + readCounter++);

                    reader.readMessage(message);   

            }
        }  catch (EOFException ex) {

            System.out.println("read EXCEPTION");

            reader.close();

            reader = new MappedBusReader(fileName, FILE_SIZE, 128);

            reader.open(); 

        }

    }



 }

where IpcMessage is:

public class IpcMessage implements MappedBusMessage, Serializable {

    public static final int TYPE = 0;

    public int messageType;
    public int value;

    public int source;
    public int destination;


    @Override
    public void write(MemoryMappedFile mem, long pos) {

        mem.putInt(pos, messageType); 
        mem.putInt(pos + 4, value); 

        mem.putInt(pos + 8, source);
        mem.putInt(pos + 12, destination);
    }

    @Override
    public void read(MemoryMappedFile mem, long pos) {

        messageType = mem.getInt(pos);
        value = mem.getInt(pos + 4);

        source = mem.getInt(pos + 8);
        destination = mem.getInt(pos + 12); 

    }

    @Override
    public int type() {
        return TYPE;
    }



}

The output I get from this test case is:

write: 0
read: 0
write: 1
read: 1
write: 2
read: 2
...
write: 8
read: 8
write: 9
read: 9
write: 10
read: 10
write: 11
read: 11
write: 12
read: 12
write: 13
read: 13
write EXCEPTION
write: 14
write: 15
write: 16
write: 17
write: 18
write: 19
write: 20
write: 21
write: 22
write: 23
3
  • I would hazard a guess that you'll get much better performance from sockets as it seems like you're doing linear IO anyway, although it depends on the use case of course. Commented Dec 21, 2016 at 13:40
  • See e.g. this answer to a similar question. FWIW, a SHM is not a memory mapped file, where disk IO will cost you a lot of performance and add complexity like managing the EOF. Commented Dec 21, 2016 at 13:48
  • @LukeBriggs: actually I have three processes in distinct JVMs which communicate among them, so maybe a multicast socket solution like Jgroups could be the solution.
    – mrtexaz
    Commented Dec 21, 2016 at 16:09

1 Answer 1

2

I received an answer to my question via email from the developer of Mappedbus, I think it is interesting so I copy it here:

This question was briefly answered here: https://github.com/caplogic/Mappedbus/issues/1

In short, mappedbus is meant for scenarios where you need to store all messages. If you don't need to do this, it might not be the right solution.

If you do need to store them, just make the file large enough to be able to contain all messages during in the life cycle of the system (for example for one day). And then have a time where you stop all components, rename the file and start them again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.