This is the example from the second quickstart tutorial with extra annotations.
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;
}
}
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;
}
}
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");
}
}