我有一个方法必须从用户上传的原始文件中创建一个zip和mat文件。
所以我决定只存储原始文件,然后在异步任务中我想创建另外两个文件。
我读到@Async
注释有两个限制:
1)仅限公共方法
2)@Async
方法必须位于与调用者类相关的differet对象中。
所以我创建了一个新类并将其放入我的异步方法但它仍然无法正常工作。
这是调用异步方法的类:
@Service
public class FleetAcquisitionServicesImpl implements FleetAcquisitionServices{
@Autowired
private DatabaseAcquisitionServices databaseAcquisitionServices;
@Autowired
private DatabaseFleetsAndCarsServices databaseFleetsAndCarsServices;
@Autowired
private FleetFolderName fleetFolderName;
private static final int NUMBER_OF_SHEET=4;
@Override
public ArrayList<String> uploadFiles(MultipartFile[] openedFiles, Integer idFleet, Integer idCar, Integer initialKm) throws FileUploadException, FileEmptyException{
ArrayList<String> filesName=new ArrayList<String>();
String fileName=null;
String carPath=null;
for (MultipartFile openedFile:openedFiles){
if (!openedFile.isEmpty()) {
//Copy the file into path specified
fileName = openedFile.getOriginalFilename();
try {
//Get the path where to store the file
Fleet fleet=databaseFleetsAndCarsServices.getFleetById(idFleet);
Car car=databaseFleetsAndCarsServices.getCarById(idCar);
carPath= fleetFolderName.createCarName(fleet, car);
if (FilenameUtils.getExtension(fileName).equals("dat")){
fileName = FilenameUtils.removeExtension(fileName)+"_km"+initialKm;
//write dat file
openedFile.transferTo(new File(carPath +"/"+ fileName+".dat"));
ZipAndMat.createZipAndMat(carPath,fileName);
}else
openedFile.transferTo(new File(carPath +"/"+ fileName));
} catch (Exception e) {
throw new FileUploadException("you failed to upload " + fileName,e);
}
filesName.add(carPath +"/"+ fileName);
} else {
throw new FileEmptyException("your file is empty " + openedFile.getOriginalFilename());
}
}
return filesName;
}
ZipAndMat.createZipAndMat(carPath,fileName)
是异步方法,它在这里:
package com.model;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.Future;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import com.mathworks.toolbox.javabuilder.MWException;
import dataconversion.Converter;
public class ZipAndMat {
@Async
public static Future<String> createZipAndMat(String filePath, String fileName){
try {
Thread.sleep(20000L);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//Open file
Path path = Paths.get(filePath, fileName + ".dat");
try {
//Create zip
byte[] zip = zipBytes(Files.readAllBytes(path),fileName+".dat");
Files.write(Paths.get(filePath +"/"+ fileName+".zip"), zip);
//create mat file
Converter objConv = new Converter();
objConv. dat2MatConversion(filePath +"/", fileName + ".dat", 0.2);
} catch (IOException e) {
return new AsyncResult<String>("Zip not created");
} catch (MWException e){
return new AsyncResult<String>("Mat not created");
}
return new AsyncResult<String>("Zip created");
}
/**
* Convert a file into zip file keeping the same name
* @param filename
* @param input
* @return
* @throws IOException
*/
private static byte[] zipBytes( byte[] input, String filename) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
ZipEntry entry = new ZipEntry(filename);
entry.setSize(input.length);
zos.putNextEntry(entry);
zos.write(input);
zos.closeEntry();
zos.close();
return baos.toByteArray();
}
}
我尝试了但@EnableAsync
调用者类和confiuration类,但它不起作用。
我的代码有什么问题?静态方法?
感谢
答案 0 :(得分:2)
您需要以下内容:
$ node index.js
************** ERROR **************
b is not defined ReferenceError: b is not defined
at Object.<anonymous> (path/index.js:11:9)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:475:10)
at startup (node.js:117:18)
at node.js:951:3
在配置类上启用Spring中的异步处理。@EnableAsync
类上的@Service
或@Component
将其作为Spring组件发现。 ZipAndMat
方法,使其不是静态。 在createZipAndMat
中自动装配这个新的Spring组件,如下所示:
FleetAcquisitionServicesImpl
然后,您不需要调用静态方法@Autowired
private ZipAndMat zipAndMat;
,而是需要在自动装配的spring组件实例上调用它,如下所示:
ZipAndMat.createZipAndMat(carPath,fileName);
答案 1 :(得分:1)
您需要执行以下操作才能启用异步
1)。 @服务 公共类ZipAndMat ......
2。)调用者类应该使用@EnableAsync
启用异步调用@EnableAsync @服务 公共类FleetAcquisitionServicesImpl实现FleetAcquisitionServices
请点击此链接了解更多详情 https://spring.io/guides/gs/async-method/