mirror of
https://github.com/PowerJob/PowerJob.git
synced 2025-07-17 00:00:04 +08:00
test PersostenceService
This commit is contained in:
parent
2ca2b901b7
commit
a18a5226a4
@ -21,6 +21,7 @@
|
|||||||
<hikaricp.version>3.4.2</hikaricp.version>
|
<hikaricp.version>3.4.2</hikaricp.version>
|
||||||
<guava.version>28.2-jre</guava.version>
|
<guava.version>28.2-jre</guava.version>
|
||||||
<fastjson.version>1.2.58</fastjson.version>
|
<fastjson.version>1.2.58</fastjson.version>
|
||||||
|
<junit.version>5.6.1</junit.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
@ -74,6 +75,14 @@
|
|||||||
<version>${fastjson.version}</version>
|
<version>${fastjson.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Junit 测试 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
|
<version>${junit.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
<!-- 开发阶段输出日志 -->
|
<!-- 开发阶段输出日志 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
@ -84,6 +93,8 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,27 +1,23 @@
|
|||||||
package com.github.kfcfans.oms.worker;
|
package com.github.kfcfans.oms.worker;
|
||||||
|
|
||||||
import akka.actor.ActorRef;
|
|
||||||
import akka.actor.ActorSystem;
|
import akka.actor.ActorSystem;
|
||||||
import akka.actor.Props;
|
import akka.actor.Props;
|
||||||
|
import com.github.kfcfans.oms.worker.actors.ProcessorTrackerActor;
|
||||||
import com.github.kfcfans.oms.worker.actors.TaskTrackerActor;
|
import com.github.kfcfans.oms.worker.actors.TaskTrackerActor;
|
||||||
import com.github.kfcfans.oms.worker.common.OhMyConfig;
|
import com.github.kfcfans.oms.worker.common.OhMyConfig;
|
||||||
import com.github.kfcfans.oms.worker.common.constants.AkkaConstant;
|
import com.github.kfcfans.oms.worker.common.constants.AkkaConstant;
|
||||||
import com.github.kfcfans.oms.worker.common.utils.NetUtils;
|
import com.github.kfcfans.oms.worker.common.utils.NetUtils;
|
||||||
import com.github.kfcfans.oms.worker.common.utils.SpringUtils;
|
import com.github.kfcfans.oms.worker.common.utils.SpringUtils;
|
||||||
import com.github.kfcfans.oms.worker.core.tracker.processor.ProcessorTracker;
|
|
||||||
import com.github.kfcfans.oms.worker.persistence.TaskPersistenceService;
|
import com.github.kfcfans.oms.worker.persistence.TaskPersistenceService;
|
||||||
import com.google.common.base.Stopwatch;
|
import com.google.common.base.Stopwatch;
|
||||||
import com.google.common.collect.Maps;
|
import com.google.common.collect.Maps;
|
||||||
import com.typesafe.config.Config;
|
import com.typesafe.config.Config;
|
||||||
import com.typesafe.config.ConfigFactory;
|
import com.typesafe.config.ConfigFactory;
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.Setter;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.ApplicationContextAware;
|
import org.springframework.context.ApplicationContextAware;
|
||||||
import org.springframework.util.StopWatch;
|
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -45,7 +41,7 @@ public class OhMyWorker implements ApplicationContextAware, InitializingBean {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterPropertiesSet() throws Exception {
|
public void afterPropertiesSet() throws Exception {
|
||||||
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void init() {
|
public void init() {
|
||||||
@ -68,11 +64,15 @@ public class OhMyWorker implements ApplicationContextAware, InitializingBean {
|
|||||||
|
|
||||||
actorSystem = ActorSystem.create(AkkaConstant.ACTOR_SYSTEM_NAME, akkaFinalConfig);
|
actorSystem = ActorSystem.create(AkkaConstant.ACTOR_SYSTEM_NAME, akkaFinalConfig);
|
||||||
actorSystem.actorOf(Props.create(TaskTrackerActor.class));
|
actorSystem.actorOf(Props.create(TaskTrackerActor.class));
|
||||||
actorSystem.actorOf(Props.create(ProcessorTracker.class));
|
actorSystem.actorOf(Props.create(ProcessorTrackerActor.class));
|
||||||
|
log.info("[OhMyWorker] akka ActorSystem({}) initialized successfully.", actorSystem);
|
||||||
|
|
||||||
// 初始化存储
|
// 初始化存储
|
||||||
TaskPersistenceService.INSTANCE.init();
|
TaskPersistenceService.INSTANCE.init();
|
||||||
|
log.info("[OhMyWorker] local storage initialized successfully.");
|
||||||
|
|
||||||
|
|
||||||
|
log.info("[OhMyWorker] OhMyWorker initialized successfully, using time: {}, congratulations!", stopwatch);
|
||||||
}catch (Exception e) {
|
}catch (Exception e) {
|
||||||
log.error("[OhMyWorker] initialize OhMyWorker failed, using {}.", stopwatch, e);
|
log.error("[OhMyWorker] initialize OhMyWorker failed, using {}.", stopwatch, e);
|
||||||
}
|
}
|
||||||
@ -85,4 +85,17 @@ public class OhMyWorker implements ApplicationContextAware, InitializingBean {
|
|||||||
public void setConfig(OhMyConfig cfg) {
|
public void setConfig(OhMyConfig cfg) {
|
||||||
config = cfg;
|
config = cfg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
System.out.println(org.h2.util.NetUtils.getLocalAddress());
|
||||||
|
|
||||||
|
OhMyConfig config = new OhMyConfig();
|
||||||
|
config.setAppName("oms");
|
||||||
|
OhMyWorker ohMyWorker = new OhMyWorker();
|
||||||
|
ohMyWorker.setConfig(config);
|
||||||
|
ohMyWorker.init();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -214,52 +214,4 @@ public class TaskDAOImpl implements TaskDAO {
|
|||||||
collection.forEach(str -> sb.append("'").append(str).append("',"));
|
collection.forEach(str -> sb.append("'").append(str).append("',"));
|
||||||
return sb.replace(sb.length() -1, sb.length(), " ) ").toString();
|
return sb.replace(sb.length() -1, sb.length(), " ) ").toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
|
|
||||||
System.out.println(getInStringCondition(Lists.newArrayList("2.1")));
|
|
||||||
|
|
||||||
TaskDAOImpl taskDAO = new TaskDAOImpl();
|
|
||||||
taskDAO.initTable();
|
|
||||||
|
|
||||||
TaskDO taskDO = new TaskDO();
|
|
||||||
taskDO.setJobId("11");
|
|
||||||
taskDO.setInstanceId("22");
|
|
||||||
taskDO.setTaskId("2.1");
|
|
||||||
taskDO.setTaskName("zzz");
|
|
||||||
taskDO.setTaskContent("hhhh".getBytes());
|
|
||||||
taskDO.setStatus(TaskStatus.WAITING_DISPATCH.getValue());
|
|
||||||
taskDO.setLastModifiedTime(System.currentTimeMillis());
|
|
||||||
taskDO.setCreatedTime(System.currentTimeMillis());
|
|
||||||
taskDO.setFailedCnt(0);
|
|
||||||
|
|
||||||
taskDAO.save(taskDO);
|
|
||||||
|
|
||||||
SimpleTaskQuery query = new SimpleTaskQuery();
|
|
||||||
query.setInstanceId("22");
|
|
||||||
query.setTaskId("2.1");
|
|
||||||
final List<TaskDO> res = taskDAO.simpleQuery(query);
|
|
||||||
System.out.println(res);
|
|
||||||
System.out.println(new String(res.get(0).getTaskContent()));
|
|
||||||
|
|
||||||
// update
|
|
||||||
TaskDO update = new TaskDO();
|
|
||||||
update.setFailedCnt(8);
|
|
||||||
taskDAO.simpleUpdate(query, update);
|
|
||||||
|
|
||||||
final List<TaskDO> res2 = taskDAO.simpleQuery(query);
|
|
||||||
System.out.println(res2);
|
|
||||||
|
|
||||||
SimpleTaskQuery query3 = new SimpleTaskQuery();
|
|
||||||
query.setInstanceId("22");
|
|
||||||
query.setQueryContent("status, count(*) as num");
|
|
||||||
query.setOtherCondition("GROUP BY status");
|
|
||||||
List<Map<String, Object>> dbRES = taskDAO.simpleQueryPlus(query);
|
|
||||||
System.out.println(dbRES);
|
|
||||||
|
|
||||||
System.out.println("=========== start to delete ===========");
|
|
||||||
System.out.println(taskDAO.batchDelete("22", Lists.newArrayList("2.1")));;
|
|
||||||
|
|
||||||
Thread.sleep(100000);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ public class TaskDO {
|
|||||||
", jobId='" + jobId + '\'' +
|
", jobId='" + jobId + '\'' +
|
||||||
", instanceId='" + instanceId + '\'' +
|
", instanceId='" + instanceId + '\'' +
|
||||||
", taskName='" + taskName + '\'' +
|
", taskName='" + taskName + '\'' +
|
||||||
", taskContent=" + new String(taskContent) +
|
", taskContent=" + (taskContent == null ? "" : new String(taskContent)) +
|
||||||
", address='" + address + '\'' +
|
", address='" + address + '\'' +
|
||||||
", status=" + status +
|
", status=" + status +
|
||||||
", result='" + result + '\'' +
|
", result='" + result + '\'' +
|
||||||
|
@ -122,4 +122,10 @@ public class TaskPersistenceService {
|
|||||||
public int batchDelete(String instanceId, List<String> taskIds) {
|
public int batchDelete(String instanceId, List<String> taskIds) {
|
||||||
return taskDAO.batchDelete(instanceId, taskIds);
|
return taskDAO.batchDelete(instanceId, taskIds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<TaskDO> listAll() {
|
||||||
|
SimpleTaskQuery query = new SimpleTaskQuery();
|
||||||
|
query.setQueryCondition("1 = 1");
|
||||||
|
return taskDAO.simpleQuery(query);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,69 @@
|
|||||||
|
package com.github.kfcfans.oms;
|
||||||
|
|
||||||
|
import com.github.kfcfans.oms.worker.common.constants.TaskStatus;
|
||||||
|
import com.github.kfcfans.oms.worker.persistence.TaskDO;
|
||||||
|
import com.github.kfcfans.oms.worker.persistence.TaskPersistenceService;
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
import org.junit.jupiter.api.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* H2 数据库持久化测试
|
||||||
|
*
|
||||||
|
* @author tjq
|
||||||
|
* @since 2020/3/23
|
||||||
|
*/
|
||||||
|
public class PersistenceServiceTest {
|
||||||
|
|
||||||
|
private static TaskPersistenceService taskPersistenceService = TaskPersistenceService.INSTANCE;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
public static void initTable() throws Exception {
|
||||||
|
taskPersistenceService.init();
|
||||||
|
|
||||||
|
List<TaskDO> taskList = Lists.newLinkedList();
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
TaskDO task = new TaskDO();
|
||||||
|
taskList.add(task);
|
||||||
|
|
||||||
|
task.setJobId("1");
|
||||||
|
task.setInstanceId("10086" + ThreadLocalRandom.current().nextInt(2));
|
||||||
|
task.setTaskId(i + "");
|
||||||
|
task.setFailedCnt(0);
|
||||||
|
task.setStatus(TaskStatus.RECEIVE_SUCCESS.getValue());
|
||||||
|
task.setTaskName("ROOT_TASK");
|
||||||
|
task.setLastModifiedTime(System.currentTimeMillis());
|
||||||
|
task.setCreatedTime(System.currentTimeMillis());
|
||||||
|
}
|
||||||
|
|
||||||
|
taskPersistenceService.batchSave(taskList);
|
||||||
|
System.out.println("=============== init data ===============");
|
||||||
|
taskList.forEach(System.out::println);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
public static void stop() throws Exception {
|
||||||
|
Thread.sleep(60000);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
public void listData() {
|
||||||
|
System.out.println("============= listData =============");
|
||||||
|
List<TaskDO> result = taskPersistenceService.listAll();
|
||||||
|
System.out.println("size: " + result.size());
|
||||||
|
result.forEach(System.out::println);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBatchDelete() {
|
||||||
|
|
||||||
|
System.out.println("=============== testBatchDelete ===============");
|
||||||
|
int delete = taskPersistenceService.batchDelete("100860", Lists.newArrayList("0", "1"));
|
||||||
|
System.out.println("delete result:" + delete);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user