博客
关于我
ElasicJob分布式定时任务
阅读量:294 次
发布时间: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/

你可能感兴趣的文章
2019年1月已到,Java 8 要收费了吗?
查看>>
最全的spring面试题和答案
查看>>
CentOS 8 已下载ntpdate 却无法使用crond进行时间同步
查看>>
坑啊,Spring的BeanUtils是这样用的,为啥会出bug?
查看>>
Mybatis的这些坑!把我坑惨了!
查看>>
在 IntelliJ IDEA 中使用 Git,太方便了!
查看>>
一个女生不主动联系你还有机会吗?
查看>>
7 个显著提升编码效率的IntelliJ IDEA必备插件
查看>>
企业API接口设计之token、timestamp、sign具体实现
查看>>
不懂别瞎搞!Redis 性能优化的 13 条军规!
查看>>
卸载 Navicat!事实已证明,正版客户端,它更牛逼……
查看>>
想彻底了解maven,有这篇文章足够了(中)
查看>>
Intellij IDEA 一些让人爱不释手的小技巧
查看>>
idea连接服务器远程调试(Dockerfile版)
查看>>
ElasicJob分布式定时任务
查看>>
feign调用上传文件接口(MultipartFile)
查看>>
centos 文件格式不对执行报错 || centos查看或者修改文件格式
查看>>
win锁屏界面用户名修改
查看>>
Java设计模式 —— 桥接模式(Bridge)
查看>>
计算机三级 信息安全技术历年真题(二)总共十套 3月底之前更完
查看>>