apache Ignite 节点生命周期事件例子

例子代码位置

https://github.com/lilihongjava/ignite_examples/tree/main/ignite-02

节点生命周期事件介绍

生命周期事件可以在节点生命周期的不同阶段执行自定义代码。

共有4个生命周期事件:

  • BEFORE_NODE_START:Ignite节点的启动程序初始化之前调用;
  • AFTER_NODE_START:Ignite节点启动之后调用;
  • BEFORE_NODE_STOP:Ignite节点的停止程序初始化之前调用;
  • AFTER_NODE_STOP:Ignite节点停止之后调用。

以下为添加一个自定义生命周期事件监听器的步骤

配置文件

IgniteConfiguration下添加自定义的类MyLifecycleBean

 <bean abstract="true" id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration">
        <!-- Set to true to enable distributed class loading for examples, default is false. -->
        <property name="peerClassLoadingEnabled" value="true"/>
        <!-- 节点生命周期 -->
        <property name="lifecycleBeans">
            <list>
                <bean class="org.lovelife110.example.MyLifecycleBean"/>
            </list>
        </property>
</bean>

MyLifecycleBean自定义类

实现LifecycleBean 接口,通过LifecycleEventType 拿到4个生命周期事件

public class MyLifecycleBean implements LifecycleBean {
    @IgniteInstanceResource
    public Ignite ignite;

    @Override
    public void onLifecycleEvent(LifecycleEventType evt) {
        if (evt == LifecycleEventType.BEFORE_NODE_START) {
            System.out.format("Ignite节点的启动程序初始化之前调用;\n");
        } else if (evt == LifecycleEventType.AFTER_NODE_START) {
            System.out.format("Ignite节点启动之后调用。\n");
            System.out.format("Ignite节点(consistentId = %s) 启动之后调用;\n", ignite.cluster().node().consistentId());
        } else if (evt == LifecycleEventType.BEFORE_NODE_STOP) {
            System.out.format("Ignite节点的停止程序初始化之前调用。\n");
        } else if (evt == LifecycleEventType.AFTER_NODE_STOP) {
            System.out.format("Ignite节点停止之后调用。\n");
        }
    }
}

启动测试

    public static void main(String[] args) throws IgniteException {
        Ignite ignite = Ignition.start("example-ignite.xml");

        ignite.close();
    }

结果日志如下:

Ignite节点的启动程序初始化之前调用;
[17:00:28] Configured plugins:
[17:00:28]   ^-- None
[17:00:28] 
[17:00:28] Configured failure handler: [hnd=StopNodeOrHaltFailureHandler [tryStop=false, timeout=0, super=AbstractFailureHandler [ignoredFailureTypes=UnmodifiableSet [SYSTEM_WORKER_BLOCKED, SYSTEM_CRITICAL_OPERATION_TIMEOUT]]]]
[17:00:28] Message queue limit is set to 0 which may lead to potential OOMEs when running cache operations in FULL_ASYNC or PRIMARY_SYNC modes due to message queues growth on sender and receiver sides.
[17:00:47] Security status [authentication=off, sandbox=off, tls/ssl=off]
[17:01:16] Data Regions Started: 4
[17:01:16]     ^--   sysMemPlc region [type=internal, persistence=false, lazyAlloc=false,
[17:01:16]       ...  initCfg=40MB, maxCfg=100MB, usedRam=0MB, freeRam=100%, allocRam=40MB]
[17:01:16]     ^--   default region [type=default, persistence=false, lazyAlloc=true,
[17:01:16]       ...  initCfg=256MB, maxCfg=3247MB, usedRam=0MB, freeRam=100%, allocRam=0MB]
[17:01:16]     ^--   TxLog region [type=internal, persistence=false, lazyAlloc=false,
[17:01:16]       ...  initCfg=40MB, maxCfg=100MB, usedRam=0MB, freeRam=100%, allocRam=40MB]
[17:01:16]     ^--   volatileDsMemPlc region [type=user, persistence=false, lazyAlloc=true,
[17:01:16]       ...  initCfg=40MB, maxCfg=100MB, usedRam=0MB, freeRam=100%, allocRam=0MB]
Ignite节点启动之后调用。
Ignite节点(consistentId = 0:0:0:0:0:0:0:1,10.1.3.201,127.0.0.1,192.168.137.1,192.168.139.1:47500) 启动之后调用;
[17:01:17] Performance suggestions for grid  (fix if possible)
[17:01:17] To disable, set -DIGNITE_PERFORMANCE_SUGGESTIONS_DISABLED=true
[17:01:17]   ^-- Enable G1 Garbage Collector (add '-XX:+UseG1GC' to JVM options)
[17:01:17]   ^-- Specify JVM heap max size (add '-Xmx<size>[g|G|m|M|k|K]' to JVM options)
[17:01:17]   ^-- Set max direct memory size if getting 'OOME: Direct buffer memory' (add '-XX:MaxDirectMemorySize=<size>[g|G|m|M|k|K]' to JVM options)
[17:01:17] Refer to this page for more performance suggestions: https://apacheignite.readme.io/docs/jvm-and-system-tuning
[17:01:17] 
[17:01:17] To start Console Management & Monitoring run ignitevisorcmd.{sh|bat}
[17:01:17] 
[17:01:17] Ignite node started OK (id=f3e79055)
[17:01:17] Topology snapshot [ver=1, locNode=f3e79055, servers=1, clients=0, state=ACTIVE, CPUs=8, offheap=3.2GB, heap=3.5GB]
[17:01:17]   ^-- Baseline [id=0, size=1, online=1, offline=0]
Ignite节点的停止程序初始化之前调用。
Ignite节点停止之后调用。
[17:01:17] Ignite node stopped OK [uptime=00:00:00.032]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lovelife110

你的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值