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 |
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 |
Back to top of page
Important Theoretical knowledge
Okay, just now we've developed a Hello world application based on the Equinox framework. We find it doesn't seem very difficult, and many of the jobs Eclipse has done for us, such as the Activator code framework and MANIFEST. MF file, we also learned how to control the OSGi console and write MANIFEST. MF files, but do you really understand how they work? Here we will focus on some of the basics of OSGi running essentials.
What is bundle?
As we have seen, writing a very common Hello world application, you must first create a plug-in project and then edit the method of its Activator class, start
actually we do this by adding a bundle to the OSGi runtime environment, then a bun What are the constituent elements that dle must have?
- MANIFEST. MF: Describes all the characteristics of bundles, including name, Output class or package, imported class or package, version number, etc., refer to table 2. MANIFEST. MF file attributes.
- Code: including the Activator class and some other interfaces and implementations, there is no special difference between this and a normal Java application.
- Resources: Of course, an application cannot have no resource files, slices, properties files, XML files, and so on, these resources can exist with bundles, or they can be joined by fragment bundles.
- The definition of the start level: You can specify it using command line parameters before starting, or you can specify it in run, and the explanation of the specific start levels, please refer to the following instructions.
What did the framework do?
Well, we already know what bundles are and how to develop a basic bundle, so we have to understand that my bundle is in the Equinox framework, what does it do to our bundles?
Figure 11. Equinox Framework Architecture
In fact, the target platform has prepared for us N bundles, they provide a variety of services, OSGi, these bundles are called the system bundle, like a refined house, you only need to bag to stay, no longer need to lay their own floor, installed ceiling.
After our bundle enters the Equinox environment, the OSGI framework does the following things:
- Read the headers information of the bundle, that is, MANIFEST. MF file;
- Loading related classes and resources;
- Parse the dependent packages;
- Call its Activator
start
method and start it;
- Provide them with services such as framework events, service events, etc.;
- Call its Activator
stop
method and stop it;
Status change of Bundles
OK, now that we understand the definition of a bundle and its lifecycle in the OSGi framework, before we see that the console can see the ss
state of all the loaded bundles by command, what are the states of the bundle and how are the transitions between these states? What are the benefits to us when we know what these status messages are?
First, find out what the state of a bundle really is:
Table 3. Bundle Status Table
State name |
meaning |
INSTALLED |
It means that the bundle has been successfully installed. |
|
|
RESOLVED |
A very common state, indicating that the bundle has been successfully parsed (that is, all dependent classes, resources are found), usually before or after the start-up |
STARTING |
Literally, it's starting, but it's not back, so your Activator don't get too complicated. |
ACTIVE |
Active, this is the state we would most like to see, which usually means that the bundle has started successfully, but does not mean that the service provided by your bundle is OK |
STOPPING |
Literally, being stopped, and not yet returned |
UNINSTALLED |
Uninstalled, the status can no longer be changed |
Here's a picture of the classic OSGi bundle change state:
Figure 12. OSGi Bundle Change state diagram
Bundle Import Export Package
OK, so far it seems that everything is fresh, but you seem to be considering what the advantages of OSGi are, and here's one of the features that almost all of the component-oriented frameworks need to achieve their purpose: service-oriented, encapsulation implementation . This is hard to do in common Java applications, where all classes are exposed to classpath, and people are free to view your implementation or even change your implementation. This is fatal for the company that wants to publish the component.
Figure 13. OSGi Bundle principle
OSGi solves this problem very well, as shown in the diagram above, where each bundle can have its own public and hidden parts, and each bundle can only see its own public parts, hidden parts, and the public parts of other bundles.
Bundle of MANIFEST. MF files provide the keywords for the Export/import package, so you can simply EXPORT the packages you want others to see and hide the implemented packages. And you can make a version number for them so that you can publish different versions of the package at the same time.
Bundle class Path
This is difficult to understand, in general, you do not need to care about this matter, unless there is a problem, you find this class is here, how is the report classnotfoundexception/noclassdefexcpetion it? Before you fall down and get ready to smash your computer monitor, take a look at how the classes in the bundle are found:
- First of all, it will find the JRE, this is obviously, this is actually found through the system environment
JAVA_HOME
, the path is generally java_home/lib/rt.jar, Tools.jar and ext directory, endorsed directory.
- Second, it will find the package exported by the system bundle.
- Then it will look for your import package, which actually contains two: one is all imported directly through the Require-bundle, and one is the package that was previously said to be imported through the import packages method.
- Look for its fragment bundle, if any.
- If it is not found, it will find its own classpath path (each bundle has its own classpath).
- Finally it tries to find the reference based on the Dynamicimport-package property.
Start level
In the Equinox environment, when we were configuring the Hello World app, we saw that we kept the framework start level at 4, and the start level of Hello World bundle to 5. The higher the start level, the higher the start order. In the actual application environment, our bundles have a certain dependence on each other, so in the starting order to be different, like building, to start from the foundation.
In fact, the initial start level of the OSGi framework is 0 and the boot sequence is as follows:
- Add the start level plus one, and if you find a matching bundle (that is, the bundle's start level and the current boot level are equal), start the bundle;
- Continue to the first step until you find that all the bundles have been started, and that the active start level and the last initiated bundle start level are the same.
The stop order is also the first to set the system's start level to 0:
- Since the current active boot level of the system is greater than the requested starting level, the system first stops the bundles that are equal to the current active start levels;
- Reduce the active start level by one and continue to the first step until you find that the active start level and the request level are equal, both 0.
Back to top of page
Develop a real-world OSGi application
We can't just stay on the level of Hello World, although that was important to us, but the reality requires that we be able to use OSGi to write exciting applications that can be accepted by the client, approved by the architect, and affirmed by the programmer. OK, let's get started. The following will focus on some of the OSGi scenarios that some real-world applications might require.
Publishing and using Services
Since the OSGi framework can easily hide implementation classes, it is natural to provide interfaces externally, and the OSGi framework provides the registration and querying capabilities of the service. OK, so let's actually do it on the basis of the Hello World project.
We need to take the following steps:
- Define a service interface and export out for other bundles to use;
- Defines a default service implementation, and hides its implementation;
- After the Bundle is started, the service needs to be registered with the Equinox framework;
- Query this service from the framework, and test the availability.
OK, in order to achieve the above requirements, we actually operate as follows:
- Define a new package to
osgi.test.helloworld.service
store the interface. The benefit of a single package is that you can simply export the package to other bundles and hide all of the implementation classes
- Create a new interface in the package above
IHello
and provide a simple string service with the following code: Listing 2. IhelloPackage osgi.test.helloworld.service; Public interface Ihello { /** * Gets the interface for hello information. * @return The Hello string. * /String Gethello ();}
- Create a new package to
osgi.test.helloworld.impl
hold the implementation class.
- Create a new class in the package above
DefaultHelloServiceImpl
to implement the above interface: Listing 3. Ihello Interface Implementationpublic class Defaulthelloserviceimpl implements Ihello { @Override public String Gethello () { return " Hello osgi,service "; } }
- registration Services, the OSGI framework provides two ways of registering, both through
bundlecontext
class:
-
Registerservice (string,object,dictionary)
Register service object object
to interface name String
, you can carry an attribute dictionary Dictionary
;
-
Registerservice (string[), object,dictionary)
Register service object object
to interface an array group string[]
, You can carry a property dictionary Dictionary
, where a service object can be registered by multiple interface names because the class can implement multiple interfaces;
We use the first method of registration to modify   Activator
class start
method, add registration code:
Listing 4. Join the registration Code public void Start (Bundlecontext context) throws Exception {System.out.println ("hel Lo World "); Context.registerservice (IHello.class.getName (), New Defaulthelloserviceimpl (), NULL); }
- In order for our service to be used by other bundles, it must be MANIFEST. To export the declaration in MF, double-click MANIFEST. MF, find runtime > Exported packages > Click Add, and select Service package: Figure 14. Select the exported Service pack
- In addition, a new bundle, similar to Hello World, is called: to test the availability of the
osgi.test.helloworld2
osgi.test.helloworld
services provided by the bundle;
- Add Import Package: MANIFEST in the second bundle. MF file, find dependencies > Imported Packages > Add ..., select the osgi.test.helloworld.service we just export Package: Figure 15. Select the Osgi.test.helloworld.service package that you just export
- Query service: Similarly, the OSGI framework provides two ways to query the service's references
ServiceReference
:
getServiceReference(String)
: A reference to the service based on the name of the interface;
getServiceReferences(String,String)
: A reference to the service based on the interface name and the filter corresponding to the name of the other filter;
- Here we use the first method of querying,
osgi.test.helloworld2
Activator
start
Adding queries and test statements in the bundle's method: Listing 5. Adding queries and test statementspublic void Start (Bundlecontext context) throws Exception { System.out.println ("Hello world2"); /** * Test Hello service from Bundle1. */ Ihello hello1 = (Ihello) context.getservice ( context.getservicereference (IHello.class.getName ())); System.out.println (Hello1.gethello ()); }
- Modify the operating environment because we have added a bundle, so we also need to add the configuration information for the new bundle in the run configuration as shown in Figure 16. Add configuration information to the new bundle
- execution, the following results are obtained: Figure 17. Execution results
Congratulations, you have succeeded!
OSGI theory Knowledge