I was attempting to store all the JMS object of the class Message recieved in one minute into a tree map against its precise time as key.After completion of one minute, I wish to serialize the map and return the byte[] to another class. Meanwhile I create a new tree map to store the next set of JMS messages for a minute.
public class StoreMessage {
private static long start_nanotime = System.nanoTime();
private static Thread thisThread = Thread.currentThread();
private static int timeToRun = 60000; // 1 minute
private static byte[] b = null;
public static Map<Long, Message> map1 = new TreeMap<Long, Message>();
public static byte[] store(Message message) {
new Thread(new Runnable() {
public void run() {
try {
sleep(timeToRun);
thisThread.interrupt();
b = serializer.serialize(map1);
new TreeMap<Long, Message>();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
while (!Thread.interrupted()) {
long precise_time = TimeUnit.MILLISECONDS.toNanos(System.currentTimeMillis()) + (System.nanoTime()
- start_nanotime);
map1.put(precise_time, message);
}
return b;
}
}
This code is some how not working. Why? It is giving me error of java.lang.OutOfMemoryError: Java heap space Also I noticed that it keeps writing only one message to the map, ie if the messages were “hi”,”good day to you”–these are two jms messages; the StoreMessage class recieves one message at a time..ie it would first receive “hi” and once this message is processed, then it would retrieve the next message. But what i noticed is, for one whole minute, when the thread is not interrupted, it writes only the first message to the map and gives an error. How do i fix these?
The problem is that your map1 is never actually reset because the assignment is missing.
Of course, you could also simply clear the map: