fix framework

This commit is contained in:
limqhz
2021-03-01 22:49:14 +08:00
parent e6bc99430f
commit 28fe094cef
15 changed files with 6 additions and 457 deletions

View File

@@ -34,7 +34,6 @@ ext {
nettyVersion = '4.1.10.Final'
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile fileTree(include: '*.jar', dir: 'src/libs')
compile project(':service')
compile "io.netty:netty-all:${nettyVersion}"

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF8"?>
<configuration>
<jmxConfigurator />
<property name="LOG_HOME" value="/home/log/api_logs"/>
<property name="LOG_HOME" value="~/logs/api_log"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
@@ -60,4 +60,4 @@
<appender-ref ref="COMMON-ERROR" />
<appender-ref ref="CONNECTION_APPENDER" />
</root>
</configuration>
</configuration>

View File

@@ -1,22 +0,0 @@
package com;
import com.sv.service.api.VenueService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.NONE)
public class VenueServiceTest {
@Resource
private VenueService venueService;
@Test
public void test(){
// System.out.println(venueService.enterVenue(12,3));
}
}

View File

@@ -1,13 +0,0 @@
package com.test.netty;
import com.test.netty.client.ClientThread;
/**
* 开启一个客户端,
*/
public class ClientTest {
public static void main(String[] args) {
ClientThread instance = ClientThread.getInstance();
instance.start();
}
}

View File

@@ -1,108 +0,0 @@
package com.test.netty.client;
import com.sv.netty.utils.JsonUtils;
import com.test.netty.client.message.HeartBeat;
import com.test.netty.client.message.MessageDTO;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 客户端处理器
*
* @author ranfi
*/
@ChannelHandler.Sharable
public class ClientHandler extends SimpleChannelInboundHandler<String> {
private static Logger logger = LoggerFactory.getLogger(ClientHandler.class);
/**
* 当通道就绪就会触发
* @param ctx
* @throws Exception
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
}
/**
* 当通道失效就会触发
* @param ctx
* @throws Exception
*/
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
super.channelInactive(ctx);
}
/**
* 当通道有读取事件时触发
* @param ctx
* @param msg
* @throws Exception
*/
@Override
public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
logger.info("接收服务器响应msg:[" + msg + "]");
// 安卓写非netty 后台实现
MessageDTO message = JsonUtils.decode(msg, MessageDTO.class);
switch (message.getMessageType()){
case LOAD:
System.out.println("LOADING" + message.getMessage());
break;
default:
System.out.println("default");
}
// MessageService.getInstance().execute(message);
}
/**
* 心跳
* @param ctx
* @param evt
*/
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
if (IdleStateEvent.class.isAssignableFrom(evt.getClass())) {
IdleStateEvent event = (IdleStateEvent) evt;
if (event.state() == IdleState.ALL_IDLE) {
ctx.writeAndFlush(getHbMessage());
}
}
}
/**
* 封装心跳请求包
* @throws Exception
*/
private HeartBeat getHbMessage() {
HeartBeat hb = new HeartBeat();
// hb.setDeviceName("DeviceIdUtil.generateDeviceId(mContext)");
hb.setDeviceName("shebeiweiyishibiehao");
hb.setVenueId(32);
hb.setDeviceType("ENTER");
return hb;
}
/**
* 处理异常
* @param ctx
* @param cause
* @throws Exception
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
logger.error("ClientHandler exceptionCaught",cause);
Channel channel = ctx.channel();
if(channel.isActive()) {
ctx.close();
}
}
}

View File

@@ -1,39 +0,0 @@
package com.test.netty.client;
import com.test.netty.client.config.Constant;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.handler.timeout.ReadTimeoutHandler;
public class ClientInitializer extends ChannelInitializer<SocketChannel> {
private final static int TIME_HEART_BEAT = 20;
public ClientThread.ReConnectHandler reConnectHandler;
public ClientHandler dmClientHandler;
public ClientInitializer(ClientThread.ReConnectHandler handler, ClientHandler dmClientHandler) {
reConnectHandler = handler;
this.dmClientHandler = dmClientHandler;
}
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("reconnect", reConnectHandler);
pipeline.addLast(new DelimiterBasedFrameDecoder(2048,
Unpooled.wrappedBuffer(Constant.DELIMITER_WORD.getBytes())));
pipeline.addLast(new StringDecoder());
pipeline.addLast(new MessageEncoder());
pipeline.addLast(new IdleStateHandler(TIME_HEART_BEAT, TIME_HEART_BEAT,TIME_HEART_BEAT));
pipeline.addLast(dmClientHandler);
}
}

View File

@@ -1,123 +0,0 @@
package com.test.netty.client;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;
/**
* 客户端通讯
*
* @author peakren
* @date 07/12/2017 10:12 PM
*/
public class ClientThread extends Thread{
private static ClientThread instance;
private volatile EventLoopGroup workerGroup;
private volatile Bootstrap bootstrap;
private volatile boolean closed = false;
private String remoteHost;
private int remotePort;
private ChannelFuture future;
public static ClientThread getInstance() {
if (instance == null) {
synchronized (ClientThread.class) {
if (instance == null) {
instance = new ClientThread("lmqhznn.goho.co", 26283);
}
}
}
return instance;
}
private ClientThread(String remoteHost, int remotePort) {
this.remoteHost = remoteHost;
this.remotePort = remotePort;
}
public void run() {
closed = false;
workerGroup = new NioEventLoopGroup();
bootstrap = new Bootstrap();
bootstrap.group(workerGroup);
bootstrap.channel(NioSocketChannel.class);
bootstrap.option(ChannelOption.TCP_NODELAY, true);
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
ReConnectHandler reConnectHandler = new ReConnectHandler();
ClientHandler dmClientHandler = new ClientHandler();
ClientInitializer channelInitializer = new ClientInitializer(reConnectHandler, dmClientHandler);
bootstrap.handler(channelInitializer);
doConnect();
}
public void clearFuture(){
future = null;
}
public void doConnect() {
System.out.println("现在开始链接了");
if (closed) {
return;
}
System.out.println("连接 = " + remoteHost + " " + remotePort);
future = bootstrap.connect(new InetSocketAddress(remoteHost, remotePort));
future.addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture f) throws Exception {
f.channel().eventLoop().schedule(new Runnable() {
@Override
public void run() {
if (!f.isSuccess()) {
doConnect();
System.out.println("等待连接");
} else {
System.out.println("已连接");
}
}
}, 2, TimeUnit.SECONDS);
}
});
}
public void close() {
closed = true;
workerGroup.shutdownGracefully();
}
public void restart() {
close();
run();
}
@ChannelHandler.Sharable
public class ReConnectHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelInactive(ChannelHandlerContext ctx) {
System.out.println("ReConnectHandler inactive");
ctx.channel().eventLoop().schedule(new Runnable() {
@Override
public void run() {
doConnect();
}
}, 1, TimeUnit.SECONDS);
}
}
}

View File

@@ -1,37 +0,0 @@
package com.test.netty.client;
import com.sv.netty.config.Constant;
import com.sv.netty.netty.message.HeartBeat;
import com.sv.netty.utils.JsonUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import java.nio.charset.Charset;
/**
* 自定义编码器, 1个字节固定头+4个字节长度+内容
*/
public class MessageEncoder extends MessageToByteEncoder {
Charset charset = Charset.forName("UTF-8");
/**
* 安卓打印日志,本地不需要
*/
private final static String TAG = "MessageEncoder";
@Override
protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {
String message = JsonUtils.encode(msg);
message = message + Constant.DELIMITER_WORD;
byte[] content = message.getBytes(charset.name());
out.writeBytes(content); //发送消息内容
}
public static void main(String[] args) {
String hello = "{\"deviceType\":\"ENTER\",\"deviceName\":\"MGX5T17907001718\",\"venueId\":41}";
HeartBeat hb = JsonUtils.decode(hello,HeartBeat.class);
System.out.println(hb);
}
}

View File

@@ -1,7 +0,0 @@
package com.test.netty.client.config;
public class Constant {
public final static String DELIMITER_WORD = "$_$";
}

View File

@@ -1,47 +0,0 @@
package com.test.netty.client.message;
import com.google.gson.annotations.Expose;
import java.io.Serializable;
/**
* 客户端心跳数据包
* HeartBeat.java
*
* @author peakren
* @date 07/12/2017 10:23 PM
*/
public class HeartBeat implements Serializable {
@Expose
private Integer venueId; //场馆号
@Expose
private String deviceName; //设备号
@Expose
private String deviceType; //出入标志
public Integer getVenueId() {
return venueId;
}
public void setVenueId(Integer venueId) {
this.venueId = venueId;
}
public String getDeviceName() {
return deviceName;
}
public void setDeviceName(String deviceName) {
this.deviceName = deviceName;
}
public String getDeviceType() {
return deviceType;
}
public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
}

View File

@@ -1,30 +0,0 @@
package com.test.netty.client.message;
import java.io.Serializable;
public class MessageDTO implements Serializable {
private MessageType messageType;
private String message;
public MessageDTO(MessageType messageType, String message) {
this.messageType = messageType;
this.message = message;
}
public MessageType getMessageType() {
return messageType;
}
public void setMessageType(MessageType messageType) {
this.messageType = messageType;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}

View File

@@ -1,20 +0,0 @@
package com.test.netty.client.message;
public enum MessageType {
LOAD("加载"),
OPENDOOR("开门"),
FAILED("开门校验失败");
private String message;
MessageType(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}