1 .添加工具类
package com.michaels.emailmanagement.email.job;
import com.michaels.emailmanagement.shared.common.BusinessCode;
import com.michaels.emailmanagement.shared.exception.MikException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.stream.Collectors;
@Slf4j
public class DataFlowUtils {
private static final String BOOT_INF = "BOOT-INF";
private static final String CLASSES = "classes";
private static final String LIB = "lib";
public static void extractJar(String jarFile, String destDir) {
java.util.jar.JarFile jar = null;
try {
log.info("processing jarFile:{}", jarFile);
jar = new java.util.jar.JarFile(jarFile);
java.util.Enumeration<JarEntry> enumEntries = jar.entries();
while (enumEntries.hasMoreElements()) {
JarEntry file = enumEntries.nextElement();
File f = new File(destDir + java.io.File.separator + file.getName());
if (file.isDirectory()) {
// if its a directory, create it
if (!f.mkdir()) {
throw new IOException("mkdirs error, path [" + f + "]");
}
continue;
}
java.io.InputStream is = null;
// get the input stream
java.io.FileOutputStream fos = null;
try {
is = jar.getInputStream(file);
fos = new java.io.FileOutputStream(f);
byte[] buffer = new byte[8192];
int read;
while ((read = is.read(buffer)) != -1) {
// write contents of 'is' to 'fos'
fos.write(buffer, 0, read);
}
} finally {
if (fos != null)
safeClose(fos);
if (is != null)
safeClose(is);
}
}
} catch (IOException e) {
log.error("unzip file error", e);
throw new MikException(HttpStatus.BAD_REQUEST, BusinessCode.MSG_00002, jarFile);
} finally {
if (jar != null){
safeClose(jar);
}
}
}
public static List<String> getFileToStagedFiles(String basePath) {
String path = basePath + File.separator + BOOT_INF;
log.info("class path {}", path);
File file = new File(path + File.separator + LIB);
File[] files = file.listFiles();
log.info("need upload {} lib files to staging locations", files.length);
List<File> ls = new ArrayList<>(Arrays.asList(files));
File classesFolder = new File(path + File.separator + CLASSES);
log.info("need upload {} folder to staging locations", classesFolder);
ls.add(classesFolder);
return ls.stream().map(File::toString).collect(Collectors.toList());
}
public static void safeClose(Closeable fis) {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
log.error("error release resource", e);
}
}
}
}
- 添加事件
package com.michaels.emailmanagement.email.job;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEvent;
@Slf4j
public class DecompressJarEvent extends ApplicationEvent {
/**
* Create a new {@code ApplicationEvent}.
*
* @param source the object on which the event initially occurred or with
* which the event is associated (never {@code null})
*/
public DecompressJarEvent(Object source) {
super(source);
log.info("start customer initialize event");
decompressJarFile();
}
private void decompressJarFile() {
String path = System.getProperty("user.dir");
String jarFile = path + java.io.File.separator + "api.jar";
log.info("jar file {} , dest dir {}", jarFile, path);
DataFlowUtils.extractJar(jarFile, path);
}
}
- 修改启动类
ConfigurableApplicationContext context = SpringApplication.run(EmailManagementApplication.class, args);
context.publishEvent(new DecompressJarEvent("decompressJarEvent"));