Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.此内容没有您所选择的语言版本。
2.5. Configure the Application
OSGi Config Admin service 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
The OSGi Config Admin service is a standard OSGi configuration mechanism that enables administrators to modify application configuration at deployment time and at run time. This contrasts the with settings made directly in a Spring XML file or a blueprint XML file, because these XML files are accessible only to the developer.
The OSGi Config Admin service relies on the following basic concepts:
- Persistent ID
- A persistent ID (PID) identifies a group of related properties. Conventionally, a PID is normally written in the same format as a Java package name. For example, the
org.ops4j.pax.webPID configures the Red Hat JBoss Fuse container's default Jetty Web server. - Properties
- A property is a name-value pair, which always belongs to a specific PID.
Setting configuration properties 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
There are two main ways to customise the properties in the OSGi Config Admin service, as follows:
- For a given a PID, PersistentID, you can create a text file under the
InstallDir/etcdirectory, which obeys the following naming convention:InstallDir/etc/PersistentID.cfg
InstallDir/etc/PersistentID.cfgCopy to Clipboard Copied! Toggle word wrap Toggle overflow You can then set the properties belonging to this PID by editing this file and adding entries of the form:Property=Value
Property=ValueCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Fuse Fabric supports another mechanism for customising OSGi Config Admin properties. In Fuse Fabric, you set OSGi Config Admin properties in a fabric profile (where a profile encapsulates the data required to deploy an application). There are two alternative ways of modifying configuration settings in a profile:
- Using the management console
- Using the
fabric:profile-editcommand in a container console (see Section 3.2.2, “Create Fabric Profiles”).
Replace IP port with a property placeholder 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
As an example of how the OSGi Config Admin service might be used in practice, consider the IP port used by the
PersonService Web service from the cxf-basic project. By modifying the Spring XML file that defines this Web service, you can make the Web service's IP port customisable through the OSGi Config Admin service.
The IP port number in the Spring XML file is replaced by a property placeholder, which resolves the port number at run time by looking up the property in the OSGi Config Admin service.
Spring XML example 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
In the
cxf-basic project, any XML files from the following location are treated as Spring XML files (the standard Maven location for Spring XML files):
cxf-basic/src/main/resources/META-INF/spring/*.xml
cxf-basic/src/main/resources/META-INF/spring/*.xml
Edit the
beans.xml file from the preceding directory and add the XML contents shown in Example 2.2, “Configuring the Port Number in Spring XML”.
Example 2.2. Configuring the Port Number in Spring XML
The highlighted text shows what is changed from the original
beans.xml file, in order to integrate the OSGi Config Admin service. Apart from defining the osgix and ctx namespaces, the main changes are as follows:
- The
osgix:cm-propertiesbean contacts the OSGi Config Admin service and retrieves all of the property settings from theorg.fusesource.example.get.startedPID. The key-value pairs in thepropchild elements specify default values for the properties (which are overridden, if corresponding settings can be retrieved from the OSGi Config Admin service). - The
ctx:property-placeholderbean makes the properties from theosgix:cm-propertiesbean accessible as property placeholders. That is, a placeholder of the form${PropName}will be replaced by the value of PropName at run time. - The
${portNumber}placeholder is used to specify the IP port number used by thePersonServiceWeb service. Note the value of the address attribute is now specified as a full HTTP address (in contrast to the address shown in Example 2.1, “Spring XML for Web Services Endpoint”), which means that the WS endpoint gets deployed into a custom Jetty server (instead of the default Jetty server).
Blueprint XML example 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
To use blueprint configuration instead of Spring configuration, replace the Spring XML file by a blueprint XML file, where the blueprint file must be added to the following location in the
cxf-basic project:
cxf-basic/src/main/resources/OSGI-INF/blueprint/*.xml
cxf-basic/src/main/resources/OSGI-INF/blueprint/*.xml
Create a
beans.xml file in the preceding location and add the XML contents shown in Example 2.3, “Configuring the Port Number in Blueprint XML”.
Example 2.3. Configuring the Port Number in Blueprint XML
The highlighted text shows the parts of the blueprint configuration that are relevant to the OSGi Config Admin service. Apart from defining the
cm namespace, the main changes are as follows:
- The
cm:property-placeholderbean contacts the OSGi Config Admin service and retrieves all of the property settings from theorg.fusesource.example.get.startedPID. The key-value pairs in thecm:default-properties/cm:propertyelements specify default values for the properties (which are overridden, if corresponding settings can be retrieved from the OSGi Config Admin service). - The
{{portNumber}}placeholder is used to specify the IP port number used by thePersonServiceWeb service.
Note
If you want to try out the blueprint XML configuration, you must ensure that the instructions for the
maven-bundle-plugin in the project's pom.xml file include the wildcard, *, in the packages listed in the Import-Package element (if the Import-Package element is not present, the wildcard is implied by default). Otherwise, you will get the error: Unresolved references to [org.osgi.service.blueprint] by class(es) on the Bundle-Classpath[Jar:dot]: [].
Deploying the configurable application 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
To deploy the configurable Web service from the
cxf-basic project, perform the following steps:
- Edit the Spring XML file,
beans.xml, to integrate the OSGi Config Admin service, as described in Example 2.2, “Configuring the Port Number in Spring XML”. - Rebuild the
cxf-basicproject with Maven. Open a command prompt, change directory to theget-started/cxf-basicdirectory, and enter the following Maven command:mvn clean install
mvn clean installCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Create the following configuration file in the
etc/directory of your Red Hat JBoss Fuse installation:InstallDir/etc/org.fusesource.example.get.started.cfg
InstallDir/etc/org.fusesource.example.get.started.cfgCopy to Clipboard Copied! Toggle word wrap Toggle overflow Edit theorg.fusesource.example.get.started.cfgfile with a text editor and add the following contents:portNumber=8182
portNumber=8182Copy to Clipboard Copied! Toggle word wrap Toggle overflow - If you have previously deployed the
get-started-basicfeature (as described in Section 2.4, “Define a Feature for the Application”), uninstall it now:JBossFuse:karaf@root> features:uninstall get-started-basic
JBossFuse:karaf@root> features:uninstall get-started-basicCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Deploy the
get-started-cxffeature, by entering the following console command:JBossFuse:karaf@root> features:install get-started-cxf
JBossFuse:karaf@root> features:install get-started-cxfCopy to Clipboard Copied! Toggle word wrap Toggle overflow - After waiting a few seconds for the bundles to start up, you can test the application by opening a command prompt, changing directory to
get-started/cxf-basic, and entering the following command:mvn -Pclient -Dexec.args="http://localhost:8182/PersonServiceCF"
mvn -Pclient -Dexec.args="http://localhost:8182/PersonServiceCF"Copy to Clipboard Copied! Toggle word wrap Toggle overflow ImportantThe URL in this command has a slightly different format from the URLs used in the previous client commands: the path part of the URL is/PersonServiceCF, instead of/cxf/PersonServiceCF. - To uninstall the feature, enter the following console command:
features:uninstall get-started-cxf
features:uninstall get-started-cxfCopy to Clipboard Copied! Toggle word wrap Toggle overflow