-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(home): add delaytimetask (#820)
- Loading branch information
Showing
85 changed files
with
2,610 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
.../home-common/src/main/java/io/holoinsight/server/home/common/util/FacadeTemplateImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...erver/home/web/common/ManageCallback.java → ...rver/home/common/util/ManageCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...e-common/src/main/java/io/holoinsight/server/home/common/util/ManageInternalCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
15 changes: 15 additions & 0 deletions
15
...r/home/home-dal/src/main/java/io/holoinsight/server/home/dal/mapper/TimedEventMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
} |
39 changes: 39 additions & 0 deletions
39
server/home/home-dal/src/main/java/io/holoinsight/server/home/dal/model/TimedEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
21 changes: 21 additions & 0 deletions
21
.../home-service/src/main/java/io/holoinsight/server/home/biz/service/TimedEventService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
43 changes: 43 additions & 0 deletions
43
...vice/src/main/java/io/holoinsight/server/home/biz/service/impl/TimedEventServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.