zoukankan      html  css  js  c++  java
  • egg.js redis subscribe multiple channel

    'use strict';
    
    function InitRedisStrategy(agent, name, channel) {
      class RedisStrategy extends agent.ScheduleStrategy {
        start() {
          // 订阅其他的分布式调度服务发送的消息,收到消息后让一个进程执行定时任务
          agent.redis.on('message', (c, message) => {
            if (c === channel) {
              this.sendOne(message)
            }
          })
        }
      }
    
      agent.schedule.use(name, RedisStrategy)
    }
    
    // {app_root}/agent.js
    module.exports = agent => {
      // class ClusterStrategy extends agent.ScheduleStrategy {
      //   start() {
      //     // 订阅其他的分布式调度服务发送的消息,收到消息后让一个进程执行定时任务
      //     // 用户在定时任务的 schedule 配置中来配置分布式调度的场景(scene)
      //     // such as mq / redis subscribe
      //     // agent.mq.subscribe(schedule.scene, () => this.sendOne());
      //   }
      // }
      // agent.schedule.use('cluster', ClusterStrategy);
    
      agent.messenger.on('egg-ready', () => {
        agent.redis.subscribe('task1');
        agent.redis.subscribe('task2');
        agent.redis.subscribe('task3');
      });
    
      InitRedisStrategy(agent, 'redis1', 'task1');
      InitRedisStrategy(agent, 'redis2', 'task2');
      InitRedisStrategy(agent, 'redis3', 'task3');
    };
  • 相关阅读:
    fork安全的gettid高效实现
    TCP_DEFER_ACCEPT的坑
    TCP Linger的坑
    Blade和其他构建工具有什么不同
    在Blade中结合gperftools检查内存泄露
    GraphViz web版
    用户场景分析
    java-二维数组——with 刘童格
    java-四则运算-五-网页版--with刘童格
    java-四则运算-四
  • 原文地址:https://www.cnblogs.com/ChobitsSP/p/8759230.html
Copyright © 2011-2022 走看看