zoukankan      html  css  js  c++  java
  • Worker+MQ解惑

        用Worker来保证数据的一致性,再加上MQ来便于水平扩展,也提升了Worker的效率。这就是传说中的Worker+MQ,又叫做可靠消息方式。另外,将任务的查询和执行分工,形成父子任务,达到真正的分布式任务,也能提升Worker效率。

    Java代码  收藏代码
    1. package com.itlong.bjxizhan.support.web.job.base;  
    2.   
    3. import com.itlong.bjxizhan.common.DbContext;  
    4. import com.itlong.bjxizhan.domain.pojo.Task;  
    5. import com.itlong.bjxizhan.support.web.service.StandardTaskService;  
    6. import org.slf4j.Logger;  
    7. import org.slf4j.LoggerFactory;  
    8.   
    9. import java.util.List;  
    10.   
    11. /** 
    12.  * Created by shenhongxi on 2016/7/12. 
    13.  */  
    14. public class JobRunnable implements Runnable {  
    15.   
    16.     private static final Logger log = LoggerFactory.getLogger(JobRunnable.class);  
    17.   
    18.     private StandardTaskService standardTaskService;  
    19.   
    20.     private List<Task> tasks;  
    21.   
    22.     private String dbKey;  
    23.   
    24.     private String tableIndex;  
    25.   
    26.     @Override  
    27.     public void run() {  
    28.         if (tasks != null) {  
    29.             try {  
    30.                 DbContext.setDbKey(dbKey);  
    31.                 DbContext.setTableIndex(tableIndex);  
    32.                 for (Task task : tasks) {  
    33.                     task.setTableIndex(tableIndex);  
    34.   
    35.                     // 1. 一个job的多个实例,谁先成功锁定任务,谁先处理任务,若处理失败则解锁任务  
    36.                     // 2. 对于1中解锁失败的,要利用另外的job来专门进行解锁  
    37.                     // 3. 将任务分成几批,并行处理  
    38.                     // 4. 这些任务的子任务分批串行处理,同样有锁定-处理-失败解锁  
    39.                     // 5. 对于4中解锁失败的,同样要利用另外的job来专门进行解锁  
    40.                     boolean locked = standardTaskService.lock(task);  
    41.                     if (!locked) continue;  
    42.   
    43.                     boolean result = standardTaskService.process(task);  
    44.   
    45.                     standardTaskService.finished(result, task);  
    46.                 }  
    47.             } catch (Exception e) {  
    48.                 log.error("Do task error", e);  
    49.                 throw new RuntimeException("Do task error");  
    50.             }  
    51.         }  
    52.     }  
    53.   
    54.     public List<Task> getTasks() {  
    55.         return tasks;  
    56.     }  
    57.   
    58.     public void setTasks(List<Task> tasks) {  
    59.         this.tasks = tasks;  
    60.     }  
    61.   
    62.     public StandardTaskService getStandardTaskService() {  
    63.         return standardTaskService;  
    64.     }  
    65.   
    66.     public void setStandardTaskService(StandardTaskService standardTaskService) {  
    67.         this.standardTaskService = standardTaskService;  
    68.     }  
    69.   
    70.     public String getTableIndex() {  
    71.         return tableIndex;  
    72.     }  
    73.   
    74.     public void setTableIndex(String tableIndex) {  
    75.         this.tableIndex = tableIndex;  
    76.     }  
    77.   
    78.     public String getDbKey() {  
    79.         return dbKey;  
    80.     }  
    81.   
    82.     public void setDbKey(String dbKey) {  
    83.         this.dbKey = dbKey;  
    84.     }  
    85. }  

     ***:org.quartz.Scheduler提供了start, bystand等方法可以对Scheduler进行管控

    京东技术
  • 相关阅读:
    RAC一个节点自动重启问题分析
    Oracle Audit 审计总结
    oracle 11g日志审计
    在线剪辑歌曲
    转载:MySQL Cluster NDB(Mysql 集群)
    U 盘多系统安装盘制作神器YUMI
    转载:网站限制IP地址访问-精确到国家/省/市IP地址
    开源企业云盘Seafile部署
    K8S(二)-创建一个pod应用
    mariadb升级
  • 原文地址:https://www.cnblogs.com/wely/p/6198675.html
Copyright © 2011-2022 走看看