Reputation: 35
I am trying to write a spring application osgi bundle which will be deployed in Apache Karaf. My bundle is generated successfully and when I install the bundle in Karaf it is showing that the bundle started successfully and the corresponding bundle status is ‘Resolved’. But I am not convinced that the bundle has started successfully.
Because just for testing purpose one of my Spring beans implements InitializingBean and in the afterPropertiesSet method I just put a sysout. That means when the application context will be loaded that sysout should be printed in the karaf console.
But it wasn’t.
The application context file is placed under src/main/resources/META-INF/spring
directory and also in the manifest file I have added the Spring-Context tag. But still it is not working properly.
I am new in OSGI and got stuck to this for more than 3 days. I followed the documentations and google results but still no luck.
Please help me in this regard. I am also attaching my little codebase over here so that you can have a look at it.
My Application context
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p = "http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/osgi
http://www.springframework.org/schema/osgi/spring-osgi.xsd">
<bean id="newService" class="com.mycompany.bundle.SpringService"/>
<bean id="springContext" class="com.mycompany.bundle.SpringContext"/>
<osgi:service id="simpleServiceOsgi" ref="newService"
interface="com.mycompany.bundle.ISpringService" />
</beans>
My pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.shamik</groupId>
<artifactId>com.shamik.bundle.new</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>bundle</packaging>
<name>com.shamik.bundle.new Bundle</name>
<description>com.mycompany.bundle OSGi bundle project.</description>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.springframework.osgi</groupId>
<artifactId>spring-osgi-core</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.osgi</groupId>
<artifactId>spring-osgi-extender</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.osgi</groupId>
<artifactId>spring-osgi-io</artifactId>
<version>1.2.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<extensions>true</extensions>
<configuration>
<excludeDependencies>*;scope=provided|runtime</excludeDependencies>
<unpackBundle>true</unpackBundle>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Version>${project.version}</Bundle-Version>
<!-- <Bundle-Activator>com.mycompany.bundle.Activator</Bundle-Activator> -->
<Export-Package>
javax.xml.parsers*,org.xml.sax*,com.mycompany.bundle*;version=${project.version},org.springframework.context*,org.springframework.stereotype*
</Export-Package>
<Import-Package>
com.mycompany.bundle*,javax.xml.parsers*,org.xml.sax*,org.springframework.context*,org.osgi*,org.springframework.stereotype*,!*
</Import-Package>
<Include-Resource>src/main/resources</Include-Resource>
<Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>
<Spring-Context>*;publish-context:=false;create-asynchronously:=true</Spring-Context>
<DynamicImport-Package>*</DynamicImport-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
Upvotes: 1
Views: 2126
Reputation: 44
"Resolved" does not mean that everything is good and started. It merely means that karaf 'found your stuff'. If everything were loaded successfully and started, then the status would be "Active". As it is, your bundle is inert.
You may need to check that the name of your application context xml file is the default name. I had thought that any xml file in that directory would be read in, but you may wish to rename to "spring.xml" as a first attempt. (I use blueprint config, and the jboss karaf build, so I can't say if this will help--though I suspect not)
Assuming that is not the problem, please open 2 terminals, and:
- in the first, launch the karaf client and execute log:tail
- in the second, launch the karaf client and execute restart <number of your bundle>
- update your post with the output from your first terminal
Ancillary comment - if you choose to adopt the newer, "blueprint", configuration standard (recommended, and basically a superset of spring config), then the default file name / location is src/main/resources/OSGI-INF/blueprint/blueprint.xml
Upvotes: 0
Reputation: 19626
You need to install the spring-dm feature of karaf. It contains the extender that scans bundle for spring contexts and starts these. Without this the bundle might be started but will not do anything.
Upvotes: 1