implemented Job Scheduler
This commit is contained in:
parent
c0238c2e3a
commit
30e75b7200
|
@ -66,6 +66,10 @@
|
|||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context-support</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc8 -->
|
||||
<dependency>
|
||||
|
@ -82,6 +86,13 @@
|
|||
</dependency>
|
||||
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz -->
|
||||
<dependency>
|
||||
<groupId>org.quartz-scheduler</groupId>
|
||||
<artifactId>quartz</artifactId>
|
||||
<version>2.3.0</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -1,10 +1,17 @@
|
|||
package com.demo.controller;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.quartz.JobBuilder;
|
||||
import org.quartz.JobDetail;
|
||||
import org.quartz.Scheduler;
|
||||
import org.quartz.Trigger;
|
||||
import org.quartz.TriggerBuilder;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
@ -14,7 +21,7 @@ import org.springframework.web.bind.annotation.RequestParam;
|
|||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.demo.service.CompareTwoTables;
|
||||
import com.unboundid.util.json.JSONObject;
|
||||
import com.demo.util.MigrationJob;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
|
@ -24,6 +31,11 @@ public class MainController {
|
|||
@Autowired
|
||||
CompareTwoTables service;
|
||||
|
||||
|
||||
|
||||
@Autowired
|
||||
private Scheduler scheduler;
|
||||
|
||||
@RequestMapping("/getCompareData")
|
||||
public Map<String, Object> demo(@RequestParam("srcTable") String srcTable,
|
||||
@RequestParam("destTable") String destTable, @RequestParam("srcColumn") String srcColumn,
|
||||
|
@ -183,7 +195,7 @@ public class MainController {
|
|||
}
|
||||
|
||||
@RequestMapping("/migrateBasedOnIgnoreColumnRule")
|
||||
public Map<String, Object> migrateBasedOnIgnoreColumnRule(@RequestBody Map<String, Object> map)
|
||||
public Map<String, Object> migrateBasedOnIgnoreColumnRule(@RequestBody Map<String, String> map)
|
||||
throws ClassNotFoundException, SQLException {
|
||||
|
||||
String status = service.migrateBasedOnIgnoreColumnRule(map);
|
||||
|
@ -196,7 +208,7 @@ public class MainController {
|
|||
}
|
||||
|
||||
@RequestMapping("/getRules")
|
||||
public List<Map<String, Object>> getRules() throws Exception {
|
||||
public List<Map<String, String>> getRules() throws Exception {
|
||||
|
||||
return service.getRules();
|
||||
|
||||
|
@ -282,4 +294,40 @@ public class MainController {
|
|||
return res;
|
||||
}
|
||||
|
||||
@RequestMapping("/scheduleMigration")
|
||||
public Map<String, Object> scheduleMigration(@RequestBody Map<String, String> map)
|
||||
throws Exception {
|
||||
|
||||
System.out.println(map);
|
||||
|
||||
|
||||
|
||||
String date = map.get("date");
|
||||
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
|
||||
|
||||
date = date.replace("T", " ");
|
||||
Date convertedCurrentDate = sdf.parse(date);
|
||||
|
||||
JobDetail job = JobBuilder.newJob(MigrationJob.class).withIdentity("dummyJobName", "group1").build();
|
||||
|
||||
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("dummyTriggerName", "group1")
|
||||
.startAt(convertedCurrentDate).build();
|
||||
|
||||
// schedule it
|
||||
|
||||
scheduler.start();
|
||||
scheduler.scheduleJob(job, trigger);
|
||||
|
||||
|
||||
Map<String, Object> res = new HashMap<>();
|
||||
|
||||
res.put("status", "Success");
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -4,8 +4,8 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
|
||||
import com.demo.service.CompareTwoTables;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -41,15 +41,25 @@ public class VerifyMigrationController {
|
|||
|
||||
@RequestMapping("/columns")
|
||||
@ResponseBody
|
||||
public Map<String,List<String>> getColumns(@RequestParam("srcTable") String srcTable,@RequestParam("destTable") String destTable) {
|
||||
|
||||
public Map<String,List<String>> getColumns(@RequestParam("srcTable") String srcTable,@RequestParam("destTable") String destTable,@RequestParam("sqlStatus") String sqlStatus) {
|
||||
|
||||
System.out.println(srcTable);
|
||||
Map<String,List<String>> tableColumns = new HashMap<String,List<String>>();
|
||||
|
||||
if(sqlStatus.equals("sqlTrue")) {
|
||||
tableColumns.put("source",service.getSourceColumnsBySql(srcTable));
|
||||
tableColumns.put("destination",service.getDestinationColumns(destTable));
|
||||
|
||||
tableColumns.put("srcKeys",new LinkedList<>());
|
||||
tableColumns.put("destKeys",service.getDestinationTablesKey(destTable));
|
||||
}else {
|
||||
|
||||
tableColumns.put("source",service.getSourceColumns(srcTable));
|
||||
tableColumns.put("destination",service.getDestinationColumns(destTable));
|
||||
|
||||
tableColumns.put("srcKeys",service.getSourceTablesKey(srcTable));
|
||||
tableColumns.put("destKeys",service.getDestinationTablesKey(destTable));
|
||||
}
|
||||
|
||||
|
||||
return tableColumns;
|
||||
|
|
|
@ -1006,7 +1006,7 @@ public class CompareTwoTables {
|
|||
|
||||
}
|
||||
|
||||
public String migrateBasedOnIgnoreColumnRule(Map<String, Object> map) throws SQLException, ClassNotFoundException {
|
||||
public String migrateBasedOnIgnoreColumnRule(Map<String, String> map) throws SQLException, ClassNotFoundException {
|
||||
|
||||
try {
|
||||
DataSource ds = dataSource.sourceDataSource();
|
||||
|
@ -1153,9 +1153,9 @@ public class CompareTwoTables {
|
|||
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> getRules() {
|
||||
public List<Map<String, String>> getRules() {
|
||||
|
||||
List<Map<String, Object>> rules = new LinkedList<>();
|
||||
List<Map<String, String>> rules = new LinkedList<>();
|
||||
|
||||
try {
|
||||
|
||||
|
@ -1176,7 +1176,7 @@ public class CompareTwoTables {
|
|||
}
|
||||
|
||||
while (rs_source.next()) {
|
||||
HashMap<String, Object> map = new HashMap<>();
|
||||
HashMap<String, String> map = new HashMap<>();
|
||||
for (String colName : rs_source_columns) {
|
||||
|
||||
map.put(colName, rs_source.getString(colName));
|
||||
|
@ -2134,4 +2134,113 @@ public class CompareTwoTables {
|
|||
return 1;
|
||||
}
|
||||
|
||||
|
||||
public void jobStart() throws ClassNotFoundException, SQLException {
|
||||
|
||||
System.out.println("-----------------jobStart-------------------");
|
||||
|
||||
|
||||
List<Map<String, String>> rulesList = getRules();
|
||||
|
||||
for (Map<String, String> mainMap : rulesList) {
|
||||
|
||||
System.out.println(mainMap);
|
||||
|
||||
Map<String, String> map = new HashMap<>();
|
||||
|
||||
map.put("sourceTable", mainMap.get("SRCTBL"));
|
||||
map.put("destTable", mainMap.get("DESTTBL"));
|
||||
map.put("srcKey", mainMap.get("SRC_KEY"));
|
||||
map.put("destKey", mainMap.get("DEST_KEY"));
|
||||
map.put("ruleName", mainMap.get("RULE_NAME"));
|
||||
map.put("value1", mainMap.get("VALUE1"));
|
||||
map.put("value2", mainMap.get("VALUE2"));
|
||||
map.put("value3", mainMap.get("VALUE3"));
|
||||
map.put("whereCondition", mainMap.get("FILTER_CONDITION"));
|
||||
map.put("SRC_COLUMNS", mainMap.get("SRC_COLUMNS"));
|
||||
map.put("DEST_COLUMNS", mainMap.get("DEST_COLUMNS"));
|
||||
map.put("value4", mainMap.get("VALUE4"));
|
||||
|
||||
|
||||
String rule = (String) map.get("ruleName");
|
||||
System.out.println("-->"+rule);
|
||||
|
||||
if(rule.equals("IgnoreColumns")){
|
||||
|
||||
|
||||
String status = migrateBasedOnIgnoreColumnRule(map);
|
||||
|
||||
System.out.println(status);
|
||||
}
|
||||
|
||||
else if(rule.equals("SplitColumns")){
|
||||
|
||||
String status = migrateBasedOnSplitRule(map);
|
||||
System.out.println(status);
|
||||
}
|
||||
|
||||
else if(rule.equals("MergeColumns")){
|
||||
|
||||
String status = migrateBasedOnMergeColumnsRule(map);
|
||||
System.out.println(status);
|
||||
|
||||
}
|
||||
|
||||
else if(rule.equals("Scalar")){
|
||||
|
||||
String status = migrateBasedOnScalarColumnsRule(map);
|
||||
System.out.println(status);
|
||||
|
||||
}
|
||||
else if(rule.equals("AggregateColumns")){
|
||||
|
||||
String status = migrateBasedOnAggregateColumnsRule(map);
|
||||
System.out.println(status);
|
||||
|
||||
}
|
||||
|
||||
else if(rule.equals("DefaultCase")){
|
||||
|
||||
String status = migrateBasedOnDefaultCaseRule(map);
|
||||
System.out.println(status);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public List<String> getSourceColumnsBySql(String srcTable) {
|
||||
|
||||
try {
|
||||
|
||||
DataSource ds = dataSource.appDataSource();
|
||||
Connection con = ds.getConnection();
|
||||
System.out.println("Connection established......");
|
||||
|
||||
Statement stmt = con.createStatement();
|
||||
|
||||
// step4 execute query
|
||||
ResultSet rs_source = stmt.executeQuery(srcTable);
|
||||
|
||||
ArrayList<String> rs_source_columns = new ArrayList<String>();
|
||||
ResultSetMetaData metadata = rs_source.getMetaData();
|
||||
int columnCount = metadata.getColumnCount();
|
||||
for (int i = 1; i <= columnCount; i++) {
|
||||
rs_source_columns.add(metadata.getColumnName(i));
|
||||
}
|
||||
|
||||
con.close();
|
||||
return rs_source_columns;
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
59
CompareTables/src/main/java/com/demo/util/MigrationJob.java
Normal file
59
CompareTables/src/main/java/com/demo/util/MigrationJob.java
Normal file
|
@ -0,0 +1,59 @@
|
|||
package com.demo.util;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.quartz.Job;
|
||||
import org.quartz.JobExecutionContext;
|
||||
import org.quartz.JobExecutionException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Configurable;
|
||||
import org.springframework.scheduling.quartz.QuartzJobBean;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.demo.service.CompareTwoTables;
|
||||
|
||||
@Component
|
||||
public class MigrationJob extends QuartzJobBean {
|
||||
|
||||
//
|
||||
@Autowired
|
||||
CompareTwoTables service;
|
||||
|
||||
@Override
|
||||
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
try {
|
||||
service.jobStart();
|
||||
} catch (ClassNotFoundException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (SQLException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// public void execute(JobExecutionContext context)
|
||||
// throws JobExecutionException {
|
||||
//
|
||||
// System.out.println("-----------------MigrationJob-------------------");
|
||||
//
|
||||
//
|
||||
// try {
|
||||
// service.jobStart();
|
||||
// } catch (ClassNotFoundException e) {
|
||||
// // TODO Auto-generated catch block
|
||||
// e.printStackTrace();
|
||||
// } catch (SQLException e) {
|
||||
// // TODO Auto-generated catch block
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
//
|
||||
//
|
||||
// }
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user