Browse Source

add:异步编程

master
zhangzhiqiang 2 years ago
parent
commit
6303079e27
  1. 5
      transactional-demo/pom.xml
  2. 19
      transactional-demo/src/main/java/nl/org/transactionaldemo/server/JDK新特性/stream流/StreamDemo.java
  3. 12
      transactional-demo/src/main/java/nl/org/transactionaldemo/server/JDK新特性/函数式接口/FuntureDemo.java
  4. 10
      transactional-demo/src/main/java/nl/org/transactionaldemo/server/事务/编程事务/TransationDemo.java

5
transactional-demo/pom.xml

@ -30,6 +30,11 @@
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId> <artifactId>fastjson</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId> <artifactId>spring-boot-starter-jdbc</artifactId>

19
transactional-demo/src/main/java/nl/org/transactionaldemo/server/JDK新特性/stream流/StreamDemo.java

@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject;
import java.util.*; import java.util.*;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -14,15 +15,27 @@ import java.util.stream.Collectors;
public class StreamDemo { public class StreamDemo {
public static void main(String[] args) { public static void main(String[] args) {
List<DataEntity> list = extracted(100); List<DataEntity> list = extracted(100);
//1.字段汇总
Set<String> collect = list.stream() for (DataEntity entity : list) {
.map(DataEntity::getName).collect(Collectors.toSet()); String classNo = entity.getClassNo();
}
//1.字段汇总 a->a.getName() DataEntity::getName
Set<Object> collect = list.stream()
.map(new Function<DataEntity, Object>() {
@Override
public Object apply(DataEntity dataEntity) {
return dataEntity.getName();
}
}).collect(Collectors.toSet());
//2.条件过滤 //2.条件过滤
List<DataEntity> collect1 = list.stream().filter(dataEntity -> dataEntity.getClassNo().equals("NO.3")).collect(Collectors.toList()); List<DataEntity> collect1 = list.stream().filter(dataEntity -> dataEntity.getClassNo().equals("NO.3")).collect(Collectors.toList());
//3.分组 //3.分组
Map<String, List<DataEntity>> map = list.stream().collect(Collectors.groupingBy(dataEntity -> dataEntity.getClassNo())); Map<String, List<DataEntity>> map = list.stream().collect(Collectors.groupingBy(dataEntity -> dataEntity.getClassNo()));
//4.转换 //4.转换
//list;->point_code = '001'
Map<String, DataEntity> map2 = list.stream().collect(HashMap::new, (k, v) -> k.put(v.getName(), v), HashMap::putAll); Map<String, DataEntity> map2 = list.stream().collect(HashMap::new, (k, v) -> k.put(v.getName(), v), HashMap::putAll);
DataEntity dataEntity = map2.get("e4563a6b-94c1-4817-97aa-d66a8b21640f");
System.out.println(dataEntity.toString());
} }

12
transactional-demo/src/main/java/nl/org/transactionaldemo/server/JDK新特性/函数式接口/FuntureDemo.java

@ -13,21 +13,23 @@ public class FuntureDemo {
numProcess.process(3); numProcess.process(3);
//2.正常业务处理:文字处理业务中需要引入数字处理业务 //2.正常业务处理:文字处理业务中需要引入数字处理业务
StringProcess process = new StringProcess(); StringProcess sringprocess = new StringProcess();
process.process(); sringprocess.process();
// NumProcess numProcess = new NumProcess();
// return "NO."+numProcess.process(3);
//3.以参数的形式带入 //3.以参数的形式带入
process.process(new NumProcess()); sringprocess.process(new NumProcess());
//4.匿名内部类 //4.匿名内部类
process.process(new Process() { sringprocess.process(new Process() {
@Override @Override
public Object process(Object o) { public Object process(Object o) {
return (Integer)o*2; return (Integer)o*2;
} }
}); });
//5.函数式接口 //5.函数式接口
process.process((a)-> ((Integer) a)*2); sringprocess.process((a)-> ((Integer) a)*2);
} }

10
transactional-demo/src/main/java/nl/org/transactionaldemo/server/事务/编程事务/TransationDemo.java

@ -7,6 +7,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition; import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import java.util.*; import java.util.*;
@ -28,7 +29,7 @@ public class TransationDemo {
@Autowired @Autowired
private PlatformTransactionManager txManager; private PlatformTransactionManager txManager;
// @Transactional @Transactional
public void creteTask(JSONArray request){ public void creteTask(JSONArray request){
List<TransactionStatus> statusCollent = new ArrayList<>(); List<TransactionStatus> statusCollent = new ArrayList<>();
@ -71,7 +72,7 @@ public class TransationDemo {
} }
} }
//封装参考
class TransactionManagerUtil{ class TransactionManagerUtil{
public static void main(String[] args) { public static void main(String[] args) {
@ -96,6 +97,10 @@ class TransactionManagerUtil{
PlatformTransactionManager txManager = null; //SpringContextHolder.getBean(PlatformTransactionManager.class); PlatformTransactionManager txManager = null; //SpringContextHolder.getBean(PlatformTransactionManager.class);
Map<String, TransactionStatus> statusMap = new HashMap<>(); Map<String, TransactionStatus> statusMap = new HashMap<>();
List<TransactionStatus> errTask; List<TransactionStatus> errTask;
DefaultTransactionDefinition init = new DefaultTransactionDefinition();
init.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
TransactionStatus initStatus = txManager.getTransaction(init);
try { try {
//创建所有事务集合 //创建所有事务集合
Consumer<String> allTransactionConsumer = task -> { Consumer<String> allTransactionConsumer = task -> {
@ -122,6 +127,7 @@ class TransactionManagerUtil{
txManager.commit(value); txManager.commit(value);
} }
} }
txManager.commit(initStatus);
} }
return null; return null;
} }

Loading…
Cancel
Save