Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
@异步Spring注释不支持';行不通_Spring_Asynchronous_Annotations - Fatal编程技术网

@异步Spring注释不支持';行不通

@异步Spring注释不支持';行不通,spring,asynchronous,annotations,Spring,Asynchronous,Annotations,我有一个方法,必须从用户上传的原始文件创建一个zip和mat文件。 所以我决定只存储原始文件,然后在异步任务中创建另外两个文件。 我读到@Async注释有两个限制: 1) 仅使用公共方法 2) @Async方法必须位于调用方类的不同对象中。 所以我创建了一个新类,并将我的异步方法放入其中,但它仍然不起作用。 这是调用异步方法的类: @Service public class FleetAcquisitionServicesImpl implements FleetAcquisitionServi

我有一个方法,必须从用户上传的原始文件创建一个zip和mat文件。 所以我决定只存储原始文件,然后在异步任务中创建另外两个文件。 我读到
@Async
注释有两个限制: 1) 仅使用公共方法 2)
@Async
方法必须位于调用方类的不同对象中。 所以我创建了一个新类,并将我的异步方法放入其中,但它仍然不起作用。 这是调用异步方法的类:

@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)
@服务
公共类FleetAcquisitionServicesImpl实现FleetAcquisitionServices{
@自动连线
私有数据库获取服务数据库获取服务;
@自动连线
专用数据库车队和车辆服务数据库车队和车辆服务;
@自动连线
私有FleetFolderName FleetFolderName;
专用静态最终整型编号(共页)为4;
@凌驾
公共ArrayList uploadFiles(MultipartFile[]openedFiles、Integer idFleet、Integer idCar、Integer initialKm)引发FileUploadException、FileEmptyException{
ArrayList filename=新的ArrayList();
字符串文件名=null;
字符串carPath=null;
for(多部分文件openedFile:openedFile){
如果(!openedFile.isEmpty()){
//将文件复制到指定的路径
fileName=openedFile.getOriginalFilename();
试一试{
//获取存储文件的路径
车队=数据库车队和车辆服务.getFleetById(idFleet);
Car Car=数据库车队和CARSSERVICES.getCarById(idCar);
carPath=fleetFolderName.createCarName(车队,汽车);
if(FilenameUtils.getExtension(fileName).equals(“dat”)){
fileName=FilenameUtils.removeExtension(fileName)+“km”+initialKm;
//写入数据文件
transferTo(新文件(carPath+“/”+fileName+“.dat”);
createZipAndMat(carPath,文件名);
}否则
transferTo(新文件(carPath+“/”+文件名));
}捕获(例外e){
抛出new FileUploadException(“您未能上载”+文件名,e);
}
添加(carPath+“/”+文件名);
}否则{
抛出新的FileEmptyException(“您的文件为空”+openedFile.getOriginalFilename());
}
}
返回文件名;
}
createZipAndMat(carPath,文件名)
是异步方法,如下所示:

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();
    }

}
package.com.model;
导入java.io.ByteArrayOutputStream;
导入java.io.IOException;
导入java.nio.file.Files;
导入java.nio.file.Path;
导入java.nio.file.path;
导入java.util.concurrent.Future;
导入java.util.zip.ZipEntry;
导入java.util.zip.ZipoutStream;
导入org.springframework.scheduling.annotation.Async;
导入org.springframework.scheduling.annotation.AsyncResult;
导入com.mathworks.toolbox.javabuilder.MWException;
导入数据转换.Converter;
公共类ZipAndMat{
@异步的
公共静态未来createZipAndMat(字符串文件路径,字符串文件名){
试一试{
线程。睡眠(20000L);
}捕捉(中断异常e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
//打开文件
Path Path=Path.get(filePath,fileName+“.dat”);
试一试{
//创建拉链
byte[]zip=zipBytes(Files.readAllBytes(path),fileName+“.dat”);
Files.write(path.get(filePath+“/”+fileName+“.zip”)、zip);
//创建mat文件
Converter objConv=新转换器();
objConv.dat2MatConversion(文件路径+“/”,文件名+“.dat”,0.2);
}捕获(IOE异常){
返回新的AsyncResult(“未创建Zip”);
}捕获(MWE){
返回新的异步结果(“未创建Mat”);
}
返回新的AsyncResult(“Zip创建”);
}
/**
*将文件转换为具有相同名称的zip文件
*@param文件名
*@param输入
*@返回
*@抛出异常
*/
私有静态字节[]zipBytes(字节[]输入,字符串文件名)引发IOException{
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
ZipOutputStream zos=新ZipOutputStream(BAS);
ZipEntry条目=新ZipEntry(文件名);
entry.setSize(输入.长度);
zos.putNextEntry(条目);
写入(输入);
zos.closeEntry();
zos.close();
返回baos.toByteArray();
}
}
我尝试了tu,但调用方类和确认类都
@EnableAsync
,但它不起作用。 我的代码有什么问题?静态方法?
谢谢

您需要执行以下操作才能启用异步

1.) @服务 公共类ZipAndMat

2.)调用方类应使用@enablesync启用异步调用

@使能同步 @服务 公共类FleetAcquisitionServicesImpl实现FleetAcquisitionServices

请点击此链接了解更多详细信息
要启用异步,您需要执行以下操作

1.) @服务 公共类ZipAndMat

2.)调用方类应使用@enablesync启用异步调用

@启用同步 @服务 公共类FleetAcquisitionServicesImpl实现FleetAcquisitionServices

请点击此链接了解更多详细信息

您需要以下东西:

  • 配置类上的
    @EnableAsync
    ,以在Spring中启用异步处理
  • @Service
    @Component
    在您的
    ZipAndMat
    类上查找它作为Spring组件
  • 更改
    createZipAndMat
    方法,使其不是静态的
  • 在中自动连接此新弹簧组件
    zipAndMat.createZipAndMat(carPath,fileName);