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.此内容没有您所选择的语言版本。
3.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 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 4.2.2, “Create Fabric Profiles”).
Replace TCP port with a property placeholder 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
As an example of how the OSGi Config Admin service might be used in practice, consider the TCP port used by the
HelloWorld Web service from the cxf-basic project. By modifying the Blueprint XML file that defines this Web service, you can make the Web service's TCP port customisable through the OSGi Config Admin service.
The TCP port number in the Blueprint 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.
Blueprint XML example 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
In the
cxf-basic project, any XML files from the following location are treated as Blueprint XML files (the standard Maven location for Blueprint XML files):
cxf-basic/src/main/resources/OSGI-INF/blueprint/*.xml
cxf-basic/src/main/resources/OSGI-INF/blueprint/*.xml
Edit the
blueprint.xml file from the preceding directory and add the XML contents shown in Example 3.1, “Configuring the Port Number in Blueprint XML”.
Example 3.1. 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 TCP port number used by theHelloWorldWeb service.
Note
For 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 Blueprint XML file,
blueprint.xml, to integrate the OSGi Config Admin service, as described in Example 3.1, “Configuring the Port Number in Blueprint 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 3.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 check the port used by the HelloWorld service, by entering the following console command:
JBossFuse:karaf@root> cxf:list-endpoints Name State Address BusID [FabricResource ] [Started ] [/fabric8 ] [io.fabric8.fabric-rest-cxf250714502 ] [HelloWorldImplPort ] [Started ] [http://0.0.0.0:8182/cxf/HelloWorld ] [org.fusesource.example.cxf-basic-cxf1456001875]
JBossFuse:karaf@root> cxf:list-endpoints Name State Address BusID [FabricResource ] [Started ] [/fabric8 ] [io.fabric8.fabric-rest-cxf250714502 ] [HelloWorldImplPort ] [Started ] [http://0.0.0.0:8182/cxf/HelloWorld ] [org.fusesource.example.cxf-basic-cxf1456001875]Copy to Clipboard Copied! Toggle word wrap Toggle overflow You can see from this that the HelloWorld service is listening on port8182. - If you want to run the Web client test against this Web service, you must customize the URL used by the client. Using a text editor, open the
SoapTest.javafile from thecxf-basic/src/test/java/org/fusesource/exampledirectory, and change the connection URL as highlighted in the following fragment:URLConnection connection = new URL("http://localhost:8182/cxf/HelloWorld").openConnection();URLConnection connection = new URL("http://localhost:8182/cxf/HelloWorld").openConnection();Copy to Clipboard Copied! Toggle word wrap Toggle overflow - You can then test the application by opening a command prompt, changing directory to
get-started/cxf-basic, and entering the following command:mvn -Ptest
mvn -PtestCopy to Clipboard Copied! Toggle word wrap Toggle overflow - 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