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

2021-12-08 17:18:04 浏览数 (1)

例子代码位置

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

代码语言:javascript复制
 <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个生命周期事件

代码语言:javascript复制
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");
        }
    }
}

启动测试

代码语言:javascript复制
    public static void main(String[] args) throws IgniteException {
        Ignite ignite = Ignition.start("example-ignite.xml");

        ignite.close();
    }

结果日志如下:

代码语言:javascript复制
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 人点赞