三个timer相关的类之一 - CIdle

本文介绍了一个低优先级的活动对象CIdle,它主要用于执行后台任务,如电子表格重新计算和文字处理器重新分页等。文章详细解释了CIdle的构造、销毁及成员函数,并提供了一个具体的实现示例。

 

class CIdle : public CActive;

Description:

- An active object that performs low-priority processing when no higher-priority active objects are ready to run. (一个低优先级的活动对象,在没有其他的高优先级的活动对象准备运行的时候才会调用这个活动对象)

 

- An idle time active object together with its associated callback function may be used to implement potentially long running background tasks, such as spreadsheet recalculation and word processor repagination.

Construction and destruction:

static IMPORT_C CIdle *NewL(TInt aPriority);

- Allocates and initialises an Idle time active object, adds it to the active scheduler, but leaves on failure.

 

protected: IMPORT_C CIdle(TInt aPriority);

- Protected constructor taking a priority value. Sets this active object's priority value.

Member functions:

 

IMPORT_C void Start(TCallBack aCallBack);

- Starts the background task.

- The background task is encapsulated in the callback. The function represented by this callback is called every time this Idle time active object is scheduled to run.(仍然有个callback的函数会被调用,CIdle实现了CActiveRunLDoCancel,我们只需要构建一个CIdle的对象,然后调用其Start方法,实现我们的Callback函数即可。)

- The callback function should be structured to perform a background task in many increments, i.e. it should voluntarily relinquish control (i.e. return) after a suitable time interval to allow other, higher priority events to be handled. (用于处理后台任务,任务比较大,但是优先级不高)

- If the callback function has further work to do, it should return a true value. This ensures that the active object is scheduled to run again later. (如果还有事情要做就返回ETrue,如果任务都完成了就返回EFalse)

- Once the callback function has finally completed its work, it should return a false value. The active object is then no longer scheduled to run.

protected: virtual IMPORT_C void RunL();

- Handles this idle active object's request completion event. It is called when nothing of a higher priority can be scheduled.

Example:

// IMPLEMENTATION SPECIFIC CONSTANTS

const TInt KPeriodicTimerInterval5Sec(5000000);

class CMyClass : public CBase

{

private: // New functions

    /**

    * The call back function.

    * /param aAny A pointer to this class.

    */

    static TInt IdleCallBack(TAny* aAny);

 

    /**

    * Notice that this is a sample fuction.

    */

    void SomeFunction();

 

private:    // Member data

    /**

    * The periodic timer. Owned by CMyClass

    */

    CIdle* iIdleTimer;

};

 

CMyClass::~CMyClass()

{

    if (iIdleTimer)

    {

       // Calling Cancel without checking if the timer is active is safe

       iIdleTimer ->Cancel();

    }

    delete iIdleTimer;

}

void CMyClass::ConstructL()

{

    if (!iIdleTimer)

    {

    iIdleTimer = CIdle::NewL( CActive::EPriorityIdle );

    }

    if (iIdleTimer ->IsActive() )

    {

       iIdleTimer ->Cancel();

    }

    iIdleTimer ->Start( TCallBack(PeriodicTimerCallBack, this ) );

}

TInt CMyClass::PeriodicTimerCallBack(TAny* aAny)

{

    CMyClass* self = static_cast<CMyClass*>( aAny );

    return self->SomeFunction();

}

void CMyClass::SomeFunction()

{

    if (finished)

{

    return EFalse;

}

else

    {

       return ETrue;

}

}

### 1. 在 IntelliJ IDEA 中编译项目 #### 编译 timer - job - admin 为 WAR 包 在 IntelliJ IDEA 中打开 `timer - job - admin` 项目,确保项目的 `pom.xml` 中打包方式为 `war`。在 IDEA 的右侧 Maven 工具窗口中,依次点击 `Lifecycle` 下的 `clean` 和 `package`,编译完成后,会在项目的 `target` 目录下生成 `timer - job - admin.war` 包。 #### 编译 timer - job - shell - executor 和 timer - job - java - executor 为 JAR 包 同样在 IntelliJ IDEA 中打开 `timer - job - shell - executor` 和 `timer - job - java - executor` 项目,确保 `pom.xml` 中打包方式为 `jar`。在 Maven 工具窗口中,对这两个项目分别执行 `Lifecycle` 下的 `clean` 和 `package` 操作,编译完成后,在各自项目的 `target` 目录下会生成对应的 JAR 包。 ### 2. 部署 timer - job - admin 到 Linux Tomcat #### 上传 WAR 包 使用 `scp` 命令将 `timer - job - admin.war` 包上传到 Linux 服务器上的 Tomcat 的 `webapps` 目录下。例如: ```bash scp /本地路径/timer - job - admin.war root@服务器IP:/tomcat/webapps/ ``` #### 启动 Tomcat 进入 Tomcat 的 `bin` 目录,执行以下命令启动 Tomcat: ```bash cd /tomcat/bin ./startup.sh ``` Tomcat 启动后会自动解压 `timer - job - admin.war` 包。 ### 3. 以 JAR 包方式启动 timer - job - shell - executor 和 timer - job - java - executor #### 上传 JAR 包 使用 `scp` 命令将 `timer - job - shell - executor.jar` 和 `timer - job - java - executor.jar` 上传到 Linux 服务器的指定目录。例如: ```bash scp /本地路径/timer - job - shell - executor.jar root@服务器IP:/指定目录/ scp /本地路径/timer - job - java - executor.jar root@服务器IP:/指定目录/ ``` #### 启动 JAR 包 在 Linux 服务器上,进入存放 JAR 包的目录,分别执行以下命令启动两个 JAR 包: ```bash cd /指定目录/ nohup java -jar timer - job - shell - executor.jar & nohup java -jar timer - job - java - executor.jar & ``` ### 4. 在任务调度中心新建并成功执行调度任务 #### 访问任务调度中心 在浏览器中输入 `http://服务器IP:Tomcat端口/timer - job - admin` 访问 XXL - JOB 任务调度中心,使用默认的用户名和密码登录。 #### 新建执行器 在任务调度中心的菜单中找到 `执行器管理`,点击 `新增`,填写执行器的相关信息,如 `AppName`、`名称` 等,点击 `保存`。 #### 新建任务 在 `任务管理` 中点击 `新增`,填写任务的基本信息,包括 `执行器`、`任务描述`、`调度型`、`调度表达式` 等。在 `任务参数` 中可以填写执行任务所需的参数。选择 `GLUE 型` 为适合的型,编写任务的执行代码,点击 `保存`。 #### 执行任务 在 `任务管理` 列表中找到新建的任务,点击 `启动` 按钮,任务会按照设定的调度规则执行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值