博客
关于我
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/

你可能感兴趣的文章
NIFI1.21.0/NIFI1.22.0/NIFI1.24.0/NIFI1.26.0_2024-06-11最新版本安装_采用HTTP方式_搭建集群_实际操作---大数据之Nifi工作笔记0050
查看>>
NIFI1.21.0_java.net.SocketException:_Too many open files 打开的文件太多_实际操作---大数据之Nifi工作笔记0051
查看>>
NIFI1.21.0_Mysql到Mysql增量CDC同步中_日期类型_以及null数据同步处理补充---大数据之Nifi工作笔记0057
查看>>
NIFI1.21.0_Mysql到Mysql增量CDC同步中_补充_插入时如果目标表中已存在该数据则自动改为更新数据_Postgresql_Hbase也适用---大数据之Nifi工作笔记0058
查看>>
NIFI1.21.0_Mysql到Mysql增量CDC同步中_补充_更新时如果目标表中不存在记录就改为插入数据_Postgresql_Hbase也适用---大数据之Nifi工作笔记0059
查看>>
NIFI1.21.0_NIFI和hadoop蹦了_200G集群磁盘又满了_Jps看不到进程了_Unable to write in /tmp. Aborting----大数据之Nifi工作笔记0052
查看>>
NIFI1.21.0_Postgresql和Mysql同时指定库_指定多表_全量同步到Mysql数据库以及Hbase数据库中---大数据之Nifi工作笔记0060
查看>>
NIFI1.21.0最新版本安装_连接phoenix_单机版_Https登录_什么都没改换了最新版本的NIFI可以连接了_气人_实现插入数据到Hbase_实际操作---大数据之Nifi工作笔记0050
查看>>
NIFI1.21.0最新版本安装_配置使用HTTP登录_默认是用HTTPS登录的_Https登录需要输入用户名密码_HTTP不需要---大数据之Nifi工作笔记0051
查看>>
NIFI1.21.0通过Postgresql11的CDC逻辑复制槽实现_指定表多表增量同步_增删改数据分发及删除数据实时同步_通过分页解决变更记录过大问题_02----大数据之Nifi工作笔记0054
查看>>
NIFI1.21.0通过Postgresql11的CDC逻辑复制槽实现_指定表多表增量同步_增加修改实时同步_使用JsonPath及自定义Python脚本_03---大数据之Nifi工作笔记0055
查看>>
NIFI1.21.0通过Postgresql11的CDC逻辑复制槽实现_指定表多表增量同步_插入修改删除增量数据实时同步_通过分页解决变更记录过大问题_01----大数据之Nifi工作笔记0053
查看>>
NIFI1.21.0通过Postgresql11的CDC逻辑复制槽实现_指定表或全表增量同步_实现指定整库同步_或指定数据表同步配置_04---大数据之Nifi工作笔记0056
查看>>
NIFI1.23.2_最新版_性能优化通用_技巧积累_使用NIFI表达式过滤表_随时更新---大数据之Nifi工作笔记0063
查看>>
NIFI从MySql中增量同步数据_通过Mysql的binlog功能_实时同步mysql数据_根据binlog实现update数据实时同步_实际操作05---大数据之Nifi工作笔记0044
查看>>
NIFI从MySql中增量同步数据_通过Mysql的binlog功能_实时同步mysql数据_根据binlog实现数据实时delete同步_实际操作04---大数据之Nifi工作笔记0043
查看>>
NIFI从MySql中增量同步数据_通过Mysql的binlog功能_实时同步mysql数据_配置binlog_使用处理器抓取binlog数据_实际操作01---大数据之Nifi工作笔记0040
查看>>
NIFI从MySql中增量同步数据_通过Mysql的binlog功能_实时同步mysql数据_配置数据路由_实现数据插入数据到目标数据库_实际操作03---大数据之Nifi工作笔记0042
查看>>
NIFI从MySql中增量同步数据_通过Mysql的binlog功能_实时同步mysql数据_配置数据路由_生成插入Sql语句_实际操作02---大数据之Nifi工作笔记0041
查看>>
NIFI从MySql中离线读取数据再导入到MySql中_03_来吧用NIFI实现_数据分页获取功能---大数据之Nifi工作笔记0038
查看>>