Commit 2cdcde71 by huangfusuper

提交缺少的类

parent b368df65
package com.byit.annotations;
import java.lang.annotation.*;
/**
* 主要应用在本地java任务上
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface JobHandler {
/**
* 任务名称
* @return 任务名称
*/
String value();
}
package com.byit.api;
import com.byit.job.model.JavaBeanJobInfo;
/**
* @program: byit-myth-job->JobOperating
* @description: 任务操作
* @author: huangfu
* @date: 2019/11/18 11:21
**/
public interface JobOperating {
/**
* 添加一个任务
* @param javaBeanJobInfo 任务的详细配置
*/
void addJob(JavaBeanJobInfo javaBeanJobInfo);
}
package com.byit.rpc;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.byit.job.handler.interfaces.IJobHandler;
import com.byit.job.model.ReturnResult;
import com.byit.utils.JobUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;
import io.netty.util.internal.StringUtil;
/**
* @program: byit-myth-job->RunJobServer
* @description: 处理器最终处理类
* @author: huangfu
* @date: 2019/11/18 16:33
**/
public class RunJobServerHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
if(req instanceof HttpRequest){
JSONObject jsonObject = analysisParam(req.content( ));
if(null == jsonObject){
throw new Exception("核心参数 为 null");
}
String jobHandelName = ((String)(jsonObject.get("jobHandelName")));
String param = ((String)(jsonObject.get("param")));
if(StringUtil.isNullOrEmpty(jobHandelName)){
throw new Exception("jobHandelName 为 null");
}
ReturnResult<String> stringReturnResult = runJob(jobHandelName, param);
System.out.println("---------服务器端-------------"+stringReturnResult.getCode());
//----------------------------------消息发送-----------------------------------
ByteBuf byteBuf = Unpooled.copiedBuffer("Hello World", CharsetUtil.UTF_8);
//HTTP响应
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,byteBuf);
//设置头信息
response.headers().set(HttpHeaderNames.CONTENT_TYPE,"text/plain");
//响应给客户端
response.headers().set(HttpHeaderNames.CONTENT_LENGTH,byteBuf.readableBytes());
ctx.writeAndFlush(response);
ctx.close();
}
}
private ReturnResult<String> runJob(String jobHandlerName,String param){
Class<? extends IJobHandler> jobClass = JobUtils.jobCache.get(jobHandlerName);
try {
IJobHandler iJobHandler = jobClass.newInstance( );
return iJobHandler.execute(param);
} catch (InstantiationException e) {
e.printStackTrace( );
} catch (IllegalAccessException e) {
e.printStackTrace( );
} catch (Exception e) {
e.printStackTrace( );
}
return null;
}
/**
* 格式化参数
* @param byteBuf
* @return
*/
private JSONObject analysisParam(ByteBuf byteBuf){
return JSON.parseObject(byteBuf.toString(CharsetUtil.UTF_8) );
}
/**
* 异常捕获
* @param ctx
* @param cause
* @throws Exception
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
package com.byit.rpc;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
/**
* @program: byit-myth-job->ServerChannelInitializer
* @description: 处理器初始化
* @author: huangfu
* @date: 2019/11/18 16:39
**/
public class ServerChannelInitializer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline( );
//将请求委托给真正的最终处理类 自定义
pipeline.addLast(new HttpServerCodec());
// http 消息聚合器
pipeline.addLast("httpAggregator",new HttpObjectAggregator(512*1024));
pipeline.addLast(new RunJobServerHandler());
}
}
package com.byit.rpc;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
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;
/**
* @program: byit-myth-job->ServerRunThread
* @description: 服务运行线程
* @author: huangfu
* @date: 2019/11/18 16:41
**/
public class ServerRunThread implements Runnable {
private Integer port;
public ServerRunThread(Integer port) {
new ScanRootPackage();
JobUtils.MARK = "SUCCESS";
this.port = port;
}
@Override
public void run() {
//启动服务引导 框架启动引导类 屏蔽网络通讯配置信息
ServerBootstrap serverBootstrap = new ServerBootstrap();
//创建线程池组
//boss接收服务请求 但是并不处理而是将请求委托给worker
EventLoopGroup boss = new NioEventLoopGroup();
//真正的处理类
EventLoopGroup worker = new NioEventLoopGroup();
//将委托类和执行类关联至启动类
serverBootstrap.group(boss,worker)
.channel(NioServerSocketChannel.class)
.childHandler(new ServerChannelInitializer());
try {
ChannelFuture sync = serverBootstrap.bind(port == null ? 8989:port).sync();
sync.channel().closeFuture().sync();
}catch (Exception e){
e.printStackTrace();
}
boss.shutdownGracefully();
worker.shutdownGracefully();
}
}
package com.byit.utils;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.byit.job.handler.interfaces.IJobHandler;
import com.byit.job.model.JavaBeanJobInfo;
import com.byit.rpc.ServerRunThread;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @program: byit-myth-job->JobFactory
* @description: 创建任务的工具类
* @author: huangfu
* @date: 2019/11/18 12:30
**/
public class JobUtils {
public static String MARK = null;
/**
* 任务的缓存
*/
public static final Map<String,Class<? extends IJobHandler>> jobCache = new ConcurrentHashMap<>();
/**
* 开始任务
*/
public static void jobServerStart(){
if(MARK==null){
new Thread(new ServerRunThread(9999)).start();
System.out.println("-------------线程启动----------------" );
}
}
/**
* 添加一个任务节点
* @param javaBeanJobInfo 任务节点的详尽配置
* @return 添加结果
*/
public static String addJob(JavaBeanJobInfo javaBeanJobInfo){
//发送请求 添加任务
String post = HttpUtil.post(javaBeanJobInfo.getRequestUrl( ), JSON.toJSONString(javaBeanJobInfo));
return post;
}
}
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