Commit 4f3255ea by huangfusuper

解决jar包运行下,无法扫描项目的情况

parent 682a0e40
......@@ -48,4 +48,15 @@ public class JavaBeanJobTask implements TimerTask {
}
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("heartbeat","PENG");
//jsonObject.put("jobHandelName","addJob");
//jsonObject.put("param","asd");
long start = System.currentTimeMillis( );
HttpUtil.post("http://10.0.55.200:8888", JSON.toJSONString(jsonObject));
System.out.println((System.currentTimeMillis( )-start) );
}
}
......@@ -10,7 +10,6 @@ import com.byit.rpc.remoting.invoker.route.RpcLoadBalance;
import lombok.extern.slf4j.Slf4j;
import java.util.Arrays;
import java.util.Collections;
import java.util.TreeSet;
/**
......@@ -58,7 +57,8 @@ public class IpUtil {
if(CollectionUtil.isEmpty(treeSet)) {
throw new PluginException(PluginEnum.NO_SERVICE_AVAILABLE);
}
electiveIp = rpcLoadBalance.route("PLUGIN-SERVER", treeSet);
//selectiveKey是对服务的唯一标识 比如轮询时他是轮询这个服务下的所由Url
electiveIp = rpcLoadBalance.route(urlsStr, treeSet);
//删除这个IP
treeSet.remove(electiveIp);
flag = survivalTest(electiveIp);
......
......@@ -26,6 +26,7 @@ public class RunJobServerHandler extends SimpleChannelInboundHandler<FullHttpReq
private static final String PONG = "PONG";
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
log.info("----------------------有请求过来了------------------------");
String responseBody = "";
if(req instanceof HttpRequest){
......@@ -59,8 +60,9 @@ public class RunJobServerHandler extends SimpleChannelInboundHandler<FullHttpReq
//响应给客户端
response.headers().set(HttpHeaderNames.CONTENT_LENGTH,byteBuf.readableBytes());
ctx.writeAndFlush(response);
ctx.close();
}
ctx.close();
}
......
package com.byit.rpc;
import com.byit.launcher.JobRunServerLauncher;
import com.byit.scan.JarRunScanProject;
import com.byit.scan.LocalFolderRunScanProject;
import com.byit.scan.base.IScanProject;
import com.byit.utils.JobUtils;
import com.byit.utils.ScanRootPackage;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import lombok.extern.slf4j.Slf4j;
/**
* @program: byit-myth-job->ServerRunThread
......@@ -15,11 +18,22 @@ import io.netty.channel.socket.nio.NioServerSocketChannel;
* @author: huangfu
* @date: 2019/11/18 16:41
**/
@Slf4j
public class ServerRunThread implements Runnable {
private Integer port;
private Integer port = 8989;
public ServerRunThread(Integer port) {
new ScanRootPackage();
//获取任务信息
IScanProject iScanProject;
if(JobUtils.isJarRun()){
log.info("-------------------jar包运行--------------------");
iScanProject = new JarRunScanProject(JobUtils.filePath());
}else{
log.info("-------------------本地运行---------------------");
iScanProject = new LocalFolderRunScanProject();
}
iScanProject.addJobCache();
JobRunServerLauncher.THREAD_RUN_MARK = "SUCCESS";
this.port = port;
}
......@@ -38,7 +52,8 @@ public class ServerRunThread implements Runnable {
.channel(NioServerSocketChannel.class)
.childHandler(new ServerChannelInitializer());
try {
ChannelFuture sync = serverBootstrap.bind(port == null ? 8989:port).sync();
ChannelFuture sync = serverBootstrap.bind(port).sync();
log.info("----------------------port:{}--------------------",port);
sync.channel().closeFuture().sync();
}catch (Exception e){
e.printStackTrace();
......
package com.byit.scan;
import com.byit.annotations.JobHandler;
import com.byit.job.handler.interfaces.IJobHandler;
import com.byit.scan.base.IScanProject;
import com.byit.utils.JobUtils;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
/**
* @program: byit-myth-job->JarRunScanProject
* @description: 这个是jar包运行时的操作
* @author: huangfu
* @date: 2019/11/29 11:16
**/
@Slf4j
public class JarRunScanProject implements IScanProject {
/**
* 任务基类
*/
private static final Class<IJobHandler> JOB_ROOT_CLASS;
/**
* 任务注解类
*/
private static final Class<JobHandler> JOB_HANDLER_CLASS;
private static final String DOT_CLASS = ".class";
private static final String DOT_MF = ".MF";
private static String rootPath;
static {
JOB_ROOT_CLASS = IJobHandler.class;
JOB_HANDLER_CLASS = JobHandler.class;
}
public JarRunScanProject(String rootPath) {
JarRunScanProject.rootPath = rootPath;
}
public static void classScan() throws IOException {
JarFile jarFile = new JarFile(rootPath);
File file = new File(rootPath);
URL url = file.toURI( ).toURL( );
//获取当前jar的类加载器
URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{url});
Enumeration<JarEntry> files = jarFile.entries( );
while (files.hasMoreElements()){
process(files.nextElement(),urlClassLoader);
}
}
private static void process(JarEntry jarEntry,ClassLoader classLoader) {
String name = jarEntry.getName( );
if(!name.endsWith(DOT_MF)){
if (name.endsWith(DOT_CLASS)) {
String classNameEndWithClass = name.replaceAll("/", ".");
String className = classNameEndWithClass.substring(0,classNameEndWithClass.length()-DOT_CLASS.length());
if(className.contains("$")){
className = className.substring(0,className.lastIndexOf("$"));
}
try {
Class objectiveClass = classLoader.loadClass(className);
if(JOB_ROOT_CLASS.isAssignableFrom(objectiveClass)){
if(objectiveClass.isAnnotationPresent(JOB_HANDLER_CLASS)){
System.out.println(className);
JobHandler annotation = (JobHandler)objectiveClass.getAnnotation(JOB_HANDLER_CLASS);
JobUtils.jobCache.put(annotation.value(),objectiveClass);
}
}
}catch (Throwable ignored){
}
}
}
}
@Override
public void addJobCache() {
try {
classScan();
} catch (IOException e) {
e.printStackTrace( );
}
}
}
package com.byit.utils;
package com.byit.scan;
import com.byit.annotations.JobHandler;
import com.byit.job.handler.interfaces.IJobHandler;
import lombok.extern.slf4j.Slf4j;
import com.byit.scan.base.IScanProject;
import com.byit.utils.JobUtils;
import java.io.File;
import java.util.ArrayList;
......@@ -10,17 +11,12 @@ import java.util.List;
import java.util.stream.Collectors;
/**
* @program: byit-myth-job->ScanRootPackage
* @description: 扫描项目
* @program: byit-myth-job->LocalFolderRunScanProject
* @description: 项目在本地运行,不是以jar包部署运行
* @author: huangfu
* @date: 2019/11/19 17:31
* @date: 2019/11/29 10:42
**/
@Slf4j
public class ScanRootPackage {
public ScanRootPackage() {
addJobCache();
}
public class LocalFolderRunScanProject implements IScanProject {
/**
* 任务基类
*/
......@@ -35,14 +31,13 @@ public class ScanRootPackage {
JOB_HANDLER_CLASS = JobHandler.class;
}
/**
* 获取项目的根路径
* @return
*/
private static String getProjectRootPath(){
return new File(ScanRootPackage.class.getResource("/").getPath()).getPath();
String thisClassPath = new File(LocalFolderRunScanProject.class.getResource("/").getPath()).getPath();
return thisClassPath;
}
/**
......@@ -58,7 +53,7 @@ public class ScanRootPackage {
.map(filePth -> {
try {
//获取类全限定名
String replace = filePth.replace(projectRootPath + File.separator, "")
String replace = filePth.replace(projectRootPath+File.separator, "")
.replace(File.separator, ".")
.replace(".class", "");
return Class.forName(replace);
......@@ -72,28 +67,18 @@ public class ScanRootPackage {
* 符合条件的类
*/
List<Class> meetTheCriteria = collect.stream( ).filter(objectiveClass ->{
if (JOB_ROOT_CLASS.isAssignableFrom(objectiveClass)) {
if(objectiveClass.isAnnotationPresent(JOB_HANDLER_CLASS)){
return true;
}
if (JOB_ROOT_CLASS.isAssignableFrom(objectiveClass)) {
if(objectiveClass.isAnnotationPresent(JOB_HANDLER_CLASS)){
return true;
}
return false;
}
return false;
}).collect(Collectors.toList( ));
return meetTheCriteria;
}
/**
* 向缓存添加任务信息
*/
private static void addJobCache(){
getClassObjective().forEach(jobClass ->{
JobHandler jobHandler = (JobHandler)jobClass.getAnnotation(JobHandler.class);
JobUtils.jobCache.put(jobHandler.value(),jobClass);
});
}
/**
* 获取项目根路径下所有的类文件
* @return
*/
......@@ -120,4 +105,13 @@ public class ScanRootPackage {
}
}
}
@Override
public void addJobCache() {
getClassObjective().forEach(jobClass ->{
JobHandler jobHandler = (JobHandler)jobClass.getAnnotation(JobHandler.class);
JobUtils.jobCache.put(jobHandler.value(),jobClass);
});
}
}
/*
package com.byit.scan;
import com.byit.annotations.JobHandler;
import com.byit.job.handler.interfaces.IJobHandler;
import com.byit.utils.JobUtils;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
*/
/**
* @program: byit-myth-job->ScanRootPackage
* @description: 扫描项目
* @author: huangfu
* @date: 2019/11/19 17:31
**//*
@Slf4j
public class ScanRootPackage {
public ScanRootPackage() {
addJobCache();
}
*/
/**
* 任务基类
*//*
private static final Class<IJobHandler> JOB_ROOT_CLASS;
*/
/**
* 任务注解类
*//*
private static final Class<JobHandler> JOB_HANDLER_CLASS;
static {
JOB_ROOT_CLASS = IJobHandler.class;
JOB_HANDLER_CLASS = JobHandler.class;
}
*/
/**
* 获取项目的根路径
* @return
*//*
private static String getProjectRootPath(){
//String path = System.getProperty("java.class.path");
*/
/*String path1 = ScanRootPackage.class.getProtectionDomain().getCodeSource().getLocation().getPath();
try {
path1 = URLDecoder.decode(path1, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace( );
}
System.out.println(path1 );*//*
String thisClassPath = new File(ScanRootPackage.class.getResource("/").getPath()).getPath();
return thisClassPath;
}
*/
/**
* 获取任务类型的CLASS对象
* 根据是否是{@link IJobHandler}判断
* @return
*//*
private static List<Class> getClassObjective() {
List<String> classFilePath = getClassFilePath( );
String projectRootPath = getProjectRootPath( );
//获取全限定名
List<Class> collect = classFilePath.stream( )
.map(filePth -> {
try {
//获取类全限定名
String replace = filePth.replace(projectRootPath+File.separator, "")
.replace(File.separator, ".")
.replace(".class", "");
return Class.forName(replace);
} catch (ClassNotFoundException e) {
e.printStackTrace( );
}
return null;
}).collect(Collectors.toList( ));
*/
/**
* 符合条件的类
*//*
List<Class> meetTheCriteria = collect.stream( ).filter(objectiveClass ->{
if (JOB_ROOT_CLASS.isAssignableFrom(objectiveClass)) {
if(objectiveClass.isAnnotationPresent(JOB_HANDLER_CLASS)){
return true;
}
}
return false;
}).collect(Collectors.toList( ));
return meetTheCriteria;
}
*/
/**
* 向缓存添加任务信息
*//*
private static void addJobCache(){
getClassObjective().forEach(jobClass ->{
JobHandler jobHandler = (JobHandler)jobClass.getAnnotation(JobHandler.class);
JobUtils.jobCache.put(jobHandler.value(),jobClass);
});
}
*/
/**
* 获取项目根路径下所有的类文件
* @return
*//*
private static List<String> getClassFilePath(){
ArrayList<String> classFileList = new ArrayList<>(10);
scan(getProjectRootPath(), classFileList);
return classFileList;
}
*/
/**
* 递归扫描
* @param rootPath
* @param list
*//*
private static void scan(String rootPath,List<String> list){
File rootFile = new File(rootPath);
File[] files = rootFile.listFiles( );
for (File file : files) {
if(file.isFile() && file.getName().endsWith(".class")){
list.add(file.getPath());
}else if(file.isDirectory()){
scan(file.getAbsolutePath(),list);
}
}
}
}
*/
package com.byit.scan.base;
import java.io.IOException;
import java.util.List;
/**
* @program: byit-myth-job->IScanProject
* @description: 扫描项目的基类
* @author: huangfu
* @date: 2019/11/29 10:41
**/
public interface IScanProject {
/**
* 将符合条件的任务添加到缓存层
*/
void addJobCache();
}
......@@ -10,6 +10,8 @@ import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
......@@ -24,6 +26,11 @@ public class JobUtils {
private static final String REQUEST_PREFIX = "http://";
private static final String REQUEST_ADD_JOB_RESOURCES_SUFFIX = "/job/addJob";
/**
* 当前项目运行环境 jar file
*/
private static final String OPERATING_ENVIRONMENT_JAR = "jar";
private static final String OPERATING_ENVIRONMENT_FILE = "file";
/**
* 任务的缓存
*/
public static final Map<String,Class<? extends IJobHandler>> jobCache = new ConcurrentHashMap<>();
......@@ -49,4 +56,31 @@ public class JobUtils {
return addRequestResult;
}
/**
* 判断当前的运行环境是什么 jar : table of Contents(目录)
* @return jar -->true 目录 -->false
*/
public static boolean isJarRun(){
boolean flag = false;
String protocol = JobUtils.class.getResource("").getProtocol();
if(OPERATING_ENVIRONMENT_JAR.equals(protocol)){
flag = true;
}else if(OPERATING_ENVIRONMENT_FILE.equals(protocol)){
flag = false;
}
return flag;
}
public static String filePath(){
String path = JobUtils.class.getProtectionDomain().getCodeSource().getLocation().getFile();
try {
path = URLDecoder.decode(path, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace( );
}
System.out.println(path );
return path;
}
}
......@@ -26,4 +26,12 @@ public enum LoadBalance {
}
return defaultRouter;
}
}
class test{
public static void main(String[] args) {
for (LoadBalance value : LoadBalance.values( )) {
System.out.println(value );
}
}
}
\ No newline at end of file
......@@ -8,6 +8,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* 轮循
* round
*
*/
......
package com.byit.job;
import com.alibaba.fastjson.JSON;
import com.byit.job.model.JavaBeanJobInfo;
import com.byit.launcher.JobRunServerLauncher;
import com.byit.rpc.remoting.invoker.route.LoadBalance;
import com.byit.utils.JobUtils;
import java.io.IOException;
......@@ -15,19 +15,18 @@ import java.io.IOException;
**/
public class Mains {
public static void main(String[] args) throws IOException {
String plServerUrl = "http://127.0.0.1:9999,http://127.0.0.1:8888";
String plServerUrl = "http://10.0.55.237:8888,http://10.0.55.232:8888";
String mythCron = "时间";
String routingStrategy = "路由策略";
String routingStrategy = LoadBalance.ROUND.name();
String blockingStrategy = "阻塞策略";
String callbackToken = "dsasadsad";
String gatewayToken="asdsadsa";
String requestIP = "127.0.0.1";
String requestIP = "10.0.55.237";
String requestPort="8080";
String param="sadsadsa";
String name="addJob";
JavaBeanJobInfo javaBeanJobInfo = new JavaBeanJobInfo(name,plServerUrl,mythCron,routingStrategy,blockingStrategy,callbackToken,gatewayToken,requestIP,requestPort,param);
JobUtils.addJob(javaBeanJobInfo);
new JobRunServerLauncher(9999);
new JobRunServerLauncher(8888);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment