OSGi Development with Eclipse IDE and JBoss AS 7
Z Jacek Laskowski - Wiki Projektanta Java EE
It's been just a couple of days since the latest candidate release of JBoss Application Server 7.0.0.CR1 (codenamed "White Rabbit") was released and with its support for OSGi Service Platform Release 4 Version 4.2 (with Apache Felix), OSGi Service Platform Release 4 Version 4.2 Compendium Specification and OSGi Service Platform Release 4 Version 4.2 Enterprise Specification (with Apache Aries Blueprint 0.3.0) it makes an interesting platform to pick low-hanging fruits of OSGi and Java EE 6 integration.
In this article I'm describing how to use Eclipse IDE 3.7 (along with JBoss Tools 3.3.0.M2 Development Release) to create and deploy a OSGi service component (as defined in OSGi Declarative Services Specification) to JBoss AS 7.0.0.CR1. I'm on the lookout for a simple yet extensive OSGi tooling environment and Eclipse IDE is always referred as such.
Spis treści |
JBoss AS 7.0.0.CR1 and its OSGi subsystem
There's the JBoss AS Documentation in the works and I'm not going to delve into JBoss AS 7 too much. There's a lot of other sources you can learn from so what I'm going to point out is solely how to set up JBoss AS 7 so it's fully configured for our OSGi development needs.
In its default configuration, JBoss AS 7's OSGi subsystem is activated at startup that's shown below.
jacek:~/apps/jboss $ ./bin/standalone.sh ========================================================================= JBoss Bootstrap Environment JBOSS_HOME: /Users/jacek/apps/jboss JAVA: java JAVA_OPTS: -Xms64m -Xmx512m -XX:MaxPermSize=256m -Djava.net.preferIPv4Stack=true -Dorg.jboss.resolver.warning=true -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000 -Djboss.modules.system.pkgs=org.jboss.byteman ========================================================================= 12:10:06,838 INFO [org.jboss.modules] JBoss Modules version 1.0.0.CR4 12:10:07,808 INFO [org.jboss.msc] JBoss MSC version 1.0.0.CR2 12:10:07,898 INFO [org.jboss.as] JBoss AS 7.0.0.CR1 "White Rabbit" starting ... 12:10:12,057 INFO [org.jboss.as.osgi] (Controller Boot Thread) Activating OSGi Subsystem ... 12:10:13,752 INFO [org.jboss.as] (Controller Boot Thread) JBoss AS 7.0.0.CR1 "White Rabbit" started in 7261ms - Started 91 of 146 services (55 services are passive or on-demand)
Its configuration is in the standalone/configuration/standalone.xml file. The first section is about JBoss AS 7's extensions and the org.jboss.as.osgi module is amongst them.
When you search for "osgi" in the file, the other match will be the OSGi subsystem's configuration. For brevity's sake, I'll only show the most important part of the configuration.
<subsystem xmlns="urn:jboss:domain:osgi:1.0" activation="lazy"> <configuration pid="org.apache.felix.webconsole.internal.servlet.OsgiManager"> <property name="manager.root"> jboss-osgi </property> </configuration> <properties> <property name="org.osgi.framework.startlevel.beginning"> 3 </property> <property name="org.osgi.framework.executionenvironment"> OSGi/Minimum-1.1,J2SE-1.4,J2SE-1.5,JavaSE-1.6 </property> </properties>
Change the org.osgi.framework.startlevel.beginning and org.osgi.framework.executionenvironment properties to the above values so the other auxiliary bundles are started upon the first OSGi bundle deployment (which is to be ours relying on a few). The properties are the standard properties described in the OSGi 4.2 Core spec, and am suggesting its reading for a better understanding of their implications.
Upon the first bundle deployment, the other bundles will get installed and activated on demand (they're mostly in lazy initialization mode).
12:11:38,908 INFO [org.jboss.osgi.framework.internal.BundleManager] (MSC service thread 1-3) JBossOSGi Framework Core - 1.0.0.CR6 12:11:39,010 INFO [org.jboss.osgi.framework.internal.BundleManager] (MSC service thread 1-2) Install bundle: system.bundle:0.0.0 12:11:39,722 INFO [org.jboss.osgi.framework.internal.BundleManager] (MSC service thread 1-2) Install bundle: org.jboss.logging:3.0.0 12:11:39,800 INFO [org.jboss.osgi.framework.internal.BundleManager] (MSC service thread 1-2) Install bundle: jboss-osgi-logging:1.0.0 12:11:39,828 INFO [org.jboss.osgi.framework.internal.BundleManager] (MSC service thread 1-3) Install bundle: org.apache.felix.log:1.0.0 12:11:39,909 INFO [org.jboss.osgi.framework.internal.BundleManager] (MSC service thread 1-1) Install bundle: org.apache.felix.metatype:1.0.4 12:11:39,912 INFO [org.jboss.osgi.framework.internal.BundleManager] (MSC service thread 1-1) Install bundle: javaee.api:0.0.0 12:11:39,973 INFO [org.jboss.osgi.framework.internal.BundleManager] (MSC service thread 1-4) Install bundle: org.apache.aries.jmx:0.3.0 12:11:39,988 INFO [org.jboss.osgi.framework.internal.BundleManager] (MSC service thread 1-4) Install bundle: jboss-as-osgi-configadmin:7.0.0.CR1 12:11:40,134 INFO [org.jboss.osgi.framework.internal.BundleManager] (MSC service thread 1-1) Install bundle: org.apache.felix.configadmin:1.2.8 12:11:40,250 INFO [org.jboss.osgi.framework.internal.BundleManager] (MSC service thread 1-1) Install bundle: jboss-osgi-jmx:1.0.10 12:11:40,273 INFO [org.jboss.osgi.framework.internal.BundleManager] (MSC service thread 1-3) Install bundle: jbosgi-webapp:1.0.2 12:11:40,389 INFO [org.jboss.osgi.framework.internal.BundleManager] (MSC service thread 1-3) Install bundle: jbosgi-blueprint:1.0.2 12:11:40,445 INFO [org.jboss.osgi.framework.internal.BundleManager] (MSC service thread 1-3) Install bundle: jbosgi-webconsole:1.0.6 12:11:40,566 INFO [org.jboss.osgi.framework.internal.BundleManager] (MSC service thread 1-2) Install bundle: org.apache.felix.webconsole:3.1.6.SP1 12:11:40,651 INFO [org.jboss.osgi.framework.internal.BundleManager] (MSC service thread 1-1) Install bundle: org.apache.felix.eventadmin:1.2.6 12:11:40,728 INFO [org.jboss.osgi.framework.internal.BundleManager] (MSC service thread 1-1) Install bundle: org.apache.aries.util:0.3.0 12:11:40,806 INFO [org.jboss.osgi.framework.internal.BundleManager] (MSC service thread 1-3) Install bundle: jboss-osgi-xerces:2.9.1.SP7 12:11:41,001 INFO [org.jboss.osgi.framework.internal.BundleManager] (MSC service thread 1-2) Install bundle: jboss-osgi-http:1.0.3 12:11:41,086 INFO [org.jboss.osgi.framework.internal.BundleManager] (MSC service thread 1-4) Install bundle: osgi.cmpn:4.2.0.200908310645 12:11:41,516 INFO [org.jboss.osgi.framework.internal.StartLevelPlugin] (MSC service thread 1-1) Starting bundles for start level: 1 12:11:41,863 INFO [org.jboss.osgi.framework.internal.HostBundleState] (MSC service thread 1-1) Bundle started: org.apache.felix.configadmin:1.2.8 12:11:41,887 INFO [org.jboss.osgi.framework.internal.HostBundleState] (MSC service thread 1-1) Bundle started: org.apache.felix.log:1.0.0 12:11:41,921 INFO [org.jboss.osgi.framework.internal.HostBundleState] (MSC service thread 1-1) Bundle started: jboss-osgi-logging:1.0.0 12:11:41,933 INFO [org.jboss.osgi.framework.internal.HostBundleState] (MSC service thread 1-1) Bundle started: jboss-as-osgi-configadmin:7.0.0.CR1 12:11:41,934 INFO [org.jboss.osgi.framework.internal.StartLevelPlugin] (MSC service thread 1-1) Starting bundles for start level: 2 12:11:42,066 INFO [org.jboss.osgi.framework.internal.HostBundleState] (MSC service thread 1-1) Bundle started: org.apache.aries.jmx:0.3.0 12:11:42,307 INFO [org.jboss.osgi.framework.internal.HostBundleState] (MSC service thread 1-1) Bundle started: jboss-osgi-jmx:1.0.10 12:11:42,341 INFO [org.jboss.osgi.framework.internal.HostBundleState] (MSC service thread 1-1) Bundle started: org.apache.felix.metatype:1.0.4 12:11:42,477 INFO [org.jboss.osgi.framework.internal.HostBundleState] (MSC service thread 1-1) Bundle started: org.apache.felix.eventadmin:1.2.6 12:11:42,730 INFO [org.jboss.osgi.framework.internal.HostBundleState] (MSC service thread 1-1) Bundle started: jboss-osgi-http:1.0.3 12:11:42,999 INFO [org.jboss.osgi.framework.internal.HostBundleState] (MSC service thread 1-1) Bundle started: org.apache.felix.webconsole:3.1.6.SP1 12:11:43,000 INFO [org.jboss.osgi.framework.internal.StartLevelPlugin] (MSC service thread 1-1) Starting bundles for start level: 3 12:11:43,185 INFO [org.jboss.osgi.framework.internal.HostBundleState] (MSC service thread 1-1) Bundle started: jbosgi-blueprint:1.0.2 12:11:43,362 INFO [org.jboss.osgi.framework.internal.HostBundleState] (MSC service thread 1-1) Bundle started: jboss-osgi-xerces:2.9.1.SP7 12:11:43,496 INFO [org.jboss.osgi.framework.internal.HostBundleState] (MSC service thread 1-1) Bundle started: jbosgi-webapp:1.0.2 12:11:43,503 INFO [org.jboss.osgi.framework.internal.FrameworkActive] (MSC service thread 1-1) OSGi Framework started
With the changes JBoss AS 7's OSGi subsystem is ready to handle the bundle of ours which we develop in Eclipse IDE.
Eclipse IDE 3.7 and JBoss Tools 3.3.0.M2
By default JBoss AS 5 is the most recent, supported version of JBoss AS in Eclipse IDE. You need an Eclipse server adapter for JBoss AS 7 and that's what JBoss Tools tooling provides.
Installing JBoss Tools 3.3.0.M2
JBoss Tools 3.3.0.M2's installation is a simple affair and boils down to a few clicks in the Help > Install New Software... wizard.
According to JBoss Tools 3.3.0.M2 Development Release page, you should use http://download.jboss.org/jbosstools/updates/development/indigo/ for the latest development release of the Core tools.
Select Web and Java EE Development category which includes JBossAS Tools and the required adapter (although the Details section below may have implied that it would not).
Once JBoss Tools is installed, you will see JBoss AS 7.0 server available in the Servers view.
Registering JBoss AS 7 server
Eclipse IDE has to know about the server location and other configuration settings. You use the Servers view of Eclipse to configure one.
Use an appropriate value for Home Directory where JBoss AS 7 CR1 resides.
The final result is as depicted on the screenshot.
You can manage the server instance easily. Start and stop it to ensure its proper working. Note the OSGi subsystem being started up.
Developing OSGi Declarative Services component
Inspired by the excellent blog post Using OSGi in JBoss AS7 was the main reason I begun my research of the advances of Eclipse IDE, JBoss Tools and JBoss AS 7's OSGi subsystem. The difference lies in the bundle we're about to create which is a OSGi Declarative Services component (not a mere bundle), and besides, no OSGi knowledge is likely required (yet some would help certainly).
Creating OSGi bundle as a Eclipse plug-in project
Create a Plug-in Project.
Name it as you wish to, i.e. jboss7-osgi-ds.
Select standard under the "an OSGi framework" radio button.
Uncheck "Generate an activator, a Java class that controls the plug-in's life cycle" as we don't use it (and neither will we be exposed to other OSGi goodies that much).
In the following panel you can select the template for your bundle. We'll use OSGi Declarative Services Example.
That's it. Press Finish.
Registering (deploying) the bundle to JBoss AS 7
The plug-in project has little to no use for JBoss AS 7 unless we think about its OSGi layer. That's what JBoss AS 7 supports and to have the project to be introduced to the server instance, use Mark as Deployable menu.
That's what registers the plug-in (OSGi bundle) project in JBoss AS 7. If the server was running, the plug-in would momentarily be deployed. Run the server and give it a try.
You should see the following error:
Caused by: org.jboss.osgi.resolver.XResolverException: Unable to resolve Module[pl.jaceklaskowski.osgi:1.0.0]: missing requirement [Module[pl.jaceklaskowski.osgi:1.0.0]] package; (&(package=org.eclipse.osgi.framework.console)(version>=1.0.0)) at org.jboss.osgi.resolver.felix.FelixResolver.resolveInternal(FelixResolver.java:117) at org.jboss.osgi.resolver.spi.AbstractResolver.resolve(AbstractResolver.java:148) at org.jboss.osgi.framework.internal.ResolverPlugin.resolve(ResolverPlugin.java:155) ... 12 more
Resolving bundle dependencies
If we had deployed the bundle to the Eclipse Equinox OSGi runtime, the error might have not surfaced. There are the bundle's requirements (dependencies) JBoss AS 7 doesn't provide and we will have to deploy them by themselves.
You will need the following bundles for a successful configuration of JBoss AS 7:
- org.eclipse.equinox.util_1.0.300.v20110502.jar
- org.eclipse.osgi.util_3.2.200.v20110110.jar
- org.eclipse.osgi_3.7.0.v20110613.jar
Copy them over to the standalone/deployments/ directory of JBoss AS 7 so they're automatically deployed.
JBoss OSGi Web Console
Open up the JBoss OSGi Web Console at http://localhost:8090/jboss-osgi.
Run our bundle - the one with id 19 - using the Start button (on the right).
Its status should change to Active and the following message will be printed out on the console:
14:23:10,204 INFO [org.jboss.osgi.framework.internal.HostBundleState] (qtp1361612306-59) Bundle started: pl.jaceklaskowski.osgi:1.0.0
Why it provides no services is still a mystery to me and am working on it. The other exceptions you may have noticed in the JBoss AS 7 console are already reported and most worked out (consult JBoss OSGi JIRA).













