Skip to content

Commit

Permalink
feat(home): add delaytimetask (#820)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsy1001de authored Mar 14, 2024
1 parent 03d5d62 commit 1f3745c
Show file tree
Hide file tree
Showing 85 changed files with 2,610 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
*/
package io.holoinsight.server.common.event;

import io.holoinsight.server.common.event.Event;

import java.util.List;
import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/
package io.holoinsight.server.home.web.common;
package io.holoinsight.server.home.common.util;

import io.holoinsight.server.common.JsonResult;

Expand All @@ -16,4 +16,6 @@ public interface FacadeTemplate {

void manage(JsonResult result, ManageCallback callback, String trace);

void manageWithTransaction(JsonResult result, ManageCallback callback, String trace);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/
package io.holoinsight.server.home.common.util;

import io.holoinsight.server.common.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.StopWatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.UUID;

/**
* @author jsy1001de
* @version 1.0: ManageTemplateImpl.java, v 0.1 2022年03月15日 12:23 下午 jinsong.yjs Exp $
*/
@Slf4j
@Service
public class FacadeTemplateImpl implements FacadeTemplate {

@Autowired
private TransactionTemplate transactionTemplate;

@Override
@SuppressWarnings("unchecked")
public void manage(JsonResult result, ManageCallback callback) {
String requestId = UUID.randomUUID().toString();

doManagerWithExceptionHandler(result, new ManageInternalCallback() {

@Override
public void call() {
// 检验参数
callback.checkParameter();
// 执行管理方法
callback.doManage();
}
}, new ManageInternalCallback() {
@Override
public void call() {
// nothing
}
}, requestId);
}

@Override
@SuppressWarnings("unchecked")
public void manage(JsonResult result, ManageCallback callback, String trace) {
doManagerWithExceptionHandler(result, new ManageInternalCallback() {

@Override
public void call() {
// 检验参数
callback.checkParameter();
// 执行管理方法
callback.doManage();
}
}, new ManageInternalCallback() {
@Override
public void call() {
// nothing
}
}, trace);
}

@Override
public void manageWithTransaction(JsonResult result, ManageCallback callback, String trace) {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
public void doInTransactionWithoutResult(final TransactionStatus status) {
doManagerWithExceptionHandler(result, new ManageInternalCallback() {

@Override
public void call() {
// 检验参数
callback.checkParameter();
// 执行管理方法
callback.doManage();
}
}, new ManageInternalCallback() {
@Override
public void call() {
// rollback
status.setRollbackOnly();
}
}, trace);
}
});
}

private void doManagerWithExceptionHandler(final JsonResult result,
ManageInternalCallback manageCallback, ManageInternalCallback exceptionCallback,
String trace) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
try {
manageCallback.call();
} catch (MonitorException e) {
exceptionCallback.call();

handleBizException(trace, result, e);
} catch (DataAccessException e) {
exceptionCallback.call();

handleDbException(trace, result, e);
} catch (IllegalArgumentException e) {
exceptionCallback.call();

handleIllegalArgumentException(trace, result, e);
} catch (Exception e) {
exceptionCallback.call();

handleUnclassifiedException(trace, result, e);
} finally {
stopWatch.stop();
log.info(trace + ", clientResult=[" + result.isSuccess() + "], clientCost=["
+ stopWatch.getTime() + "]");
}
}

private void handleBizException(String trace, JsonResult result, MonitorException e) {
log.error(trace + ", MonitorException: " + e.getMessage(), e);
JsonResult.fillFailResultTo(result, ResultCodeEnum.MONITOR_SYSTEM_ERROR.getResultCode(),
e.getMessage());
}

private void handleDbException(String trace, JsonResult result, DataAccessException e) {
log.error(trace + ", DataAccessException: " + e.getMessage(), e);
JsonResult.fillFailResultTo(result, ResultCodeEnum.DATAACCESS_ERROE.getResultCode(),
e.getMessage());
}

private void handleIllegalArgumentException(String trace, JsonResult result,
IllegalArgumentException e) {
log.error(trace + ", IllegalAccessException: " + e.getMessage(), e);
JsonResult.fillFailResultTo(result, ResultCodeEnum.PARAMETER_ILLEGAL.getResultCode(),
e.getMessage());
}

private void handleUnclassifiedException(String trace, JsonResult result, Exception e) {
log.error(trace + ", Exception: " + e.getMessage(), e);
JsonResult.fillFailResultTo(result, ResultCodeEnum.SYSTEM_ERROR.getResultCode(),
e.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/
package io.holoinsight.server.home.web.common;
package io.holoinsight.server.home.common.util;

/**
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/

package io.holoinsight.server.home.common.util;

/**
* @author jsy1001de
* @version 1.0: ManageInternalCallback.java, Date: 2024-03-14 Time: 12:35
*/
public interface ManageInternalCallback {
void call();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/

package io.holoinsight.server.home.dal.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import io.holoinsight.server.home.dal.model.TimedEvent;

/**
* @author jsy1001de
* @version 1.0: TimedEventMapper.java, Date: 2024-03-14 Time: 14:37
*/
public interface TimedEventMapper extends BaseMapper<TimedEvent> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/

package io.holoinsight.server.home.dal.model;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;

import javax.persistence.Table;
import java.util.Date;

/**
* @author jsy1001de
* @version 1.0: TimedEvent.java, Date: 2024-03-14 Time: 14:35
*/
@Data
@Table(name = "timed_event")
public class TimedEvent {
@TableId(type = IdType.AUTO)
public Long id;

public String topic;

public String status;

public String data;

public Integer retryTimes;

public String guardianServer;

public Date timeoutAt;

public Date gmtCreate;

public Date gmtModified;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/

package io.holoinsight.server.home.biz.service;

import com.baomidou.mybatisplus.extension.service.IService;
import io.holoinsight.server.home.dal.model.TimedEvent;

import java.util.List;

/**
* @author jsy1001de
* @version 1.0: TimedEventService.java, Date: 2024-03-14 Time: 14:38
*/
public interface TimedEventService extends IService<TimedEvent> {

List<TimedEvent> selectPendingWardEvents(String guardianServer);

TimedEvent seletForUpdate(Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/

package io.holoinsight.server.home.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.holoinsight.server.home.biz.service.TimedEventService;
import io.holoinsight.server.home.dal.mapper.TimedEventMapper;
import io.holoinsight.server.home.dal.model.TimedEvent;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author jsy1001de
* @version 1.0: TimedEventService.java, Date: 2024-03-14 Time: 14:38
*/
@Service
public class TimedEventServiceImpl extends ServiceImpl<TimedEventMapper, TimedEvent>
implements TimedEventService {

@Override
public List<TimedEvent> selectPendingWardEvents(String guardianServer) {
Map<String, Object> columnMap = new HashMap<>();
columnMap.put("guardian_server", guardianServer);
columnMap.put("status", "NEW");
return listByMap(columnMap);
}

@Override
public TimedEvent seletForUpdate(Long id) {
QueryWrapper<TimedEvent> wrapper = new QueryWrapper<>();
wrapper.eq("id", id);
wrapper.last("for update");


return this.baseMapper.selectOne(wrapper);
}
}
4 changes: 4 additions & 0 deletions server/home/home-task/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,9 @@
<artifactId>netty</artifactId>
<version>3.6.0.Final</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
</dependency>
</dependencies>
</project>
Loading

0 comments on commit 1f3745c

Please sign in to comment.