karaf-blueprint小例子

最近开始用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会执行方法中的打印。

初级点,不过算一个好的开端吧。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值