HelloWorld
In general, learning a new technology, programmers are accustomed to first develop a Hello World application, which seems to be an "industry standard." OK, let's get started, it's not difficult to develop a simple OSGi application, as follows: Build a plug-in project, File > New > Project, select Plug-in development > Plug-in PROJEC T
Figure 1. New Plug-in Project
In the first wizard to build the project, fill in the name of the project: Osgi.test.helloworld, using the default project path. Note the choice of the target platform, because our project is a generic OSGi bundle, so choose Equinox.
Figure 2. Fill in the project name and select the target platform
In the next wizard interface, fill in the required plug-in information (note that the plug-in concept in Eclipse is basically similar to the concept of bundles in OSGi), which needs to be populated with OSGi provider (vendors) and classpath. If there is no special design, these two fields can generally be ignored. The last part about activator, if not a fragment bundle, needs to be filled in, unless your bundle implements its own framework for event monitoring, which seems unnecessary. Therefore, it is recommended to use the default settings, as shown in Figure 3:
Figure 3. Use default settings
Activator: This is the program entry that is called first when the bundle starts, equivalent to the main function in the Java module. The difference is that main needs to be called through the command line, and the Activator of OSGi is passively accepting calls from the OSGi framework, and will not start until the message is received.
Best Practice: Do not write too many startup code in Activator, otherwise it will affect the bundle start speed, the related service start can be placed in the service listener. The last step, do not use any template, so tick off the default options, click Done, as shown in Figure 4:
Figure 4. Check out the default options
Complete, the basic plug-in view as shown in Figure 5,eclipse will create the same path in the project name Java Package, which contains the Activator class, and the configuration information for the plugin is also placed in the MANIFEST. MF file, we will have quite a lot of work to do in the future.
Figure 5. Basic plug-in view
Edit Activator.java, enter the Hello World statement, the code is as follows:
Listing 1. Edit Activator.java
Package Osgi.test.helloworld;
Import Org.osgi.framework.BundleActivator;
Import Org.osgi.framework.BundleContext;
public class Activator implements Bundleactivator {
/* * (Non-javadoc)
* @see Org.osgi.framework.BundleActivator
* #start (org.osgi.framework.BundleContext) */public
void Start (Bundlecontext context) throws Exception {
System.out.println ("Hello World");
}
/*
* (non-javadoc)
* @see org.osgi.framework.BundleActivator
* #stop ( Org.osgi.framework.BundleContext)
*
/public void Stop (Bundlecontext context) throws Exception {
}
} |
We can see that each Activator actually implements the Bundleactivator interface, which enables Activator to accept the call of the framework. Once the framework is started, each bundle's Activator will be invoked when each bundle is started.
Note: The bundle's Activator must contain a parameterless constructor so that the framework can use Class.newinstance () to reflect the Activator instance that constructs the bundle.
Here we fill in the Start method with the Hello world string we want to output. So, how do you start this bundle? Execution: Select Run > Open run Dialog, go to the Run menu, right-click in the OSGi framework to select New OSGi runtime Environment, as shown in figure:
Figure 6. New OSGi Runtime Environment
In the Run Environment dialog box on the right, enter the name of the running environment, start level and dependent plug-ins, because we do not need other third-party plug-ins at present, so only need to tick the system Org.eclipse.osgi plug-in, if not select this plugin, Hello World Will not run. As shown in Figure 7, only if you click on the Validate Bundles button and you are prompted for no problem, it indicates that your operating environment is basically OK.
Figure 7. Select the Org.eclipse.osgi plugin
Dependent plug-in selection:
Figure 8. Dependent plug-in selection
OK, if your environment is OK, click Run.
Figure 9. Running an OSGi project
Congratulations, you've succeeded.
Back to top of page
OSGi Console
The OSGi console is relatively fresh for developers who are accustomed to developing common Java applications. In general, with the OSGi console, you can manage the lifecycle of all bundles in the system, as well as view the system environment, start, stop the entire frame, set the boot level, and so on. As shown in Figure 10, you can see the status of all bundles by typing SS:
Figure 10. View the status of all bundles
The main console commands are listed below:
Table 1. Equinox OSGi main console command table
Category |
Command |
Meaning |
Control framework |
Launch |
Start frame |
Shutdown |
Stop frame |
Close |
Close, exit frame |
Exit |
Exit immediately, equivalent to System.exit |
Init |
Uninstall all bundles (if already shutdown) |
SetProp |
Set properties, at run time |
Control bundles |
Install |
Installation |
Uninstall |
Unloading |
Start |
Start |
Stop |
Stop it |
Refresh |
Refresh |
Update |
Update |
Show status |
Status |
Show installed bundles and registered services |
Ss |
Show the simple state of all bundles |
Services |
Show details about registration services |
Packages |
Show the status of the import and export package |
Bundles |
Show the status of all installed bundles |
Headers |
Display bundles's header information, namely MANIFEST. What's in MF |
Log |
Display LOG Entry Information |
Other |
Exec |
Execute a command (blocking state) in another process |
Fork |
Unlike EXEC, it doesn't cause blocking. |
Gc |
Promote garbage collection |
Getprop |
Get a property, or a property |
Control the start level |
Sl |
Get start level information for a bundle or the entire frame |
Setfwsl |
Set the start level of the frame |
Setbsl |
Set the start level of the bundle |
Setibsl |
Set the start level of the initialization bundle |
Back to top of page
MANIFEST. Mf
MANIFEST. MF may appear in any Jar package that includes the main class information, typically in the Meta-inf directory, so this file is not an OSGi-specific thing, but simply adds some attributes, which just keeps the OSGI environment consistent with the normal Java environment and facilitates deployment in older systems. Table 2 lists the important attributes in this file and their meanings:
Table 2. MANIFEST. MF file Properties
Property name |
Meaning |
Bundle-activator |
The initiator of the Bundle |
Bundle-symbolicname |
Names, typically named with JAVA package path-like names |
Bundle-version |
Version, note that different versions of bundles with the same name can be deployed simultaneously |
Export-package |
The exported package declaration, the other bundles can be directly referenced |
Import-package |
The imported Package |
Eclipse-lazystart |
Whether to start only if it is referenced |
Require-bundle |
Fully dependent bundles, not recommended |
Bundle-classpath |
The class path of this bundle can contain some other resource paths |
Bundle-requiredexecutionenvironment |
The execution environment required for this bundle, such as the JDK version declaration |
Categories: Notes, OSGI