The following will create a simple OSGi bundle and run it within Eclipse. At the end of this chapter you will also export your bundle to use it later in a standalone OSGi server.
Create the following thread class.
package de.vogella.osgi.firstbundle.internal;
public class MyThread extends Thread {
private volatile boolean active = true;
public void run() {
while (active) {
System.out.println("Hello OSGI console");
try {
Thread.sleep(5000);
} catch (Exception e) {
System.out.println("Thread interrupted " + e.getMessage());
}
}
}
public void stopThread() {
active = false;
}
}
Change the class Activator.java to the following.
package de.vogella.osgi.firstbundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import de.vogella.osgi.firstbundle.internal.MyThread;
public class Activator implements BundleActivator {
private MyThread myThread;
public void start(BundleContext context) throws Exception {
System.out.println("Starting de.vogella.osgi.firstbundle");
myThread = new MyThread();
myThread.start();
}
public void stop(BundleContext context) throws Exception {
System.out.println("Stopping de.vogella.osgi.firstbundle");
myThread.stopThread();
myThread.join();
}
}
Select your manifest.mf, right-click, select Run As-> Run Configuration. Create a OSGi Framework launch configuration. Deselect all bundles except your de.vogella.osgi.firstbundle. Press then "Add Required bundles".
Run this configuration. This should give you the following output. Every 5 second a new message should be written to the console.
Export your bundle. This will allow you to install it into a OSGi runtime. Select your bundle and choose File -> Export -> Plug-in Development -> "Deployable plug-ins and fragment".
Unflag the option to export the source.
0 comments:
Post a Comment