OSGi karaf scheduler

2018/03/23 07:58
阅读数 14

OSGi karaf scheduler

karaf 中提供了定时任务管理,只需安装 feature:install scheduler 即可,然后在 karaf 容器中发布 org.apache.karaf.scheduler.Job 服务,具体见 4.19. Scheduler

1. 环境准备

<parent>
    <groupId>org.apache.karaf</groupId>
    <artifactId>karaf</artifactId>
    <version>4.1.5</version>
</parent>
<groupId>com.github.binarylei</groupId>
<artifactId>osgi-scheduler</artifactId>
<packaging>bundle</packaging>

<dependencies>
    <dependency>
        <groupId>org.osgi</groupId>
        <artifactId>org.osgi.core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.osgi</groupId>
        <artifactId>org.osgi.compendium</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.karaf.scheduler</groupId>
        <artifactId>org.apache.karaf.scheduler.core</artifactId>
    </dependency>
</dependencies>

<build>
    <resources>
        <resource>
            <directory>${project.basedir}/src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <configuration>
               <!-- <obrRepository>NONE</obrRepository>
                <instructions>
                    <_include>-bnd.bnd</_include>
                </instructions>-->
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.aries.blueprint</groupId>
            <artifactId>blueprint-maven-plugin</artifactId>
            <version>1.5.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>blueprint-generate</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <scanPaths>
                    <scanPath>com.github.binarylei</scanPath>
                </scanPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

2. 编写定时任务

package com.github.binarylei.scheduler;

import org.apache.karaf.scheduler.Job;
import org.apache.karaf.scheduler.JobContext;
import org.apache.karaf.scheduler.Scheduler;
import org.osgi.service.component.annotations.Component;

/**
 * @author: leigang
 * @version: 2018-03-22
 */
public class SchedulerPing implements Job {

    @Override
    public void execute(JobContext context) {
        context.getConfiguration();
        System.out.println("---------------");
    }
}

在 OSGI-INF/blueprint 中 blueprint.xml 配制文件发布 Job 服务:

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 https://osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">

    <bean id="schedulerPing" class="com.github.binarylei.scheduler.SchedulerPing"/>   
    <service interface="org.apache.karaf.scheduler.Job" ref="schedulerPing">
        <service-properties>
            <entry key="scheduler.expression" value="0/2 * * * * ?"/>
        </service-properties>
    </service>    
</blueprint>
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
在线直播报名
返回顶部
顶部