博客
关于我
ElasicJob分布式定时任务
阅读量:305 次
发布时间:2019-03-03

本文共 6664 字,大约阅读时间需要 22 分钟。

1.需要zookeeper配置中心。下载 解压后在bin里通过start启动就可以了。 最后可以通过zooinspector客户端连接就可以了。

2.pom加入坐标

com.dangdang
elastic-job-lite-core
2.1.5

3.编写定时任务。写需要定时任务执行的业务逻辑

import com.dangdang.ddframe.job.api.ShardingContext;import com.dangdang.ddframe.job.api.simple.SimpleJob;import java.util.List;import java.util.Map;/** * ElasticJobLite定时任务业务逻辑处理类 */public class ArchivieJob implements SimpleJob {       /**     * 需求:每隔两秒钟执⾏⼀次定时任务(resume表中未归档的数据归档到resume_bak表中,     * 每次归档1条记录)     * execute执行我们的业务逻辑(execute方法每次定时任务执行都会执行一次)     * @param shardingContext     */    @Override    public void execute(ShardingContext shardingContext) {           int shardingItem = shardingContext.getShardingItem();        System.out.println("=====>>>>"+shardingItem);        String shardingParameter = shardingContext.getShardingParameter();        System.out.println("=====>>>>"+shardingParameter);        // 1  从resume表中查询1条记录(未归档)        String selectSql = "select * from resume where state = '未归档' and education='"+shardingParameter+"' limit 1 ";        List
> maps = JdbcUtil.executeQuery(selectSql); if(null == maps){ System.out.println("数据处理完毕!!!!"); return; } // 2 未归档改未已归档 Map
stringObjectMap = maps.get(0); Object id = stringObjectMap.get("id"); Object name = stringObjectMap.get("name"); Object education = stringObjectMap.get("education"); System.out.println("=====>>>"+id+","+name+","+education); String updateSql = "update resume set state='已归档' where id=?"; JdbcUtil.executeUpdate(updateSql,id); // 3 归档这条记录,把这条记录插入到resume_bak中 String insertSql = "insert into resume_bak select * from resume where id=?"; JdbcUtil.executeUpdate(insertSql,id); }}

4.执行定时任务

import com.dangdang.ddframe.job.config.JobCoreConfiguration;import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;import com.dangdang.ddframe.job.lite.api.JobScheduler;import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;public class ElasicJobMain {       public static void main(String[] args) {               // 配置分布式协调服务(注册中心) Zookeeper            ZookeeperConfiguration zookeeeperConfiguration = new ZookeeperConfiguration("192.168.1.6:2181","data-archive-job");            ZookeeperRegistryCenter zookeeperRegistryCenter = new ZookeeperRegistryCenter(zookeeeperConfiguration);            zookeeperRegistryCenter.init();            // 配置任务 (时间事件,定时任务业务逻辑,调度器)            JobCoreConfiguration jobCoreConfiguration = JobCoreConfiguration.newBuilder("archive-job", "*/2 * * * * ?", 3).shardingItemParameters("0=doctor,1=bachelor,2=master").build();            SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(jobCoreConfiguration,ArchivieJob.class.getName());            JobScheduler jobScheduler = new JobScheduler(zookeeperRegistryCenter, LiteJobConfiguration.newBuilder(simpleJobConfiguration).overwrite(true).build());            jobScheduler.init();    }}

其他辅助的类

import java.sql.*;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class JdbcUtil {       //url    private static String url = "jdbc:mysql://localhost:3306/bank?characterEncoding=utf8&useSSL=false";    //user    private static String user = "root";    //password    private static String password = "123456";    //驱动程序类    private static String driver = "com.mysql.jdbc.Driver";    static {           try {               Class.forName(driver);        } catch (ClassNotFoundException e) {               // TODO Auto-generated catch block            e.printStackTrace();        }    }    public static Connection getConnection() {           try {               return DriverManager.getConnection(url, user,                    password);        } catch (SQLException e) {               e.printStackTrace();        }        return null;    }    public static void close(ResultSet rs, PreparedStatement ps,                             Connection con) {           if (rs != null) {               try {                   rs.close();            } catch (SQLException e) {                   // TODO Auto-generated catch block                e.printStackTrace();            } finally {                   if (ps != null) {                       try {                           ps.close();                    } catch (SQLException e) {                           // TODO Auto-generated catch block                        e.printStackTrace();                    } finally {                           if (con != null) {                               try {                                   con.close();                            } catch (SQLException e) {                                   // TODO Auto-generated catch block                                e.printStackTrace();                            }                        }                    }                }            }        }    }    public static void executeUpdate(String sql,Object...obj) {           Connection con = getConnection();        PreparedStatement ps = null;        try {               ps = con.prepareStatement(sql);            for (int i = 0; i < obj.length; i++) {                   ps.setObject(i + 1, obj[i]);            }            ps.executeUpdate();        } catch (SQLException e) {               // TODO Auto-generated catch block            e.printStackTrace();        } finally {               close(null, ps, con);        }    }    public static List
> executeQuery(String sql, Object...obj) { Connection con = getConnection(); ResultSet rs = null; PreparedStatement ps = null; try { ps = con.prepareStatement(sql); for (int i = 0; i < obj.length; i++) { ps.setObject(i + 1, obj[i]); } rs = ps.executeQuery(); List
> list = new ArrayList<>(); int count = rs.getMetaData().getColumnCount(); while (rs.next()) { Map
map = new HashMap
(); for (int i = 0; i < count; i++) { Object ob = rs.getObject(i + 1); String key = rs.getMetaData().getColumnName(i + 1); map.put(key, ob); } list.add(map); } return list; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { close(rs, ps, con); } return null; }}

转载地址:http://tduq.baihongyu.com/

你可能感兴趣的文章
mysql 排序id_mysql如何按特定id排序
查看>>
Mysql 提示:Communication link failure
查看>>
mysql 插入是否成功_PDO mysql:如何知道插入是否成功
查看>>
Mysql 数据库InnoDB存储引擎中主要组件的刷新清理条件:脏页、RedoLog重做日志、Insert Buffer或ChangeBuffer、Undo Log
查看>>
mysql 数据库中 count(*),count(1),count(列名)区别和效率问题
查看>>
mysql 数据库备份及ibdata1的瘦身
查看>>
MySQL 数据库备份种类以及常用备份工具汇总
查看>>
mysql 数据库存储引擎怎么选择?快来看看性能测试吧
查看>>
MySQL 数据库操作指南:学习如何使用 Python 进行增删改查操作
查看>>
MySQL 数据库的高可用性分析
查看>>
MySQL 数据库设计总结
查看>>
Mysql 数据库重置ID排序
查看>>
Mysql 数据类型一日期
查看>>
MySQL 数据类型和属性
查看>>
mysql 敲错命令 想取消怎么办?
查看>>
Mysql 整形列的字节与存储范围
查看>>
mysql 断电数据损坏,无法启动
查看>>
MySQL 日期时间类型的选择
查看>>
Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
查看>>
MySQL 是如何加锁的?
查看>>