主要是展示一下如何在书写异步任务判断的时候,如何根据返回值类型进行重复使用相同接口里面的不同实现类的方法
public interface Exceutor {
String getTaskType();
void excetuor(String s);
}
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 {
@Override
public String getTaskType() {
return TaskTypeConstants.PAY_INVOKE;
}
@Override
public 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 {
@Override
public String getTaskType() {
return TaskTypeConstants.PAY_STATUS_PUSH;
}
@Override
public 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 {
@Autowired
private TrxService trxService;
@Autowired
private List<Exceutor> processorList;
private Map<String, Exceutor> processorMap;
@Test
public void contextLoads() {
processorMap = processorList.stream().collect(Collectors.toMap(Exceutor::getTaskType, e -> e));
processorMap.forEach((e,value)-> System.out.println("键:"+e+"值:"+value));
Exceutor processor = processorMap.get("PAY_STATUS_PUSH");
processor.excetuor("6");
}
}