package org.demo.scheduletest.service;
import com.rabbitmq.client.Channel;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
@Data
@Slf4j
public class DataSyncTask implements Runnable {
private String name;
private Channel channel;
private long deliveryTag;
public DataSyncTask(String name, Channel channel, long deliveryTag) {
this.name = name;
this.channel = channel;
this.deliveryTag = deliveryTag;
}
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see Thread#run()
*/
@Override
public void run() {
log.info("[DataSyncTask] run task start, name = {}", name);
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
try {
channel.basicAck(deliveryTag, true);
log.info("[DataSyncTask] run task end, name = {}", name);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
InitTask,服务启动执行Task管理器
package org.demo.scheduletest.service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
/**
* @author zhe.xiao
* @version 1.0
* @since 2025/1/9 上午11:38
*/
@Slf4j
@Component
public class InitTask implements ApplicationRunner {
/**
* Callback used to run the bean.
*
* @param args incoming application arguments
* @throws Exception on error
*/
@Override
public void run(ApplicationArguments args) throws Exception {
DataSyncTaskManager.getManager().runTaskDaemon();
}
}
定义接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace _001_线性表
{interface IListDS<T>//定义接口{int GetLength();void Clear();bool IsEmpty();void Add(T item);void Inser…