Reputation: 27
I'm currently using OSGi
to develop a datamanager that periodically obtains data from some sensors. The way I'm acquiring the data is as follows:
public void run() {
while (!stop || !Thread.currentThread().isInterrupted()) {
try {
List<DataEntry> aux;
long millis = System.currentTimeMillis();
for (DataLogger dl : loggers) {
String name = dl.getDriverName();
aux = dataTable.get(name);
if (aux == null) {
aux = new ArrayList<DataEntry>();
}
dl.readValue();
DataEntry de = new DataEntry(dl.getCurrentValue(), millis);
aux.add(de);
dataTable.put(dl.getDriverName(), (ArrayList<DataEntry>) aux);
}
Thread.sleep(PERIOD);
// ***** Exceptions *****
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The problem I'm facing is that I cannot interact with the console
whilst the thread
is running, which means there is no way I can stop the bundle execution from the console and interact with the execution.
Is there any way to have the bundle
running in a different thread than the main one?
*** UPDATE: I was invoking the run method directly, instead of starting the thread and that was the problem.
Upvotes: 0
Views: 92
Reputation: 3323
You've omitted the most important part of your code, which is how the run() method actually gets executed. The fact that it's called run() suggests that the method is in a class that implements Runnable which in turn suggests that you are actually starting a new Thread() to execute it. However I am guessing from your question that you are actually not doing that and running the method directly from the thread that is starting the bundles (invoking it from the BundleActivator's start() method). If that is indeed the case, no other bundles will start and this bundle will remain in STARTING state. To fix that, spawn a new Thread.
But I might be off, please publish the rest of your code! :)
Upvotes: 1