yangyoupeng

添加启动时命令行文件加载,实现配置文件的动态加载

......@@ -54,19 +54,44 @@ public class ApiJarSyncer {
this.localapiManager=apiManager;
}
public void syncJarFiles(){
//查询本地文件系统上面已经存储的jar的文件信息
List<FileInfo> localFileInfoList=localapiManager.displayAllApiArchiveFile();
indexFileNames(localFileInfoList);
List<JarFileInfo> resultList= repostiry.queryAllJarEvent();
//Repository中查询到jar如果属于自己的。我们要将之进行删除
doClean(resultList);
List<JarFileInfo> notExistJarInfos=notExistedJars(resultList,localFileInfoList);
doSyncJarFiles(notExistJarInfos);
}
/**
* Method name: doClean <BR>
* Description: 清除:1、存在于Repository中,但是也存在本地的,这一些不需要同步
* 2、存在于Repository中、曾经属于本地,但是目前不存在与本地,这些需要删除Repository中的信息<BR>
* Remark: <BR>
* @param resultList void<BR>
*/
private void doClean(List<JarFileInfo> resultList) {
Iterator<JarFileInfo> itr=resultList.iterator();
while(itr.hasNext()){
JarFileInfo jarFileInfo = itr.next();
//Repository中查询到jar如果属于自己的。我们要将之进行删除内存中的信息
if(repostiry.getHost().equalsIgnoreCase(jarFileInfo.getHost())){
//Repository中查询到jar如果属于自己的,但是本地文件系统上面又不存在,那么就删除repostiry上的jarFileinfo
//这种情况的场景:在环境初始化之后,可能会手动删除本地文件上面的某些jar包,此时需要将respositry上面的相关信息也要删除
if(!jarFileIndexOnlocal.contains(jarFileInfo.getJarFileName())){
LOG.info("begian to delete jar event info [{}] on repository",jarFileInfo);
try{
boolean result=repostiry.delelteJarEvent(jarFileInfo);
LOG.info("delete jar event info [{}] on repository,result is [{}]",jarFileInfo,result);
}catch(Exception e){
LOG.error("fail to delete jar event info [{}] on repository,cause by[{}]",jarFileInfo,e.getMessage());
e.printStackTrace();
}
}
itr.remove();
}
}
List<FileInfo> localFileInfoList=localapiManager.displayAllApiArchiveFile();
List<JarFileInfo> notExistJarInfos=notExistedJars(resultList,localFileInfoList);
doSyncJarFiles(notExistJarInfos);
}
public void doSyncJarFiles(List<JarFileInfo> notExistJarInfos){
int successSyncCount=0;
......@@ -80,8 +105,8 @@ public class ApiJarSyncer {
reults=buildRequestAndDownLoad(jarFileInfo);
LOG.info("sync jar file [{}],result is [{}]",jarFileInfo,reults.isSuccess());
}catch(Exception e){
LOG.info("sync jar file [{}] failed due to [{}]",jarFileInfo,e.getMessage());
e.printStackTrace();
LOG.info("sync jar file [{}] failed due to",jarFileInfo,e.getMessage());
}
if(reults.isSuccess()){
successSyncCount++;
......@@ -149,7 +174,7 @@ public class ApiJarSyncer {
public List<JarFileInfo> notExistedJars(List<JarFileInfo> jarInfos,List<FileInfo> localFileInfoList){
indexFileNames(localFileInfoList);
List<JarFileInfo> resultList=new ArrayList<JarFileInfo>();
for(JarFileInfo jarInfo:jarInfos){
if(jarFileIndexOnlocal.contains(jarInfo.getJarFileName())){
......
......@@ -70,6 +70,19 @@ public class JarLoader {
throw new IOException("Error, could not add URL to system classloader");
}
}
public static void addURLtoClassLoader(URL u,ClassLoader parentLoader) throws IOException{
URLClassLoader sysloader = (URLClassLoader)parentLoader;
Class<?> sysclass = URLClassLoader.class;
try {
Method method = sysclass.getDeclaredMethod("addURL",parameters);
method.setAccessible(true);
method.invoke(sysloader,new Object[]{ u });
} catch (Throwable t) {
t.printStackTrace();
throw new IOException("Error, could not add URL to system classloader");
}
}
}
......
......@@ -2,12 +2,14 @@ package com.zhaoonline.support.gateway.listener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URLClassLoader;
import java.nio.file.Path;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zhaoonline.support.gateway.apijar.ApiClassLoaderManager;
import com.zhaoonline.support.gateway.apijar.ClassLoaderExistException;
import com.zhaoonline.support.gateway.apijar.JarLoader;
import com.zhaoonline.support.gateway.config.GWConstants;
......
......@@ -6,6 +6,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
......@@ -158,6 +159,17 @@ public class JarEventRepository {
}
return jarFileInfo;
}
public boolean delelteJarEvent(JarFileInfo jarFileInfo) {
DeleteResponse deleteResponse=client.prepareDelete(GWConstants.DEFAUL_JAREVENT_INDEX, DEFAUL_JAREVENT_TYPE, jarFileInfo.getJarFileName()).get();
if(deleteResponse!=null){
LOG.info("success to delete jar File event info:[{}] with id:[{}]",jarFileInfo,deleteResponse.getId());
return true;
}
return false;
}
}
......
......@@ -2,8 +2,11 @@ application.message=\u8D75\u6D8C\u5728\u7EBFAPI GateWay\u6B22\u8FCE\u60A8
spring.application.name=apigateway
server.port=80
server.address=192.168.0.162
#server.address=192.168.3.79
#server.address=192.168.0.162
server.address=192.168.3.79
es.host=192.168.0.162
es.port=9300
spring.mvc.view.prefix: /WEB-INF/view/
spring.mvc.view.suffix: .jsp
......
......@@ -11,7 +11,8 @@
<property name="clusterName" value="zhaoonline"></property>
<property name="hostPorts">
<map>
<entry key="192.168.0.162" value="9300"/>
<!--<entry key="192.168.0.162" value="9300"/>-->
<entry key="${es.host}" value="${es.port}"/>
</map>
</property>
</bean>
......
......@@ -3,6 +3,7 @@ package com.zhaoonline.support.gateway.apijar;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
......@@ -193,5 +194,20 @@ public class TestJarLoader {
tempJarFilestream.close();
}
@Test
public void testTestAddToThreadCurrentClassLoader() throws IOException, ClassNotFoundException, InterruptedException{
ClassLoader parenclassLoader=Thread.currentThread().getContextClassLoader();
//URLClassLoader urlClassLoader=JarLoader.loadFromJar(Thread.currentThread().getContextClassLoader(),jarDir);
Path jarFilePath =Paths.get(jarDir);
File jarFile=jarFilePath.toFile();
JarLoader.addURLtoClassLoader(jarFile.toURI().toURL(), parenclassLoader);
Class<?> userClassBeforeLoad2=null;
userClassBeforeLoad2 = parenclassLoader.loadClass("com.test.User");
URLClassLoader urlloader = (URLClassLoader)parenclassLoader;
urlloader.close();
Assert.assertNotNull(userClassBeforeLoad2);
Thread.sleep(10000);
}
}
......