Producer/Consumer example.

This is the example from the second quickstart tutorial with extra annotations.

Message.java

package com.test;

import org.sourceforge.jemm.Entity;

@Entity // 1
public class Message {
        protected final String message;  // 2
        
        public Message(String message) {
                this.message = message;
        }
        
        public String getMessage() {
                return message;
        }       
}
  1. The @Entity Annotation tells Jemm that this is a persistent class.
  2. The stored message (in a protected field with accessors.

Consumer.java

package com.test;

import org.sourceforge.jemm.RemoteStore;
import org.sourceforge.jemm.Session;
import org.sourceforge.jemm.collections.JemmList;

public class Consumer {

        public static void main( String[] args ) {
                Session.setStore(new RemoteStore(args[0],Integer.parseInt(args[1]))); // 1

                System.out.println("Consumer started");
                JemmList<Message> queue = getQueue(); // 2
                
                for(int i=0;i<120;i++) {  // 3
                        while(queue.size() > 0) {  // 4
                                Message message = queue.remove(0); // 5
                                System.out.println("Got message: " + message.getMessage());
                        }
                        
                        try {
                                Thread.sleep(1000);
                        } catch (InterruptedException e) {
                                // do nothing
                        }
                }
                
                Session.shutdown();
                System.out.println("Consumer finished");
    }

        // put here so the code can be shared with Producer
        @SuppressWarnings("unchecked")
        public static JemmList<Message> getQueue() {  // 6
                JemmList<Message> queue = (JemmList<Message>) Session.getRoot("messageQueue");                          
                if(queue == null)
                        queue = (JemmList<Message>) Session.setRootIfNull("messageQueue", 
                                        new JemmList<Message>());
                return queue;
        }
}
  1. Create a RemoteStore with the hostname and port of the JemmServer. There is no error checking in this example, if there parameters were invalid or the server was not running an exception will occur
  2. Use the static method to get the queue, the logic has been factored out so it can be shared with the Producer class.
  3. Loop for 120 seconds, checking the queue every second.
  4. Checks whether there are any messages on the queue
  5. Removes the first message from the queue. - n.b. because the check and fetch are separate operations there is a potential race condition if a second consumer was running (with one message in the queue, both threads could pass the check, then one reads the message and the other gets an exception.)
  6. Standard root logic to ensure that the there is no race condition on the setting of root values.

Producer.java

package com.test;

import java.util.Random;

import org.sourceforge.jemm.RemoteStore;
import org.sourceforge.jemm.Session;
import org.sourceforge.jemm.collections.JemmList;

public class Producer {

        public static void main( String[] args ) {
                Session.setStore(new RemoteStore(args[0],Integer.parseInt(args[1]))); // 1
                
                System.out.println("Producer started");
                JemmList<Message> queue = Consumer.getQueue(); // 2 

                Random random = new Random();
                for(int i=0;i<10;i++) {
                        try {
                                int delay = random.nextInt(4000)+1; 
                                System.out.println("Waiting " + delay + "ms");
                                Thread.sleep(delay); // 3
                        } catch (InterruptedException e) {
                                // do nothing
                        }
                        
                        
                        Message message = new Message("Hello " + i); // 4
                        System.out.println("Sending message " + message.getMessage());
                        queue.add(message);  // 5
                }

                Session.shutdown();
                System.out.println("Producer finished");
    }
}
  1. Create a RemoteStore exactly as with Consumer.
  2. Use the static method to get the queue, as per Consumer.
  3. Add random delays for the message sending.
  4. Creates a message.
  5. Add it to the queue and now any JEMM client of that RemoteStore can see the message.