最近开始用karaf,试了个blueprint的小例子,记录下,后面会继续深入。
两个项目,service和client;装了个EIK的插件,然后创建maven项目,选择karaf-blueprint模板,会自动创建项目框架和一个接口文件和实现文件。修改下,即可以通过maven编译打包。程序如下:
public interface MyService {
public void echo(String message);
public void startup();
}
public class MyServiceImpl implements MyService {
public void echo(String message) {
System.out.println( "Echo processed: "+message);
}
public void startup() {
System.out.println("Start up");
}
}
blueprint配置文件如下:
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" >
<bean id="serviceBean" class="thkaraf.test.MyServiceImpl" init-method="startup"/>
<service ref="serviceBean" interface="thkaraf.test.MyService"/>
</blueprint>
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>thkaraf</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>bundle</packaging>
<name>test Blueprint Bundle</name>
<description>test OSGi blueprint bundle project.</description>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.4.0</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Version>${project.version}</Bundle-Version>
<Export-Package>
thkaraf.test*;version=${project.version}
</Export-Package>
<Import-Package>
*
</Import-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
执行打包后在target目录下生成test-0.0.1-SNAPSHOT.jar文件,直接放入karaf的deploy目录即可。
客户端程序如下:
public class MyServiceClient{
MyService myService;
public MyService getMyService() {
return myService;
}
public void setMyService(MyService myService) {
this.myService = myService;
}
public void doTest(){
System.out.println("Start doTest");
myService.echo("这是一个测试");
System.out.println("End doTest");
}
}
blueprint配置文件:
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
<reference id="serviceBean" interface="thkaraf.test.MyService"/>
<bean id="serviceClent" class="thkaraf.testclient.MyServiceClient" init-method="doTest">
<property name="myService" ref="serviceBean" />
</bean>
</blueprint>
pom.xml如下:
<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>thkaraf</groupId>
<artifactId>testclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>bundle</packaging>
<name>testclient Blueprint Bundle</name>
<description>testclient OSGi blueprint bundle project.</description>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.4.0</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Version>${project.version}</Bundle-Version>
<Export-Package>
thkaraf.testclient*;version=${project.version}
</Export-Package>
<Import-Package>
*
</Import-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>thkaraf</groupId>
<artifactId>test</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
编译后生成testclient-0.0.1-SNAPSHOT.jar文件,放入karaf的deploy目录,karaf会执行方法中的打印。
初级点,不过算一个好的开端吧。