I'm trying to implement a service in OSGi which should wait for incoming data from another bundle and process the data when it receives it.
I'm using a LinkedBlockingQueue
, because I don't know how many packets I will receive.
My code looks like this:
public class MyClass {
protected static LinkedBlockingQueue<JSONObject> inputQueue = new LinkedBlockingQueue<JSONObject>();
private ExecutorService workerpool = Executors.newFixedThreadPool(4);
public void startMyBundle() {
start();
}
protected void start() {
new Thread(new Runnable() {
public void run() {
while(true){
workerpool.execute(new Runnable() {
public void run() {
try {
process(inputQueue.take());
} catch (InterruptedException e) {
System.out.println("thread was interrupted.");
}
}
});
}
}
}).start();
}
public void transmitIn(JSONObject packet) {
try {
inputQueue.put(packet);
} catch (InterruptedException e) {
}
}
protected void process(JSONObject packet) {
//Some processing
}
When I'm running this, and only send one packet to the service, the packet is first processed as it should, but then my processor uses all its capacity and most of the time I get an OutOfMemoryError
looking like this:
java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "[Timer] - Periodical Task (Bundle 46) (Bundle 46)"
What can be the cause of this?