OSGI notes (i) Helloworld_osgi

Source: Internet
Author: User
Recently changed jobs, a lot of new things to learn, work more, blog has not been updated, sin ah. In recent days to see the knowledge of OSGi, while learning to remember it, to record their own way. Talk less, start.

The concept of OSGi I will not say, Baidu Google a bunch of random look at the, anyway, I am not a lecturer, more learning will understand. Today, just start by remembering a helloworld,yes,you are my world!

In general, learning a new technology, programmers are accustomed to first development of a Hello World application, which seems to be an "industrial standard." OK, let's get started, it's not difficult to develop a simple OSGi application: Create a plug-in project, File > New > Project, select Plug-in development > Plug-in PROJEC T

Figure 1. New Plug-in Project


In establishing the project's first wizard, fill in the project name: Osgi.test.helloworld, using the default engineering path. Note the choice of the target platform, because our project is a generic OSGi bundle, so choose Equinox.

Figure 2. Fill in Project name and select target platform


In the next wizard interface, fill in some of the plug-in information you need (note that the plug-in concept in Eclipse is basically similar to the concept of bundle in OSGi), where you need to fill in the OSGi provider (vendor) and classpath. If you don't have a special design, you can generally ignore these two fields. The last part about Activator is that if it's not a fragment bundle it needs to be filled in, which doesn't seem to be necessary unless your bundle to implement the framework's event monitor yourself. Therefore, it is recommended that you use the default settings, as shown in Figure 3:


Figure 3. Use default settings


Activator: This is the program entry that was first invoked when the bundle started, equivalent to the main function in the Java module. The difference is that main needs to be invoked through the command line, and OSGi's Activator is passively accepting calls from the OSGi framework, which starts only when the message is received.

Best Practice: Do not write too many startup code in the Activator, otherwise it will affect the bundle boot speed and the associated service startup can be placed in the service listener. The final step is not to use any of the templates, so check out the default options and click Finish, as shown in Figure 4: 

Figure 4. Check out the default option
 

Complete, the basic plug-in view as shown in Figure 5,eclipse will build the same path under the project name Java Package, which contains the Activator class, and plug-in configuration information is also placed in the MANIFEST. MF file, we have quite a lot of work to do in the future.  

Figure 5. Basic Plug-in View
 

Edit Activator.java, enter Hello World statement, code as follows: 

Listing 1. Edit Activ Ator.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 { 
	    <strong>system.out.println ("Hello World"); </strong >
	 } * * 
	 (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 invocation of the framework. After the frame is started, each bundle is invoked to invoke each bundle's Activator.

Note: The bundle Activator must contain parameterless constructors so that the framework can use Class.newinstance () to reflect the Activator instance of the constructed bundle.

Here we fill in the Hello World string we want to output in the Start method. So, how can we start this bundle? Executive: Select Run > Open run Dialog, go to the running menu, right-click in the OSGi framework and select New OSGi runtime Environment, as shown in:

Figure 6. New OSGi Runtime Environment


In the right side of the running Environment dialog box, enter the name of the running environment, start level and dependent plug-ins, because we do not need other third-party plug-ins, so we only need to check the system Org.eclipse.osgi Plug-ins, if not selected this plugin, Hello World Will not run. As shown in Figure 7, only when you click on the Validate Bundles button, and the hint is no problem, it shows that your operating environment is basically OK.

Figure 7. Select Org.eclipse.osgi Plugin


Dependent plug-in selection:

Figure 8. Choice of dependent Plug-ins


OK, if your running environment is OK, click Run.

Figure 9. Running an OSGi project


Congratulations on your success.

OSGi Console

The OSGi console is still relatively new to developers who are accustomed to developing common Java applications. In general, through the OSGi console, you can manage the lifecycle of all bundle in the system, as well as view the system environment, start, stop the entire frame, set the startup level, and so on. As shown in Figure 10, you can view the status of all bundle by typing SS:
Figure 10. View the status of all bundle

The main console commands are listed below:
Table 1. Equinox OSGi's main console command table
Category command meaning control framework launch start frame shutdown Stop frame close closes, exit frame exit exit immediately, equivalent to system.exit init uninstall all bundle (provided it has shutdown) SetProp set Set properties, at run Time Control bundle Install Install uninstall uninstall start stop Refresh Refresh Update show state status status show installed bundle and registered service Ss Show All Bundle's simple status services display the details of the registration service Packages show the status of the import, export package bundles show the status of all installed bundles Headers Display bundles header information, that is MANIFEST.M Content log in F display log entry information other Exec executes a command (blocking state) in another process Fork and Exec do not cause blocking Gc to prompt garbage collection Getprop Get Properties, or a property to control startup level Sl get a bu Ndle or the start level information for the entire frame SETFWSL setting the start level of the frame SETBSL setting bundle start level SETIBSL setting initialization bundle start level

MANIFEST. Mf

MANIFEST. MF may appear in any Jar package that includes main class information, typically located 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 and the common Java environment consistent and easy to deploy on older systems. Table 2 lists the important attributes in this file and their implications:
Table 2. MANIFEST. MF file Properties
Property name meaning bundle-activator Bundle initiator bundle-symbolicname name, typically using a JAVA package path-like name named Bundle-version version, note that different versions of the same name Bundle can deploy Export-package exported Package declarations at the same time, and other bundle can directly reference Import-package imported Package Eclipse-lazystart Whether it is only started when referenced Require-bundle Bundle, which does not recommend Bundle-classpath this Bundle class path, can contain other resource paths bundle-requiredexecutionenvironment this Bundle the required execution environment, such as the JDK version declaration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.