本文主要是介绍异步任务判断执行和重复使用实现类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
主要是展示一下如何在书写异步任务判断的时候,如何根据返回值类型进行重复使用相同接口里面的不同实现类的方法
/*** 父类接口* **/
public interface Exceutor {String getTaskType();void excetuor(String s);
}
/*** 异步处理任务的任务类型** @author yangziqian*/
public interface TaskTypeConstants {/*** 支付结果查询定时任务*/String PAY_RESULT_QUERY = "PAY_RESULT_QUERY";/*** 发起支付定时任务*/String PAY_INVOKE = "PAY_INVOKE";/*** 支付状态变更消息推送*/String PAY_STATUS_PUSH = "PAY_STATUS_PUSH";
}
import com.data.pay.service.Exceutor;
import com.data.pay.service.TaskTypeConstants;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;@Component
public class E1 implements Exceutor {@Overridepublic String getTaskType() {return TaskTypeConstants.PAY_INVOKE;}@Overridepublic void excetuor(String s) {System.out.println("我是E1");}
}
import com.data.pay.service.Exceutor;
import com.data.pay.service.TaskTypeConstants;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;@Component
public class E2 implements Exceutor {@Overridepublic String getTaskType() {return TaskTypeConstants.PAY_STATUS_PUSH;}@Overridepublic void excetuor(String s) {System.out.println("我是E2");}
}
/**
*测试类
**/
import com.data.pay.lambda.TrxService;
import com.data.pay.service.Exceutor;
import com.data.pay.service.impl.E1;
import com.data.pay.service.impl.E2;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;@SpringBootTest
class PayDemo1ApplicationTests {@Autowiredprivate TrxService trxService;@Autowiredprivate List<Exceutor> processorList;private Map<String, Exceutor> processorMap;@Testpublic void contextLoads() {//以Exceutor的getTaskType方法返回值作为键,Exceutor的实现类作为值processorMap = processorList.stream().collect(Collectors.toMap(Exceutor::getTaskType, e -> e));//遍历打印出来很明显键是Exceutor::getTaskType的返回值,值是Exceutor的子类实现对象processorMap.forEach((e,value)-> System.out.println("键:"+e+"值:"+value));Exceutor processor = processorMap.get("PAY_STATUS_PUSH");processor.excetuor("6");}}
这篇关于异步任务判断执行和重复使用实现类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!