I'm having a problem trying to integrate an OSGi bundle that implements a countdown in order to send a message in an specific time lapse. This is made by a runnable class, or thread.
The idea is the next; I've got bundle Sender
, bundle Receiver
and bundle Countdown
.
The Sender bundle sends a message after a time lapse, f.e: 5 seconds.
Sender ->Create message
Countdown -> Count to 5 s.
Receiver ->Receive the message after 5 s.
This is just a "metaphore" of my application, but well, I believe you got the idea.
The problem I'm experiencing is the next one: when the bundle Countdown
is running its 5 seconds countdown, the bundle Sender
stops working (it's OKAY, no problem here), but the bundle Receiver
also stops working. My idea was to implement the countdown in a separate bundle just to avoid other bundles to stop working, but I guess I was wrong.
So, my question is... how could I implement a countdown that doesn't unnecessarily stop all the bundles of the OSGi application?
Thanks in advance.
Some code:
Countdown
public void countDown()
{
int i = delay;
while (i>0){
try {
i--;
Thread.sleep(1000); // 1000ms = 1 s
}
catch (InterruptedException e) {
//countdown failure? --> nah
}
}
}
Sender
Thread t = new Thread(new Countdown(5));
t.start();
while (t.isAlive());
System.out.println("Sending the message");
OSGi bundles are neither separate processes, nor separate threads. A bundle doesn't "run"—its code finds itself on the execution path of zero or more threads at any point.
Therefore your question is not at all about OSGi, but about general multithreading in Java. If you want to block a thread while it waits for an answer, and want some other code to keep running, then you must see to it that the other code has its own thread to run in.